Bump to 1.3.1
[slixmpp.git] / sleekxmpp / plugins / xep_0048 / bookmarks.py
blob0bb5ae389ca754698c8e19cab34c179ee9dda80a
1 """
2 SleekXMPP: The Sleek XMPP Library
3 Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout
4 This file is part of SleekXMPP.
6 See the file LICENSE for copying permission.
7 """
9 import logging
11 from sleekxmpp import Iq
12 from sleekxmpp.plugins import BasePlugin
13 from sleekxmpp.exceptions import XMPPError
14 from sleekxmpp.xmlstream.handler import Callback
15 from sleekxmpp.xmlstream.matcher import StanzaPath
16 from sleekxmpp.xmlstream import register_stanza_plugin
17 from sleekxmpp.plugins.xep_0048 import stanza, Bookmarks, Conference, URL
20 log = logging.getLogger(__name__)
23 class XEP_0048(BasePlugin):
25 name = 'xep_0048'
26 description = 'XEP-0048: Bookmarks'
27 dependencies = set(['xep_0045', 'xep_0049', 'xep_0060', 'xep_0163', 'xep_0223'])
28 stanza = stanza
29 default_config = {
30 'auto_join': False,
31 'storage_method': 'xep_0049'
34 def plugin_init(self):
35 register_stanza_plugin(self.xmpp['xep_0060'].stanza.Item, Bookmarks)
37 self.xmpp['xep_0049'].register(Bookmarks)
38 self.xmpp['xep_0163'].register_pep('bookmarks', Bookmarks)
40 self.xmpp.add_event_handler('session_start', self._autojoin)
42 def plugin_end(self):
43 self.xmpp.del_event_handler('session_start', self._autojoin)
45 def _autojoin(self, __):
46 if not self.auto_join:
47 return
49 try:
50 result = self.get_bookmarks(method=self.storage_method)
51 except XMPPError:
52 return
54 if self.storage_method == 'xep_0223':
55 bookmarks = result['pubsub']['items']['item']['bookmarks']
56 else:
57 bookmarks = result['private']['bookmarks']
59 for conf in bookmarks['conferences']:
60 if conf['autojoin']:
61 log.debug('Auto joining %s as %s', conf['jid'], conf['nick'])
62 self.xmpp['xep_0045'].joinMUC(conf['jid'], conf['nick'],
63 password=conf['password'])
65 def set_bookmarks(self, bookmarks, method=None, **iqargs):
66 if not method:
67 method = self.storage_method
68 return self.xmpp[method].store(bookmarks, **iqargs)
70 def get_bookmarks(self, method=None, **iqargs):
71 if not method:
72 method = self.storage_method
74 loc = 'storage:bookmarks' if method == 'xep_0223' else 'bookmarks'
76 return self.xmpp[method].retrieve(loc, **iqargs)