annotate salut.py @ 2:77b77f48c975

don't expect queries to specify all the fields
author souliane <souliane@mailoo.org>
date Thu, 23 Jul 2015 21:05:51 +0200
parents 92549e4336a6
children 593345584d21
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)
0
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()
2
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
97 except StopIteration:
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
98 raise ValueError # TODO: proper error handling
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
99 else:
0
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 parsed_form = data_form.Form.fromElement(form_elt)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101 for col in ('jid', 'description'):
2
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
102 try:
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
103 value = parsed_form[col].strip()
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
104 except KeyError:
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
105 pass
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
106 else:
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
107 if value:
77b77f48c975 don't expect queries to specify all the fields
souliane <souliane@mailoo.org>
parents: 1
diff changeset
108 args[col] = value
0
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if args:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 query.append("WHERE")
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112 query.append(" AND ".join(("%s LIKE ?" % col for col in args)))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 row_iter = self.db.execute(' '.join(query), tuple(['%'+arg+'%' for arg in args.values()]))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 query_elt = result.addElement('query', NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 x_form = data_form.Form('result', formNamespace = NS_SEARCH)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 x_form_elt = x_form.toElement()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 reported_elt = x_form_elt.addElement('reported')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 jid_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 jid_field_elt['label'] = 'Jabber ID'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 jid_field_elt['var'] = 'jid'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 description_field_elt = reported_elt.addElement('field')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
128 description_field_elt['label'] = 'Description'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
129 description_field_elt['var'] = 'description'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130 for row in row_iter:
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
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def onSubscribe(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
141 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 from_ = jid.JID(request['from'])
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 request_cmd = request.elements(NS_COMMANDS, 'command').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147 command_elt = result.addElement('command', NS_COMMANDS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 session_id = request_cmd['sessionid']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 except KeyError:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151 session_id = None
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 if session_id is None:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 # first request, we send the form
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 command_elt['status'] = 'executing'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156 session_id = str(uuid.uuid4())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 actions_elt = command_elt.addElement('actions')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 actions_elt['execute'] = 'next'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
159 actions_elt.addElement('next')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160 form = data_form.Form('form', instructions=FORM_INSTRUCTIONS, title=_('Directory subscription'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
161 infos = data_form.Field('fixed', value=SUBSCRIBE_TXT)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
162 desc = data_form.Field('text-single', 'description', label=_(u"Some words about you"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 form.addField(infos)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164 form.addField(desc)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165 command_elt.addChild(form.toElement())
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 else:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
167 req_forms = request_cmd.elements(data_form.NS_X_DATA, 'x')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 try:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 req_form = req_forms.next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
170 parsed_form = data_form.Form.fromElement(req_form)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
171 description = parsed_form['description']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
172 except (StopIteration, KeyError):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
173 raise ValueError # TODO: properly cancel the command
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
174 self.db.execute('REPLACE INTO directory(jid,description) VALUES (?,?)', (from_.userhost(), description))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
175 self.db.connection.commit()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176 command_elt['status'] = 'completed'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 note_elt = command_elt.addElement('note')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
178 note_elt['type'] = 'info'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
179 note_elt.addContent(SUBSCRIBE_DONE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
180 command_elt['sessionid'] = session_id
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
181 command_elt['node'] = request_cmd['node']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
183
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 def onUnsubscribe(self, request):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 result = domish.Element((None, 'iq'))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186 result['type'] = 'result'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
187 result['id'] = request['id']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
188 result['to'] = request['from']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
189 from_ = jid.JID(request['from'])
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 request_cmd = request.elements(NS_COMMANDS, 'command').next()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 command_elt = result.addElement('command', NS_COMMANDS)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.db.execute('DELETE FROM directory WHERE jid=?', (from_.userhost(),))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 self.db.connection.commit()
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194 command_elt['status'] = 'completed'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 note_elt = command_elt.addElement('note')
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196 note_elt['type'] = 'info'
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 note_elt.addContent(UNSUBSCRIBE_DONE)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198 command_elt['node'] = request_cmd['node']
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199 self.xmlstream.send(result)
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
200
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
201 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
202 return [disco.DiscoFeature(NS_SEARCH),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
203 disco.DiscoIdentity(u"directory", u"user", u"salut"),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
204 disco.DiscoFeature(NS_COMMANDS),
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
205 ID_CMD_LIST]
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
206
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
207 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ret = []
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
209 if nodeIdentifier == NS_COMMANDS:
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
210 ret.append(disco.DiscoItem(target, "subscribe", "Subscribe to the directory"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
211 ret.append(disco.DiscoItem(target, "unsubscribe", "Unsubscribe from the directory"))
d1bc50b64974 initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
212 return ret