annotate src/plugins/plugin_xep_0115.py @ 849:c5a8f602662b

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