Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0115.py @ 2562:26edcf3a30eb
core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor
sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Apr 2018 19:44:50 +0200 |
parents | src/plugins/plugin_xep_0115.py@60758de1c227 |
children | e70023e84974 |
comparison
equal
deleted
inserted
replaced
2561:bd30dc3ffe5a | 2562:26edcf3a30eb |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for managing xep-0115 | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.constants import Const as C | |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
24 from twisted.words.xish import domish | |
25 from twisted.words.protocols.jabber import jid | |
26 from twisted.internet import defer, error | |
27 from zope.interface import implements | |
28 from wokkel import disco, iwokkel | |
29 | |
30 try: | |
31 from twisted.words.protocols.xmlstream import XMPPHandler | |
32 except ImportError: | |
33 from wokkel.subprotocols import XMPPHandler | |
34 | |
35 PRESENCE = '/presence' | |
36 NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps' | |
37 NS_CAPS_OPTIMIZE = 'http://jabber.org/protocol/caps#optimize' | |
38 CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]' | |
39 | |
40 PLUGIN_INFO = { | |
41 C.PI_NAME: "XEP 0115 Plugin", | |
42 C.PI_IMPORT_NAME: "XEP-0115", | |
43 C.PI_TYPE: "XEP", | |
44 C.PI_MODES: C.PLUG_MODE_BOTH, | |
45 C.PI_PROTOCOLS: ["XEP-0115"], | |
46 C.PI_DEPENDENCIES: [], | |
47 C.PI_MAIN: "XEP_0115", | |
48 C.PI_HANDLER: "yes", | |
49 C.PI_DESCRIPTION: _("""Implementation of entity capabilities""") | |
50 } | |
51 | |
52 | |
53 class XEP_0115(object): | |
54 cap_hash = None # capabilities hash is class variable as it is common to all profiles | |
55 | |
56 def __init__(self, host): | |
57 log.info(_("Plugin XEP_0115 initialization")) | |
58 self.host = host | |
59 host.trigger.add("Presence send", self._presenceTrigger) | |
60 | |
61 def getHandler(self, client): | |
62 return XEP_0115_handler(self, client.profile) | |
63 | |
64 @defer.inlineCallbacks | |
65 def _prepareCaps(self, client): | |
66 # we have to calculate hash for client | |
67 # because disco infos/identities may change between clients | |
68 | |
69 # optimize check | |
70 client._caps_optimize = yield self.host.hasFeature(client, NS_CAPS_OPTIMIZE) | |
71 if client._caps_optimize: | |
72 log.info(_(u"Caps optimisation enabled")) | |
73 client._caps_sent = False | |
74 else: | |
75 log.warning(_(u"Caps optimisation not available")) | |
76 | |
77 # hash generation | |
78 _infos = yield client.discoHandler.info(client.jid, client.jid, '') | |
79 disco_infos = disco.DiscoInfo() | |
80 for item in _infos: | |
81 disco_infos.append(item) | |
82 disco_infos = disco.DiscoInfo() | |
83 cap_hash = client._caps_hash = self.host.memory.disco.generateHash(disco_infos) | |
84 log.info("Our capability hash has been generated: [{cap_hash}]".format( | |
85 cap_hash = cap_hash)) | |
86 log.debug("Generating capability domish.Element") | |
87 c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c')) | |
88 c_elt['hash'] = 'sha-1' | |
89 c_elt['node'] = C.APP_URL | |
90 c_elt['ver'] = cap_hash | |
91 client._caps_elt = c_elt | |
92 if client._caps_optimize: | |
93 client._caps_sent = False | |
94 if cap_hash not in self.host.memory.disco.hashes: | |
95 self.host.memory.disco.hashes[cap_hash] = disco_infos | |
96 self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile) | |
97 | |
98 def _presenceAddElt(self, client, obj): | |
99 if client._caps_optimize: | |
100 if client._caps_sent: | |
101 return | |
102 client.caps_sent = True | |
103 obj.addChild(client._caps_elt) | |
104 | |
105 def _presenceTrigger(self, client, obj, presence_d): | |
106 if not hasattr(client, "_caps_optimize"): | |
107 presence_d.addCallback(lambda __: self._prepareCaps(client)) | |
108 | |
109 presence_d.addCallback(lambda __: self._presenceAddElt(client, obj)) | |
110 return True | |
111 | |
112 | |
113 class XEP_0115_handler(XMPPHandler): | |
114 implements(iwokkel.IDisco) | |
115 | |
116 def __init__(self, plugin_parent, profile): | |
117 self.plugin_parent = plugin_parent | |
118 self.host = plugin_parent.host | |
119 self.profile = profile | |
120 | |
121 def connectionInitialized(self): | |
122 self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update) | |
123 | |
124 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
125 return [disco.DiscoFeature(NS_ENTITY_CAPABILITY), disco.DiscoFeature(NS_CAPS_OPTIMIZE)] | |
126 | |
127 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
128 return [] | |
129 | |
130 def update(self, presence): | |
131 """ | |
132 Manage the capabilities of the entity | |
133 | |
134 Check if we know the version of this capabilities and get the capabilities if necessary | |
135 """ | |
136 from_jid = jid.JID(presence['from']) | |
137 c_elem = presence.elements(NS_ENTITY_CAPABILITY, 'c').next() | |
138 try: | |
139 c_ver = c_elem['ver'] | |
140 c_hash = c_elem['hash'] | |
141 c_node = c_elem['node'] | |
142 except KeyError: | |
143 log.warning(_(u'Received invalid capabilities tag: %s') % c_elem.toXml()) | |
144 return | |
145 | |
146 if c_ver in self.host.memory.disco.hashes: | |
147 # we already know the hash, we update the jid entity | |
148 log.debug(u"hash [%(hash)s] already in cache, updating entity [%(jid)s]" % {'hash': c_ver, 'jid': from_jid.full()}) | |
149 self.host.memory.updateEntityData(from_jid, C.ENTITY_CAP_HASH, c_ver, profile_key=self.profile) | |
150 return | |
151 | |
152 if c_hash != 'sha-1': # unknown hash method | |
153 log.warning(_(u'Unknown hash method for entity capabilities: [%(hash_method)s] (entity: %(jid)s, node: %(node)s)') % {'hash_method':c_hash, 'jid': from_jid, 'node': c_node}) | |
154 | |
155 def cb(dummy): | |
156 computed_hash = self.host.memory.getEntityDatum(from_jid, C.ENTITY_CAP_HASH, self.profile) | |
157 if computed_hash != c_ver: | |
158 log.warning(_(u'Computed hash differ from given hash:\ngiven: [%(given_hash)s]\ncomputed: [%(computed_hash)s]\n(entity: %(jid)s, node: %(node)s)') % {'given_hash':c_ver, 'computed_hash': computed_hash, 'jid': from_jid, 'node': c_node}) | |
159 | |
160 def eb(failure): | |
161 if isinstance(failure.value, error.ConnectionDone): | |
162 return | |
163 msg = failure.value.condition if hasattr(failure.value, 'condition') else failure.getErrorMessage() | |
164 log.error(_(u"Couldn't retrieve disco info for {jid}: {error}").format(jid=from_jid.full(), error=msg)) | |
165 | |
166 d = self.host.getDiscoInfos(self.parent, from_jid) | |
167 d.addCallbacks(cb, eb) | |
168 # TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3 |