annotate src/plugins/plugin_xep_0055.py @ 817:c39117d00f35

jp: refactoring: - imports from sat_frontends.jp instead of local imports - added __init__.py - commands now inherits from a base class: each base.CommandBase instance is a subcommand - new arguments are added in CommandBase.add_parser_options methods, starting point si CommandBase.run or CommandBase.connected if a profile connection is needed - commands are exported using a __commands__ variable at the top of the module - sub-subcommand are easily added by using an other CommandBase instance as parent instead of using a Jp instance. In this case, the parent subcommand must be the one exported, and have a subcommands iterable (see cmd_file or cmd_pipe for examples). - options which are often used (like --profile) are automatically added on demand (use_profile=True, use_progress=True) - commands are automatically loaded when there are in a module named cmd_XXX - restored --connect option - restored progress bar - restored getVersion bridge call on jp --version - fixed file and pipe commands - fixed forgotten translations - fixed non SàT compliant docstrings - better about/version dialog
author Goffi <goffi@goffi.org>
date Mon, 10 Feb 2014 13:44:09 +0100
parents 1fe00f0c9a91
children 40ad5a344332
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_
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from logging import debug, info, warning, error
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber.xmlstream import IQ
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from wokkel import data_form
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
25 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
26 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
27 from sat.tools import xml_tools
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
28
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
29 NS_SEARCH = 'jabber:iq:search'
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 PLUGIN_INFO = {
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
32 "name": "Jabber Search",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "import_name": "XEP-0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
34 "type": "XEP",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "protocols": ["XEP-0055"],
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "main": "XEP_0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "handler": "no",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "description": _("""Implementation of Jabber Search""")
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
40
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 class XEP_0055(object):
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 def __init__(self, host):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
45 info(_("Jabber search plugin initialization"))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.host = host
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
47 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
48 method=self._getSearchUI,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
49 async=True)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
50 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
51 method=self._searchRequest,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 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
57
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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69
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
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 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
72 """ 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
73 @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
74
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 """
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 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
77 form_ui.addText(_("Please enter the search jid"), 'instructions')
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 799
diff changeset
78 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
79 form_ui.addLabel("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
80 form_ui.addString("jid", value="users.jabberfr.org") # TODO: replace users.jabberfr.org by any XEP-0055 compatible service discovered on current server
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 return {'xmlui': form_ui.toXml()}
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
82
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
83 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
84 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
85 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
86 return d
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
87
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def getSearchUI(self, to_jid, profile_key):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
89 """ Ask for a search interface
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @param to_jid: XEP-0055 compliant search entity
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
91 @param profile_key: %(doc_profile_key)s
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @return: XMLUI search interface """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
93 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
94 fields_request = IQ(client.xmlstream, 'get')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
95 fields_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
96 fields_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
97 fields_request.addElement('query', NS_SEARCH)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
98 d = fields_request.send(to_jid.full())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
99 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
100 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
101
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def _fieldsOk(self, answer, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """got fields available"""
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
104 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
105 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
106 except StopIteration:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
107 info(_("No query element found"))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
108 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
109 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 except StopIteration:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
112 info(_("No data form found"))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
113 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
114 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
115 return xml_tools.dataForm2XMLUI(parsed_form, "")
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
116
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def _fieldsErr(self, failure, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
118 """ Called when something is wrong with fields request """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
119 info(_("Fields request failure: %s") % str(failure.value))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return failure
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
121
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
122 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
123 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
124 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
125 except KeyError:
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 warning ("session id doesn't exist, session has probably expired")
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 # 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
128 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
129
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 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
131 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
132 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
133 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
134 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
135 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
136
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
137 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
138 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
139 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
140 return d
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
141
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
142 def searchRequest(self, to_jid, search_dict, profile_key):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """ Actually do a search, according to filled data
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
144 @param to_jid: XEP-0055 compliant search entity
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
145 @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
146 @param profile_key: %(doc_profile_key)s
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
147 @return: XMLUI search result """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
148 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
149 search_request = IQ(client.xmlstream, 'set')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
150 search_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
151 search_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
152 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
153 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
154 x_form.makeFields(search_dict)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
155 query_elt.addChild(x_form.toElement())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
156 d = search_request.send(to_jid.full())
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
157 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
158 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
159
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def _searchOk(self, answer, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
161 """got search available"""
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
162 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
163 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
164 except StopIteration:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
165 info(_("No query element found"))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
166 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
167 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
168 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
169 except StopIteration:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
170 info(_("No data form found"))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
171 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
172 return xml_tools.dataFormResult2XMLUI(form_elt)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
173
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
174 def _searchErr(self, failure, profile):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
175 """ Called when something is wrong with search request """
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
176 info(_("Search request failure: %s") % str(failure.value))
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
177 return failure