Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0048.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 378188abe941 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
21 from sat.core import exceptions | 21 from sat.core import exceptions |
22 from sat.core.constants import Const as C | 22 from sat.core.constants import Const as C |
23 from sat.memory.persistent import PersistentBinaryDict | 23 from sat.memory.persistent import PersistentBinaryDict |
24 from sat.tools import xml_tools | 24 from sat.tools import xml_tools |
25 from sat.core.log import getLogger | 25 from sat.core.log import getLogger |
26 | |
26 log = getLogger(__name__) | 27 log = getLogger(__name__) |
27 from twisted.words.xish import domish | 28 from twisted.words.xish import domish |
28 from twisted.words.protocols.jabber import jid | 29 from twisted.words.protocols.jabber import jid |
29 from twisted.words.protocols.jabber.error import StanzaError | 30 from twisted.words.protocols.jabber.error import StanzaError |
30 | 31 |
31 from twisted.internet import defer | 32 from twisted.internet import defer |
32 | 33 |
33 NS_BOOKMARKS = 'storage:bookmarks' | 34 NS_BOOKMARKS = "storage:bookmarks" |
34 | 35 |
35 PLUGIN_INFO = { | 36 PLUGIN_INFO = { |
36 C.PI_NAME: "Bookmarks", | 37 C.PI_NAME: "Bookmarks", |
37 C.PI_IMPORT_NAME: "XEP-0048", | 38 C.PI_IMPORT_NAME: "XEP-0048", |
38 C.PI_TYPE: "XEP", | 39 C.PI_TYPE: "XEP", |
39 C.PI_PROTOCOLS: ["XEP-0048"], | 40 C.PI_PROTOCOLS: ["XEP-0048"], |
40 C.PI_DEPENDENCIES: ["XEP-0045"], | 41 C.PI_DEPENDENCIES: ["XEP-0045"], |
41 C.PI_RECOMMENDATIONS: ["XEP-0049"], | 42 C.PI_RECOMMENDATIONS: ["XEP-0049"], |
42 C.PI_MAIN: "XEP_0048", | 43 C.PI_MAIN: "XEP_0048", |
43 C.PI_HANDLER: "no", | 44 C.PI_HANDLER: "no", |
44 C.PI_DESCRIPTION: _("""Implementation of bookmarks""") | 45 C.PI_DESCRIPTION: _("""Implementation of bookmarks"""), |
45 } | 46 } |
46 | 47 |
47 | 48 |
48 class XEP_0048(object): | 49 class XEP_0048(object): |
49 MUC_TYPE = 'muc' | 50 MUC_TYPE = "muc" |
50 URL_TYPE = 'url' | 51 URL_TYPE = "url" |
51 MUC_KEY = 'jid' | 52 MUC_KEY = "jid" |
52 URL_KEY = 'url' | 53 URL_KEY = "url" |
53 MUC_ATTRS = ('autojoin', 'name') | 54 MUC_ATTRS = ("autojoin", "name") |
54 URL_ATTRS = ('name',) | 55 URL_ATTRS = ("name",) |
55 | 56 |
56 def __init__(self, host): | 57 def __init__(self, host): |
57 log.info(_("Bookmarks plugin initialization")) | 58 log.info(_("Bookmarks plugin initialization")) |
58 self.host = host | 59 self.host = host |
59 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) | 60 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) |
60 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) | 61 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) |
61 host.importMenu((D_("Groups"), D_("Bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) | 62 host.importMenu( |
62 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True) | 63 (D_("Groups"), D_("Bookmarks")), |
63 host.bridge.addMethod("bookmarksList", ".plugin", in_sign='sss', out_sign='a{sa{sa{ss}}}', method=self._bookmarksList, async=True) | 64 self._bookmarksMenu, |
64 host.bridge.addMethod("bookmarksRemove", ".plugin", in_sign='ssss', out_sign='', method=self._bookmarksRemove, async=True) | 65 security_limit=0, |
65 host.bridge.addMethod("bookmarksAdd", ".plugin", in_sign='ssa{ss}ss', out_sign='', method=self._bookmarksAdd, async=True) | 66 help_string=D_("Use and manage bookmarks"), |
67 ) | |
68 self.__selected_id = host.registerCallback( | |
69 self._bookmarkSelectedCb, with_data=True | |
70 ) | |
71 host.bridge.addMethod( | |
72 "bookmarksList", | |
73 ".plugin", | |
74 in_sign="sss", | |
75 out_sign="a{sa{sa{ss}}}", | |
76 method=self._bookmarksList, | |
77 async=True, | |
78 ) | |
79 host.bridge.addMethod( | |
80 "bookmarksRemove", | |
81 ".plugin", | |
82 in_sign="ssss", | |
83 out_sign="", | |
84 method=self._bookmarksRemove, | |
85 async=True, | |
86 ) | |
87 host.bridge.addMethod( | |
88 "bookmarksAdd", | |
89 ".plugin", | |
90 in_sign="ssa{ss}ss", | |
91 out_sign="", | |
92 method=self._bookmarksAdd, | |
93 async=True, | |
94 ) | |
66 try: | 95 try: |
67 self.private_plg = self.host.plugins["XEP-0049"] | 96 self.private_plg = self.host.plugins["XEP-0049"] |
68 except KeyError: | 97 except KeyError: |
69 self.private_plg = None | 98 self.private_plg = None |
70 try: | 99 try: |
72 except KeyError: | 101 except KeyError: |
73 log.info(_("Text commands not available")) | 102 log.info(_("Text commands not available")) |
74 | 103 |
75 @defer.inlineCallbacks | 104 @defer.inlineCallbacks |
76 def profileConnected(self, client): | 105 def profileConnected(self, client): |
77 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, client.profile) | 106 local = client.bookmarks_local = PersistentBinaryDict( |
107 NS_BOOKMARKS, client.profile | |
108 ) | |
78 yield local.load() | 109 yield local.load() |
79 if not local: | 110 if not local: |
80 local[XEP_0048.MUC_TYPE] = dict() | 111 local[XEP_0048.MUC_TYPE] = dict() |
81 local[XEP_0048.URL_TYPE] = dict() | 112 local[XEP_0048.URL_TYPE] = dict() |
82 private = yield self._getServerBookmarks('private', client.profile) | 113 private = yield self._getServerBookmarks("private", client.profile) |
83 pubsub = client.bookmarks_pubsub = None | 114 pubsub = client.bookmarks_pubsub = None |
84 | 115 |
85 for bookmarks in (local, private, pubsub): | 116 for bookmarks in (local, private, pubsub): |
86 if bookmarks is not None: | 117 if bookmarks is not None: |
87 for (room_jid, data) in bookmarks[XEP_0048.MUC_TYPE].items(): | 118 for (room_jid, data) in bookmarks[XEP_0048.MUC_TYPE].items(): |
88 if data.get('autojoin', 'false') == 'true': | 119 if data.get("autojoin", "false") == "true": |
89 nick = data.get('nick', client.jid.user) | 120 nick = data.get("nick", client.jid.user) |
90 self.host.plugins['XEP-0045'].join(client, room_jid, nick, {}) | 121 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
91 | 122 |
92 @defer.inlineCallbacks | 123 @defer.inlineCallbacks |
93 def _getServerBookmarks(self, storage_type, profile): | 124 def _getServerBookmarks(self, storage_type, profile): |
94 """Get distants bookmarks | 125 """Get distants bookmarks |
95 | 126 |
99 - 'pubsub': XEP-0223 storage | 130 - 'pubsub': XEP-0223 storage |
100 @param profile: %(doc_profile)s | 131 @param profile: %(doc_profile)s |
101 @return: data dictionary, or None if feature is not available | 132 @return: data dictionary, or None if feature is not available |
102 """ | 133 """ |
103 client = self.host.getClient(profile) | 134 client = self.host.getClient(profile) |
104 if storage_type == 'private': | 135 if storage_type == "private": |
105 try: | 136 try: |
106 bookmarks_private_xml = yield self.private_plg.privateXMLGet('storage', NS_BOOKMARKS, profile) | 137 bookmarks_private_xml = yield self.private_plg.privateXMLGet( |
107 data = client.bookmarks_private = self._bookmarkElt2Dict(bookmarks_private_xml) | 138 "storage", NS_BOOKMARKS, profile |
139 ) | |
140 data = client.bookmarks_private = self._bookmarkElt2Dict( | |
141 bookmarks_private_xml | |
142 ) | |
108 except (StanzaError, AttributeError): | 143 except (StanzaError, AttributeError): |
109 log.info(_("Private XML storage not available")) | 144 log.info(_("Private XML storage not available")) |
110 data = client.bookmarks_private = None | 145 data = client.bookmarks_private = None |
111 elif storage_type == 'pubsub': | 146 elif storage_type == "pubsub": |
112 raise NotImplementedError | 147 raise NotImplementedError |
113 else: | 148 else: |
114 raise ValueError("storage_type must be 'private' or 'pubsub'") | 149 raise ValueError("storage_type must be 'private' or 'pubsub'") |
115 defer.returnValue(data) | 150 defer.returnValue(data) |
116 | 151 |
122 - 'private': XEP-0049 storage | 157 - 'private': XEP-0049 storage |
123 - 'pubsub': XEP-0223 storage | 158 - 'pubsub': XEP-0223 storage |
124 @param bookmarks_elt (domish.Element): bookmarks XML | 159 @param bookmarks_elt (domish.Element): bookmarks XML |
125 @param profile: %(doc_profile)s | 160 @param profile: %(doc_profile)s |
126 """ | 161 """ |
127 if storage_type == 'private': | 162 if storage_type == "private": |
128 yield self.private_plg.privateXMLStore(bookmarks_elt, profile) | 163 yield self.private_plg.privateXMLStore(bookmarks_elt, profile) |
129 elif storage_type == 'pubsub': | 164 elif storage_type == "pubsub": |
130 raise NotImplementedError | 165 raise NotImplementedError |
131 else: | 166 else: |
132 raise ValueError("storage_type must be 'private' or 'pubsub'") | 167 raise ValueError("storage_type must be 'private' or 'pubsub'") |
133 | 168 |
134 def _bookmarkElt2Dict(self, storage_elt): | 169 def _bookmarkElt2Dict(self, storage_elt): |
140 - value (dict): data as for addBookmark | 175 - value (dict): data as for addBookmark |
141 """ | 176 """ |
142 conf_data = {} | 177 conf_data = {} |
143 url_data = {} | 178 url_data = {} |
144 | 179 |
145 conference_elts = storage_elt.elements(NS_BOOKMARKS, 'conference') | 180 conference_elts = storage_elt.elements(NS_BOOKMARKS, "conference") |
146 for conference_elt in conference_elts: | 181 for conference_elt in conference_elts: |
147 try: | 182 try: |
148 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) | 183 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) |
149 except KeyError: | 184 except KeyError: |
150 log.warning ("invalid bookmark found, igoring it:\n%s" % conference_elt.toXml()) | 185 log.warning( |
186 "invalid bookmark found, igoring it:\n%s" % conference_elt.toXml() | |
187 ) | |
151 continue | 188 continue |
152 | 189 |
153 data = conf_data[room_jid] = {} | 190 data = conf_data[room_jid] = {} |
154 | 191 |
155 for attr in XEP_0048.MUC_ATTRS: | 192 for attr in XEP_0048.MUC_ATTRS: |
156 if conference_elt.hasAttribute(attr): | 193 if conference_elt.hasAttribute(attr): |
157 data[attr] = conference_elt[attr] | 194 data[attr] = conference_elt[attr] |
158 try: | 195 try: |
159 data['nick'] = unicode(conference_elt.elements(NS_BOOKMARKS, 'nick').next()) | 196 data["nick"] = unicode( |
197 conference_elt.elements(NS_BOOKMARKS, "nick").next() | |
198 ) | |
160 except StopIteration: | 199 except StopIteration: |
161 pass | 200 pass |
162 # TODO: manage password (need to be secured, see XEP-0049 §4) | 201 # TODO: manage password (need to be secured, see XEP-0049 §4) |
163 | 202 |
164 url_elts = storage_elt.elements(NS_BOOKMARKS, 'url') | 203 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") |
165 for url_elt in url_elts: | 204 for url_elt in url_elts: |
166 try: | 205 try: |
167 url = url_elt[XEP_0048.URL_KEY] | 206 url = url_elt[XEP_0048.URL_KEY] |
168 except KeyError: | 207 except KeyError: |
169 log.warning ("invalid bookmark found, igoring it:\n%s" % url_elt.toXml()) | 208 log.warning("invalid bookmark found, igoring it:\n%s" % url_elt.toXml()) |
170 continue | 209 continue |
171 data = url_data[url] = {} | 210 data = url_data[url] = {} |
172 for attr in XEP_0048.URL_ATTRS: | 211 for attr in XEP_0048.URL_ATTRS: |
173 if url_elt.hasAttribute(attr): | 212 if url_elt.hasAttribute(attr): |
174 data[attr] = url_elt[attr] | 213 data[attr] = url_elt[attr] |
183 - value (dict): data as for addBookmark | 222 - value (dict): data as for addBookmark |
184 @return (domish.Element): bookmark element | 223 @return (domish.Element): bookmark element |
185 """ | 224 """ |
186 rooms_data = data.get(XEP_0048.MUC_TYPE, {}) | 225 rooms_data = data.get(XEP_0048.MUC_TYPE, {}) |
187 urls_data = data.get(XEP_0048.URL_TYPE, {}) | 226 urls_data = data.get(XEP_0048.URL_TYPE, {}) |
188 storage_elt = domish.Element((NS_BOOKMARKS, 'storage')) | 227 storage_elt = domish.Element((NS_BOOKMARKS, "storage")) |
189 for room_jid in rooms_data: | 228 for room_jid in rooms_data: |
190 conference_elt = storage_elt.addElement('conference') | 229 conference_elt = storage_elt.addElement("conference") |
191 conference_elt[XEP_0048.MUC_KEY] = room_jid.full() | 230 conference_elt[XEP_0048.MUC_KEY] = room_jid.full() |
192 for attr in XEP_0048.MUC_ATTRS: | 231 for attr in XEP_0048.MUC_ATTRS: |
193 try: | 232 try: |
194 conference_elt[attr] = rooms_data[room_jid][attr] | 233 conference_elt[attr] = rooms_data[room_jid][attr] |
195 except KeyError: | 234 except KeyError: |
196 pass | 235 pass |
197 try: | 236 try: |
198 conference_elt.addElement('nick', content=rooms_data[room_jid]['nick']) | 237 conference_elt.addElement("nick", content=rooms_data[room_jid]["nick"]) |
199 except KeyError: | 238 except KeyError: |
200 pass | 239 pass |
201 | 240 |
202 for url in urls_data: | 241 for url in urls_data: |
203 url_elt = storage_elt.addElement('url') | 242 url_elt = storage_elt.addElement("url") |
204 url_elt[XEP_0048.URL_KEY] = url | 243 url_elt[XEP_0048.URL_KEY] = url |
205 for attr in XEP_0048.URL_ATTRS: | 244 for attr in XEP_0048.URL_ATTRS: |
206 try: | 245 try: |
207 url_elt[attr] = url[attr] | 246 url_elt[attr] = url[attr] |
208 except KeyError: | 247 except KeyError: |
210 | 249 |
211 return storage_elt | 250 return storage_elt |
212 | 251 |
213 def _bookmarkSelectedCb(self, data, profile): | 252 def _bookmarkSelectedCb(self, data, profile): |
214 try: | 253 try: |
215 room_jid_s, nick = data['index'].split(' ', 1) | 254 room_jid_s, nick = data["index"].split(" ", 1) |
216 room_jid = jid.JID(room_jid_s) | 255 room_jid = jid.JID(room_jid_s) |
217 except (KeyError, RuntimeError): | 256 except (KeyError, RuntimeError): |
218 log.warning(_("No room jid selected")) | 257 log.warning(_("No room jid selected")) |
219 return {} | 258 return {} |
220 | 259 |
221 client = self.host.getClient(profile) | 260 client = self.host.getClient(profile) |
222 d = self.host.plugins['XEP-0045'].join(client, room_jid, nick, {}) | 261 d = self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
262 | |
223 def join_eb(failure): | 263 def join_eb(failure): |
224 log.warning(u"Error while trying to join room: {}".format(failure)) | 264 log.warning(u"Error while trying to join room: {}".format(failure)) |
225 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here | 265 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here |
226 return {} | 266 return {} |
267 | |
227 d.addCallbacks(lambda dummy: {}, join_eb) | 268 d.addCallbacks(lambda dummy: {}, join_eb) |
228 return d | 269 return d |
229 | 270 |
230 def _bookmarksMenu(self, data, profile): | 271 def _bookmarksMenu(self, data, profile): |
231 """ XMLUI activated by menu: return Gateways UI | 272 """ XMLUI activated by menu: return Gateways UI |
232 @param profile: %(doc_profile)s | 273 @param profile: %(doc_profile)s |
233 | 274 |
234 """ | 275 """ |
235 client = self.host.getClient(profile) | 276 client = self.host.getClient(profile) |
236 xmlui = xml_tools.XMLUI(title=_('Bookmarks manager')) | 277 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) |
237 adv_list = xmlui.changeContainer('advanced_list', columns=3, selectable='single', callback_id=self.__selected_id) | 278 adv_list = xmlui.changeContainer( |
238 for bookmarks in (client.bookmarks_local, client.bookmarks_private, client.bookmarks_pubsub): | 279 "advanced_list", |
280 columns=3, | |
281 selectable="single", | |
282 callback_id=self.__selected_id, | |
283 ) | |
284 for bookmarks in ( | |
285 client.bookmarks_local, | |
286 client.bookmarks_private, | |
287 client.bookmarks_pubsub, | |
288 ): | |
239 if bookmarks is None: | 289 if bookmarks is None: |
240 continue | 290 continue |
241 for (room_jid, data) in sorted(bookmarks[XEP_0048.MUC_TYPE].items(), key=lambda item: item[1].get('name',item[0].user)): | 291 for (room_jid, data) in sorted( |
292 bookmarks[XEP_0048.MUC_TYPE].items(), | |
293 key=lambda item: item[1].get("name", item[0].user), | |
294 ): | |
242 room_jid_s = room_jid.full() | 295 room_jid_s = room_jid.full() |
243 adv_list.setRowIndex(u'%s %s' % (room_jid_s, data.get('nick') or client.jid.user)) | 296 adv_list.setRowIndex( |
244 xmlui.addText(data.get('name','')) | 297 u"%s %s" % (room_jid_s, data.get("nick") or client.jid.user) |
298 ) | |
299 xmlui.addText(data.get("name", "")) | |
245 xmlui.addJid(room_jid) | 300 xmlui.addJid(room_jid) |
246 if data.get('autojoin', 'false') == 'true': | 301 if data.get("autojoin", "false") == "true": |
247 xmlui.addText('autojoin') | 302 xmlui.addText("autojoin") |
248 else: | 303 else: |
249 xmlui.addEmpty() | 304 xmlui.addEmpty() |
250 adv_list.end() | 305 adv_list.end() |
251 xmlui.addDivider('dash') | 306 xmlui.addDivider("dash") |
252 xmlui.addText(_("add a bookmark")) | 307 xmlui.addText(_("add a bookmark")) |
253 xmlui.changeContainer("pairs") | 308 xmlui.changeContainer("pairs") |
254 xmlui.addLabel(_('Name')) | 309 xmlui.addLabel(_("Name")) |
255 xmlui.addString('name') | 310 xmlui.addString("name") |
256 xmlui.addLabel(_('jid')) | 311 xmlui.addLabel(_("jid")) |
257 xmlui.addString('jid') | 312 xmlui.addString("jid") |
258 xmlui.addLabel(_('Nickname')) | 313 xmlui.addLabel(_("Nickname")) |
259 xmlui.addString('nick', client.jid.user) | 314 xmlui.addString("nick", client.jid.user) |
260 xmlui.addLabel(_('Autojoin')) | 315 xmlui.addLabel(_("Autojoin")) |
261 xmlui.addBool('autojoin') | 316 xmlui.addBool("autojoin") |
262 xmlui.changeContainer("vertical") | 317 xmlui.changeContainer("vertical") |
263 xmlui.addButton(self.__bm_save_id, _("Save"), ('name', 'jid', 'nick', 'autojoin')) | 318 xmlui.addButton(self.__bm_save_id, _("Save"), ("name", "jid", "nick", "autojoin")) |
264 return {'xmlui': xmlui.toXml()} | 319 return {"xmlui": xmlui.toXml()} |
265 | 320 |
266 def _bookmarksSaveCb(self, data, profile): | 321 def _bookmarksSaveCb(self, data, profile): |
267 bm_data = xml_tools.XMLUIResult2DataFormResult(data) | 322 bm_data = xml_tools.XMLUIResult2DataFormResult(data) |
268 try: | 323 try: |
269 location = jid.JID(bm_data.pop('jid')) | 324 location = jid.JID(bm_data.pop("jid")) |
270 except KeyError: | 325 except KeyError: |
271 raise exceptions.InternalError("Can't find mandatory key") | 326 raise exceptions.InternalError("Can't find mandatory key") |
272 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile) | 327 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile) |
273 d.addCallback(lambda dummy: {}) | 328 d.addCallback(lambda dummy: {}) |
274 return d | 329 return d |
275 | 330 |
276 @defer.inlineCallbacks | 331 @defer.inlineCallbacks |
277 def addBookmark(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE): | 332 def addBookmark( |
333 self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE | |
334 ): | |
278 """Store a new bookmark | 335 """Store a new bookmark |
279 | 336 |
280 @param type_: bookmark type, one of: | 337 @param type_: bookmark type, one of: |
281 - XEP_0048.MUC_TYPE: Multi-User chat room | 338 - XEP_0048.MUC_TYPE: Multi-User chat room |
282 - XEP_0048.URL_TYPE: web page URL | 339 - XEP_0048.URL_TYPE: web page URL |
291 - "pubsub": PubSub private storage (XEP-0223) | 348 - "pubsub": PubSub private storage (XEP-0223) |
292 - "private": Private XML storage (XEP-0049) | 349 - "private": Private XML storage (XEP-0049) |
293 - "local": Store in SàT database | 350 - "local": Store in SàT database |
294 @param profile_key: %(doc_profile_key)s | 351 @param profile_key: %(doc_profile_key)s |
295 """ | 352 """ |
296 assert storage_type in ('auto', 'pubsub', 'private', 'local') | 353 assert storage_type in ("auto", "pubsub", "private", "local") |
297 if type_ == XEP_0048.URL_TYPE and {'autojoin', 'nick'}.intersection(data.keys()): | 354 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(data.keys()): |
298 raise ValueError("autojoin or nick can't be used with URLs") | 355 raise ValueError("autojoin or nick can't be used with URLs") |
299 client = self.host.getClient(profile_key) | 356 client = self.host.getClient(profile_key) |
300 if storage_type == 'auto': | 357 if storage_type == "auto": |
301 if client.bookmarks_pubsub is not None: | 358 if client.bookmarks_pubsub is not None: |
302 storage_type = 'pubsub' | 359 storage_type = "pubsub" |
303 elif client.bookmarks_private is not None: | 360 elif client.bookmarks_private is not None: |
304 storage_type = 'private' | 361 storage_type = "private" |
305 else: | 362 else: |
306 storage_type = 'local' | 363 storage_type = "local" |
307 log.warning(_("Bookmarks will be local only")) | 364 log.warning(_("Bookmarks will be local only")) |
308 log.info(_('Type selected for "auto" storage: %s') % storage_type) | 365 log.info(_('Type selected for "auto" storage: %s') % storage_type) |
309 | 366 |
310 if storage_type == 'local': | 367 if storage_type == "local": |
311 client.bookmarks_local[type_][location] = data | 368 client.bookmarks_local[type_][location] = data |
312 yield client.bookmarks_local.force(type_) | 369 yield client.bookmarks_local.force(type_) |
313 else: | 370 else: |
314 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) | 371 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) |
315 bookmarks[type_][location] = data | 372 bookmarks[type_][location] = data |
316 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) | 373 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
317 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) | 374 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) |
318 | 375 |
319 @defer.inlineCallbacks | 376 @defer.inlineCallbacks |
320 def removeBookmark(self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE): | 377 def removeBookmark( |
378 self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE | |
379 ): | |
321 """Remove a stored bookmark | 380 """Remove a stored bookmark |
322 | 381 |
323 @param type_: bookmark type, one of: | 382 @param type_: bookmark type, one of: |
324 - XEP_0048.MUC_TYPE: Multi-User chat room | 383 - XEP_0048.MUC_TYPE: Multi-User chat room |
325 - XEP_0048.URL_TYPE: web page URL | 384 - XEP_0048.URL_TYPE: web page URL |
329 - "pubsub": PubSub private storage (XEP-0223) | 388 - "pubsub": PubSub private storage (XEP-0223) |
330 - "private": Private XML storage (XEP-0049) | 389 - "private": Private XML storage (XEP-0049) |
331 - "local": Store in SàT database | 390 - "local": Store in SàT database |
332 @param profile_key: %(doc_profile_key)s | 391 @param profile_key: %(doc_profile_key)s |
333 """ | 392 """ |
334 assert storage_type in ('all', 'pubsub', 'private', 'local') | 393 assert storage_type in ("all", "pubsub", "private", "local") |
335 client = self.host.getClient(profile_key) | 394 client = self.host.getClient(profile_key) |
336 | 395 |
337 if storage_type in ('all', 'local'): | 396 if storage_type in ("all", "local"): |
338 try: | 397 try: |
339 del client.bookmarks_local[type_][location] | 398 del client.bookmarks_local[type_][location] |
340 yield client.bookmarks_local.force(type_) | 399 yield client.bookmarks_local.force(type_) |
341 except KeyError: | 400 except KeyError: |
342 log.debug("Bookmark is not present in local storage") | 401 log.debug("Bookmark is not present in local storage") |
343 | 402 |
344 if storage_type in ('all', 'private'): | 403 if storage_type in ("all", "private"): |
345 bookmarks = yield self._getServerBookmarks('private', client.profile) | 404 bookmarks = yield self._getServerBookmarks("private", client.profile) |
346 try: | 405 try: |
347 del bookmarks[type_][location] | 406 del bookmarks[type_][location] |
348 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) | 407 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
349 yield self._setServerBookmarks('private', bookmark_elt, client.profile) | 408 yield self._setServerBookmarks("private", bookmark_elt, client.profile) |
350 except KeyError: | 409 except KeyError: |
351 log.debug("Bookmark is not present in private storage") | 410 log.debug("Bookmark is not present in private storage") |
352 | 411 |
353 if storage_type == 'pubsub': | 412 if storage_type == "pubsub": |
354 raise NotImplementedError | 413 raise NotImplementedError |
355 | 414 |
356 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): | 415 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): |
357 """Return stored bookmarks | 416 """Return stored bookmarks |
358 | 417 |
379 data = bookmarks_ori[type_] | 438 data = bookmarks_ori[type_] |
380 for bookmark in data: | 439 for bookmark in data: |
381 ret[_storage_location][bookmark.full()] = data[bookmark].copy() | 440 ret[_storage_location][bookmark.full()] = data[bookmark].copy() |
382 return ret | 441 return ret |
383 | 442 |
384 for _storage_location in ('local', 'private', 'pubsub'): | 443 for _storage_location in ("local", "private", "pubsub"): |
385 if storage_location in ('all', _storage_location): | 444 if storage_location in ("all", _storage_location): |
386 ret[_storage_location] = {} | 445 ret[_storage_location] = {} |
387 if _storage_location in ('private',): | 446 if _storage_location in ("private",): |
388 # we update distant bookmarks, just in case an other client added something | 447 # we update distant bookmarks, just in case an other client added something |
389 d = self._getServerBookmarks(_storage_location, client.profile) | 448 d = self._getServerBookmarks(_storage_location, client.profile) |
390 else: | 449 else: |
391 d = defer.succeed(None) | 450 d = defer.succeed(None) |
392 d.addCallback(fillBookmarks, _storage_location) | 451 d.addCallback(fillBookmarks, _storage_location) |
393 ret_d.addCallback(lambda dummy: d) | 452 ret_d.addCallback(lambda dummy: d) |
394 | 453 |
395 return ret_d | 454 return ret_d |
396 | 455 |
397 def _bookmarksRemove(self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE): | 456 def _bookmarksRemove( |
457 self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE | |
458 ): | |
398 """Return stored bookmarks | 459 """Return stored bookmarks |
399 | 460 |
400 @param type_: bookmark type, one of: | 461 @param type_: bookmark type, one of: |
401 - XEP_0048.MUC_TYPE: Multi-User chat room | 462 - XEP_0048.MUC_TYPE: Multi-User chat room |
402 - XEP_0048.URL_TYPE: web page URL | 463 - XEP_0048.URL_TYPE: web page URL |
410 """ | 471 """ |
411 if type_ == XEP_0048.MUC_TYPE: | 472 if type_ == XEP_0048.MUC_TYPE: |
412 location = jid.JID(location) | 473 location = jid.JID(location) |
413 return self.removeBookmark(type_, location, storage_location, profile_key) | 474 return self.removeBookmark(type_, location, storage_location, profile_key) |
414 | 475 |
415 def _bookmarksAdd(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE): | 476 def _bookmarksAdd( |
477 self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE | |
478 ): | |
416 if type_ == XEP_0048.MUC_TYPE: | 479 if type_ == XEP_0048.MUC_TYPE: |
417 location = jid.JID(location) | 480 location = jid.JID(location) |
418 return self.addBookmark(type_, location, data, storage_type, profile_key) | 481 return self.addBookmark(type_, location, data, storage_type, profile_key) |
419 | 482 |
420 def cmd_bookmark(self, client, mess_data): | 483 def cmd_bookmark(self, client, mess_data): |
425 - remove: remove bookmark(s) for this room | 488 - remove: remove bookmark(s) for this room |
426 """ | 489 """ |
427 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 490 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
428 | 491 |
429 options = mess_data["unparsed"].strip().split() | 492 options = mess_data["unparsed"].strip().split() |
430 if options and options[0] not in ('autojoin', 'remove'): | 493 if options and options[0] not in ("autojoin", "remove"): |
431 txt_cmd.feedBack(client, _("Bad arguments"), mess_data) | 494 txt_cmd.feedBack(client, _("Bad arguments"), mess_data) |
432 return False | 495 return False |
433 | 496 |
434 room_jid = mess_data["to"].userhostJID() | 497 room_jid = mess_data["to"].userhostJID() |
435 | 498 |
436 if "remove" in options: | 499 if "remove" in options: |
437 self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key = client.profile) | 500 self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key=client.profile) |
438 txt_cmd.feedBack(client, _("All [%s] bookmarks are being removed") % room_jid.full(), mess_data) | 501 txt_cmd.feedBack( |
502 client, | |
503 _("All [%s] bookmarks are being removed") % room_jid.full(), | |
504 mess_data, | |
505 ) | |
439 return False | 506 return False |
440 | 507 |
441 data = { "name": room_jid.user, | 508 data = { |
442 "nick": client.jid.user, | 509 "name": room_jid.user, |
443 "autojoin": "true" if "autojoin" in options else "false", | 510 "nick": client.jid.user, |
444 } | 511 "autojoin": "true" if "autojoin" in options else "false", |
512 } | |
445 self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile) | 513 self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile) |
446 txt_cmd.feedBack(client, _("Bookmark added"), mess_data) | 514 txt_cmd.feedBack(client, _("Bookmark added"), mess_data) |
447 | 515 |
448 return False | 516 return False |