annotate src/plugins/plugin_xep_0050.py @ 1071:eef1f200d733

plugin XEP-0050: form returned by completed command are now managed
author Goffi <goffi@goffi.org>
date Sat, 14 Jun 2014 17:26:22 +0200
parents 301b342c697a
children 387bbc459d8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Ad-Hoc Commands (XEP-0050)
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)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
20 from sat.core.i18n import _, D_
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 821
diff changeset
21 from sat.core.constants import Const as C
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
22 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
23 log = getLogger(__name__)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
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: 944
diff changeset
25 from twisted.words.protocols import jabber
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.xish import domish
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 916
diff changeset
27 from twisted.internet import defer
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
28 from wokkel import disco, iwokkel, data_form, compat
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core import exceptions
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
30 from sat.memory.memory import Sessions
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from uuid import uuid4
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
32 from sat.tools import xml_tools
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
33
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from zope.interface import implements
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
35
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from twisted.words.protocols.xmlstream import XMPPHandler
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from wokkel.subprotocols import XMPPHandler
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
40
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 from collections import namedtuple
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
42
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from collections import OrderedDict # only available from python 2.7
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 from ordereddict import OrderedDict
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
47
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 IQ_SET = '/iq[@type="set"]'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 NS_COMMANDS = "http://jabber.org/protocol/commands"
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 ID_CMD_LIST = disco.DiscoIdentity("automation", "command-list")
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 ID_CMD_NODE = disco.DiscoIdentity("automation", "command-node")
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 CMD_REQUEST = IQ_SET + '/command[@xmlns="' + NS_COMMANDS + '"]'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
53
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 SHOWS = OrderedDict([('default', _('Online')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ('away', _('Away')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 ('chat', _('Free for chat')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 ('dnd', _('Do not disturb')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
58 ('xa', _('Left')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
59 ('disconnect', _('Disconnect'))])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
60
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
61 PLUGIN_INFO = {
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
62 "name": "Ad-Hoc Commands",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "import_name": "XEP-0050",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 "type": "XEP",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 "protocols": ["XEP-0050"],
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "main": "XEP_0050",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "handler": "yes",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 "description": _("""Implementation of Ad-Hoc Commands""")
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
69 }
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
70
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
71
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 class AdHocError(Exception):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
73
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def __init__(self, error_const):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 """ Error to be used from callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @param error_const: one of XEP_0050.ERROR
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
78 assert(error_const in XEP_0050.ERROR)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.callback_error = error_const
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
80
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 class AdHocCommand(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 implements(iwokkel.IDisco)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
83
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def __init__(self, parent, callback, label, node, features, timeout, allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, client):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.parent = parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.callback = callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.label = label
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.node = node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.features = [disco.DiscoFeature(feature) for feature in features]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.allowed_jids, self.allowed_groups, self.allowed_magics, self.forbidden_jids, self.forbidden_groups = allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.client = client
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
92 self.sessions = Sessions(timeout=timeout)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
93
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def getName(self, xml_lang=None):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return self.label
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
96
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def isAuthorised(self, requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 if '@ALL@' in self.allowed_magics:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 forbidden = set(self.forbidden_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 for group in self.forbidden_groups:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 forbidden.update(self.client.roster.getJidsFromGroup(group))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if requestor.userhostJID() in forbidden:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 allowed = set(self.allowed_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 for group in self.allowed_groups:
821
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 try:
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 allowed.update(self.client.roster.getJidsFromGroup(group))
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109 except exceptions.UnknownGroupError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
110 log.warning(_("The groups [%(group)s] is unknown for profile [%(profile)s])" % {'group':group, 'profile':self.client.profile}))
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 if requestor.userhostJID() in allowed:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
113 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
114
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 916
diff changeset
116 # identities = [ID_CMD_LIST if self.node == NS_COMMANDS else ID_CMD_NODE] # FIXME
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return [disco.DiscoFeature(NS_COMMANDS)] + self.features
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
118
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
121
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def _sendAnswer(self, callback_data, session_id, request):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
123 """ Send result of the command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
124 @param callback_data: tuple (payload, status, actions, note) with:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
125 - payload (domish.Element) usualy containing data form
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
126 - status: current status, see XEP_0050.STATUS
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
127 - actions: list of allowed actions (see XEP_0050.ACTION). First action is the default one. Default to EXECUTE
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
128 - note: optional additional note: either None or a tuple with (note type, human readable string),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
129 note type being in XEP_0050.NOTE
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
130 @param session_id: current session id
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @return: deferred
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
133 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
134 payload, status, actions, note = callback_data
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
135 assert(isinstance(payload, domish.Element) or payload is None)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
136 assert(status in XEP_0050.STATUS)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if not actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
138 actions = [XEP_0050.ACTION.EXECUTE]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
139 result = domish.Element((None, 'iq'))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
140 result['type'] = 'result'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 result['id'] = request['id']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
142 result['to'] = request['from']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
143 command_elt = result.addElement('command', NS_COMMANDS)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
144 command_elt['sessionid'] = session_id
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
145 command_elt['node'] = self.node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
146 command_elt['status'] = status
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
147
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if status != XEP_0050.STATUS.CANCELED:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
149 if status != XEP_0050.STATUS.COMPLETED:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
150 actions_elt = command_elt.addElement('actions')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
151 actions_elt['execute'] = actions[0]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
152 for action in actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
153 actions_elt.addElement(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
154
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if note is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
156 note_type, note_mess = note
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
157 note_elt = command_elt.addElement('note', content=note_mess)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
158 note_elt['type'] = note_type
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
159
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if payload is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
161 command_elt.addChild(payload)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
162
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
163 self.client.xmlstream.send(result)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if status in (XEP_0050.STATUS.COMPLETED, XEP_0050.STATUS.CANCELED):
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
165 del self.sessions[session_id]
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
166
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
167 def _sendError(self, error_constant, session_id, request):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
168 """ Send error stanza
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
169 @param error_constant: one of XEP_OO50.ERROR
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
170 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
171 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
172 xmpp_condition, cmd_condition = error_constant
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
173 iq_elt = jabber.error.StanzaError(xmpp_condition).toResponse(request)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if cmd_condition:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
175 error_elt = iq_elt.elements(None, "error").next()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
176 error_elt.addElement(cmd_condition, NS_COMMANDS)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
177 self.client.xmlstream.send(iq_elt)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
178 del self.sessions[session_id]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
179
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def onRequest(self, command_elt, requestor, action, session_id):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if not self.isAuthorised(requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
182 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
183 if session_id:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
184 try:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
185 session_data = self.sessions[session_id]
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
186 except KeyError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
187 return self._sendError(XEP_0050.ERROR.SESSION_EXPIRED, session_id, command_elt.parent)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
188 if session_data['requestor'] != requestor:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
189 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
190 else:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
191 session_id, session_data = self.sessions.newSession()
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
192 session_data['requestor'] = requestor
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if action == XEP_0050.ACTION.CANCEL:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
194 d = defer.succeed((None, XEP_0050.STATUS.CANCELED, None, None))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
195 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
196 d = defer.maybeDeferred(self.callback, command_elt, session_data, action, self.node, self.client.profile)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
197 d.addCallback(self._sendAnswer, session_id, command_elt.parent)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
198 d.addErrback(lambda failure, request: self._sendError(failure.value.callback_error, session_id, request), command_elt.parent)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
199
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
200
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
201 class XEP_0050(object):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
202 STATUS = namedtuple('Status', ('EXECUTING', 'COMPLETED', 'CANCELED'))('executing', 'completed', 'canceled')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
203 ACTION = namedtuple('Action', ('EXECUTE', 'CANCEL', 'NEXT', 'PREV'))('execute', 'cancel', 'next', 'prev')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
204 NOTE = namedtuple('Note', ('INFO','WARN','ERROR'))('info','warn','error')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
205 ERROR = namedtuple('Error', ('MALFORMED_ACTION', 'BAD_ACTION', 'BAD_LOCALE', 'BAD_PAYLOAD', 'BAD_SESSIONID', 'SESSION_EXPIRED',
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
206 'FORBIDDEN', 'ITEM_NOT_FOUND', 'FEATURE_NOT_IMPLEMENTED', 'INTERNAL'))(('bad-request', 'malformed-action'),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
207 ('bad-request', 'bad-action'), ('bad-request', 'bad-locale'), ('bad-request','bad-payload'),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ('bad-request','bad-sessionid'), ('not-allowed','session-expired'), ('forbidden', None),
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
209 ('item-not-found', None), ('feature-not-implemented', None), ('internal-server-error', None)) # XEP-0050 §4.4 Table 5
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
210
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
211 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
212 log.info(_("plugin XEP-0050 initialization"))
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
213 self.host = host
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
214 self.requesting = Sessions()
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
215 self.answering = {}
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
216 host.bridge.addMethod("requestCommand", ".plugin", in_sign='ss', out_sign='s',
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
217 method=self._requestCommandsList,
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
218 async=True)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
219 self.__requesting_id = host.registerCallback(self._requestingEntity, with_data=True)
809
743b757777d3 core: security limit in menus
Goffi <goffi@goffi.org>
parents: 802
diff changeset
220 host.importMenu((D_("Service"), D_("commands")), self._commandsMenu, security_limit=4, help_string=D_("Execute ad-hoc commands"))
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
221
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
222 def getHandler(self, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
223 return XEP_0050_handler(self)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
224
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
225 def profileConnected(self, profile):
821
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
226 self.addAdHocCommand(self._statusCallback, _("Status"), profile_key=profile)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
227
753
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
228 def profileDisconnected(self, profile):
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
229 try:
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
230 del self.answering[profile]
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
231 except KeyError:
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
232 pass
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
233
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
234 def _items2XMLUI(self, items):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
235 """ Convert discovery items to XMLUI dialog """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
236 # TODO: manage items on different jids
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
237 form_ui = xml_tools.XMLUI("form", submit_id=self.__requesting_id)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
238
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
239 form_ui.addText(_("Please select a command"), 'instructions')
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
240
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
241 options = [(item.nodeIdentifier, item.name) for item in items]
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
242 form_ui.addList("node", options)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
243 return form_ui
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
244
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
245 def _commandsAnswer2XMLUI(self, iq_elt, session_id, session_data):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
246 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
247 Convert command answer to an ui for frontend
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
248 @param iq_elt: command result
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
249 @param session_id: id of the session used with the frontend
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
250 @param profile_key: %(doc_profile_key)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
251
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
252 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
253 command_elt = iq_elt.elements(NS_COMMANDS, "command").next()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
254 status = command_elt.getAttribute('status', XEP_0050.STATUS.EXECUTING)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
255 if status in [XEP_0050.STATUS.COMPLETED, XEP_0050.STATUS.CANCELED]:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
256 # the command session is finished, we purge our session
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
257 del self.requesting[session_id]
1071
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
258 if status == XEP_0050.STATUS.COMPLETED:
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
259 session_id = None
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
260 else:
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
261 return None
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
262 remote_session_id = command_elt.getAttribute('sessionid')
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
263 if remote_session_id:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
264 session_data['remote_id'] = remote_session_id
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
265 data_elt = command_elt.elements(data_form.NS_X_DATA, 'x').next()
1071
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
266 if session_id is None:
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
267 return xml_tools.dataFormResult2XMLUI(data_elt)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
268 form = data_form.Form.fromElement(data_elt)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
269 return xml_tools.dataForm2XMLUI(form, self.__requesting_id, session_id=session_id)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
270
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
271 def _requestingEntity(self, data, profile):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
272 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
273 request and entity and create XMLUI accordingly
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
274 @param data: data returned by previous XMLUI (first one must come from self._commandsMenu)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
275 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
276 @return: callback dict result (with "xmlui" corresponding to the answering dialog, or empty if it's finished without error)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
277
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
278 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
279 # TODO: cancel, prev and next are not managed
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
280 # TODO: managed answerer errors
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
281 # TODO: manage nodes with a non data form payload
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
282 if "session_id" not in data:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
283 # we just had the jid, we now request it for the available commands
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
284 session_id, session_data = self.requesting.newSession(profile=profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
285 entity = jid.JID(data[xml_tools.SAT_FORM_PREFIX+'jid'])
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
286 session_data['jid'] = entity
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
287 d = self.requestCommandsList(entity, profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
288
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
289 def sendItems(xmlui):
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 773
diff changeset
290 xmlui.session_id = session_id # we need to keep track of the session
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
291 return {'xmlui': xmlui.toXml()}
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
292
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
293 d.addCallback(sendItems)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
294 else:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
295 # we have started a several forms sessions
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
296 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
297 session_data = self.requesting.profileGet(data["session_id"], profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
298 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
299 log.warning ("session id doesn't exist, session has probably expired")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
300 # TODO: send error dialog
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
301 return defer.succeed({})
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
302 session_id = data["session_id"]
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
303 entity = session_data['jid']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
304 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
305 node = session_data['node']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
306 # node has already been received
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
307 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
308 # it's the first time we know the node, we save it in session data
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
309 node = session_data['node'] = data[xml_tools.SAT_FORM_PREFIX+'node']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
310
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
311 client = self.host.getClient(profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
312
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
313 # we request execute node's command
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
314 iq_elt = compat.IQ(client.xmlstream, 'set')
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
315 iq_elt['to'] = entity.full()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
316 command_elt = iq_elt.addElement("command", NS_COMMANDS)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
317 command_elt['node'] = session_data['node']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
318 command_elt['action'] = XEP_0050.ACTION.EXECUTE
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
319 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
320 # remote_id is the XEP_0050 sessionid used by answering command
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
321 # while session_id is our own session id used with the frontend
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
322 command_elt['sessionid'] = session_data['remote_id']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
323 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
324 pass
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
325
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
326 command_elt.addChild(xml_tools.XMLUIResultToElt(data)) # We add the XMLUI result to the command payload
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
327 d = iq_elt.send()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
328 d.addCallback(self._commandsAnswer2XMLUI, session_id, session_data)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
329 d.addCallback(lambda xmlui: {'xmlui': xmlui.toXml()} if xmlui is not None else {})
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
330
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
331 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
332
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
333 def _commandsMenu(self, menu_data, profile):
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
334 """ First XMLUI activated by menu: ask for target jid
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
335 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
336
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
337 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
338 form_ui = xml_tools.XMLUI("form", submit_id=self.__requesting_id)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
339 form_ui.addText(_("Please enter target jid"), 'instructions')
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
340 form_ui.changeContainer("pairs")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
341 form_ui.addLabel("jid")
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
342 form_ui.addString("jid")
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
343 return {'xmlui': form_ui.toXml()}
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
344
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
345 def _statusCallback(self, command_elt, session_data, action, node, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
346 """ Ad-hoc command used to change the "show" part of status """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
347 actions = session_data.setdefault('actions',[])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
348 actions.append(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
349
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
350 if len(actions) == 1:
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
351 # it's our first request, we ask the desired new status
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
352 status = XEP_0050.STATUS.EXECUTING
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
353 form = data_form.Form('form', title=_('status selection'))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
354 show_options = [data_form.Option(name, label) for name, label in SHOWS.items()]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
355 field = data_form.Field('list-single', 'show', options=show_options, required=True)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
356 form.addField(field)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
357
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
358 payload = form.toElement()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
359 note = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
360
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
361 elif len(actions) == 2:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
362 # we should have the answer here
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
363 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
364 x_elt = command_elt.elements(data_form.NS_X_DATA,'x').next()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
365 answer_form = data_form.Form.fromElement(x_elt)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
366 show = answer_form['show']
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 916
diff changeset
367 except (KeyError, StopIteration):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
368 raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
369 if show not in SHOWS:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
370 raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
371 if show == "disconnect":
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
372 self.host.disconnect(profile)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
373 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
374 self.host.setPresence(show=show, profile_key=profile)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
375
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
376 # job done, we can end the session
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
377 form = data_form.Form('form', title=_(u'Updated'))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
378 form.addField(data_form.Field('fixed', u'Status updated'))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
379 status = XEP_0050.STATUS.COMPLETED
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
380 payload = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
381 note = (self.NOTE.INFO, _(u"Status updated"))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
382 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
383 raise AdHocError(XEP_0050.ERROR.INTERNAL)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
384
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
385 return (payload, status, None, note)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
386
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
387 def _requestCommandsList(self, to_jid_s, profile_key):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
388 d = self.requestCommandsList(jid.JID(to_jid_s), profile_key)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
389 d.addCallback(lambda xmlui: xmlui.toXml())
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
390 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
391
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
392 def requestCommandsList(self, to_jid, profile_key):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
393 """ Request available commands
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
394 @param to_jid: the entity answering the commands
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
395 @param profile_key: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
396
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
397 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
398 client = self.host.getClient(profile_key)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
399 d = client.disco.requestItems(to_jid, NS_COMMANDS)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
400 d.addCallback(self._items2XMLUI)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
401 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
402
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
403 def addAdHocCommand(self, callback, label, node="", features = None, timeout = 600, allowed_jids = None, allowed_groups = None,
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 821
diff changeset
404 allowed_magics = None, forbidden_jids = None, forbidden_groups = None, profile_key=C.PROF_KEY_NONE):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
405 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
406
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
407 Add an ad-hoc command for the current profile
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
408
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
409 @param callback: method associated with this ad-hoc command which return the payload data (see AdHocCommand._sendAnswer), can return a deferred
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
410 @param label: label associated with this command on the main menu
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
411 @param node: disco item node associated with this command. None or "" to use autogenerated node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
412 @param features: features associated with the payload (list of strings), usualy data form
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
413 @param timeout: delay between two requests before canceling the session (in seconds)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
414 @param allowed_jids: list of allowed entities
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
415 @param allowed_groups: list of allowed roster groups
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
416 @param allowed_magics: list of allowed magic keys, can be:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
417 @ALL@: allow everybody
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
418 @PROFILE_BAREJID@: allow only the jid of the profile
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
419 @param forbidden_jids: black list of entities which can't access this command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
420 @param forbidden_groups: black list of groups which can't access this command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
421 @param profile_key: profile key associated with this command, @ALL@ means can be accessed with every profiles
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
422 @return: node of the added command, useful to remove the command later
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
423 """
821
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
424 # FIXME: "@ALL@" for profile_key seems useless and dangerous
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
425
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
426 node = node.strip()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
427 if not node:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
428 node = "%s_%s" % ('COMMANDS', uuid4())
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
429
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
430 if features is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
431 features = [data_form.NS_X_DATA]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
432
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
433 if allowed_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
434 allowed_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
435 if allowed_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
436 allowed_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
437 if allowed_magics is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
438 allowed_magics = ['@PROFILE_BAREJID@']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
439 if forbidden_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
440 forbidden_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
441 if forbidden_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
442 forbidden_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
443
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
444 for client in self.host.getClients(profile_key):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
445 #TODO: manage newly created/removed profiles
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
446 _allowed_jids = (allowed_jids + [client.jid.userhostJID()]) if '@PROFILE_BAREJID@' in allowed_magics else allowed_jids
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
447 ad_hoc_command = AdHocCommand(self, callback, label, node, features, timeout, _allowed_jids,
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
448 allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, client)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
449 ad_hoc_command.setHandlerParent(client)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
450 profile_commands = self.answering.setdefault(client.profile, {})
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
451 profile_commands[node] = ad_hoc_command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
452
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
453 def onCmdRequest(self, request, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
454 request.handled = True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
455 requestor = jid.JID(request['from'])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
456 command_elt = request.elements(NS_COMMANDS, 'command').next()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
457 action = command_elt.getAttribute('action', self.ACTION.EXECUTE)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
458 node = command_elt.getAttribute('node')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
459 if not node:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
460 raise exceptions.DataError
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
461 sessionid = command_elt.getAttribute('sessionid')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
462 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
463 command = self.answering[profile][node]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
464 except KeyError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
465 raise exceptions.DataError
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
466 command.onRequest(command_elt, requestor, action, sessionid)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
467
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
468
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
469 class XEP_0050_handler(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
470 implements(iwokkel.IDisco)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
471
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
472 def __init__(self, plugin_parent):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
473 self.plugin_parent = plugin_parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
474
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
475 def connectionInitialized(self):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
476 self.xmlstream.addObserver(CMD_REQUEST, self.plugin_parent.onCmdRequest, profile=self.parent.profile)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
477
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
478 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
479 identities = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
480 if nodeIdentifier == NS_COMMANDS and self.plugin_parent.answering.get(self.parent.profile): # we only add the identity if we have registred commands
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
481 identities.append(ID_CMD_LIST)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
482 return [disco.DiscoFeature(NS_COMMANDS)] + identities
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
483
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
484 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
485 ret = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
486 if nodeIdentifier == NS_COMMANDS:
753
7f98f53f6997 plugin XEP-0050: fixed getDiscoItems when no commands were added + fixed commands purge on profile disconnection
Goffi <goffi@goffi.org>
parents: 728
diff changeset
487 for command in self.plugin_parent.answering.get(self.parent.profile,{}).values():
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
488 if command.isAuthorised(requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
489 ret.append(disco.DiscoItem(self.parent.jid, command.node, command.getName())) #TODO: manage name language
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
490 return ret