diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2014-08-17 21:53:34 +0200 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2014-09-01 02:47:15 +0200 |
commit | 17174016ec6603afe87a65282f9b7eb55f2eafeb (patch) | |
tree | 1e815d4ab942bf3b8ac932d3a1213b7a00659e26 /slixmpp/plugins/xep_0009 | |
parent | ed37174a2b40d2d90aa6a0c7f778108687d39602 (diff) | |
download | slixmpp-17174016ec6603afe87a65282f9b7eb55f2eafeb.tar.gz slixmpp-17174016ec6603afe87a65282f9b7eb55f2eafeb.tar.bz2 slixmpp-17174016ec6603afe87a65282f9b7eb55f2eafeb.tar.xz slixmpp-17174016ec6603afe87a65282f9b7eb55f2eafeb.zip |
Remove all trailing whitespaces.
Diffstat (limited to 'slixmpp/plugins/xep_0009')
-rw-r--r-- | slixmpp/plugins/xep_0009/rpc.py | 436 |
1 files changed, 218 insertions, 218 deletions
diff --git a/slixmpp/plugins/xep_0009/rpc.py b/slixmpp/plugins/xep_0009/rpc.py index ff17c417..674d13d6 100644 --- a/slixmpp/plugins/xep_0009/rpc.py +++ b/slixmpp/plugins/xep_0009/rpc.py @@ -1,218 +1,218 @@ -"""
- Slixmpp: The Slick XMPP Library
- Copyright (C) 2011 Nathanael C. Fritz, Dann Martens (TOMOTON).
- This file is part of Slixmpp.
-
- See the file LICENSE for copying permission.
-"""
-
-import logging
-
-from slixmpp import Iq
-from slixmpp.xmlstream import ET, register_stanza_plugin
-from slixmpp.xmlstream.handler import Callback
-from slixmpp.xmlstream.matcher import MatchXPath
-from slixmpp.plugins import BasePlugin
-from slixmpp.plugins.xep_0009 import stanza
-from slixmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, MethodResponse
-
-
-log = logging.getLogger(__name__)
-
-
-class XEP_0009(BasePlugin):
-
- name = 'xep_0009'
- description = 'XEP-0009: Jabber-RPC'
- dependencies = set(['xep_0030'])
- stanza = stanza
-
- def plugin_init(self):
- register_stanza_plugin(Iq, RPCQuery)
- register_stanza_plugin(RPCQuery, MethodCall)
- register_stanza_plugin(RPCQuery, MethodResponse)
-
- self.xmpp.register_handler(
- Callback('RPC Call', MatchXPath('{%s}iq/{%s}query/{%s}methodCall' % (self.xmpp.default_ns, RPCQuery.namespace, RPCQuery.namespace)),
- self._handle_method_call)
- )
- self.xmpp.register_handler(
- Callback('RPC Call', MatchXPath('{%s}iq/{%s}query/{%s}methodResponse' % (self.xmpp.default_ns, RPCQuery.namespace, RPCQuery.namespace)),
- self._handle_method_response)
- )
- self.xmpp.register_handler(
- Callback('RPC Call', MatchXPath('{%s}iq/{%s}error' % (self.xmpp.default_ns, self.xmpp.default_ns)),
- self._handle_error)
- )
- self.xmpp.add_event_handler('jabber_rpc_method_call', self._on_jabber_rpc_method_call)
- self.xmpp.add_event_handler('jabber_rpc_method_response', self._on_jabber_rpc_method_response)
- self.xmpp.add_event_handler('jabber_rpc_method_fault', self._on_jabber_rpc_method_fault)
- self.xmpp.add_event_handler('jabber_rpc_error', self._on_jabber_rpc_error)
- self.xmpp.add_event_handler('error', self._handle_error)
- #self.activeCalls = []
-
- self.xmpp['xep_0030'].add_feature('jabber:iq:rpc')
- self.xmpp['xep_0030'].add_identity('automation','rpc')
-
- def make_iq_method_call(self, pto, pmethod, params):
- iq = self.xmpp.makeIqSet()
- iq.attrib['to'] = pto
- iq.attrib['from'] = self.xmpp.boundjid.full
- iq.enable('rpc_query')
- iq['rpc_query']['method_call']['method_name'] = pmethod
- iq['rpc_query']['method_call']['params'] = params
- return iq;
-
- def make_iq_method_response(self, pid, pto, params):
- iq = self.xmpp.makeIqResult(pid)
- iq.attrib['to'] = pto
- iq.attrib['from'] = self.xmpp.boundjid.full
- iq.enable('rpc_query')
- iq['rpc_query']['method_response']['params'] = params
- return iq
-
- def make_iq_method_response_fault(self, pid, pto, params):
- iq = self.xmpp.makeIqResult(pid)
- iq.attrib['to'] = pto
- iq.attrib['from'] = self.xmpp.boundjid.full
- iq.enable('rpc_query')
- iq['rpc_query']['method_response']['params'] = None
- iq['rpc_query']['method_response']['fault'] = params
- return iq
-
-# def make_iq_method_error(self, pto, pid, pmethod, params, code, type, condition):
-# iq = self.xmpp.makeIqError(pid)
-# iq.attrib['to'] = pto
-# iq.attrib['from'] = self.xmpp.boundjid.full
-# iq['error']['code'] = code
-# iq['error']['type'] = type
-# iq['error']['condition'] = condition
-# iq['rpc_query']['method_call']['method_name'] = pmethod
-# iq['rpc_query']['method_call']['params'] = params
-# return iq
-
- def _item_not_found(self, iq):
- payload = iq.get_payload()
- iq.reply().error().set_payload(payload);
- iq['error']['code'] = '404'
- iq['error']['type'] = 'cancel'
- iq['error']['condition'] = 'item-not-found'
- return iq
-
- def _undefined_condition(self, iq):
- payload = iq.get_payload()
- iq.reply().error().set_payload(payload)
- iq['error']['code'] = '500'
- iq['error']['type'] = 'cancel'
- iq['error']['condition'] = 'undefined-condition'
- return iq
-
- def _forbidden(self, iq):
- payload = iq.get_payload()
- iq.reply().error().set_payload(payload)
- iq['error']['code'] = '403'
- iq['error']['type'] = 'auth'
- iq['error']['condition'] = 'forbidden'
- return iq
-
- def _recipient_unvailable(self, iq):
- payload = iq.get_payload()
- iq.reply().error().set_payload(payload)
- iq['error']['code'] = '404'
- iq['error']['type'] = 'wait'
- iq['error']['condition'] = 'recipient-unavailable'
- return iq
-
- def _handle_method_call(self, iq):
- type = iq['type']
- if type == 'set':
- log.debug("Incoming Jabber-RPC call from %s", iq['from'])
- self.xmpp.event('jabber_rpc_method_call', iq)
- else:
- if type == 'error' and ['rpc_query'] is None:
- self.handle_error(iq)
- else:
- log.debug("Incoming Jabber-RPC error from %s", iq['from'])
- self.xmpp.event('jabber_rpc_error', iq)
-
- def _handle_method_response(self, iq):
- if iq['rpc_query']['method_response']['fault'] is not None:
- log.debug("Incoming Jabber-RPC fault from %s", iq['from'])
- #self._on_jabber_rpc_method_fault(iq)
- self.xmpp.event('jabber_rpc_method_fault', iq)
- else:
- log.debug("Incoming Jabber-RPC response from %s", iq['from'])
- self.xmpp.event('jabber_rpc_method_response', iq)
-
- def _handle_error(self, iq):
- print("['XEP-0009']._handle_error -> ERROR! Iq is '%s'" % iq)
- print("#######################")
- print("### NOT IMPLEMENTED ###")
- print("#######################")
-
- def _on_jabber_rpc_method_call(self, iq, forwarded=False):
- """
- A default handler for Jabber-RPC method call. If another
- handler is registered, this one will defer and not run.
-
- If this handler is called by your own custom handler with
- forwarded set to True, then it will run as normal.
- """
- if not forwarded and self.xmpp.event_handled('jabber_rpc_method_call') > 1:
- return
- # Reply with error by default
- error = self.client.plugin['xep_0009']._item_not_found(iq)
- error.send()
-
- def _on_jabber_rpc_method_response(self, iq, forwarded=False):
- """
- A default handler for Jabber-RPC method response. If another
- handler is registered, this one will defer and not run.
-
- If this handler is called by your own custom handler with
- forwarded set to True, then it will run as normal.
- """
- if not forwarded and self.xmpp.event_handled('jabber_rpc_method_response') > 1:
- return
- error = self.client.plugin['xep_0009']._recpient_unavailable(iq)
- error.send()
-
- def _on_jabber_rpc_method_fault(self, iq, forwarded=False):
- """
- A default handler for Jabber-RPC fault response. If another
- handler is registered, this one will defer and not run.
-
- If this handler is called by your own custom handler with
- forwarded set to True, then it will run as normal.
- """
- if not forwarded and self.xmpp.event_handled('jabber_rpc_method_fault') > 1:
- return
- error = self.client.plugin['xep_0009']._recpient_unavailable(iq)
- error.send()
-
- def _on_jabber_rpc_error(self, iq, forwarded=False):
- """
- A default handler for Jabber-RPC error response. If another
- handler is registered, this one will defer and not run.
-
- If this handler is called by your own custom handler with
- forwarded set to True, then it will run as normal.
- """
- if not forwarded and self.xmpp.event_handled('jabber_rpc_error') > 1:
- return
- error = self.client.plugin['xep_0009']._recpient_unavailable(iq, iq.get_payload())
- error.send()
-
- def _send_fault(self, iq, fault_xml): #
- fault = self.make_iq_method_response_fault(iq['id'], iq['from'], fault_xml)
- fault.send()
-
- def _send_error(self, iq):
- print("['XEP-0009']._send_error -> ERROR! Iq is '%s'" % iq)
- print("#######################")
- print("### NOT IMPLEMENTED ###")
- print("#######################")
-
- def _extract_method(self, stanza):
- xml = ET.fromstring("%s" % stanza)
- return xml.find("./methodCall/methodName").text
+""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2011 Nathanael C. Fritz, Dann Martens (TOMOTON). + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +from slixmpp import Iq +from slixmpp.xmlstream import ET, register_stanza_plugin +from slixmpp.xmlstream.handler import Callback +from slixmpp.xmlstream.matcher import MatchXPath +from slixmpp.plugins import BasePlugin +from slixmpp.plugins.xep_0009 import stanza +from slixmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, MethodResponse + + +log = logging.getLogger(__name__) + + +class XEP_0009(BasePlugin): + + name = 'xep_0009' + description = 'XEP-0009: Jabber-RPC' + dependencies = set(['xep_0030']) + stanza = stanza + + def plugin_init(self): + register_stanza_plugin(Iq, RPCQuery) + register_stanza_plugin(RPCQuery, MethodCall) + register_stanza_plugin(RPCQuery, MethodResponse) + + self.xmpp.register_handler( + Callback('RPC Call', MatchXPath('{%s}iq/{%s}query/{%s}methodCall' % (self.xmpp.default_ns, RPCQuery.namespace, RPCQuery.namespace)), + self._handle_method_call) + ) + self.xmpp.register_handler( + Callback('RPC Call', MatchXPath('{%s}iq/{%s}query/{%s}methodResponse' % (self.xmpp.default_ns, RPCQuery.namespace, RPCQuery.namespace)), + self._handle_method_response) + ) + self.xmpp.register_handler( + Callback('RPC Call', MatchXPath('{%s}iq/{%s}error' % (self.xmpp.default_ns, self.xmpp.default_ns)), + self._handle_error) + ) + self.xmpp.add_event_handler('jabber_rpc_method_call', self._on_jabber_rpc_method_call) + self.xmpp.add_event_handler('jabber_rpc_method_response', self._on_jabber_rpc_method_response) + self.xmpp.add_event_handler('jabber_rpc_method_fault', self._on_jabber_rpc_method_fault) + self.xmpp.add_event_handler('jabber_rpc_error', self._on_jabber_rpc_error) + self.xmpp.add_event_handler('error', self._handle_error) + #self.activeCalls = [] + + self.xmpp['xep_0030'].add_feature('jabber:iq:rpc') + self.xmpp['xep_0030'].add_identity('automation','rpc') + + def make_iq_method_call(self, pto, pmethod, params): + iq = self.xmpp.makeIqSet() + iq.attrib['to'] = pto + iq.attrib['from'] = self.xmpp.boundjid.full + iq.enable('rpc_query') + iq['rpc_query']['method_call']['method_name'] = pmethod + iq['rpc_query']['method_call']['params'] = params + return iq; + + def make_iq_method_response(self, pid, pto, params): + iq = self.xmpp.makeIqResult(pid) + iq.attrib['to'] = pto + iq.attrib['from'] = self.xmpp.boundjid.full + iq.enable('rpc_query') + iq['rpc_query']['method_response']['params'] = params + return iq + + def make_iq_method_response_fault(self, pid, pto, params): + iq = self.xmpp.makeIqResult(pid) + iq.attrib['to'] = pto + iq.attrib['from'] = self.xmpp.boundjid.full + iq.enable('rpc_query') + iq['rpc_query']['method_response']['params'] = None + iq['rpc_query']['method_response']['fault'] = params + return iq + +# def make_iq_method_error(self, pto, pid, pmethod, params, code, type, condition): +# iq = self.xmpp.makeIqError(pid) +# iq.attrib['to'] = pto +# iq.attrib['from'] = self.xmpp.boundjid.full +# iq['error']['code'] = code +# iq['error']['type'] = type +# iq['error']['condition'] = condition +# iq['rpc_query']['method_call']['method_name'] = pmethod +# iq['rpc_query']['method_call']['params'] = params +# return iq + + def _item_not_found(self, iq): + payload = iq.get_payload() + iq.reply().error().set_payload(payload); + iq['error']['code'] = '404' + iq['error']['type'] = 'cancel' + iq['error']['condition'] = 'item-not-found' + return iq + + def _undefined_condition(self, iq): + payload = iq.get_payload() + iq.reply().error().set_payload(payload) + iq['error']['code'] = '500' + iq['error']['type'] = 'cancel' + iq['error']['condition'] = 'undefined-condition' + return iq + + def _forbidden(self, iq): + payload = iq.get_payload() + iq.reply().error().set_payload(payload) + iq['error']['code'] = '403' + iq['error']['type'] = 'auth' + iq['error']['condition'] = 'forbidden' + return iq + + def _recipient_unvailable(self, iq): + payload = iq.get_payload() + iq.reply().error().set_payload(payload) + iq['error']['code'] = '404' + iq['error']['type'] = 'wait' + iq['error']['condition'] = 'recipient-unavailable' + return iq + + def _handle_method_call(self, iq): + type = iq['type'] + if type == 'set': + log.debug("Incoming Jabber-RPC call from %s", iq['from']) + self.xmpp.event('jabber_rpc_method_call', iq) + else: + if type == 'error' and ['rpc_query'] is None: + self.handle_error(iq) + else: + log.debug("Incoming Jabber-RPC error from %s", iq['from']) + self.xmpp.event('jabber_rpc_error', iq) + + def _handle_method_response(self, iq): + if iq['rpc_query']['method_response']['fault'] is not None: + log.debug("Incoming Jabber-RPC fault from %s", iq['from']) + #self._on_jabber_rpc_method_fault(iq) + self.xmpp.event('jabber_rpc_method_fault', iq) + else: + log.debug("Incoming Jabber-RPC response from %s", iq['from']) + self.xmpp.event('jabber_rpc_method_response', iq) + + def _handle_error(self, iq): + print("['XEP-0009']._handle_error -> ERROR! Iq is '%s'" % iq) + print("#######################") + print("### NOT IMPLEMENTED ###") + print("#######################") + + def _on_jabber_rpc_method_call(self, iq, forwarded=False): + """ + A default handler for Jabber-RPC method call. If another + handler is registered, this one will defer and not run. + + If this handler is called by your own custom handler with + forwarded set to True, then it will run as normal. + """ + if not forwarded and self.xmpp.event_handled('jabber_rpc_method_call') > 1: + return + # Reply with error by default + error = self.client.plugin['xep_0009']._item_not_found(iq) + error.send() + + def _on_jabber_rpc_method_response(self, iq, forwarded=False): + """ + A default handler for Jabber-RPC method response. If another + handler is registered, this one will defer and not run. + + If this handler is called by your own custom handler with + forwarded set to True, then it will run as normal. + """ + if not forwarded and self.xmpp.event_handled('jabber_rpc_method_response') > 1: + return + error = self.client.plugin['xep_0009']._recpient_unavailable(iq) + error.send() + + def _on_jabber_rpc_method_fault(self, iq, forwarded=False): + """ + A default handler for Jabber-RPC fault response. If another + handler is registered, this one will defer and not run. + + If this handler is called by your own custom handler with + forwarded set to True, then it will run as normal. + """ + if not forwarded and self.xmpp.event_handled('jabber_rpc_method_fault') > 1: + return + error = self.client.plugin['xep_0009']._recpient_unavailable(iq) + error.send() + + def _on_jabber_rpc_error(self, iq, forwarded=False): + """ + A default handler for Jabber-RPC error response. If another + handler is registered, this one will defer and not run. + + If this handler is called by your own custom handler with + forwarded set to True, then it will run as normal. + """ + if not forwarded and self.xmpp.event_handled('jabber_rpc_error') > 1: + return + error = self.client.plugin['xep_0009']._recpient_unavailable(iq, iq.get_payload()) + error.send() + + def _send_fault(self, iq, fault_xml): # + fault = self.make_iq_method_response_fault(iq['id'], iq['from'], fault_xml) + fault.send() + + def _send_error(self, iq): + print("['XEP-0009']._send_error -> ERROR! Iq is '%s'" % iq) + print("#######################") + print("### NOT IMPLEMENTED ###") + print("#######################") + + def _extract_method(self, stanza): + xml = ET.fromstring("%s" % stanza) + return xml.find("./methodCall/methodName").text |