annotate sat/plugins/plugin_xep_0050.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 0112c1f7dcf0
children 8dd9db785ac8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
728
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)
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
23
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
24 log = getLogger(__name__)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 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
26 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
27 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
28 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
29 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
30 from sat.core import exceptions
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
31 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
32 from uuid import uuid4
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
33 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
34
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from zope.interface import implements
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
36
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 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
39 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from wokkel.subprotocols import XMPPHandler
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 from collections import namedtuple
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
43
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
45 from collections import OrderedDict # only available from python 2.7
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 except ImportError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 from ordereddict import OrderedDict
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
48
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 IQ_SET = '/iq[@type="set"]'
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 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
51 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
52 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
53 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
54
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
55 SHOWS = OrderedDict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
56 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
57 ("default", _("Online")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
58 ("away", _("Away")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
59 ("chat", _("Free for chat")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
60 ("dnd", _("Do not disturb")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
61 ("xa", _("Left")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
62 ("disconnect", _("Disconnect")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
63 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
64 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
65
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
67 C.PI_NAME: "Ad-Hoc Commands",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
68 C.PI_IMPORT_NAME: "XEP-0050",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
69 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
70 C.PI_PROTOCOLS: ["XEP-0050"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
71 C.PI_MAIN: "XEP_0050",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
72 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
73 C.PI_DESCRIPTION: _("""Implementation of Ad-Hoc Commands"""),
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 }
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
75
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 class AdHocError(Exception):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def __init__(self, error_const):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 """ Error to be used from callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @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
81 """
1517
4eca4f5e21ec plugin XEP-0050: minor parentheses removal
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
82 assert error_const in XEP_0050.ERROR
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.callback_error = error_const
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
84
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
85
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 class AdHocCommand(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 implements(iwokkel.IDisco)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
88
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
89 def __init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
90 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
91 parent,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
92 callback,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
93 label,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
94 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
95 features,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
96 timeout,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
97 allowed_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
98 allowed_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
99 allowed_magics,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
100 forbidden_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
101 forbidden_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
102 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
103 ):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self.parent = parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.callback = callback
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.label = label
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.node = node
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.features = [disco.DiscoFeature(feature) for feature in features]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
109 self.allowed_jids, self.allowed_groups, self.allowed_magics, self.forbidden_jids, self.forbidden_groups = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
110 allowed_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
111 allowed_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
112 allowed_magics,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
113 forbidden_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
114 forbidden_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
115 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.client = client
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
117 self.sessions = Sessions(timeout=timeout)
728
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 getName(self, xml_lang=None):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return self.label
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 isAuthorised(self, requestor):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
123 if "@ALL@" in self.allowed_magics:
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
124 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
125 forbidden = set(self.forbidden_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
126 for group in self.forbidden_groups:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
127 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
128 if requestor.userhostJID() in forbidden:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
130 allowed = set(self.allowed_jids)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
131 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
132 try:
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
133 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
134 except exceptions.UnknownGroupError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
135 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
136 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
137 u"The groups [%(group)s] is unknown for profile [%(profile)s])"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
138 % {"group": group, "profile": self.client.profile}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
139 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
140 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if requestor.userhostJID() in allowed:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return True
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return False
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
144
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
145 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
146 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
147 nodeIdentifier != NS_COMMANDS
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
148 ): # FIXME: we should manage other disco nodes here
1494
74ddb4cc2ff9 plugin XEP-0050: return disco for commands only on commands namespace
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
149 return []
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
150 # 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
151 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
152
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
153 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
154 return []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
155
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
156 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
157 """ Send result of the command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
158 @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
159 - 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
160 - 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
161 - 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
162 - 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
163 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
164 @param session_id: current session id
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
165 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
166 @return: deferred
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
167 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
168 payload, status, actions, note = callback_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
169 assert isinstance(payload, domish.Element) or payload is None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
170 assert status in XEP_0050.STATUS
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
171 if not actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
172 actions = [XEP_0050.ACTION.EXECUTE]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
173 result = domish.Element((None, "iq"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
174 result["type"] = "result"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
175 result["id"] = request["id"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
176 result["to"] = request["from"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
177 command_elt = result.addElement("command", NS_COMMANDS)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
178 command_elt["sessionid"] = session_id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
179 command_elt["node"] = self.node
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
180 command_elt["status"] = status
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
181
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
182 if status != XEP_0050.STATUS.CANCELED:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
183 if status != XEP_0050.STATUS.COMPLETED:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
184 actions_elt = command_elt.addElement("actions")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
185 actions_elt["execute"] = actions[0]
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
186 for action in actions:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
187 actions_elt.addElement(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
188
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if note is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
190 note_type, note_mess = note
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
191 note_elt = command_elt.addElement("note", content=note_mess)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
192 note_elt["type"] = note_type
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
193
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
194 if payload is not None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
195 command_elt.addChild(payload)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
196
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
197 self.client.send(result)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
198 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
199 del self.sessions[session_id]
728
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 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
202 """ Send error stanza
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
203 @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
204 @param request: original request (domish.Element)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
205 """
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
206 xmpp_condition, cmd_condition = error_constant
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
207 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
208 if cmd_condition:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
209 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
210 error_elt.addElement(cmd_condition, NS_COMMANDS)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
211 self.client.send(iq_elt)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
212 del self.sessions[session_id]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
213
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
214 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
215 if not self.isAuthorised(requestor):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
216 return self._sendError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
217 XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
218 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
219 if session_id:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
220 try:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
221 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
222 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
223 return self._sendError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
224 XEP_0050.ERROR.SESSION_EXPIRED, session_id, command_elt.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
225 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
226 if session_data["requestor"] != requestor:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
227 return self._sendError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
228 XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
229 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
230 else:
758
86224a13cc1d plugin xep-0050: using new Sessions class
Goffi <goffi@goffi.org>
parents: 753
diff changeset
231 session_id, session_data = self.sessions.newSession()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
232 session_data["requestor"] = requestor
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if action == XEP_0050.ACTION.CANCEL:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
234 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
235 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
236 d = defer.maybeDeferred(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
237 self.callback,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
238 command_elt,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
239 session_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
240 action,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
241 self.node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
242 self.client.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
243 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
244 d.addCallback(self._sendAnswer, session_id, command_elt.parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
245 d.addErrback(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
246 lambda failure, request: self._sendError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
247 failure.value.callback_error, session_id, request
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
248 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
249 command_elt.parent,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
250 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
251
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
252
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
253 class XEP_0050(object):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
254 STATUS = namedtuple("Status", ("EXECUTING", "COMPLETED", "CANCELED"))(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
255 "executing", "completed", "canceled"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
256 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
257 ACTION = namedtuple("Action", ("EXECUTE", "CANCEL", "NEXT", "PREV"))(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
258 "execute", "cancel", "next", "prev"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
259 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
260 NOTE = namedtuple("Note", ("INFO", "WARN", "ERROR"))("info", "warn", "error")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
261 ERROR = namedtuple(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
262 "Error",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
263 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
264 "MALFORMED_ACTION",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
265 "BAD_ACTION",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
266 "BAD_LOCALE",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
267 "BAD_PAYLOAD",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
268 "BAD_SESSIONID",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
269 "SESSION_EXPIRED",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
270 "FORBIDDEN",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
271 "ITEM_NOT_FOUND",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
272 "FEATURE_NOT_IMPLEMENTED",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
273 "INTERNAL",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
274 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
275 )(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
276 ("bad-request", "malformed-action"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
277 ("bad-request", "bad-action"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
278 ("bad-request", "bad-locale"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
279 ("bad-request", "bad-payload"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
280 ("bad-request", "bad-sessionid"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
281 ("not-allowed", "session-expired"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
282 ("forbidden", None),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
283 ("item-not-found", None),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
284 ("feature-not-implemented", None),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
285 ("internal-server-error", None),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
286 ) # 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
287
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
288 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 944
diff changeset
289 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
290 self.host = host
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
291 self.requesting = Sessions()
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.answering = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
293 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
294 "adHocRun",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
295 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
296 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
297 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
298 method=self._run,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
299 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
300 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
301 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
302 "adHocList",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
303 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
304 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
305 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
306 method=self._list,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
307 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
308 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
309 self.__requesting_id = host.registerCallback(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
310 self._requestingEntity, with_data=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
311 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
312 host.importMenu(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
313 (D_("Service"), D_("Commands")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
314 self._commandsMenu,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
315 security_limit=2,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
316 help_string=D_("Execute ad-hoc commands"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
317 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
318
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
319 def getHandler(self, client):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
320 return XEP_0050_handler(self)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
321
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
322 def profileConnected(self, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
323 self.addAdHocCommand(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
324 self._statusCallback, _("Status"), profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
325 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
326
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
327 def profileDisconnected(self, client):
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
328 try:
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
329 del self.answering[client.profile]
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
330 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
331 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
332
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
333 def _items2XMLUI(self, items, no_instructions):
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
334 """ 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
335 # 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
336 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
337
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
338 if not no_instructions:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
339 form_ui.addText(_("Please select a command"), "instructions")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
340
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
341 options = [(item.nodeIdentifier, item.name) for item in items]
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
342 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
343 return form_ui
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
344
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
345 def _getDataLvl(self, type_):
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
346 """Return the constant corresponding to <note/> type attribute value
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
347
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
348 @param type_: note type (see XEP-0050 §4.3)
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
349 @return: a C.XMLUI_DATA_LVL_* constant
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
350 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
351 if type_ == "error":
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
352 return C.XMLUI_DATA_LVL_ERROR
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
353 elif type_ == "warn":
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
354 return C.XMLUI_DATA_LVL_WARNING
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
355 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
356 if type_ != "info":
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
357 log.warning(_(u"Invalid note type [%s], using info") % type_)
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
358 return C.XMLUI_DATA_LVL_INFO
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
359
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
360 def _mergeNotes(self, notes):
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
361 """Merge notes with level prefix (e.g. "ERROR: the message")
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
362
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
363 @param notes (list): list of tuple (level, message)
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
364 @return: list of messages
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
365 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
366 lvl_map = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
367 C.XMLUI_DATA_LVL_INFO: "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
368 C.XMLUI_DATA_LVL_WARNING: "%s: " % _("WARNING"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
369 C.XMLUI_DATA_LVL_ERROR: "%s: " % _("ERROR"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
370 }
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
371 return [u"%s%s" % (lvl_map[lvl], msg) for lvl, msg in notes]
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
372
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
373 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
374 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
375 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
376 @param iq_elt: command result
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
377 @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
378 @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
379
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
380 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
381 command_elt = iq_elt.elements(NS_COMMANDS, "command").next()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
382 status = command_elt.getAttribute("status", XEP_0050.STATUS.EXECUTING)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
383 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
384 # 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
385 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
386 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
387 session_id = None
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
388 else:
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
389 return None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
390 remote_session_id = command_elt.getAttribute("sessionid")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
391 if remote_session_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
392 session_data["remote_id"] = remote_session_id
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
393 notes = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
394 for note_elt in command_elt.elements(NS_COMMANDS, "note"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
395 notes.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
396 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
397 self._getDataLvl(note_elt.getAttribute("type", "info")),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
398 unicode(note_elt),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
399 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
400 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
401 for data_elt in command_elt.elements(data_form.NS_X_DATA, "x"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
402 if data_elt["type"] in ("form", "result"):
2585
0112c1f7dcf0 plugin XEP-0050: filter form received from form-processing entity, to only keep "form" and "result" types
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
403 break
0112c1f7dcf0 plugin XEP-0050: filter form received from form-processing entity, to only keep "form" and "result" types
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
404 else:
0112c1f7dcf0 plugin XEP-0050: filter form received from form-processing entity, to only keep "form" and "result" types
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
405 # no matching data element found
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
406 if status != XEP_0050.STATUS.COMPLETED:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
407 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
408 _("No known payload found in ad-hoc command result, aborting")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
409 )
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
410 del self.requesting[session_id]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
411 return xml_tools.XMLUI(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
412 C.XMLUI_DIALOG,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
413 dialog_opt={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
414 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
415 C.XMLUI_DATA_MESS: _("No payload found"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
416 C.XMLUI_DATA_LVL: C.XMLUI_DATA_LVL_ERROR,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
417 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
418 )
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
419 if not notes:
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
420 # the status is completed, and we have no note to show
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
421 return None
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
422
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
423 # if we have only one note, we show a dialog with the level of the note
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
424 # if we have more, we show a dialog with "info" level, and all notes merged
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
425 dlg_level = notes[0][0] if len(notes) == 1 else C.XMLUI_DATA_LVL_INFO
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
426 return xml_tools.XMLUI(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
427 C.XMLUI_DIALOG,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
428 dialog_opt={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
429 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
430 C.XMLUI_DATA_MESS: u"\n".join(self._mergeNotes(notes)),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
431 C.XMLUI_DATA_LVL: dlg_level,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
432 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
433 session_id=session_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
434 )
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
435
1071
eef1f200d733 plugin XEP-0050: form returned by completed command are now managed
Goffi <goffi@goffi.org>
parents: 993
diff changeset
436 if session_id is None:
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
437 return xml_tools.dataFormEltResult2XMLUI(data_elt)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
438 form = data_form.Form.fromElement(data_elt)
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
439 # we add any present note to the instructions
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
440 form.instructions.extend(self._mergeNotes(notes))
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
441 return xml_tools.dataForm2XMLUI(form, self.__requesting_id, session_id=session_id)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
442
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
443 def _requestingEntity(self, data, profile):
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
444 def serialise(ret_data):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
445 if "xmlui" in ret_data:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
446 ret_data["xmlui"] = ret_data["xmlui"].toXml()
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
447 return ret_data
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
448
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
449 d = self.requestingEntity(data, profile)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
450 d.addCallback(serialise)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
451 return d
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
452
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
453 def requestingEntity(self, data, profile):
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
454 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
455 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
456 @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
457 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
458 @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
459
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
460 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
461 if C.bool(data.get("cancelled", C.BOOL_FALSE)):
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
462 return defer.succeed({})
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
463 client = self.host.getClient(profile)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
464 # 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
465 # TODO: managed answerer errors
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
466 # 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
467 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
468 # we just had the jid, we now request it for the available commands
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
469 session_id, session_data = self.requesting.newSession(profile=client.profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
470 entity = jid.JID(data[xml_tools.SAT_FORM_PREFIX + "jid"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
471 session_data["jid"] = entity
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
472 d = self.list(client, entity)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
473
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
474 def sendItems(xmlui):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
475 xmlui.session_id = session_id # we need to keep track of the session
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
476 return {"xmlui": xmlui}
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
477
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
478 d.addCallback(sendItems)
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
479 else:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
480 # 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
481 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
482 session_data = self.requesting.profileGet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
483 data["session_id"], client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
484 )
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
485 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
486 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
487 # TODO: send error dialog
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
488 return defer.succeed({})
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
489 session_id = data["session_id"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
490 entity = session_data["jid"]
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
491 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
492 session_data["node"]
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
493 # node has already been received
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
494 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
495 # it's the first time we know the node, we save it in session data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
496 session_data["node"] = data[xml_tools.SAT_FORM_PREFIX + "node"]
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
497
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
498 # we request execute node's command
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
499 iq_elt = compat.IQ(client.xmlstream, "set")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
500 iq_elt["to"] = entity.full()
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
501 command_elt = iq_elt.addElement("command", NS_COMMANDS)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
502 command_elt["node"] = session_data["node"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
503 command_elt["action"] = XEP_0050.ACTION.EXECUTE
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
504 try:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
505 # 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
506 # while session_id is our own session id used with the frontend
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
507 command_elt["sessionid"] = session_data["remote_id"]
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
508 except KeyError:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
509 pass
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
510
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
511 command_elt.addChild(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
512 xml_tools.XMLUIResultToElt(data)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
513 ) # We add the XMLUI result to the command payload
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
514 d = iq_elt.send()
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
515 d.addCallback(self._commandsAnswer2XMLUI, session_id, session_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
516 d.addCallback(lambda xmlui: {"xmlui": xmlui} if xmlui is not None else {})
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
517
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
518 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
519
773
eac23b1aad90 core: dynamics menus refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
520 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
521 """ 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
522 @param profile: %(doc_profile)s
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
523
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
524 """
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
525 form_ui = xml_tools.XMLUI("form", submit_id=self.__requesting_id)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
526 form_ui.addText(_("Please enter target jid"), "instructions")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 798
diff changeset
527 form_ui.changeContainer("pairs")
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
528 form_ui.addLabel("jid")
1539
98f92a054539 plugins: lowered security limits for non dangerous actions. A reference documentation is in progress to choose values: http://wiki.goffi.org/wiki/Security_Limits/en
Goffi <goffi@goffi.org>
parents: 1517
diff changeset
529 form_ui.addString("jid", value=self.host.getClient(profile).jid.host)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
530 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
531
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
532 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
533 """ Ad-hoc command used to change the "show" part of status """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
534 actions = session_data.setdefault("actions", [])
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
535 actions.append(action)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
536
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
537 if len(actions) == 1:
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
538 # 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
539 status = XEP_0050.STATUS.EXECUTING
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
540 form = data_form.Form("form", title=_("status selection"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
541 show_options = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
542 data_form.Option(name, label) for name, label in SHOWS.items()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
543 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
544 field = data_form.Field(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
545 "list-single", "show", options=show_options, required=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
546 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
547 form.addField(field)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
548
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
549 payload = form.toElement()
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
550 note = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
551
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
552 elif len(actions) == 2:
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
553 # 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
554 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
555 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next()
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
556 answer_form = data_form.Form.fromElement(x_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
557 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
558 except (KeyError, StopIteration):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
559 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
560 if show not in SHOWS:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
561 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
562 if show == "disconnect":
1505
5ddc4cf251fa plugin XEP-0050: pre-fill the target server's field with user's host.
souliane <souliane@mailoo.org>
parents: 1495
diff changeset
563 self.host.disconnect(profile)
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
564 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
565 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
566
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
567 # job done, we can end the session
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
568 form = data_form.Form("form", title=_(u"Updated"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
569 form.addField(data_form.Field("fixed", u"Status updated"))
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
570 status = XEP_0050.STATUS.COMPLETED
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
571 payload = None
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
572 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
573 else:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
574 raise AdHocError(XEP_0050.ERROR.INTERNAL)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
575
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
576 return (payload, status, None, note)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
577
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
578 def _run(self, service_jid_s="", node="", profile_key=C.PROF_KEY_NONE):
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
579 client = self.host.getClient(profile_key)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
580 service_jid = jid.JID(service_jid_s) if service_jid_s else None
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
581 d = self.run(client, service_jid, node or None)
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
582 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
583 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
584
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
585 @defer.inlineCallbacks
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
586 def run(self, client, service_jid=None, node=None):
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
587 """run an ad-hoc command
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
588
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
589 @param service_jid(jid.JID, None): jid of the ad-hoc service
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
590 None to use profile's server
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
591 @param node(unicode, None): node of the ad-hoc commnad
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
592 None to get initial list
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
593 @return(unicode): command page XMLUI
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
594 """
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
595 if service_jid is None:
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
596 service_jid = jid.JID(client.jid.host)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
597 session_id, session_data = self.requesting.newSession(profile=client.profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
598 session_data["jid"] = service_jid
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
599 if node is None:
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
600 xmlui = yield self.list(client, service_jid)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
601 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
602 session_data["node"] = node
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
603 cb_data = yield self.requestingEntity(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
604 {"session_id": session_id}, client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
605 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
606 xmlui = cb_data["xmlui"]
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
607
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
608 xmlui.session_id = session_id
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
609 defer.returnValue(xmlui)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
610
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
611 def _list(self, to_jid_s, profile_key):
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
612 client = self.host.getClient(profile_key)
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
613 to_jid = jid.JID(to_jid_s) if to_jid_s else None
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
614 d = self.list(client, to_jid, no_instructions=True)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
615 d.addCallback(lambda xmlui: xmlui.toXml())
763
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
616 return d
ab851b46009c plugin xep-0050 (ad-hoc commands): requesting part. first draft
Goffi <goffi@goffi.org>
parents: 758
diff changeset
617
2406
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
618 def list(self, client, to_jid, no_instructions=False):
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
619 """Request available commands
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
620
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
621 @param to_jid(jid.JID, None): the entity answering the commands
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
622 None to use profile's server
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
623 @param no_instructions(bool): if True, don't add instructions widget
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
624 """
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
625 d = self.host.getDiscoItems(client, to_jid, NS_COMMANDS)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
626 d.addCallback(self._items2XMLUI, no_instructions)
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
627 return d
1e5b2c35964e plugin XEP-0050: renamed "requestCommandsList" method to "list" + added a run command + fixes:
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
628
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
629 def addAdHocCommand(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
630 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
631 callback,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
632 label,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
633 node=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
634 features=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
635 timeout=600,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
636 allowed_jids=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
637 allowed_groups=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
638 allowed_magics=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
639 forbidden_jids=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
640 forbidden_groups=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
641 profile_key=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
642 ):
1110
36c1bbb8ca24 plugin XEP-0050: notes are now managed:
Goffi <goffi@goffi.org>
parents: 1079
diff changeset
643 """Add an ad-hoc command for the current profile
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
644
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
645 @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
646 @param label: label associated with this command on the main menu
1494
74ddb4cc2ff9 plugin XEP-0050: return disco for commands only on commands namespace
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
647 @param node: disco item node associated with this command. None to use autogenerated node
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
648 @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
649 @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
650 @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
651 @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
652 @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
653 @ALL@: allow everybody
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
654 @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
655 @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
656 @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
657 @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
658 @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
659 """
821
38bc9abd6722 plugin XEP-0050: fixed UnknownGroupError management + "@ALL@" profile for status command
Goffi <goffi@goffi.org>
parents: 811
diff changeset
660 # 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
661
1495
987e74cbb839 plugin XEP-0050: fixed crash on empty node in addAdHocCommand
Goffi <goffi@goffi.org>
parents: 1494
diff changeset
662 if node is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
663 node = "%s_%s" % ("COMMANDS", uuid4())
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
664
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
665 if features is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
666 features = [data_form.NS_X_DATA]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
667
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
668 if allowed_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
669 allowed_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
670 if allowed_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
671 allowed_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
672 if allowed_magics is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
673 allowed_magics = ["@PROFILE_BAREJID@"]
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
674 if forbidden_jids is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
675 forbidden_jids = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
676 if forbidden_groups is None:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
677 forbidden_groups = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
678
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
679 for client in self.host.getClients(profile_key):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
680 # TODO: manage newly created/removed profiles
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
681 _allowed_jids = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
682 (allowed_jids + [client.jid.userhostJID()])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
683 if "@PROFILE_BAREJID@" in allowed_magics
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
684 else allowed_jids
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
685 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
686 ad_hoc_command = AdHocCommand(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
687 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
688 callback,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
689 label,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
690 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
691 features,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
692 timeout,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
693 _allowed_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
694 allowed_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
695 allowed_magics,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
696 forbidden_jids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
697 forbidden_groups,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
698 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
699 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
700 ad_hoc_command.setHandlerParent(client)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
701 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
702 profile_commands[node] = ad_hoc_command
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
703
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
704 def onCmdRequest(self, request, profile):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
705 request.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
706 requestor = jid.JID(request["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
707 command_elt = request.elements(NS_COMMANDS, "command").next()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
708 action = command_elt.getAttribute("action", self.ACTION.EXECUTE)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
709 node = command_elt.getAttribute("node")
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
710 if not node:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
711 raise exceptions.DataError
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
712 sessionid = command_elt.getAttribute("sessionid")
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
713 try:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
714 command = self.answering[profile][node]
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
715 except KeyError:
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
716 raise exceptions.DataError
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
717 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
718
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
719
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
720 class XEP_0050_handler(XMPPHandler):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
721 implements(iwokkel.IDisco)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
722
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
723 def __init__(self, plugin_parent):
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
724 self.plugin_parent = plugin_parent
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
725
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
726 def connectionInitialized(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
727 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
728 CMD_REQUEST, self.plugin_parent.onCmdRequest, profile=self.parent.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
729 )
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
730
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
731 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
732 identities = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
733 if nodeIdentifier == NS_COMMANDS and self.plugin_parent.answering.get(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
734 self.parent.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
735 ): # we only add the identity if we have registred commands
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
736 identities.append(ID_CMD_LIST)
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
737 return [disco.DiscoFeature(NS_COMMANDS)] + identities
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
738
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
739 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
740 ret = []
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
741 if nodeIdentifier == NS_COMMANDS:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
742 for command in self.plugin_parent.answering.get(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
743 self.parent.profile, {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
744 ).values():
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
745 if command.isAuthorised(requestor):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
746 ret.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
747 disco.DiscoItem(self.parent.jid, command.node, command.getName())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2585
diff changeset
748 ) # TODO: manage name language
728
e07afabc4a25 plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
diff changeset
749 return ret