annotate src/plugins/plugin_xep_0050.py @ 934:34dd9287dfe5

plugin account: bug fix profile deletion from the database + unsubscribe the contacts
author souliane <souliane@mailoo.org>
date Mon, 24 Mar 2014 16:21:14 +0100
parents 1a759096ccbd
children c6d8fc63b1db
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
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
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 error as xmpp_error
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.internet import defer, reactor
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
27 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
28 from sat.core import exceptions
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
29 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
30 from uuid import uuid4
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
31 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
32
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from zope.interface import implements
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
34
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from wokkel.subprotocols import XMPPHandler
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
39
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from collections import namedtuple
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
41
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 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
44 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from ordereddict import OrderedDict
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
46
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 IQ_SET = '/iq[@type="set"]'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 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
49 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
50 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
51 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
52
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 SHOWS = OrderedDict([('default', _('Online')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 ('away', _('Away')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ('chat', _('Free for chat')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 ('dnd', _('Do not disturb')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 ('xa', _('Left')),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
58 ('disconnect', _('Disconnect'))])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
59
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
60 PLUGIN_INFO = {
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "name": "Ad-Hoc Commands",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
62 "import_name": "XEP-0050",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "type": "XEP",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 "protocols": ["XEP-0050"],
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 "main": "XEP_0050",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "handler": "yes",
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "description": _("""Implementation of Ad-Hoc Commands""")
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 }
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 class AdHocError(Exception):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
72
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __init__(self, error_const):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 """ Error to be used from callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @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
76 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78 self.callback_error = error_const
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
79
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 class AdHocCommand(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 implements(iwokkel.IDisco)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
82
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 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
84 self.parent = parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.callback = callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.label = label
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.node = node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 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
89 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
90 self.client = client
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
91 self.sessions = Sessions(timeout=timeout)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
92
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def getName(self, xml_lang=None):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 return self.label
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
95
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def isAuthorised(self, requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if '@ALL@' in self.allowed_magics:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 forbidden = set(self.forbidden_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 for group in self.forbidden_groups:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 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
102 if requestor.userhostJID() in forbidden:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
103 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 allowed = set(self.allowed_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 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
106 try:
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 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
108 except exceptions.UnknownGroupError:
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109 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
110 if requestor.userhostJID() in allowed:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
113
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 identities = [ID_CMD_LIST if self.node == NS_COMMANDS else ID_CMD_NODE]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
116 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
117
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
118 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
119 return []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
120
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 """ Send result of the command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @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
124 - 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
125 - 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
126 - 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
127 - 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
128 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
129 @param session_id: current session id
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
130 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @return: deferred
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
133 payload, status, actions, note = callback_data
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
134 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
135 assert(status in XEP_0050.STATUS)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
136 if not actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
137 actions = [XEP_0050.ACTION.EXECUTE]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
138 result = domish.Element((None, 'iq'))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
139 result['type'] = 'result'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
140 result['id'] = request['id']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 result['to'] = request['from']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
142 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
143 command_elt['sessionid'] = session_id
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
144 command_elt['node'] = self.node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
145 command_elt['status'] = status
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
146
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if status != XEP_0050.STATUS.CANCELED:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if status != XEP_0050.STATUS.COMPLETED:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
149 actions_elt = command_elt.addElement('actions')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
150 actions_elt['execute'] = actions[0]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
151 for action in actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
152 actions_elt.addElement(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
153
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
154 if note is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
155 note_type, note_mess = note
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
156 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
157 note_elt['type'] = note_type
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
158
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
159 if payload is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
160 command_elt.addChild(payload)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
161
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
162 self.client.xmlstream.send(result)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
163 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
164 del self.sessions[session_id]
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
165
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
166 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
167 """ Send error stanza
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
168 @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
169 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
170 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
171 xmpp_condition, cmd_condition = error_constant
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
172 iq_elt = xmpp_error.StanzaError(xmpp_condition).toResponse(request)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if cmd_condition:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
174 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
175 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
176 self.client.xmlstream.send(iq_elt)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
177 del self.sessions[session_id]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
178
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
179 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
180 if not self.isAuthorised(requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
181 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
182 if session_id:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
183 try:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
184 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
185 except KeyError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
186 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
187 if session_data['requestor'] != requestor:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
188 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
189 else:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
190 session_id, session_data = self.sessions.newSession()
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
191 session_data['requestor'] = requestor
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if action == XEP_0050.ACTION.CANCEL:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
193 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
194 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
195 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
196 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
197 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
198
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 class XEP_0050(object):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
201 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
202 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
203 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
204 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
205 '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
206 ('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
207 ('bad-request','bad-sessionid'), ('not-allowed','session-expired'), ('forbidden', None),
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ('item-not-found', None), ('feature-not-implemented', None), ('internal-server-error', None)) # XEP-0050 §4.6 Table 5
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
209
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
210 def __init__(self, host):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
211 info(_("plugin XEP-0050 initialization"))
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
212 self.host = host
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
213 self.requesting = Sessions()
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
214 self.answering = {}
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
215 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
216 method=self._requestCommandsList,
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
217 async=True)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
218 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
219 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
220
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
221 def getHandler(self, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
222 return XEP_0050_handler(self)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
223
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
224 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
225 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
226
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
227 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
228 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
229 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
230 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
231 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
232
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
233 def _items2XMLUI(self, items):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
234 """ 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
235 # 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
236 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
237
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
238 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
239
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
240 options = [(item.nodeIdentifier, item.name) for item in items]
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
241 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
242 return form_ui
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
243
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
244 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
245 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
246 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
247 @param iq_elt: command result
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
248 @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
249 @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
250
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 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
253 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
254 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
255 # 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
256 del self.requesting[session_id]
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
257 return None
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
258 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
259 if remote_session_id:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
260 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
261 data_elt = command_elt.elements(data_form.NS_X_DATA, 'x').next()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
262 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
263 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
264
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
265 def _requestingEntity(self, data, profile):
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
266 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
267 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
268 @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
269 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
270 @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
271
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 # 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
274 # TODO: managed answerer errors
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
275 # 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
276 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
277 # 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
278 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
279 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
280 session_data['jid'] = entity
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
281 d = self.requestCommandsList(entity, profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
282
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
283 def sendItems(xmlui):
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 773
diff changeset
284 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
285 return {'xmlui': xmlui.toXml()}
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
286
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
287 d.addCallback(sendItems)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
288 else:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
289 # 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
290 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
291 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
292 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
293 warning ("session id doesn't exist, session has probably expired")
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
294 # TODO: send error dialog
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
295 return defer.succeed({})
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
296 session_id = data["session_id"]
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
297 entity = session_data['jid']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
298 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
299 node = session_data['node']
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
300 # node has already been received
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
301 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
302 # 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
303 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
304
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
305 client = self.host.getClient(profile)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
306 if not client:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
307 raise exceptions.ProfileUnknownError
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
308
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
309 # 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
310 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
311 iq_elt['to'] = entity.full()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
312 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
313 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
314 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
315 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
316 # 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
317 # 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
318 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
319 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
320 pass
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
321
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
322 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
323 d = iq_elt.send()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
324 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
325 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
326
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
327 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
328
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
329 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
330 """ 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
331 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
332
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
333 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
334 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
335 form_ui.addText(_("Please enter target jid"), 'instructions')
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
336 form_ui.changeContainer("pairs")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
337 form_ui.addLabel("jid")
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
338 form_ui.addString("jid")
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
339 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
340
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
341 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
342 """ 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
343 actions = session_data.setdefault('actions',[])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
344 actions.append(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
345
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
346 if len(actions) == 1:
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
347 # 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
348 status = XEP_0050.STATUS.EXECUTING
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
349 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
350 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
351 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
352 form.addField(field)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
353
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
354 payload = form.toElement()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
355 note = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
356
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
357 elif len(actions) == 2:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
358 # 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
359 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
360 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
361 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
362 show = answer_form['show']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
363 except KeyError, StopIteration:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
364 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
365 if show not in SHOWS:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
366 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
367 if show == "disconnect":
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
368 self.host.disconnect(profile)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
369 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
370 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
371
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
372 # 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
373 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
374 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
375 status = XEP_0050.STATUS.COMPLETED
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
376 payload = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
377 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
378 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
379 raise AdHocError(XEP_0050.ERROR.INTERNAL)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
380
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
381 return (payload, status, None, note)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
382
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
383 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
384 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
385 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
386 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
387
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
388 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
389 """ Request available commands
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
390 @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
391 @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
392
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
393 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
394 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
395 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
396 d.addCallback(self._items2XMLUI)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
397 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
398
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
399 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
400 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
401 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
402
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
403 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
404
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
405 @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
406 @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
407 @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
408 @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
409 @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
410 @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
411 @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
412 @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
413 @ALL@: allow everybody
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
414 @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
415 @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
416 @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
417 @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
418 @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
419 """
821
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
420 # 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
421
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
422 node = node.strip()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
423 if not node:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
424 node = "%s_%s" % ('COMMANDS', uuid4())
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 if features is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
427 features = [data_form.NS_X_DATA]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
428
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
429 if allowed_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
430 allowed_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
431 if allowed_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
432 allowed_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
433 if allowed_magics is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
434 allowed_magics = ['@PROFILE_BAREJID@']
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
435 if forbidden_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
436 forbidden_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
437 if forbidden_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
438 forbidden_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
439
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
440 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
441 #TODO: manage newly created/removed profiles
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
442 _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
443 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
444 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
445 ad_hoc_command.setHandlerParent(client)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
446 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
447 profile_commands[node] = ad_hoc_command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
448
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
449 def onCmdRequest(self, request, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
450 request.handled = True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
451 requestor = jid.JID(request['from'])
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
452 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
453 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
454 node = command_elt.getAttribute('node')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
455 if not node:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
456 raise exceptions.DataError
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
457 sessionid = command_elt.getAttribute('sessionid')
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
458 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
459 command = self.answering[profile][node]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
460 except KeyError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
461 raise exceptions.DataError
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
462 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
463
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
464
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
465 class XEP_0050_handler(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
466 implements(iwokkel.IDisco)
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 def __init__(self, plugin_parent):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
469 self.plugin_parent = plugin_parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
470
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
471 def connectionInitialized(self):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
472 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
473
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
474 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
475 identities = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
476 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
477 identities.append(ID_CMD_LIST)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
478 return [disco.DiscoFeature(NS_COMMANDS)] + identities
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
479
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
480 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
481 ret = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
482 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
483 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
484 if command.isAuthorised(requestor):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
485 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
486 return ret