annotate salut.py @ 0:d1bc50b64974

initial commit
author Goffi <goffi@goffi.org>
date Tue, 25 Feb 2014 22:22:18 +0100
parents
children 92549e4336a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
3
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for account creation (experimental)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
6
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from twisted.words.xish import domish
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from wokkel.subprotocols import XMPPHandler, IQHandlerMixin
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from wokkel import disco, iwokkel, data_form
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from zope.interface import implements
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import uuid
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import sqlite3
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from os import path
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from collections import OrderedDict
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import gettext
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 gettext.install('sat', "i18n", unicode=True)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
31
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 DATABASE = "salut.db"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 ID_CMD_LIST = disco.DiscoIdentity("automation", "command-list")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 NS_COMMANDS = "http://jabber.org/protocol/commands"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35 NS_SEARCH = 'jabber:iq:search'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36 QUERY_SEARCH = "/query[@xmlns='jabber:iq:search']"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 SUBSCRIBE_CMD = "/iq[@type='set']/command[@xmlns='"+NS_COMMANDS+"' and @node='subscribe']"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 UNSUBSCRIBE_CMD = "/iq[@type='set']/command[@xmlns='"+NS_COMMANDS+"' and @node='unsubscribe']"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39 INSTRUCTIONS = _(u'This is a minimal directory for the Libervia demo (Salut à Toi project). For testing purpose only.')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 FORM_INSTRUCTIONS = [INSTRUCTIONS]
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42 SUBSCRIBE_TXT = _(u"Please give some words about you, then submit to be registered on the directory")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43 SUBSCRIBE_DONE = _("You are now registered on the directory")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 UNSUBSCRIBE_DONE = _("You have been removed from the directory")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45 DB_CREATE = ['PRAGMA user_version=0',
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46 'CREATE TABLE directory (jid TEXT PRIMARY KEY, description TEXT)']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 class SalutGateway(XMPPHandler, IQHandlerMixin):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 implements(iwokkel.IDisco)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def __init__(self, component=False):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 XMPPHandler.__init__(self)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 IQHandlerMixin.__init__(self)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.component = component
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.discoHandler = disco.DiscoHandler()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 new_db = not path.exists(DATABASE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 conn = sqlite3.connect(DATABASE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.db = conn.cursor() # we use SQLite in a blocking way, performance is not such a big deal here
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 if new_db:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 for statement in DB_CREATE:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.db.execute(statement)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def connectionMade(self):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 print "Connected!"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.xmlstream.addObserver("/iq[@type='get']" + QUERY_SEARCH, self.handleFieldsRequest)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.xmlstream.addObserver("/iq[@type='set']" + QUERY_SEARCH, self.handleSearchRequest)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.xmlstream.addObserver(SUBSCRIBE_CMD, self.onSubscribe)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.xmlstream.addObserver(UNSUBSCRIBE_CMD, self.onUnsubscribe)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.discoHandler.setHandlerParent(self.parent)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def connectionLost(self, reason):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 print "Disconnected!"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
73
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def handleFieldsRequest(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
75 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
76 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79 query_elt = result.addElement('query', NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80 instructions_elt = query_elt.addElement('instructions', content=INSTRUCTIONS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 form = data_form.Form('form', title=_('Directory search'),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 instructions=FORM_INSTRUCTIONS,
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 formNamespace=NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 form.addField(data_form.Field('fixed', label=_('Enter part of description or jid to find somebody,')))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 form.addField(data_form.Field('fixed', label=('let empty to have a full list of people')))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86 form.addField(data_form.Field('text-single', 'jid', label=_('jid')))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87 form.addField(data_form.Field('text-single', 'description', label=_('Description')))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 query_elt.addChild(form.toElement())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def handleSearchRequest(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92 query = ["SELECT jid, description FROM directory"]
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 args = OrderedDict()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 query_elt = request.elements(NS_SEARCH, 'query').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
96 form_elt = query_elt.elements(data_form.NS_X_DATA, 'x').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 parsed_form = data_form.Form.fromElement(form_elt)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 for col in ('jid', 'description'):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99 value = parsed_form[col].strip()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if value:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101 args[col] = value
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
102 except (StopIteration, KeyError):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
103 raise ValueError # TODO: proper error handling
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
104
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if args:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
106 query.append("WHERE")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107 query.append(" AND ".join(("%s LIKE ?" % col for col in args)))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
108
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109 row_iter = self.db.execute(' '.join(query), tuple(['%'+arg+'%' for arg in args.values()]))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 query_elt = result.addElement('query', NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116 x_form = data_form.Form('result', formNamespace = NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 x_form_elt = x_form.toElement()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 reported_elt = x_form_elt.addElement('reported')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 jid_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 jid_field_elt['label'] = 'Jabber ID'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 jid_field_elt['var'] = 'jid'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 description_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 description_field_elt['label'] = 'Description'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 description_field_elt['var'] = 'description'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 for row in row_iter:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 for col, value in zip(('jid', 'description'), row):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 item_elt = x_form_elt.addElement('item')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
128 field_elt = item_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
129 field_elt['var'] = col
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130 value_elt = field_elt.addElement('value', content=value)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132 query_elt.addChild(x_form_elt)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
134
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def onSubscribe(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
139 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 from_ = jid.JID(request['from'])
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
141 request_cmd = request.elements(NS_COMMANDS, 'command').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 command_elt = result.addElement('command', NS_COMMANDS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 session_id = request_cmd['sessionid']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 except KeyError:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 session_id = None
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if session_id is None:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 # first request, we send the form
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 command_elt['status'] = 'executing'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151 session_id = str(uuid.uuid4())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152 actions_elt = command_elt.addElement('actions')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 actions_elt['execute'] = 'next'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 actions_elt.addElement('next')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 form = data_form.Form('form', instructions=FORM_INSTRUCTIONS, title=_('Directory subscription'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156 infos = data_form.Field('fixed', value=SUBSCRIBE_TXT)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 desc = data_form.Field('text-single', 'description', label=_(u"Some words about you"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 form.addField(infos)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
159 form.addField(desc)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160 command_elt.addChild(form.toElement())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
161 else:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
162 req_forms = request_cmd.elements(data_form.NS_X_DATA, 'x')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164 req_form = req_forms.next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165 parsed_form = data_form.Form.fromElement(req_form)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 description = parsed_form['description']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
167 except (StopIteration, KeyError):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 raise ValueError # TODO: properly cancel the command
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self.db.execute('REPLACE INTO directory(jid,description) VALUES (?,?)', (from_.userhost(), description))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
170 self.db.connection.commit()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
171 command_elt['status'] = 'completed'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
172 note_elt = command_elt.addElement('note')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
173 note_elt['type'] = 'info'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
174 note_elt.addContent(SUBSCRIBE_DONE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
175 command_elt['sessionid'] = session_id
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176 command_elt['node'] = request_cmd['node']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
178
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def onUnsubscribe(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
180 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
181 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
183 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 from_ = jid.JID(request['from'])
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 request_cmd = request.elements(NS_COMMANDS, 'command').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186 command_elt = result.addElement('command', NS_COMMANDS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
187 self.db.execute('DELETE FROM directory WHERE jid=?', (from_.userhost(),))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
188 self.db.connection.commit()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
189 command_elt['status'] = 'completed'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 note_elt = command_elt.addElement('note')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 note_elt['type'] = 'info'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
192 note_elt.addContent(UNSUBSCRIBE_DONE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 command_elt['node'] = request_cmd['node']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 return [disco.DiscoFeature(NS_SEARCH),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198 disco.DiscoIdentity(u"directory", u"user", u"salut"),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199 disco.DiscoFeature(NS_COMMANDS),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
200 ID_CMD_LIST]
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
201
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
202 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
203 ret = []
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if nodeIdentifier == NS_COMMANDS:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
205 ret.append(disco.DiscoItem(target, "subscribe", "Subscribe to the directory"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
206 ret.append(disco.DiscoItem(target, "unsubscribe", "Unsubscribe from the directory"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return ret