Mercurial > salut
annotate salut.py @ 3:593345584d21 default tip
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 02 Sep 2015 14:02:50 +0200 |
parents | 77b77f48c975 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for account creation (experimental) | |
1 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
6 # Copyright (C) 2015 Adrien Cossa (souliane@mailoo.org) |
0 | 7 |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU Affero General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 from twisted.words.xish import domish | |
22 from wokkel.subprotocols import XMPPHandler, IQHandlerMixin | |
23 from wokkel import disco, iwokkel, data_form | |
24 from twisted.words.protocols.jabber import jid | |
25 from zope.interface import implements | |
26 import uuid | |
27 import sqlite3 | |
28 from os import path | |
29 from collections import OrderedDict | |
30 import gettext | |
31 gettext.install('sat', "i18n", unicode=True) | |
32 | |
33 DATABASE = "salut.db" | |
34 ID_CMD_LIST = disco.DiscoIdentity("automation", "command-list") | |
35 NS_COMMANDS = "http://jabber.org/protocol/commands" | |
36 NS_SEARCH = 'jabber:iq:search' | |
37 QUERY_SEARCH = "/query[@xmlns='jabber:iq:search']" | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
38 UPDATE_CMD = "/iq[@type='set']/command[@xmlns='"+NS_COMMANDS+"' and @node='update']" |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
39 INSTRUCTIONS = _(u'Update your subscription to the Jabber search directory.') |
0 | 40 FORM_INSTRUCTIONS = [INSTRUCTIONS] |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
41 SUBSCRIBE_DONE = _("You registration to the directory has been updated.") |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
42 UNSUBSCRIBE_DONE = _("You have been removed from the directory.") |
0 | 43 DB_CREATE = ['PRAGMA user_version=0', |
44 'CREATE TABLE directory (jid TEXT PRIMARY KEY, description TEXT)'] | |
45 | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
46 |
0 | 47 class SalutGateway(XMPPHandler, IQHandlerMixin): |
48 implements(iwokkel.IDisco) | |
49 | |
50 def __init__(self, component=False): | |
51 XMPPHandler.__init__(self) | |
52 IQHandlerMixin.__init__(self) | |
53 self.component = component | |
54 self.discoHandler = disco.DiscoHandler() | |
55 new_db = not path.exists(DATABASE) | |
56 conn = sqlite3.connect(DATABASE) | |
57 self.db = conn.cursor() # we use SQLite in a blocking way, performance is not such a big deal here | |
58 if new_db: | |
59 for statement in DB_CREATE: | |
60 self.db.execute(statement) | |
61 | |
62 def connectionMade(self): | |
63 print "Connected!" | |
64 self.xmlstream.addObserver("/iq[@type='get']" + QUERY_SEARCH, self.handleFieldsRequest) | |
65 self.xmlstream.addObserver("/iq[@type='set']" + QUERY_SEARCH, self.handleSearchRequest) | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
66 self.xmlstream.addObserver(UPDATE_CMD, self.onUpdate) |
0 | 67 self.discoHandler.setHandlerParent(self.parent) |
68 | |
69 def connectionLost(self, reason): | |
70 print "Disconnected!" | |
71 | |
72 def handleFieldsRequest(self, request): | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
73 """Respond to a fields request with the fields' list. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
74 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
75 @param request (domish.Element): the request to respond to |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
76 """ |
0 | 77 result = domish.Element((None, 'iq')) |
78 result['type'] = 'result' | |
79 result['id'] = request['id'] | |
80 result['to'] = request['from'] | |
81 query_elt = result.addElement('query', NS_SEARCH) | |
82 instructions_elt = query_elt.addElement('instructions', content=INSTRUCTIONS) | |
83 form = data_form.Form('form', title=_('Directory search'), | |
84 instructions=FORM_INSTRUCTIONS, | |
85 formNamespace=NS_SEARCH) | |
86 form.addField(data_form.Field('fixed', label=_('Enter part of description or jid to find somebody,'))) | |
87 form.addField(data_form.Field('fixed', label=('let empty to have a full list of people'))) | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
88 form.addField(data_form.Field('text-single', 'jid', label=_('Jabber ID'))) |
0 | 89 form.addField(data_form.Field('text-single', 'description', label=_('Description'))) |
90 query_elt.addChild(form.toElement()) | |
91 self.xmlstream.send(result) | |
92 | |
93 def handleSearchRequest(self, request): | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
94 """Respond to a search request with the search results. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
95 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
96 @param request (domish.Element): the request to respond to |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
97 """ |
0 | 98 args = OrderedDict() |
99 try: | |
100 query_elt = request.elements(NS_SEARCH, 'query').next() | |
101 form_elt = query_elt.elements(data_form.NS_X_DATA, 'x').next() | |
2
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
102 except StopIteration: |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
103 raise ValueError # TODO: proper error handling |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
104 else: |
0 | 105 parsed_form = data_form.Form.fromElement(form_elt) |
106 for col in ('jid', 'description'): | |
2
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
107 try: |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
108 value = parsed_form[col].strip() |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
109 except KeyError: |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
110 pass |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
111 else: |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
112 if value: |
77b77f48c975
don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents:
1
diff
changeset
|
113 args[col] = value |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
114 |
0 | 115 result = domish.Element((None, 'iq')) |
116 result['type'] = 'result' | |
117 result['id'] = request['id'] | |
118 result['to'] = request['from'] | |
119 query_elt = result.addElement('query', NS_SEARCH) | |
120 x_form = data_form.Form('result', formNamespace = NS_SEARCH) | |
121 x_form_elt = x_form.toElement() | |
122 reported_elt = x_form_elt.addElement('reported') | |
123 jid_field_elt = reported_elt.addElement('field') | |
124 jid_field_elt['label'] = 'Jabber ID' | |
125 jid_field_elt['var'] = 'jid' | |
126 description_field_elt = reported_elt.addElement('field') | |
127 description_field_elt['label'] = 'Description' | |
128 description_field_elt['var'] = 'description' | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
129 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
130 for row in self._getSubscriptions(args): |
0 | 131 for col, value in zip(('jid', 'description'), row): |
132 item_elt = x_form_elt.addElement('item') | |
133 field_elt = item_elt.addElement('field') | |
134 field_elt['var'] = col | |
135 value_elt = field_elt.addElement('value', content=value) | |
136 | |
137 query_elt.addChild(x_form_elt) | |
138 self.xmlstream.send(result) | |
139 | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
140 def _getSubscriptions(self, args=None): |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
141 """Retrieve the subscriptions to the directory. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
142 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
143 @param args (OrderedDict): arguments to filter the request |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
144 @return: list[(boolean, unicode)] |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
145 """ |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
146 query = ["SELECT jid, description FROM directory"] |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
147 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
148 if args: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
149 query.append("WHERE") |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
150 query.append(" AND ".join(("%s LIKE ?" % col for col in args))) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
151 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
152 return self.db.execute(' '.join(query), tuple(['%'+arg+'%' for arg in args.values()])) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
153 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
154 def _getSubscription(self, jid): |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
155 """Retrieve the subscription of the given JID. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
156 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
157 @param jid (jid.JID): JID to search for |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
158 @return: (boolean, unicode) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
159 """ |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
160 return self._getSubscriptions(OrderedDict({'jid': jid.userhost()})).fetchone() |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
161 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
162 def _setSubscription(self, from_jid, subscribed, description): |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
163 """Set the subscription of the given JID. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
164 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
165 @param from_jid (jid.JID): JID to subscribe (or unsubscribe) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
166 @param subscribed (boolean): set to True to subscribe and False to unsubscribe |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
167 @param description (unicode): description |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
168 """ |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
169 if subscribed: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
170 self.db.execute('REPLACE INTO directory(jid,description) VALUES (?,?)', (from_jid.userhost(), description)) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
171 else: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
172 self.db.execute('DELETE FROM directory WHERE jid=?', (from_jid.userhost(),)) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
173 self.db.connection.commit() |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
174 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
175 def onUpdate(self, request): |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
176 """Process the request to update a subscription. |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
177 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
178 @param request (domish.Element): the update request |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
179 """ |
0 | 180 result = domish.Element((None, 'iq')) |
181 result['type'] = 'result' | |
182 result['id'] = request['id'] | |
183 result['to'] = request['from'] | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
184 from_jid = jid.JID(request['from']) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
185 |
0 | 186 request_cmd = request.elements(NS_COMMANDS, 'command').next() |
187 command_elt = result.addElement('command', NS_COMMANDS) | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
188 command_elt['node'] = request_cmd['node'] |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
189 |
0 | 190 try: |
191 session_id = request_cmd['sessionid'] | |
192 except KeyError: | |
193 session_id = None | |
194 | |
195 if session_id is None: | |
196 # first request, we send the form | |
197 command_elt['status'] = 'executing' | |
198 session_id = str(uuid.uuid4()) | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
199 actions_elt = command_elt.addElement("actions") |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
200 actions_elt['execute'] = 'complete' |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
201 actions_elt.addElement('complete') |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
202 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
203 subscription = self._getSubscription(from_jid) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
204 subscribed, desc = ('true', subscription[1]) if subscription else ('false', '') |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
205 |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
206 form = data_form.Form('form', title=_('Directory subscription'), |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
207 instructions=FORM_INSTRUCTIONS, |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
208 formNamespace=NS_SEARCH) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
209 form.addField(data_form.Field('boolean', 'subscribed', label=_('Subscribed'), values=[subscribed])) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
210 form.addField(data_form.Field('text-single', 'description', label=_(u"Some words about you"), values=[desc])) |
0 | 211 command_elt.addChild(form.toElement()) |
212 else: | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
213 try: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
214 request_form = request_cmd.elements(data_form.NS_X_DATA).next() |
0 | 215 except (StopIteration, KeyError): |
216 raise ValueError # TODO: properly cancel the command | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
217 parsed_form = data_form.Form.fromElement(request_form) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
218 subscribed = parsed_form['subscribed'] in ("true", "1") |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
219 description = parsed_form['description'] |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
220 self._setSubscription(from_jid, subscribed, description) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
221 |
0 | 222 command_elt['status'] = 'completed' |
223 note_elt = command_elt.addElement('note') | |
224 note_elt['type'] = 'info' | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
225 if subscribed: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
226 note_elt.addContent(SUBSCRIBE_DONE) |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
227 else: |
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
228 note_elt.addContent(UNSUBSCRIBE_DONE) |
0 | 229 |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
230 command_elt['sessionid'] = session_id |
0 | 231 self.xmlstream.send(result) |
232 | |
233 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
234 return [disco.DiscoFeature(NS_SEARCH), | |
235 disco.DiscoIdentity(u"directory", u"user", u"salut"), | |
236 disco.DiscoFeature(NS_COMMANDS), | |
237 ID_CMD_LIST] | |
238 | |
239 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
240 ret = [] | |
241 if nodeIdentifier == NS_COMMANDS: | |
3
593345584d21
replace ad-hoc commands "subscribe" and "unsubscribe" with a single "update" command
souliane <souliane@mailoo.org>
parents:
2
diff
changeset
|
242 ret.append(disco.DiscoItem(target, "update", "Update your subscription to the directory")) |
0 | 243 return ret |