comparison src/plugins/plugin_xep_0115.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 218149ea1a35
children 6b10442e8920
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, error, warning 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.xish import domish 24 from twisted.words.xish import domish
24 from twisted.words.protocols.jabber import jid 25 from twisted.words.protocols.jabber import jid
25 from twisted.internet import defer 26 from twisted.internet import defer
26 from zope.interface import implements 27 from zope.interface import implements
27 from wokkel import disco, iwokkel 28 from wokkel import disco, iwokkel
50 51
51 class XEP_0115(object): 52 class XEP_0115(object):
52 cap_hash = None # capabilities hash is class variable as it is common to all profiles 53 cap_hash = None # capabilities hash is class variable as it is common to all profiles
53 54
54 def __init__(self, host): 55 def __init__(self, host):
55 info(_("Plugin XEP_0115 initialization")) 56 log.info(_("Plugin XEP_0115 initialization"))
56 self.host = host 57 self.host = host
57 host.trigger.add("Disco handled", self._checkHash) 58 host.trigger.add("Disco handled", self._checkHash)
58 host.trigger.add("Presence send", self._presenceTrigger) 59 host.trigger.add("Presence send", self._presenceTrigger)
59 60
60 def getHandler(self, profile): 61 def getHandler(self, profile):
67 if XEP_0115.cap_hash is None: 68 if XEP_0115.cap_hash is None:
68 disco_d.addCallback(lambda dummy: self.host.hasFeature(NS_CAPS_OPTIMIZE, profile_key=profile)) 69 disco_d.addCallback(lambda dummy: self.host.hasFeature(NS_CAPS_OPTIMIZE, profile_key=profile))
69 def updateOptimize(optimize): 70 def updateOptimize(optimize):
70 client.caps_optimize = optimize 71 client.caps_optimize = optimize
71 if optimize: 72 if optimize:
72 info(_("Caps optimisation enabled")) 73 log.info(_("Caps optimisation enabled"))
73 client.caps_sent = False 74 client.caps_sent = False
74 else: 75 else:
75 warning(_("Caps optimisation not available")) 76 log.warning(_("Caps optimisation not available"))
76 disco_d.addCallback(updateOptimize) 77 disco_d.addCallback(updateOptimize)
77 disco_d.addCallback(lambda dummy: self.recalculateHash(profile)) 78 disco_d.addCallback(lambda dummy: self.recalculateHash(profile))
78 return True 79 return True
79 80
80 def _presenceTrigger(self, client, obj): 81 def _presenceTrigger(self, client, obj):
92 _infos = yield client.discoHandler.info(client.jid, client.jid, '') 93 _infos = yield client.discoHandler.info(client.jid, client.jid, '')
93 disco_infos = disco.DiscoInfo() 94 disco_infos = disco.DiscoInfo()
94 for item in _infos: 95 for item in _infos:
95 disco_infos.append(item) 96 disco_infos.append(item)
96 cap_hash = self.host.memory.disco.generateHash(disco_infos) 97 cap_hash = self.host.memory.disco.generateHash(disco_infos)
97 info("Our capability hash has been generated: [%s]" % cap_hash) 98 log.info("Our capability hash has been generated: [%s]" % cap_hash)
98 debug("Generating capability domish.Element") 99 log.debug("Generating capability domish.Element")
99 c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c')) 100 c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c'))
100 c_elt['hash'] = 'sha-1' 101 c_elt['hash'] = 'sha-1'
101 c_elt['node'] = C.APP_URL 102 c_elt['node'] = C.APP_URL
102 c_elt['ver'] = cap_hash 103 c_elt['ver'] = cap_hash
103 XEP_0115.cap_hash = cap_hash 104 XEP_0115.cap_hash = cap_hash
138 try: 139 try:
139 c_ver = c_elem['ver'] 140 c_ver = c_elem['ver']
140 c_hash = c_elem['hash'] 141 c_hash = c_elem['hash']
141 c_node = c_elem['node'] 142 c_node = c_elem['node']
142 except KeyError: 143 except KeyError:
143 warning(_('Received invalid capabilities tag')) 144 log.warning(_('Received invalid capabilities tag'))
144 return 145 return
145 146
146 if c_ver in self.host.memory.disco.hashes: 147 if c_ver in self.host.memory.disco.hashes:
147 # we already know the hash, we update the jid entity 148 # we already know the hash, we update the jid entity
148 debug ("hash [%(hash)s] already in cache, updating entity [%(jid)s]" % {'hash': c_ver, 'jid': from_jid.full()}) 149 log.debug ("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, self.profile) 150 self.host.memory.updateEntityData(from_jid, C.ENTITY_CAP_HASH, c_ver, self.profile)
150 return 151 return
151 152
152 yield self.host.getDiscoInfos(from_jid, self.profile) 153 yield self.host.getDiscoInfos(from_jid, self.profile)
153 if c_hash != 'sha-1': 154 if c_hash != 'sha-1':
154 #unknown hash method 155 #unknown hash method
155 warning(_('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}) 156 log.warning(_('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})
156 computed_hash = self.host.memory.getEntityDatum(from_jid, C.ENTITY_CAP_HASH, self.profile) 157 computed_hash = self.host.memory.getEntityDatum(from_jid, C.ENTITY_CAP_HASH, self.profile)
157 if computed_hash != c_ver: 158 if computed_hash != c_ver:
158 warning(_('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 log.warning(_('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
160 # TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3 161 # TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3