annotate src/plugins/plugin_xep_0055.py @ 1156:cc022c1cc09f

misc (install): fixed self.root access when it's not used
author Goffi <goffi@goffi.org>
date Wed, 03 Sep 2014 17:55:05 +0200
parents 301b342c697a
children 16484ebb695b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
3
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jabber Search (xep-0055)
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 809
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
6
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
11
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
16
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
19
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
20 from sat.core.i18n import _, D_
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
21 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
22 log = getLogger(__name__)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.xmlstream import IQ
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
25 from twisted.internet import defer
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import data_form
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.exceptions import DataError
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
28 from sat.memory.memory import Sessions
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
29 from sat.tools import xml_tools
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
30
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
31 NS_SEARCH = 'jabber:iq:search'
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
32
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
34 "name": "Jabber Search",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "import_name": "XEP-0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "type": "XEP",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "protocols": ["XEP-0055"],
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "main": "XEP_0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "handler": "no",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "description": _("""Implementation of Jabber Search""")
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
42
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
43
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class XEP_0055(object):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
45
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
47 log.info(_("Jabber search plugin initialization"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.host = host
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
49 host.bridge.addMethod("getSearchUI", ".plugin", in_sign='ss', out_sign='s',
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
50 method=self._getSearchUI,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
51 async=True)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
52 host.bridge.addMethod("searchRequest", ".plugin", in_sign='sa{ss}s', out_sign='s',
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
53 method=self._searchRequest,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
54 async=True)
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
55 self._sessions = Sessions()
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
56 self.__menu_cb_id = host.registerCallback(self._menuCb, with_data=True)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
57 self.__search_request_id = host.registerCallback(self._xmluiSearchRequest, with_data=True)
809
743b757777d3 core: security limit in menus
Goffi <goffi@goffi.org>
parents: 802
diff changeset
58 host.importMenu((D_("Communication"), D_("Search directory")), self._searchMenu, security_limit=1, help_string=D_("Search use directory"))
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
59
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
60 def _menuCb(self, data, profile):
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
61 entity = jid.JID(data[xml_tools.SAT_FORM_PREFIX+'jid'])
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
62 d = self.getSearchUI(entity, profile)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
63 def gotXMLUI(xmlui):
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
64 session_id, session_data = self._sessions.newSession(profile=profile)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
65 session_data['jid'] = entity
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
66 xmlui.session_id = session_id # we need to keep track of the session
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
67 xmlui.submit_id = self.__search_request_id
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
68 return {'xmlui': xmlui.toXml()}
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
69 d.addCallback(gotXMLUI)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
70 return d
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
71
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
72
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
73 def _searchMenu(self, menu_data, profile):
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
74 """ First XMLUI activated by menu: ask for target jid
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
75 @param profile: %(doc_profile)s
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
76
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
77 """
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
78 form_ui = xml_tools.XMLUI("form", title=_("Search directory"), submit_id=self.__menu_cb_id)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
79 form_ui.addText(_("Please enter the search jid"), 'instructions')
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 799
diff changeset
80 form_ui.changeContainer("pairs")
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
81 form_ui.addLabel("jid")
867
40ad5a344332 plugin XEP-0055: salut.libervia.org is temporarly used as default value
Goffi <goffi@goffi.org>
parents: 811
diff changeset
82 # form_ui.addString("jid", value="users.jabberfr.org") # TODO: replace users.jabberfr.org by any XEP-0055 compatible service discovered on current server
40ad5a344332 plugin XEP-0055: salut.libervia.org is temporarly used as default value
Goffi <goffi@goffi.org>
parents: 811
diff changeset
83 form_ui.addString("jid", value="salut.libervia.org") # TODO: replace salut.libervia.org by any XEP-0055 compatible service discovered on current server
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
84 return {'xmlui': form_ui.toXml()}
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
85
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def _getSearchUI(self, to_jid_s, profile_key):
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
87 d = self.getSearchUI(jid.JID(to_jid_s), profile_key)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
88 d.addCallback(lambda xmlui: xmlui.toXml())
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
89 return d
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
90
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def getSearchUI(self, to_jid, profile_key):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
92 """ Ask for a search interface
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @param to_jid: XEP-0055 compliant search entity
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @param profile_key: %(doc_profile_key)s
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @return: XMLUI search interface """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
96 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
97 fields_request = IQ(client.xmlstream, 'get')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
98 fields_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
99 fields_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
100 fields_request.addElement('query', NS_SEARCH)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
101 d = fields_request.send(to_jid.full())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
102 d.addCallbacks(self._fieldsOk, self._fieldsErr, callbackArgs=[client.profile], errbackArgs=[client.profile])
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
103 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
104
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def _fieldsOk(self, answer, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
106 """got fields available"""
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
107 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
108 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
110 log.info(_("No query element found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
111 raise DataError # FIXME: StanzaError is probably more appropriate, check the RFC
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
112 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
113 form_elt = query_elts.elements(data_form.NS_X_DATA, 'x').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
114 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
115 log.info(_("No data form found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
116 raise NotImplementedError("Only search through data form is implemented so far")
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
117 parsed_form = data_form.Form.fromElement(form_elt)
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
118 return xml_tools.dataForm2XMLUI(parsed_form, "")
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
119
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def _fieldsErr(self, failure, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
121 """ Called when something is wrong with fields request """
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
122 log.info(_("Fields request failure: %s") % str(failure.value))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return failure
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
124
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
125 def _xmluiSearchRequest(self, raw_data, profile):
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
126 try:
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
127 session_data = self._sessions.profileGet(raw_data["session_id"], profile)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
128 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
129 log.warning ("session id doesn't exist, session has probably expired")
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
130 # TODO: send error dialog
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
131 return defer.succeed({})
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
132
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
133 data = xml_tools.XMLUIResult2DataFormResult(raw_data)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
134 entity =session_data['jid']
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
135 d = self.searchRequest(entity, data, profile)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
136 d.addCallback(lambda xmlui: {'xmlui':xmlui.toXml()})
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
137 del self._sessions[raw_data["session_id"]]
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
138 return d
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
139
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def _searchRequest(self, to_jid_s, search_dict, profile_key):
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
141 d = self.searchRequest(jid.JID(to_jid_s), search_dict, profile_key)
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
142 d.addCallback(lambda xmlui: xmlui.toXml())
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
143 return d
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
144
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
145 def searchRequest(self, to_jid, search_dict, profile_key):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
146 """ Actually do a search, according to filled data
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
147 @param to_jid: XEP-0055 compliant search entity
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
148 @param search_dict: filled data, corresponding to the form obtained in getSearchUI
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
149 @param profile_key: %(doc_profile_key)s
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
150 @return: XMLUI search result """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
151 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
152 search_request = IQ(client.xmlstream, 'set')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
153 search_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
154 search_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
155 query_elt = search_request.addElement('query', NS_SEARCH)
725
7c806491c76a plugin XEP-0055: fixed form's namespace
Goffi <goffi@goffi.org>
parents: 631
diff changeset
156 x_form = data_form.Form('submit', formNamespace = NS_SEARCH)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
157 x_form.makeFields(search_dict)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
158 query_elt.addChild(x_form.toElement())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
159 d = search_request.send(to_jid.full())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
160 d.addCallbacks(self._searchOk, self._searchErr, callbackArgs=[client.profile], errbackArgs=[client.profile])
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
162
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def _searchOk(self, answer, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
164 """got search available"""
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
165 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
166 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
167 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
168 log.info(_("No query element found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
169 raise DataError # FIXME: StanzaError is probably more appropriate, check the RFC
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
170 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
171 form_elt = query_elts.elements(data_form.NS_X_DATA, 'x').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
172 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
173 log.info(_("No data form found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
174 raise NotImplementedError("Only search through data form is implemented so far")
799
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
175 return xml_tools.dataFormResult2XMLUI(form_elt)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
176
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def _searchErr(self, failure, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
178 """ Called when something is wrong with search request """
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
179 log.info(_("Search request failure: %s") % str(failure.value))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
180 return failure