1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
import logging
import base64
import hashlib
from xml.etree import cElementTree as ET
from sleekxmpp.xmlstream.stanzabase import ElementBase, register_stanza_plugin
from sleekxmpp.plugins import base
from sleekxmpp.xmlstream.handler.callback import Callback
from sleekxmpp.xmlstream.matcher.xpath import MatchXPath
from sleekxmpp.stanza.iq import Iq
from object import Object
from permission import PermissionError
class AliasQuery(ElementBase):
namespace = 'alias:query'
name = 'query'
plugin_attrib = 'alias'
interfaces = set(('node', 'type', 'content', 'permission', 'key'))
sub_interfaces = set(('content', 'permission'))
def addItem(self, node, key, permission = None):
item = AliasItem(None, self)
item['node'] = node
item['key']= key
if permission is not None:
item['permission'] = str(permission)
class AliasItem(ElementBase):
namespace = 'alias:query'
name = 'item'
plugin_attrib = 'item'
interfaces = set(('node', 'permission', 'key' ))
class AliasPlugin(base.base_plugin):
def plugin_init(self):
self.description = 'Plugin to handle alias queries'
register_stanza_plugin(Iq, AliasQuery)
query_parser = MatchXPath('{{{}}}iq/{{{}}}query'.format(self.xmpp.default_ns,
AliasQuery.namespace))
self.xmpp.register_handler(Callback('Alias queries', query_parser,
self.handle_alias_query))
def post_init(self):
base.base_plugin.post_init(self)
self.xmpp.plugin['xep_0030'].add_feature("alias:query")
def send_permission_error(self, iq, message):
node = iq['alias']['node']
iq.reply()
iq['alias']['type'] = 'error'
iq['alias']['node'] = node
iq['alias']['permission'] = message
iq.send()
def handle_alias_query(self, iq):
try:
callee = base64.b64decode(iq['to'].user)
except TypeError:
logging.error("callee field not base64 encoded")
caller = iq['from'].bare
node = iq['alias']['node']
if not node:
node = hashlib.sha1(callee).hexdigest()
node = Object(callee, node)
if iq['alias']['type'] == 'items':
try:
childs = node.get_child_list(caller)
except PermissionError:
self.send_permission_error(iq, 'Permission')
else:
iq.reply()
iq['alias']['type'] = 'items'
iq['alias']['node'] = node.hash
for name, perm, key in childs:
iq['alias'].addItem(name, key, perm)
iq.send()
if iq['alias']['type'] == 'content':
try:
content = node.get_content(caller)
key = node.get_key(caller)
except PermissionError:
self.send_permission_error(iq, 'Permission')
else:
iq.reply()
iq['alias']['type'] = 'content'
iq['alias']['node'] = node.hash
iq['alias']['content'] = content
iq['alias']['key'] = key
iq.send()
|