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
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)
1
92549e4336a6 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 0
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # (at your option) any later version.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.xish import domish
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from wokkel.subprotocols import XMPPHandler, IQHandlerMixin
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from wokkel import disco, iwokkel, data_form
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from zope.interface import implements
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import uuid
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import sqlite3
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from os import path
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from collections import OrderedDict
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import gettext
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
31 gettext.install('sat', "i18n", unicode=True)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 DATABASE = "salut.db"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 ID_CMD_LIST = disco.DiscoIdentity("automation", "command-list")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35 NS_COMMANDS = "http://jabber.org/protocol/commands"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36 NS_SEARCH = 'jabber:iq:search'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43 DB_CREATE = ['PRAGMA user_version=0',
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 'CREATE TABLE directory (jid TEXT PRIMARY KEY, description TEXT)']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 class SalutGateway(XMPPHandler, IQHandlerMixin):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 implements(iwokkel.IDisco)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, component=False):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51 XMPPHandler.__init__(self)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 IQHandlerMixin.__init__(self)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.component = component
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.discoHandler = disco.DiscoHandler()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 new_db = not path.exists(DATABASE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 conn = sqlite3.connect(DATABASE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 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
58 if new_db:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 for statement in DB_CREATE:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.db.execute(statement)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def connectionMade(self):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 print "Connected!"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.xmlstream.addObserver("/iq[@type='get']" + QUERY_SEARCH, self.handleFieldsRequest)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.discoHandler.setHandlerParent(self.parent)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def connectionLost(self, reason):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70 print "Disconnected!"
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 query_elt = result.addElement('query', NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 instructions_elt = query_elt.addElement('instructions', content=INSTRUCTIONS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 form = data_form.Form('form', title=_('Directory search'),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 instructions=FORM_INSTRUCTIONS,
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 formNamespace=NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86 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
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 form.addField(data_form.Field('text-single', 'description', label=_('Description')))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 query_elt.addChild(form.toElement())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 args = OrderedDict()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 query_elt = request.elements(NS_SEARCH, 'query').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 parsed_form = data_form.Form.fromElement(form_elt)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 query_elt = result.addElement('query', NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 x_form = data_form.Form('result', formNamespace = NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 x_form_elt = x_form.toElement()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 reported_elt = x_form_elt.addElement('reported')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 jid_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 jid_field_elt['label'] = 'Jabber ID'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 jid_field_elt['var'] = 'jid'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 description_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 description_field_elt['label'] = 'Description'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131 for col, value in zip(('jid', 'description'), row):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132 item_elt = x_form_elt.addElement('item')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 field_elt = item_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
134 field_elt['var'] = col
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135 value_elt = field_elt.addElement('value', content=value)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137 query_elt.addChild(x_form_elt)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
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']
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186 request_cmd = request.elements(NS_COMMANDS, 'command').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 session_id = request_cmd['sessionid']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
192 except KeyError:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 session_id = None
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 if session_id is None:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196 # first request, we send the form
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 command_elt['status'] = 'executing'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
211 command_elt.addChild(form.toElement())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
215 except (StopIteration, KeyError):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
222 command_elt['status'] = 'completed'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
223 note_elt = command_elt.addElement('note')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
231 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
232
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
234 return [disco.DiscoFeature(NS_SEARCH),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
235 disco.DiscoIdentity(u"directory", u"user", u"salut"),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
236 disco.DiscoFeature(NS_COMMANDS),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
237 ID_CMD_LIST]
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
238
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
240 ret = []
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
243 return ret