summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0054/vcard_temp.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-04-07 20:50:02 -0400
committerLance Stout <lancestout@gmail.com>2012-04-07 20:50:02 -0400
commit259c84e99a314fa610b824ff5c92d73b7f3ad64f (patch)
treeb530d7fa251dd29746a1572a5ba49b59ed61a2f3 /sleekxmpp/plugins/xep_0054/vcard_temp.py
parent739128866858670a20b9c0ce5d623ca9848aef3b (diff)
downloadslixmpp-259c84e99a314fa610b824ff5c92d73b7f3ad64f.tar.gz
slixmpp-259c84e99a314fa610b824ff5c92d73b7f3ad64f.tar.bz2
slixmpp-259c84e99a314fa610b824ff5c92d73b7f3ad64f.tar.xz
slixmpp-259c84e99a314fa610b824ff5c92d73b7f3ad64f.zip
Add initial XEP-0054 plugin.
Diffstat (limited to 'sleekxmpp/plugins/xep_0054/vcard_temp.py')
-rw-r--r--sleekxmpp/plugins/xep_0054/vcard_temp.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/xep_0054/vcard_temp.py b/sleekxmpp/plugins/xep_0054/vcard_temp.py
new file mode 100644
index 00000000..9be4c098
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0054/vcard_temp.py
@@ -0,0 +1,118 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp import Iq
+from sleekxmpp.exceptions import XMPPError
+from sleekxmpp.xmlstream import register_stanza_plugin
+from sleekxmpp.xmlstream.handler import Callback
+from sleekxmpp.xmlstream.matcher import StanzaPath
+from sleekxmpp.plugins import BasePlugin
+from sleekxmpp.plugins.xep_0054 import VCardTemp, stanza
+
+
+log = logging.getLogger(__name__)
+
+
+class XEP_0054(BasePlugin):
+
+ """
+ XEP-0054: vcard-temp
+ """
+
+ name = 'xep_0054'
+ description = 'XEP-0054: vcard-temp'
+ dependencies = set(['xep_0030', 'xep_0082'])
+ stanza = stanza
+
+ def plugin_init(self):
+ """
+ Start the XEP-0054 plugin.
+ """
+ register_stanza_plugin(Iq, VCardTemp)
+
+ self.xmpp['xep_0030'].add_feature('vcard-temp')
+
+ self.api.register(self._set_vcard, 'set_vcard', default=True)
+ self.api.register(self._get_vcard, 'get_vcard', default=True)
+ self.api.register(self._del_vcard, 'del_vcard', default=True)
+
+ self._vcard_cache = {}
+
+ self.xmpp.register_handler(
+ Callback('VCardTemp',
+ StanzaPath('iq/vcard_temp'),
+ self._handle_get_vcard))
+
+ def make_vcard(self):
+ return VCardTemp()
+
+ def get_vcard(self, jid=None, ifrom=None, local=False, cached=False,
+ block=True, callback=None, timeout=None):
+ if self.xmpp.is_component and jid.bare == self.xmpp.boundjid.bare:
+ local = True
+
+ if local or cached:
+ vcard = self.api['get_vcard'](jid, None, ifrom)
+ if not isinstance(vcard, Iq):
+ iq = self.xmpp.Iq()
+ iq.append(vcard)
+ return iq
+ return vcard
+
+ iq = self.xmpp.Iq()
+ iq['to'] = jid
+ iq['from'] = ifrom
+ iq['type'] = 'get'
+ iq.enable('vcard_temp')
+
+ vcard = iq.send(block=block, callback=callback, timeout=timeout)
+
+ self.api['set_vcard'](vcard['from'], args=vcard['vcard_temp'])
+ return vcard
+
+ def publish_vcard(self, vcard=None, jid=None, block=True, ifrom=None,
+ callback=None, timeout=None):
+ if self.xmpp.is_component:
+ self.api['set_vcard'](jid, None, ifrom, vcard)
+ return
+
+ iq = self.xmpp.Iq()
+ iq['to'] = jid
+ iq['from'] = ifrom
+ iq['type'] = 'set'
+ iq.append(vcard)
+ return iq.send(block=block, callback=callback, timeout=timeout)
+
+ def _handle_get_vcard(self, iq):
+ if iq['type'] == 'result':
+ self.api['set_vcard'](jid=iq['from'], args=iq['vcard_temp'])
+ return
+ elif iq['type'] == 'get':
+ vcard = self.api['get_vard'](iq['from'].bare)
+ if isinstance(vcard, Iq):
+ vcard.send()
+ else:
+ iq.reply()
+ iq.append(vcard)
+ iq.send()
+ else:
+ raise XMPPError('service-unavailable')
+
+ # =================================================================
+
+ def _set_vcard(self, jid, node, ifrom, vcard):
+ self._vcard_cache[jid.bare] = vcard
+
+ def _get_vcard(self, jid, node, ifrom, vcard):
+ return self._vcard_cache.get(jid.bare, VCardTemp())
+
+ def _del_vcard(self, jid, node, ifrom, vcard):
+ if jid.bare in self._vcard_cache:
+ del self._vcard_cache[jid.bare]