1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
"""
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.
Poezio start scenario:
- upon inital connection, poezio will disco#info the server
- the available storage methods will be stored in the available_storage dict
(either 'pep' or 'privatexml')
- if only one is available, poezio will set the use_bookmarks_method config option
to it. If both are, it will be set to 'privatexml' (or if it was previously set, the
value will be kept).
- it will then query the preferred storages for bookmarks and cache them locally
(Bookmark objects with a method='remote' attribute)
Adding a remote bookmark:
- New Bookmark object added to the list with storage='remote'
- All bookmarks are sent to the storage selected in use_bookmarks_method
if there was an error, the user is notified.
"""
import functools
import logging
from gettext import gettext as _
from slixmpp.plugins.xep_0048 import Bookmarks, Conference, URL
from slixmpp import JID
from common import safeJID
from config import config
log = logging.getLogger(__name__)
class Bookmark(object):
def __init__(self, jid, name=None, autojoin=False, nick=None, password=None, method='local'):
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 ('local', 'remote'):
log.debug('Could not set bookmark storing method: %s', value)
return
self._method = value
def __repr__(self):
return '<%s%s|%s>' % (self.jid,
('/'+self.nick) if self.nick else '',
self.method)
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:
el['nick'] = self.nick
if self.password:
el['password'] = self.password
return el
def local(self):
"""Generate a str for local storage"""
local = self.jid
if self.nick:
local += '/%s' % self.nick
local += ':'
if self.password:
config.set_and_save('password', self.password, section=self.jid)
return local
@functools.singledispatch
@staticmethod
def parse(el):
"""
Generate a Bookmark object from a <conference/> element
(this is a fallback for raw XML Elements)
"""
jid = el.get('jid')
name = el.get('name')
autojoin = True if el.get('autojoin', 'false').lower() in ('true', '1') else False
nick = None
for n in el.iter('nick'):
nick = n.text
password = None
for p in el.iter('password'):
password = p.text
return Bookmark(jid, name, autojoin, nick, password, method='remote')
@staticmethod
@parse.register(Conference)
def parse_from_stanza(el):
"""
Parse a Conference element into a Bookmark object
"""
jid = el['jid']
autojoin = el['autojoin']
password = el['password']
nick = el['nick']
name = el['name']
return Bookmark(jid, name, autojoin, nick, password, method='remote')
class BookmarkList(object):
def __init__(self):
self.bookmarks = []
preferred = config.get('use_bookmarks_method').lower()
if preferred not in ('pep', 'privatexml'):
preferred = 'privatexml'
self.preferred = preferred
self.available_storage = {
'privatexml': False,
'pep': False,
}
def __getitem__(self, key):
if isinstance(key, (str, JID)):
for i in self.bookmarks:
if key == i.jid:
return i
else:
return self.bookmarks[key]
def __in__(self, key):
if isinstance(key, (str, JID)):
for bookmark in self.bookmarks:
if bookmark.jid == key:
return True
else:
return key in self.bookmarks
return False
def remove(self, key):
if isinstance(key, (str, JID)):
for i in self.bookmarks[:]:
if i.jid == key:
self.bookmarks.remove(i)
else:
self.bookmarks.remove(key)
def __iter__(self):
return iter(self.bookmarks)
def local(self):
return [bm for bm in self.bookmarks if bm.method == 'local']
def remote(self):
return [bm for bm in self.bookmarks if bm.method == 'remote']
def set(self, new):
self.bookmarks = new
def append(self, bookmark):
bookmark_exists = self[bookmark.jid]
if not bookmark_exists:
self.bookmarks.append(bookmark)
else:
self.bookmarks.remove(bookmark_exists)
self.bookmarks.append(bookmark)
def set_bookmarks_method(self, value):
if self.available_storage.get(value):
self.preferred = value
config.set_and_save('use_bookmarks_method', value)
def save_remote(self, xmpp, callback):
"""Save the remote bookmarks."""
if not any(self.available_storage.values()):
return
method = 'xep_0049' if self.preferred == 'privatexml' else 'xep_0223'
if method:
xmpp.plugin['xep_0048'].set_bookmarks(stanza_storage(self.bookmarks),
method=method,
callback=callback)
def save_local(self):
"""Save the local bookmarks."""
local = ''.join(bookmark.local() for bookmark in self if bookmark.method == 'local')
config.set_and_save('rooms', local)
def save(self, xmpp, core=None, callback=None):
"""Save all the bookmarks."""
self.save_local()
def _cb(iq):
if callback:
callback(iq)
if iq["type"] == "error" and core:
core.information('Could not save remote bookmarks.', 'Error')
elif core:
core.information('Bookmarks saved', 'Info')
if config.get('use_remote_bookmarks'):
self.save_remote(xmpp, _cb)
def get_pep(self, xmpp, callback):
"""Add the remotely stored bookmarks via pep to the list."""
def _cb(iq):
if iq['type'] == 'result':
for conf in iq['pubsub']['items']['item']['bookmarks']['conferences']:
if isinstance(conf, URL):
continue
b = Bookmark.parse(conf)
self.append(b)
if callback:
callback(iq)
xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0223', callback=_cb)
def get_privatexml(self, xmpp, callback):
"""
Fetch the remote bookmarks stored via privatexml.
"""
def _cb(iq):
if iq['type'] == 'result':
for conf in iq['private']['bookmarks']['conferences']:
b = Bookmark.parse(conf)
self.append(b)
if callback:
callback(iq)
xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0049', callback=_cb)
def get_remote(self, xmpp, information, callback):
"""Add the remotely stored bookmarks to the list."""
if xmpp.anon or not any(self.available_storage.values()):
information(_('No remote bookmark storage available'), 'Warning')
return
if self.preferred == 'privatexml':
self.get_privatexml(xmpp, callback=callback)
else:
self.get_pep(xmpp, callback=callback)
def get_local(self):
"""Add the locally stored bookmarks to the list."""
rooms = config.get('rooms')
if not rooms:
return
rooms = rooms.split(':')
for room in rooms:
jid = safeJID(room)
if jid.bare == '':
continue
if jid.resource != '':
nick = jid.resource
else:
nick = None
passwd = config.get_by_tabname('password', jid.bare, fallback=False) or None
b = Bookmark(jid.bare, autojoin=True, nick=nick, password=passwd, method='local')
self.append(b)
def stanza_storage(bookmarks):
"""Generate a <storage/> stanza with the conference elements."""
storage = Bookmarks()
for b in (b for b in bookmarks if b.method == 'remote'):
storage.append(b.stanza())
return storage
|