Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0048.py @ 993:301b342c697a
core: use of the new core.log module:
/!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Apr 2014 19:19:19 +0200 |
parents | 93359853e4bc |
children | 291eb8216f6e |
comparison
equal
deleted
inserted
replaced
992:f51a1895275c | 993:301b342c697a |
---|---|
20 from sat.core.i18n import _, D_ | 20 from sat.core.i18n import _, D_ |
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 logging import debug, info, warning, error | 25 from sat.core.log import getLogger |
26 log = getLogger(__name__) | |
26 from twisted.words.xish import domish | 27 from twisted.words.xish import domish |
27 from twisted.words.protocols.jabber import jid | 28 from twisted.words.protocols.jabber import jid |
28 from twisted.words.protocols.jabber.error import StanzaError | 29 from twisted.words.protocols.jabber.error import StanzaError |
29 | 30 |
30 from twisted.internet import defer | 31 from twisted.internet import defer |
51 URL_KEY = 'url' | 52 URL_KEY = 'url' |
52 MUC_ATTRS = ('autojoin', 'name') | 53 MUC_ATTRS = ('autojoin', 'name') |
53 URL_ATTRS = ('name',) | 54 URL_ATTRS = ('name',) |
54 | 55 |
55 def __init__(self, host): | 56 def __init__(self, host): |
56 info(_("Bookmarks plugin initialization")) | 57 log.info(_("Bookmarks plugin initialization")) |
57 self.host = host | 58 self.host = host |
58 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) | 59 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) |
59 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) | 60 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) |
60 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) | 61 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) |
61 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True) | 62 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True) |
67 except KeyError: | 68 except KeyError: |
68 self.private_plg = None | 69 self.private_plg = None |
69 try: | 70 try: |
70 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 71 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
71 except KeyError: | 72 except KeyError: |
72 info(_("Text commands not available")) | 73 log.info(_("Text commands not available")) |
73 | 74 |
74 @defer.inlineCallbacks | 75 @defer.inlineCallbacks |
75 def profileConnected(self, profile): | 76 def profileConnected(self, profile): |
76 client = self.host.getClient(profile) | 77 client = self.host.getClient(profile) |
77 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, profile) | 78 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, profile) |
104 if storage_type == 'private': | 105 if storage_type == 'private': |
105 try: | 106 try: |
106 bookmarks_private_xml = yield self.private_plg.privateXMLGet('storage', NS_BOOKMARKS, profile) | 107 bookmarks_private_xml = yield self.private_plg.privateXMLGet('storage', NS_BOOKMARKS, profile) |
107 data = client.bookmarks_private = self._bookmarkElt2Dict(bookmarks_private_xml) | 108 data = client.bookmarks_private = self._bookmarkElt2Dict(bookmarks_private_xml) |
108 except (StanzaError, AttributeError): | 109 except (StanzaError, AttributeError): |
109 info(_("Private XML storage not available")) | 110 log.info(_("Private XML storage not available")) |
110 data = client.bookmarks_private = None | 111 data = client.bookmarks_private = None |
111 elif storage_type == 'pubsub': | 112 elif storage_type == 'pubsub': |
112 raise NotImplementedError | 113 raise NotImplementedError |
113 else: | 114 else: |
114 raise ValueError("storage_type must be 'private' or 'pubsub'") | 115 raise ValueError("storage_type must be 'private' or 'pubsub'") |
145 conference_elts = storage_elt.elements(NS_BOOKMARKS, 'conference') | 146 conference_elts = storage_elt.elements(NS_BOOKMARKS, 'conference') |
146 for conference_elt in conference_elts: | 147 for conference_elt in conference_elts: |
147 try: | 148 try: |
148 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) | 149 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) |
149 except KeyError: | 150 except KeyError: |
150 warning ("invalid bookmark found, igoring it:\n%s" % conference_elt.toXml()) | 151 log.warning ("invalid bookmark found, igoring it:\n%s" % conference_elt.toXml()) |
151 continue | 152 continue |
152 | 153 |
153 data = conf_data[room_jid] = {} | 154 data = conf_data[room_jid] = {} |
154 | 155 |
155 for attr in XEP_0048.MUC_ATTRS: | 156 for attr in XEP_0048.MUC_ATTRS: |
164 url_elts = storage_elt.elements(NS_BOOKMARKS, 'url') | 165 url_elts = storage_elt.elements(NS_BOOKMARKS, 'url') |
165 for url_elt in url_elts: | 166 for url_elt in url_elts: |
166 try: | 167 try: |
167 url = url_elt[XEP_0048.URL_KEY] | 168 url = url_elt[XEP_0048.URL_KEY] |
168 except KeyError: | 169 except KeyError: |
169 warning ("invalid bookmark found, igoring it:\n%s" % url_elt.toXml()) | 170 log.warning ("invalid bookmark found, igoring it:\n%s" % url_elt.toXml()) |
170 continue | 171 continue |
171 data = url_data[url] = {} | 172 data = url_data[url] = {} |
172 for attr in XEP_0048.URL_ATTRS: | 173 for attr in XEP_0048.URL_ATTRS: |
173 if url_elt.hasAttribute(attr): | 174 if url_elt.hasAttribute(attr): |
174 data[attr] = url_elt[attr] | 175 data[attr] = url_elt[attr] |
213 def _bookmarkSelectedCb(self, data, profile): | 214 def _bookmarkSelectedCb(self, data, profile): |
214 try: | 215 try: |
215 room_jid_s, nick = data['index'].split(' ', 1) | 216 room_jid_s, nick = data['index'].split(' ', 1) |
216 room_jid = jid.JID(room_jid_s) | 217 room_jid = jid.JID(room_jid_s) |
217 except (KeyError, RuntimeError): | 218 except (KeyError, RuntimeError): |
218 warning(_("No room jid selected")) | 219 log.warning(_("No room jid selected")) |
219 return {} | 220 return {} |
220 | 221 |
221 d = self.host.plugins['XEP-0045'].join(room_jid, nick, {}, profile_key=profile) | 222 d = self.host.plugins['XEP-0045'].join(room_jid, nick, {}, profile_key=profile) |
222 def join_eb(failure): | 223 def join_eb(failure): |
223 warning("Error while trying to join room: %s" % failure) | 224 log.warning("Error while trying to join room: %s" % failure) |
224 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here | 225 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here |
225 return {} | 226 return {} |
226 d.addCallbacks(lambda dummy: {}, join_eb) | 227 d.addCallbacks(lambda dummy: {}, join_eb) |
227 return d | 228 return d |
228 | 229 |
301 storage_type = 'pubsub' | 302 storage_type = 'pubsub' |
302 elif client.bookmarks_private is not None: | 303 elif client.bookmarks_private is not None: |
303 storage_type = 'private' | 304 storage_type = 'private' |
304 else: | 305 else: |
305 storage_type = 'local' | 306 storage_type = 'local' |
306 warning(_("Bookmarks will be local only")) | 307 log.warning(_("Bookmarks will be local only")) |
307 info(_('Type selected for "auto" storage: %s') % storage_type) | 308 log.info(_('Type selected for "auto" storage: %s') % storage_type) |
308 | 309 |
309 if storage_type == 'local': | 310 if storage_type == 'local': |
310 client.bookmarks_local[type_][location] = data | 311 client.bookmarks_local[type_][location] = data |
311 yield client.bookmarks_local.force(type_) | 312 yield client.bookmarks_local.force(type_) |
312 else: | 313 else: |
336 if storage_type in ('all', 'local'): | 337 if storage_type in ('all', 'local'): |
337 try: | 338 try: |
338 del client.bookmarks_local[type_][location] | 339 del client.bookmarks_local[type_][location] |
339 yield client.bookmarks_local.force(type_) | 340 yield client.bookmarks_local.force(type_) |
340 except KeyError: | 341 except KeyError: |
341 debug("Bookmark is not present in local storage") | 342 log.debug("Bookmark is not present in local storage") |
342 | 343 |
343 if storage_type in ('all', 'private'): | 344 if storage_type in ('all', 'private'): |
344 bookmarks = yield self._getServerBookmarks('private', client.profile) | 345 bookmarks = yield self._getServerBookmarks('private', client.profile) |
345 try: | 346 try: |
346 del bookmarks[type_][location] | 347 del bookmarks[type_][location] |
347 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) | 348 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
348 yield self._setServerBookmarks('private', bookmark_elt, client.profile) | 349 yield self._setServerBookmarks('private', bookmark_elt, client.profile) |
349 except KeyError: | 350 except KeyError: |
350 debug("Bookmark is not present in private storage") | 351 log.debug("Bookmark is not present in private storage") |
351 | 352 |
352 if storage_type == 'pubsub': | 353 if storage_type == 'pubsub': |
353 raise NotImplementedError | 354 raise NotImplementedError |
354 | 355 |
355 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): | 356 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): |
420 """(Un)bookmark a MUC room | 421 """(Un)bookmark a MUC room |
421 | 422 |
422 @command (group): [autojoin | remove] | 423 @command (group): [autojoin | remove] |
423 - autojoin: join room automatically on connection | 424 - autojoin: join room automatically on connection |
424 """ | 425 """ |
425 debug("Catched bookmark command") | 426 log.debug("Catched bookmark command") |
426 client = self.host.getClient(profile) | 427 client = self.host.getClient(profile) |
427 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 428 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
428 | 429 |
429 if mess_data['type'] != "groupchat": | 430 if mess_data['type'] != "groupchat": |
430 #/bookmark command does nothing if we are not on a group chat | 431 #/bookmark command does nothing if we are not on a group chat |
431 info("Ignoring /bookmark command on a non groupchat message") | 432 log.info("Ignoring /bookmark command on a non groupchat message") |
432 return True | 433 return True |
433 | 434 |
434 options = mess_data["unparsed"].strip().split() | 435 options = mess_data["unparsed"].strip().split() |
435 if options and options[0] not in ('autojoin', 'remove'): | 436 if options and options[0] not in ('autojoin', 'remove'): |
436 txt_cmd.feedBack(_("Bad arguments"), mess_data, profile) | 437 txt_cmd.feedBack(_("Bad arguments"), mess_data, profile) |