Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0115.py @ 472:813a869315dc
misc: Add the forgotten sat.memory to setup.py.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 14 Apr 2012 11:09:31 +0200 |
parents | cf005701624b |
children | 2a072735e459 |
rev | line source |
---|---|
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for managing xep-0115 |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, error, warning |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.xish import domish |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.protocols.jabber import client, jid, xmlstream |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import error as jab_error |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber.xmlstream import IQ |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
27 from sat.memory.persistent import PersistentBinaryDict |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 import os.path |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 import types |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from zope.interface import implements |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from wokkel import disco, iwokkel |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from hashlib import sha1 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from base64 import b64encode |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 try: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 from twisted.words.protocols.xmlstream import XMPPHandler |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 except ImportError: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 from wokkel.subprotocols import XMPPHandler |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 PRESENCE = '/presence' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 PLUGIN_INFO = { |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 "name": "XEP 0115 Plugin", |
291 | 49 "import_name": "XEP-0115", |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 "type": "XEP", |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 "protocols": ["XEP-0115"], |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 "dependencies": [], |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 "main": "XEP_0115", |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 "handler": "yes", |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 "description": _("""Implementation of entity capabilities""") |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 } |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 class HashGenerationError(Exception): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 pass |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 class ByteIdentity(object): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 """This class manage identity as bytes (needed for i;octet sort), |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 it is used for the hash generation""" |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 def __init__(self, identity, lang=None): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 assert isinstance(identity, disco.DiscoIdentity) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 self.category = identity.category.encode('utf-8') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 self.idType = identity.type.encode('utf-8') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 self.name = identity.name.encode('utf-8') if identity.name else '' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 self.lang = lang.encode('utf-8') if lang else '' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 def __str__(self): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 class XEP_0115(): |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
77 cap_hash = None #capabilities hash is class variable as it is common to all profiles |
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
78 #TODO: this code is really dirty, need to clean it and try to move it to Wokkel |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def __init__(self, host): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 info(_("Plugin XEP_0115 initialization")) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 self.host = host |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 host.trigger.add("Disco Handled", self.checkHash) |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
84 self.hash_cache = PersistentBinaryDict(NS_ENTITY_CAPABILITY) #key = hash or jid, value = features |
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
85 self.hash_cache.load() |
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
86 self.jid_hash = {} #jid to hash mapping, map to a discoInfo features if the hash method is unknown |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 def checkHash(self, profile): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 if not XEP_0115.cap_hash: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 XEP_0115.cap_hash = self.generateHash(profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 else: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 self.presenceHack(profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return True |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 def getHandler(self, profile): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 return XEP_0115_handler(self, profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 def presenceHack(self, profile): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 """modify SatPresenceProtocol to add capabilities data""" |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 client=self.host.getClient(profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 presenceInst = client.presence |
283
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
102 c_elt = domish.Element((NS_ENTITY_CAPABILITY,'c')) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
103 c_elt['hash']='sha-1' |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
104 c_elt['node']='http://wiki.goffi.org/wiki/Salut_%C3%A0_Toi' |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
105 c_elt['ver']=XEP_0115.cap_hash |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
106 presenceInst._c_elt = c_elt |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
107 if "_legacy_send" in dir(presenceInst): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
108 debug('capabilities already added to presence instance') |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
109 return |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def hacked_send(self, obj): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 obj.addChild(self._c_elt) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 self._legacy_send(obj) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 new_send = types.MethodType(hacked_send, presenceInst, presenceInst.__class__) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 presenceInst._legacy_send = presenceInst.send |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 presenceInst.send = new_send |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 def generateHash(self, profile_key="@DEFAULT@"): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """This method generate a sha1 hash as explained in xep-0115 #5.1 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 it then store it in XEP_0115.cap_hash""" |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 profile = self.host.memory.getProfileName(profile_key) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 if not profile: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 error ('Requesting hash for an inexistant profile') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 raise HashGenerationError |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 client = self.host.getClient(profile_key) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 if not client: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 error ('Requesting hash for an inexistant client') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 raise HashGenerationError |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 def generateHash_2(services, profile): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 _s=[] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 byte_identities = [ByteIdentity(identity) for identity in filter(lambda x:isinstance(x,disco.DiscoIdentity),services)] #FIXME: lang must be managed here |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 byte_identities.sort(key=lambda i:i.lang) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 byte_identities.sort(key=lambda i:i.idType) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 byte_identities.sort(key=lambda i:i.category) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 for identity in byte_identities: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 _s.append(str(identity)) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 _s.append('<') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 byte_features = [feature.encode('utf-8') for feature in filter(lambda x:isinstance(x,disco.DiscoFeature),services)] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 byte_features.sort() #XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 for feature in byte_features: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 _s.append(feature) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 _s.append('<') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 #TODO: manage XEP-0128 data form here |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 XEP_0115.cap_hash = b64encode(sha1(''.join(_s)).digest()) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 debug(_('Capability hash generated: [%s]') % XEP_0115.cap_hash) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 self.presenceHack(profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 services = client.discoHandler.info(client.jid, client.jid, '').addCallback(generateHash_2, profile) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 class XEP_0115_handler(XMPPHandler): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 implements(iwokkel.IDisco) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 def __init__(self, plugin_parent, profile): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 self.plugin_parent = plugin_parent |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 self.host = plugin_parent.host |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 self.profile = profile |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 def connectionInitialized(self): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 return [disco.DiscoFeature(NS_ENTITY_CAPABILITY)] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 return [] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 def _updateCache(self, discoResult, from_jid, key): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 """Actually update the cache |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
171 @param discoResult: result of the requestInfo""" |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 if key: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 self.plugin_parent.jid_hash[from_jid] = key |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
174 self.plugin_parent.hash_cache[key] = discoResult.features |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 else: |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
176 #No key, that means unknown hash method |
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
177 self.plugin_parent.jid_hash[from_jid] = discoResult.features |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 def update(self, presence): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 """ |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 Manage the capabilities of the entity |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 Check if we know the version of this capatilities |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 and get the capibilities if necessary |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 """ |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 from_jid = jid.JID(presence['from']) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 c_elem = filter (lambda x:x.name == "c", presence.elements())[0] #We only want the "c" element |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 try: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 ver=c_elem['ver'] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 hash=c_elem['hash'] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 node=c_elem['node'] |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 except KeyError: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 warning('Received invalid capabilities tag') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 return |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
195 if not from_jid in self.plugin_parent.jid_hash: |
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
196 if ver in self.plugin_parent.hash_cache: |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 #we know that hash, we just link it with the jid |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 self.plugin_parent.jid_hash[from_jid] = ver |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 else: |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 if hash!='sha-1': |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 #unknown hash method |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 warning('Unknown hash for entity capabilities: [%s]' % hash) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 self.parent.disco.requestInfo(from_jid).addCallback(self._updateCache, from_jid, ver if hash=='sha-1' else None ) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 #TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 |