2014-03-22 23:15:01 +00:00
|
|
|
"""
|
|
|
|
Bookmarks module
|
|
|
|
|
|
|
|
Therein the bookmark class is defined, representing one conference room.
|
|
|
|
This object is used to generate elements for both local and remote
|
|
|
|
bookmark storage. It can also parse xml Elements.
|
|
|
|
|
|
|
|
This module also defines several functions for retrieving and updating
|
|
|
|
bookmarks, both local and remote.
|
|
|
|
"""
|
2012-03-13 20:39:06 +00:00
|
|
|
import logging
|
2011-11-28 20:27:40 +00:00
|
|
|
from sys import version_info
|
2011-11-26 22:05:09 +00:00
|
|
|
|
2014-07-24 00:07:08 +00:00
|
|
|
from slixmpp.plugins.xep_0048 import Bookmarks, Conference
|
2012-08-06 13:38:09 +00:00
|
|
|
from common import safeJID
|
2011-11-26 22:05:09 +00:00
|
|
|
from config import config
|
|
|
|
|
2012-03-13 20:39:06 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2014-04-06 17:52:59 +00:00
|
|
|
def xml_iter(xml, tag=''):
|
2011-11-28 20:27:40 +00:00
|
|
|
if version_info[1] >= 2:
|
|
|
|
return xml.iter(tag)
|
|
|
|
else:
|
|
|
|
return xml.getiterator(tag)
|
|
|
|
|
2011-11-26 22:05:09 +00:00
|
|
|
preferred = config.get('use_bookmarks_method', 'pep').lower()
|
|
|
|
if preferred not in ('pep', 'privatexml'):
|
2011-11-26 22:26:15 +00:00
|
|
|
preferred = 'privatexml'
|
2012-03-13 20:39:06 +00:00
|
|
|
not_preferred = 'privatexml' if preferred == 'pep' else 'privatexml'
|
2011-11-26 22:05:09 +00:00
|
|
|
methods = ('local', preferred, not_preferred)
|
|
|
|
|
2011-11-26 14:47:41 +00:00
|
|
|
|
|
|
|
class Bookmark(object):
|
2011-11-26 22:05:09 +00:00
|
|
|
possible_methods = methods
|
|
|
|
|
2012-03-13 20:39:06 +00:00
|
|
|
def __init__(self, jid, name=None, autojoin=False, nick=None, password=None, method='privatexml'):
|
2011-11-26 14:47:41 +00:00
|
|
|
self.jid = jid
|
|
|
|
self.name = name or jid
|
|
|
|
self.autojoin = autojoin
|
|
|
|
self.nick = nick
|
|
|
|
self.password = password
|
|
|
|
self._method = method
|
|
|
|
|
|
|
|
@property
|
|
|
|
def method(self):
|
|
|
|
return self._method
|
|
|
|
|
|
|
|
@method.setter
|
|
|
|
def method(self, value):
|
|
|
|
if value not in self.possible_methods:
|
2012-03-13 20:39:06 +00:00
|
|
|
log.debug('Could not set bookmark storing method: %s', value)
|
2011-11-26 14:47:41 +00:00
|
|
|
return
|
|
|
|
self._method = value
|
|
|
|
|
2011-11-26 22:05:09 +00:00
|
|
|
def __repr__(self):
|
2013-06-08 10:28:07 +00:00
|
|
|
return '<%s%s%s>' % (self.jid, ('/'+self.nick) if self.nick else '', '|autojoin' if self.autojoin else '')
|
2011-11-26 22:05:09 +00:00
|
|
|
|
2011-11-26 14:47:41 +00:00
|
|
|
def stanza(self):
|
|
|
|
"""
|
|
|
|
Generate a <conference/> stanza from the instance
|
|
|
|
"""
|
|
|
|
el = Conference()
|
|
|
|
el['name'] = self.name
|
|
|
|
el['jid'] = self.jid
|
|
|
|
el['autojoin'] = 'true' if self.autojoin else 'false'
|
|
|
|
if self.nick:
|
2013-01-17 16:37:06 +00:00
|
|
|
el['nick'] = self.nick
|
2011-11-26 14:47:41 +00:00
|
|
|
if self.password:
|
2013-01-17 16:37:06 +00:00
|
|
|
el['password'] = self.password
|
2011-11-26 14:47:41 +00:00
|
|
|
return el
|
2011-11-26 22:06:11 +00:00
|
|
|
|
2011-11-27 16:46:20 +00:00
|
|
|
def local(self):
|
|
|
|
"""Generate a str for local storage"""
|
|
|
|
local = self.jid
|
|
|
|
if self.nick:
|
|
|
|
local += '/%s' % self.nick
|
|
|
|
local += ':'
|
2012-12-20 17:50:34 +00:00
|
|
|
if self.password:
|
|
|
|
config.set_and_save('password', self.password, section=self.jid)
|
2011-11-27 16:46:20 +00:00
|
|
|
return local
|
|
|
|
|
2014-03-22 23:15:01 +00:00
|
|
|
@staticmethod
|
2011-11-26 22:06:11 +00:00
|
|
|
def parse_from_element(el, method=None):
|
|
|
|
"""
|
|
|
|
Generate a Bookmark object from a <conference/> element
|
|
|
|
"""
|
|
|
|
jid = el.get('jid')
|
|
|
|
name = el.get('name')
|
2012-01-26 09:04:42 +00:00
|
|
|
autojoin = True if el.get('autojoin', 'false').lower() in ('true', '1') else False
|
2011-11-26 22:06:11 +00:00
|
|
|
nick = None
|
2014-04-06 17:52:59 +00:00
|
|
|
for n in xml_iter(el, 'nick'):
|
|
|
|
nick = n.text
|
2011-11-26 22:06:11 +00:00
|
|
|
password = None
|
2014-04-06 17:52:59 +00:00
|
|
|
for p in xml_iter(el, 'password'):
|
2011-11-26 22:06:11 +00:00
|
|
|
password = p.text
|
|
|
|
|
|
|
|
return Bookmark(jid, name, autojoin, nick, password, method)
|
|
|
|
|
2011-11-26 22:07:31 +00:00
|
|
|
bookmarks = []
|
|
|
|
|
2011-11-26 22:08:41 +00:00
|
|
|
def get_by_jid(value):
|
|
|
|
"""
|
|
|
|
Get a bookmark by bare jid
|
|
|
|
"""
|
|
|
|
for item in bookmarks:
|
|
|
|
if item.jid == value:
|
|
|
|
return item
|
|
|
|
|
|
|
|
def remove(value):
|
|
|
|
"""
|
2011-11-27 19:24:44 +00:00
|
|
|
Remove a bookmark (with its jid or directly the Bookmark object).
|
2011-11-26 22:08:41 +00:00
|
|
|
"""
|
|
|
|
if isinstance(value, str):
|
|
|
|
value = get_by_jid(value)
|
|
|
|
bookmarks.remove(value)
|
|
|
|
|
2011-11-27 19:24:44 +00:00
|
|
|
def stanza_storage(method):
|
|
|
|
"""Generate a <storage/> stanza with the conference elements."""
|
2013-01-17 16:37:06 +00:00
|
|
|
storage = Bookmarks()
|
2014-04-08 19:06:21 +00:00
|
|
|
for b in (b for b in bookmarks if b.method == method):
|
2011-11-26 22:09:03 +00:00
|
|
|
storage.append(b.stanza())
|
|
|
|
return storage
|
|
|
|
|
2011-11-26 22:08:41 +00:00
|
|
|
def save_pep(xmpp):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Save the remote bookmarks via PEP."""
|
2013-01-17 16:37:06 +00:00
|
|
|
xmpp.plugin['xep_0048'].set_bookmarks(stanza_storage('pep'),
|
|
|
|
method='xep_0223')
|
2011-11-26 22:08:41 +00:00
|
|
|
|
|
|
|
def save_privatexml(xmpp):
|
2011-11-27 19:24:44 +00:00
|
|
|
""""Save the remote bookmarks with privatexml."""
|
2013-01-17 16:37:06 +00:00
|
|
|
xmpp.plugin['xep_0048'].set_bookmarks(stanza_storage('privatexml'),
|
|
|
|
method='xep_0049')
|
2011-11-26 22:08:41 +00:00
|
|
|
|
2012-04-15 21:55:50 +00:00
|
|
|
def save_remote(xmpp, method=preferred):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Save the remote bookmarks."""
|
2014-04-05 12:19:22 +00:00
|
|
|
method = 'privatexml' if method != 'pep' else 'pep'
|
2011-11-27 19:24:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if method is 'privatexml':
|
2013-01-17 16:37:06 +00:00
|
|
|
xmpp.plugin['xep_0048'].set_bookmarks(stanza_storage('privatexml'),
|
|
|
|
method='xep_0049')
|
2011-11-27 19:24:44 +00:00
|
|
|
else:
|
2013-01-17 16:37:06 +00:00
|
|
|
xmpp.plugin['xep_0048'].set_bookmarks(stanza_storage('pep'),
|
|
|
|
method='xep_0223')
|
2014-04-06 17:52:59 +00:00
|
|
|
except Exception:
|
|
|
|
log.error("Could not save the bookmarks:", exc_info=True)
|
2011-11-27 19:24:44 +00:00
|
|
|
return False
|
|
|
|
return True
|
2011-11-26 22:08:41 +00:00
|
|
|
|
|
|
|
def save_local():
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Save the local bookmarks."""
|
2014-04-06 17:52:59 +00:00
|
|
|
local = ''.join(bookmark.local() for bookmark in bookmarks if bookmark.method is 'local')
|
|
|
|
config.set_and_save('rooms', local)
|
2011-11-26 22:08:41 +00:00
|
|
|
|
|
|
|
def save(xmpp, core=None):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Save all the bookmarks."""
|
2011-11-26 22:08:41 +00:00
|
|
|
save_local()
|
2014-03-24 22:25:06 +00:00
|
|
|
if config.get('use_remote_bookmarks', True):
|
2011-11-27 19:24:44 +00:00
|
|
|
preferred = config.get('use_bookmarks_method', 'privatexml')
|
|
|
|
if not save_remote(xmpp, method=preferred) and core:
|
|
|
|
core.information('Could not save bookmarks.', 'Error')
|
2012-03-13 20:39:06 +00:00
|
|
|
return False
|
2011-11-27 19:24:44 +00:00
|
|
|
elif core:
|
2012-03-13 20:39:06 +00:00
|
|
|
core.information('Bookmarks saved', 'Info')
|
2011-11-27 19:24:44 +00:00
|
|
|
return True
|
2011-11-26 22:08:41 +00:00
|
|
|
|
2011-11-26 22:07:31 +00:00
|
|
|
def get_pep(xmpp):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Add the remotely stored bookmarks via pep to the list."""
|
2011-11-26 22:07:31 +00:00
|
|
|
try:
|
2013-01-17 16:37:06 +00:00
|
|
|
iq = xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0223', block=True)
|
2011-11-26 22:07:31 +00:00
|
|
|
except:
|
|
|
|
return False
|
2014-04-06 17:52:59 +00:00
|
|
|
for conf in xml_iter(iq.xml, '{storage:bookmarks}conference'):
|
2011-11-26 22:07:31 +00:00
|
|
|
b = Bookmark.parse_from_element(conf, method='pep')
|
|
|
|
if not get_by_jid(b.jid):
|
|
|
|
bookmarks.append(b)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_privatexml(xmpp):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Add the remotely stored bookmarks via privatexml to the list."""
|
2011-11-26 22:07:31 +00:00
|
|
|
try:
|
2013-01-17 16:37:06 +00:00
|
|
|
iq = xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0049', block=True)
|
2011-11-26 22:07:31 +00:00
|
|
|
except:
|
|
|
|
return False
|
2014-04-06 17:52:59 +00:00
|
|
|
for conf in xml_iter(iq.xml, '{storage:bookmarks}conference'):
|
2011-11-26 22:07:31 +00:00
|
|
|
b = Bookmark.parse_from_element(conf, method='privatexml')
|
|
|
|
if not get_by_jid(b.jid):
|
|
|
|
bookmarks.append(b)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_remote(xmpp):
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Add the remotely stored bookmarks to the list."""
|
2011-11-26 22:07:31 +00:00
|
|
|
if xmpp.anon:
|
|
|
|
return
|
2013-01-17 16:37:06 +00:00
|
|
|
method = config.get('use_bookmarks_method', '')
|
|
|
|
if not method:
|
|
|
|
pep, privatexml = True, True
|
|
|
|
for method in methods[1:]:
|
|
|
|
if method == 'pep':
|
|
|
|
pep = get_pep(xmpp)
|
|
|
|
else:
|
|
|
|
privatexml = get_privatexml(xmpp)
|
|
|
|
if pep and not privatexml:
|
|
|
|
config.set_and_save('use_bookmarks_method', 'pep')
|
|
|
|
elif privatexml and not pep:
|
|
|
|
config.set_and_save('use_bookmarks_method', 'privatexml')
|
|
|
|
elif not pep and not privatexml:
|
|
|
|
config.set_and_save('use_bookmarks_method', '')
|
|
|
|
else:
|
2011-11-26 22:07:31 +00:00
|
|
|
if method == 'pep':
|
2013-01-17 16:37:06 +00:00
|
|
|
get_pep(xmpp)
|
2011-11-26 22:07:31 +00:00
|
|
|
else:
|
2013-01-17 16:37:06 +00:00
|
|
|
get_privatexml(xmpp)
|
2011-11-26 22:07:31 +00:00
|
|
|
|
|
|
|
def get_local():
|
2011-11-27 19:24:44 +00:00
|
|
|
"""Add the locally stored bookmarks to the list."""
|
2011-11-26 22:07:31 +00:00
|
|
|
rooms = config.get('rooms', '')
|
|
|
|
if not rooms:
|
|
|
|
return
|
|
|
|
rooms = rooms.split(':')
|
|
|
|
for room in rooms:
|
2012-08-06 13:38:09 +00:00
|
|
|
jid = safeJID(room)
|
2011-11-26 22:07:31 +00:00
|
|
|
if jid.bare == '':
|
|
|
|
continue
|
|
|
|
if jid.resource != '':
|
|
|
|
nick = jid.resource
|
|
|
|
else:
|
|
|
|
nick = None
|
2012-12-20 17:50:34 +00:00
|
|
|
passwd = config.get_by_tabname('password', '', jid.bare, fallback=False) or None
|
|
|
|
b = Bookmark(jid.bare, autojoin=True, nick=nick, password=passwd, method='local')
|
2011-11-26 22:07:31 +00:00
|
|
|
if not get_by_jid(b.jid):
|
|
|
|
bookmarks.append(b)
|