Ejemplo n.º 1
0
 def testConvertArray(self):
     params = [[1,2,3], ('a', 'b', 'c')]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <array>
                         <data>
                             <value><i4>1</i4></value>
                             <value><i4>2</i4></value>
                             <value><i4>3</i4></value>
                         </data>
                     </array>
                 </value>
             </param>
             <param>
                 <value>
                     <array>
                         <data>
                             <value><string>a</string></value>
                             <value><string>b</string></value>
                             <value><string>c</string></value>
                         </data>
                     </array>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Array to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(list(map(list, params)), xml2py(expected_xml),
                      "XML to array conversion")
Ejemplo n.º 2
0
 def testConvertStruct(self):
     params = [{"foo": "bar", "baz": False}]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <struct>
                         <member>
                             <name>foo</name>
                             <value><string>bar</string></value>
                         </member>
                         <member>
                             <name>baz</name>
                             <value><boolean>0</boolean></value>
                         </member>
                     </struct>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Struct to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                      "XML to struct conversion")
Ejemplo n.º 3
0
 def testConvertBase64(self):
     params = [rpcbase64(base64.b64encode(b"Hello, world!"))]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <base64>SGVsbG8sIHdvcmxkIQ==</base64>
                 </value>
             </param>
         </params>
     """)
     alternate_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <Base64>SGVsbG8sIHdvcmxkIQ==</Base64>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Base64 to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(list(map(lambda x: x.decode(), params)),
                      list(map(lambda x: x.decode(), xml2py(expected_xml))),
                      "XML to base64 conversion")
     self.assertEqual(list(map(lambda x: x.decode(), params)),
                      list(map(lambda x: x.decode(), xml2py(alternate_xml))),
                      "Alternate XML to base64 conversion")
Ejemplo n.º 4
0
    def send_rpc_iq(self, command, *argv, timeout=None, callback=None, timeout_callback=None):
        """ Compose a specific message  """

        my_iq = self.make_iq_set()
        my_iq['to'] = '[email protected]/rpc'
        my_iq['from'] = self.boundjid.full
        my_iq.enable('rpc_query')
        my_iq['rpc_query']['method_call']['method_name'] = command
        my_iq['rpc_query']['method_call']['params'] = py2xml(*argv)

        return my_iq.send(timeout=timeout, callback=callback, timeout_callback=timeout_callback)
Ejemplo n.º 5
0
    def send_rpc_iq(self, timeout=None, callback=None,
                  timeout_callback=None):
        
        iq = self.make_iq_set()
        iq['to'] = '[email protected]/rpc'
        iq['from'] = self.boundjid.full
        iq.enable('rpc_query')
        iq['rpc_query']['method_call']['method_name'] = 'RemoteInterface.getAll'
        iq['rpc_query']['method_call']['params'] = py2xml('de',4,0,0)        

        return iq.send(timeout=timeout, callback=callback,timeout_callback=timeout_callback)
Ejemplo n.º 6
0
 def _call_remote(self, pto, pmethod, callback, *arguments):
     iq = self._client.plugin['xep_0009'].make_iq_method_call(pto, pmethod, py2xml(*arguments))
     pid = iq['id']
     if callback is None:
         future = Future()
         self._register_callback(pid, future)
         iq.send()
         return future.get_value(30)
     else:
         log.debug("[RemoteSession] _call_remote %s", callback)
         self._register_callback(pid, callback)
         iq.send()
Ejemplo n.º 7
0
 def _call_remote(self, pto, pmethod, callback, *arguments):
     iq = self._client.plugin['xep_0009'].make_iq_method_call(pto, pmethod, py2xml(*arguments))
     pid = iq['id']
     if callback is None:
         future = Future()
         self._register_callback(pid, future)
         iq.send()
         return future.get_value(30)
     else:
         log.debug("[RemoteSession] _call_remote %s", callback)
         self._register_callback(pid, callback)
         iq.send()
Ejemplo n.º 8
0
 def testMethodResponse(self):
     iq = self.Iq()
     iq['rpc_query']['method_response']['params'] = py2xml(*())
     self.check(iq, """
         <iq>
             <query xmlns="jabber:iq:rpc">
                 <methodResponse>
                     <params />
                 </methodResponse>
             </query>
         </iq>
     """, use_values=False)
Ejemplo n.º 9
0
 def testMethodCall(self):
     iq = self.Iq()
     iq['rpc_query']['method_call']['method_name'] = 'system.exit'
     iq['rpc_query']['method_call']['params'] = py2xml(*())
     self.check(iq, """
         <iq>
             <query xmlns="jabber:iq:rpc">
                 <methodCall>
                     <methodName>system.exit</methodName>
                     <params />
                 </methodCall>
             </query>
         </iq>
     """, use_values=False)
Ejemplo n.º 10
0
    def send_cryptMessage(self,
                          message,
                          timeout=None,
                          callback=None,
                          timeout_callback=None):
        rpcmessage = rpcbase64(message)
        iq = self.client.make_iq_set()
        iq['to'] = '[email protected]/rpc'
        iq['from'] = self.client.boundjid.full
        iq.enable('rpc_query')
        iq['rpc_query']['method_call'][
            'method_name'] = 'RemoteInterface.cryptMessage'
        iq['rpc_query']['method_call']['params'] = py2xml(rpcmessage)

        return iq.send(timeout=timeout,
                       callback=callback,
                       timeout_callback=timeout_callback)
Ejemplo n.º 11
0
    def send_rpc_iq(self,
                    command,
                    *argv,
                    timeout=None,
                    callback=None,
                    timeout_callback=None):

        iq = self.make_iq_set()
        iq['to'] = '[email protected]/rpc'
        iq['from'] = self.boundjid.full
        iq.enable('rpc_query')
        iq['rpc_query']['method_call']['method_name'] = command
        iq['rpc_query']['method_call']['params'] = py2xml(*argv)

        return iq.send(timeout=timeout,
                       callback=callback,
                       timeout_callback=timeout_callback)
Ejemplo n.º 12
0
 def testConvertString(self):
     params = ["'This' & \"That\""]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <string>&apos;This&apos; &amp; &quot;That&quot;</string>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "String to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                     "XML to string conversion")
Ejemplo n.º 13
0
 def testConvertNil(self):
     params = [None]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <nil />
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Nil to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                      "XML to nil conversion")
Ejemplo n.º 14
0
 def testConvertUnicodeString(self):
     params = ["おはよう"]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <string>おはよう</string>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "String to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                     "XML to string conversion")
Ejemplo n.º 15
0
 def testConvertDouble(self):
     params = [3.14159265]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <double>3.14159265</double>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Double to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                      "XML to double conversion")
Ejemplo n.º 16
0
 def testConvertDateTime(self):
     params = [rpctime("20111220T01:50:00")]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <dateTime.iso8601>20111220T01:50:00</dateTime.iso8601>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "DateTime to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(list(map(lambda x: x.iso8601(), params)),
                      list(map(lambda x: x.iso8601(), xml2py(expected_xml))),
                      None)
Ejemplo n.º 17
0
 def _on_jabber_rpc_method_call(self, iq):
     iq.enable('rpc_query')
     params = iq['rpc_query']['method_call']['params']
     args = xml2py(params)
     pmethod = iq['rpc_query']['method_call']['method_name']
     try:
         with self._lock:
             entry = self._entries[pmethod]
             rules = self._acls[entry.get_endpoint_FQN()]
         if ACL.check(rules, iq['from'], pmethod):
             return_value = entry.call_method(args)
         else:
             raise AuthorizationException(
                 "Unauthorized access to %s from %s!" %
                 (pmethod, iq['from']))
         if return_value is None:
             return_value = ()
         response = self._client.plugin['xep_0009'].make_iq_method_response(
             iq['id'], iq['from'], py2xml(*return_value))
         response.send()
     except InvocationException as ie:
         fault = dict()
         fault['code'] = 500
         fault['string'] = ie.get_message()
         self._client.plugin['xep_0009']._send_fault(iq, fault2xml(fault))
     except AuthorizationException as ae:
         log.error(ae.get_message())
         error = self._client.plugin['xep_0009']._forbidden(iq)
         error.send()
     except Exception as e:
         if isinstance(e, KeyError):
             log.error("No handler available for %s!", pmethod)
             error = self._client.plugin['xep_0009']._item_not_found(iq)
         else:
             traceback.print_exc(file=sys.stderr)
             log.error("An unexpected problem occurred invoking method %s!",
                       pmethod)
             error = self._client.plugin['xep_0009']._undefined_condition(
                 iq)
         #! print "[REMOTE.PY] _handle_remote_procedure_call AN ERROR SHOULD BE SENT NOW %s " % e
         error.send()
Ejemplo n.º 18
0
    def send_cryptExchangeLocalKeys2(self,
                                     jid,
                                     key,
                                     timeout=None,
                                     callback=None,
                                     timeout_callback=None):

        rpckey = rpcbase64(key)

        iq = self.client.make_iq_set()
        iq['to'] = '[email protected]/rpc'
        iq['xmlns'] = 'jabber:client'
        # iq['from'] = '*****@*****.**' # self.boundjid.full
        iq.enable('rpc_query')
        iq['rpc_query']['method_call'][
            'method_name'] = 'RemoteInterface.cryptExchangeLocalKeys2'
        iq['rpc_query']['method_call']['params'] = py2xml(
            jid, rpckey, 'SCRAM-SHA-256', 0)

        return iq.send(timeout=timeout,
                       callback=callback,
                       timeout_callback=timeout_callback)
Ejemplo n.º 19
0
 def testConvertBoolean(self):
     params = [True, False]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <boolean>1</boolean>
                 </value>
             </param>
             <param>
                 <value>
                     <boolean>0</boolean>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Boolean to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                      "XML to boolean conversion")
Ejemplo n.º 20
0
 def testConvertInteger(self):
     params = [32767, -32768]
     params_xml = py2xml(*params)
     expected_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <i4>32767</i4>
                 </value>
             </param>
             <param>
                 <value>
                     <i4>-32768</i4>
                 </value>
             </param>
         </params>
     """)
     alternate_xml = self.parse_xml("""
         <params xmlns="jabber:iq:rpc">
             <param>
                 <value>
                     <int>32767</int>
                 </value>
             </param>
             <param>
                 <value>
                     <int>-32768</int>
                 </value>
             </param>
         </params>
     """)
     self.assertTrue(self.compare(expected_xml, params_xml),
                     "Integer to XML conversion\nExpected: %s\nGot: %s" % (
                         tostring(expected_xml), tostring(params_xml)))
     self.assertEqual(params, xml2py(expected_xml),
                      "XML to boolean conversion")
     self.assertEqual(params, xml2py(alternate_xml),
                      "Alternate XML to boolean conversion")
Ejemplo n.º 21
0
 def _on_jabber_rpc_method_call(self, iq):
     iq.enable('rpc_query')
     params = iq['rpc_query']['method_call']['params']
     args = xml2py(params)
     pmethod = iq['rpc_query']['method_call']['method_name']
     try:
         with self._lock:
             entry = self._entries[pmethod]
             rules = self._acls[entry.get_endpoint_FQN()]
         if ACL.check(rules, iq['from'], pmethod):
             return_value = entry.call_method(args)
         else:
             raise AuthorizationException("Unauthorized access to %s from %s!" % (pmethod, iq['from']))
         if return_value is None:
             return_value = ()
         response = self._client.plugin['xep_0009'].make_iq_method_response(iq['id'], iq['from'], py2xml(*return_value))
         response.send()
     except InvocationException as ie:
         fault = dict()
         fault['code'] = 500
         fault['string'] = ie.get_message()
         self._client.plugin['xep_0009']._send_fault(iq, fault2xml(fault))
     except AuthorizationException as ae:
         log.error(ae.get_message())
         error = self._client.plugin['xep_0009']._forbidden(iq)
         error.send()
     except Exception as e:
         if isinstance(e, KeyError):
             log.error("No handler available for %s!", pmethod)
             error = self._client.plugin['xep_0009']._item_not_found(iq)
         else:
             traceback.print_exc(file=sys.stderr)
             log.error("An unexpected problem occurred invoking method %s!", pmethod)
             error = self._client.plugin['xep_0009']._undefined_condition(iq)
         #! print "[REMOTE.PY] _handle_remote_procedure_call AN ERROR SHOULD BE SENT NOW %s " % e
         error.send()