annotate src/plugins/plugin_xep_0300.py @ 1793:f39ca2832774

jp: add command "roster purge" to remove the contacts with no from/to subscription
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 18:15:20 +0100
parents d17772b0fe22
children 2daf7b4c6756
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Hash functions (XEP-0300)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1618
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.log import getLogger
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = getLogger(__name__)
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
23 from sat.core import exceptions
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
25 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
26 from twisted.internet import threads
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
27 from twisted.internet import defer
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
28 from zope.interface import implements
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
29 from wokkel import disco, iwokkel
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
30 from collections import OrderedDict
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import hashlib
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 PLUGIN_INFO = {
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "name": "Cryptographic Hash Functions",
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "import_name": "XEP-0300",
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "type": "XEP",
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "protocols": ["XEP-0300"],
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "main": "XEP_0300",
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
40 "handler": "yes",
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "description": _("""Management of cryptographic hashes""")
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 }
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
44 NS_HASHES = "urn:xmpp:hashes:1"
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
45 NS_HASHES_FUNCTIONS = u"urn:xmpp:hash-function-text-names:{}"
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
46 BUFFER_SIZE = 2**12
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
47 ALGO_DEFAULT = 'sha-256'
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
48
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 class XEP_0300(object):
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
51 ALGOS = OrderedDict((
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
52 (u'md5', hashlib.md5),
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
53 (u'sha-1', hashlib.sha1),
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
54 (u'sha-256', hashlib.sha256),
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
55 (u'sha-512', hashlib.sha512),
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
56 ))
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 log.info(_("plugin Hashes initialization"))
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
61 def getHandler(self, profile):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
62 return XEP_0300_handler()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
63
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
64 def getHasher(self, algo):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
65 """Return hasher instance
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
66
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
67 /!\\ blocking method, considere using calculateHashElt
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
68 if you want to hash a big file
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
69 @param algo(unicode): one of the XEP_300.ALGOS keys
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
70 @return (hash object): same object s in hashlib.
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
71 update method need to be called for each chunh
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
72 diget or hexdigest can be used at the end
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
73 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
74 return self.ALGOS[algo]()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
75
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
76 @defer.inlineCallbacks
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
77 def getBestPeerAlgo(self, to_jid, profile):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
78 """Return the best available hashing algorith of other peer
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
79
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
80 @param to_jid(jid.JID): peer jid
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
81 @parm profile: %(doc_profile)s
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
82 @return (D(unicode, None)): best available algorithm,
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
83 or None if hashing is not possible
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
84 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
85 for algo in reversed(XEP_0300.ALGOS):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
86 has_feature = yield self.host.hasFeature(NS_HASHES_FUNCTIONS.format(algo), to_jid, profile)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
87 if has_feature:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
88 log.debug(u"Best hashing algorithm found for {jid}: {algo}".format(
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
89 jid=to_jid.full(),
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
90 algo=algo))
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
91 defer.returnValue(algo)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
92
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
93 def calculateHashBlocking(self, file_obj, hasher):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
94 """Calculate hash in a blocking way
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
95
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
96 @param file_obj(file): a file-like object
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
97 @param hasher(callable): the method to call to initialise hash object
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
98 @return (str): the hex digest of the hash
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
99 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
100 hash_ = hasher()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
101 while True:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
102 buf = file_obj.read(BUFFER_SIZE)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
103 if not buf:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
104 break
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
105 hash_.update(buf)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
106 return hash_.hexdigest()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
107
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
108 def calculateHashElt(self, file_obj=None, algo=ALGO_DEFAULT):
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 """Compute hash and build hash element
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
111 @param file_obj(file, None): file-like object to use to calculate the hash
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
112 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
113 @return (D(domish.Element)): hash element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
114 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
115 def hashCalculated(hash_):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
116 return self.buildHashElt(hash_, algo)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
117 hasher = self.ALGOS[algo]
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
118 hash_d = threads.deferToThread(self.calculateHashBlocking, file_obj, hasher)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
119 hash_d.addCallback(hashCalculated)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
120 return hash_d
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
121
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
122 def buildHashElt(self, hash_=None, algo=ALGO_DEFAULT):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
123 """Compute hash and build hash element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
124
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
125 @param hash_(None, str): hash to use, or None for an empty element
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 @return (domish.Element): computed hash
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 """
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 hash_elt = domish.Element((NS_HASHES, 'hash'))
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
130 if hash_ is not None:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
131 hash_elt.addContent(hash_)
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 hash_elt['algo']=algo
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 return hash_elt
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
135 def parseHashElt(self, parent):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
136 """Find and parse a hash element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
137
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
138 if multiple elements are found, the strongest managed one is returned
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
139 @param (domish.Element): parent of <hash/> element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
140 @return (tuple[unicode, str]): (algo, hash) tuple
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
141 both values can be None if <hash/> is empty
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
142 @raise exceptions.NotFound: the element is not present
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
143 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
144 algos = XEP_0300.ALGOS.keys()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
145 hash_elt = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
146 best_algo = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
147 best_value = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
148 for hash_elt in parent.elements(NS_HASHES, 'hash'):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
149 algo = hash_elt.getAttribute('algo')
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
150 try:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
151 idx = algos.index(algo)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
152 except ValueError:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
153 log.warning(u"Proposed {} algorithm is not managed".format(algo))
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
154 algo = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
155 continue
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
156
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
157 if best_algo is None or algos.index(best_algo) < idx:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
158 best_algo = algo
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
159 best_value = str(hash_elt) or None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
160
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
161 if not hash_elt:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
162 raise exceptions.NotFound
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
163 return best_algo, best_value
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
164
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
165
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
166 class XEP_0300_handler(XMPPHandler):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
167 implements(iwokkel.IDisco)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
168
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
169 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
170 hash_functions_names = [disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) for algo in XEP_0300.ALGOS]
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
171 return [disco.DiscoFeature(NS_HASHES)] + hash_functions_names
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
172
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
173 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
174 return []