| ... |
... |
@@ -17,16 +17,15 @@ |
|
17
|
17
|
|
|
18
|
18
|
from nbxmpp.namespaces import Namespace
|
|
19
|
19
|
from nbxmpp.protocol import Iq
|
|
20
|
|
-from nbxmpp.protocol import isResultNode
|
|
21
|
20
|
from nbxmpp.protocol import Node
|
|
22
|
|
-from nbxmpp.util import call_on_response
|
|
23
|
|
-from nbxmpp.util import callback
|
|
24
|
|
-from nbxmpp.util import raise_error
|
|
25
|
21
|
from nbxmpp.structs import AdHocCommand
|
|
26
|
22
|
from nbxmpp.structs import AdHocCommandNote
|
|
27
|
23
|
from nbxmpp.const import AdHocStatus
|
|
28
|
24
|
from nbxmpp.const import AdHocAction
|
|
29
|
25
|
from nbxmpp.const import AdHocNoteType
|
|
|
26
|
+from nbxmpp.errors import StanzaError
|
|
|
27
|
+from nbxmpp.errors import MalformedStanzaError
|
|
|
28
|
+from nbxmpp.task import iq_request_task
|
|
30
|
29
|
from nbxmpp.modules.discovery import get_disco_request
|
|
31
|
30
|
from nbxmpp.modules.base import BaseModule
|
|
32
|
31
|
|
| ... |
... |
@@ -38,22 +37,21 @@ class AdHoc(BaseModule): |
|
38
|
37
|
self._client = client
|
|
39
|
38
|
self.handlers = []
|
|
40
|
39
|
|
|
41
|
|
- @call_on_response('_command_list_received')
|
|
|
40
|
+ @iq_request_task
|
|
42
|
41
|
def request_command_list(self, jid=None):
|
|
|
42
|
+ _task = yield
|
|
|
43
|
+
|
|
43
|
44
|
if jid is None:
|
|
44
|
45
|
jid = self._client.get_bound_jid().bare
|
|
45
|
|
- return get_disco_request(Namespace.DISCO_ITEMS,
|
|
46
|
|
- jid,
|
|
47
|
|
- node=Namespace.COMMANDS)
|
|
48
|
|
-
|
|
49
|
|
- @callback
|
|
50
|
|
- def _command_list_received(self, stanza):
|
|
51
|
|
- if not isResultNode(stanza):
|
|
52
|
|
- return raise_error(self._log.info, stanza)
|
|
|
46
|
+ response = yield get_disco_request(Namespace.DISCO_ITEMS,
|
|
|
47
|
+ jid,
|
|
|
48
|
+ node=Namespace.COMMANDS)
|
|
|
49
|
+ if response.isError():
|
|
|
50
|
+ raise StanzaError(response)
|
|
53
|
51
|
|
|
54
|
|
- payload = stanza.getQueryPayload()
|
|
|
52
|
+ payload = response.getQueryPayload()
|
|
55
|
53
|
if payload is None:
|
|
56
|
|
- return raise_error(self._log.warning, stanza, 'stanza-malformed')
|
|
|
54
|
+ raise MalformedStanzaError('query payload missing', response)
|
|
57
|
55
|
|
|
58
|
56
|
command_list = []
|
|
59
|
57
|
for item in payload:
|
| ... |
... |
@@ -62,15 +60,15 @@ class AdHoc(BaseModule): |
|
62
|
60
|
try:
|
|
63
|
61
|
command_list.append(AdHocCommand(**item.getAttrs()))
|
|
64
|
62
|
except Exception as error:
|
|
65
|
|
- self._log.warning(error)
|
|
66
|
|
- return raise_error(self._log.warning,
|
|
67
|
|
- stanza,
|
|
68
|
|
- 'stanza-malformed')
|
|
|
63
|
+ raise MalformedStanzaError(f'invalid item attributes: {error}',
|
|
|
64
|
+ response)
|
|
69
|
65
|
|
|
70
|
|
- return command_list
|
|
|
66
|
+ yield command_list
|
|
71
|
67
|
|
|
72
|
|
- @call_on_response('_command_result_received')
|
|
|
68
|
+ @iq_request_task
|
|
73
|
69
|
def execute_command(self, command, action=None, dataform=None):
|
|
|
70
|
+ _task = yield
|
|
|
71
|
+
|
|
74
|
72
|
if action is None:
|
|
75
|
73
|
action = AdHocAction.EXECUTE
|
|
76
|
74
|
attrs = {'node': command.node,
|
| ... |
... |
@@ -79,21 +77,13 @@ class AdHoc(BaseModule): |
|
79
|
77
|
if command.sessionid is not None:
|
|
80
|
78
|
attrs['sessionid'] = command.sessionid
|
|
81
|
79
|
|
|
82
|
|
- command_node = Node('command', attrs=attrs)
|
|
83
|
|
- if dataform is not None:
|
|
84
|
|
- command_node.addChild(node=dataform)
|
|
85
|
|
- iq = Iq('set', to=command.jid)
|
|
86
|
|
- iq.addChild(node=command_node)
|
|
87
|
|
- return iq
|
|
|
80
|
+ response = yield _make_command(command, attrs, dataform)
|
|
|
81
|
+ if response.isError():
|
|
|
82
|
+ raise StanzaError(response)
|
|
88
|
83
|
|
|
89
|
|
- @callback
|
|
90
|
|
- def _command_result_received(self, stanza):
|
|
91
|
|
- if not isResultNode(stanza):
|
|
92
|
|
- return raise_error(self._log.info, stanza)
|
|
93
|
|
-
|
|
94
|
|
- command = stanza.getTag('command', namespace=Namespace.COMMANDS)
|
|
|
84
|
+ command = response.getTag('command', namespace=Namespace.COMMANDS)
|
|
95
|
85
|
if command is None:
|
|
96
|
|
- return raise_error(self._log.warning, stanza, 'stanza-malformed')
|
|
|
86
|
+ raise MalformedStanzaError('command node missing', response)
|
|
97
|
87
|
|
|
98
|
88
|
attrs = command.getAttrs()
|
|
99
|
89
|
notes = []
|
| ... |
... |
@@ -111,8 +101,8 @@ class AdHoc(BaseModule): |
|
111
|
101
|
for action in actions_.getChildren():
|
|
112
|
102
|
actions.append(AdHocAction(action.getName()))
|
|
113
|
103
|
|
|
114
|
|
- return AdHocCommand(
|
|
115
|
|
- jid=str(stanza.getFrom()),
|
|
|
104
|
+ yield AdHocCommand(
|
|
|
105
|
+ jid=str(response.getFrom()),
|
|
116
|
106
|
name=None,
|
|
117
|
107
|
node=attrs['node'],
|
|
118
|
108
|
sessionid=attrs.get('sessionid'),
|
| ... |
... |
@@ -121,5 +111,13 @@ class AdHoc(BaseModule): |
|
121
|
111
|
actions=actions,
|
|
122
|
112
|
notes=notes)
|
|
123
|
113
|
except Exception as error:
|
|
124
|
|
- self._log.warning(error)
|
|
125
|
|
- return raise_error(self._log.warning, stanza, 'stanza-malformed')
|
|
|
114
|
+ raise MalformedStanzaError(str(error), response)
|
|
|
115
|
+
|
|
|
116
|
+
|
|
|
117
|
+def _make_command(command, attrs, dataform):
|
|
|
118
|
+ command_node = Node('command', attrs=attrs)
|
|
|
119
|
+ if dataform is not None:
|
|
|
120
|
+ command_node.addChild(node=dataform)
|
|
|
121
|
+ iq = Iq('set', to=command.jid)
|
|
|
122
|
+ iq.addChild(node=command_node)
|
|
|
123
|
+ return iq
|