changeset 2121:ae2134211ac1

XMPP -> wiki XML RPC, the beginning...
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Thu, 14 Jun 2007 01:19:20 +0200
parents 2223b127380a
children 69412724ce93
files jabberbot/commands.py jabberbot/main.py jabberbot/xmlrpcbot.py jabberbot/xmppbot.py
diffstat 4 files changed, 77 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/jabberbot/commands.py	Thu Jun 14 01:18:43 2007 +0200
+++ b/jabberbot/commands.py	Thu Jun 14 01:19:20 2007 +0200
@@ -46,7 +46,7 @@
     
     def __init__(self, jid):
         self.jid = jid
-        self.payload = None
+        self.data = None
         
 class GetPage(BaseDataCommand):
     
--- a/jabberbot/main.py	Thu Jun 14 01:18:43 2007 +0200
+++ b/jabberbot/main.py	Thu Jun 14 01:19:20 2007 +0200
@@ -33,7 +33,7 @@
     
     try:
         xmpp_bot = XMPPBot(BotConfig, commands_from_xmpp, commands_to_xmpp)
-        xmlrpc_client = XMLRPCClient(BotConfig, commands_from_xmpp)
+        xmlrpc_client = XMLRPCClient(BotConfig, commands_from_xmpp, commands_to_xmpp)
         xmlrpc_server = XMLRPCServer(BotConfig, commands_to_xmpp)
         
         xmpp_bot.start()
--- a/jabberbot/xmlrpcbot.py	Thu Jun 14 01:18:43 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Thu Jun 14 01:19:20 2007 +0200
@@ -6,11 +6,14 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+import Queue
 import time, xmlrpclib
 from threading import Thread
 from SimpleXMLRPCServer import SimpleXMLRPCServer
 
 import jabberbot.commands as cmd
+from jabberbot.multicall import MultiCall
+
 
 class XMLRPCClient(Thread):
     """XMLRPC Client
@@ -19,16 +22,71 @@
     a wiki, as inctructed by command objects received from
     the XMPP component"""
     
-    def __init__(self, config, commands):
+    def __init__(self, config, commands_in, commands_out):
         """
         @param commands: an output command queue
         """
         Thread.__init__(self)
-        self.commands = commands
+        self.commands_in = commands_in
+        self.commands_out = commands_out
         self.config = config
-        
+        self.url = config.wiki_url + "?action=xmlrpc2"
+        self.connection = self.create_connection()
+        self.token = None
+        self.multicall = None
+
     def run(self):
         """Starts the server / thread"""
+        while True:
+            try:
+                command = self.commands_in.get(True, 2)
+                self.execute_command(command)
+            except Queue.Empty:
+                pass
+            
+    def create_connection(self):
+        return xmlrpclib.ServerProxy(self.url, allow_none=True, verbose=self.config.verbose)
+                
+    def execute_command(self, command):
+        """Execute commands coming from the XMPP component"""
+        
+        # FIXME: make this kind of automatic
+        if isinstance(command, cmd.GetPage):
+            self.get_page(command)
+        elif isinstance(command, cmd.GetPageHTML):
+            self.get_page_html(command)
+    
+    def get_auth_token(self):
+        token = self.connection.getAuthToken("grzywacz", "ppp")
+        if token:
+            self.token = token
+    
+    def get_page(self, command):
+        if self.multicall is not None:
+            del self.multicall
+        self.multicall = MultiCall(self.connection)
+        
+        if not self.token:
+            self.get_auth_token()
+            
+        if not self.token:
+            # FIXME: notify the user that he may not have full rights on the wiki
+            pass
+        
+        self.multicall.applyAuthToken(self.token)
+        self.multicall.getPage(command.pagename)
+        token_result, getpage_result = self.multicall()
+
+        # We get a dict only when Fault happens
+        if isinstance(getpage_result, dict):
+            error_str = u"""The page couldn't be retrieved. The reason is: "%s"."""
+            command.data = error_str % getpage_result["faultString"]
+        else:
+            command.data = getpage_result
+     
+        self.commands_out.put_nowait(command)
+    
+    def get_page_html(self, command):
         pass
 
 
--- a/jabberbot/xmppbot.py	Thu Jun 14 01:18:43 2007 +0200
+++ b/jabberbot/xmppbot.py	Thu Jun 14 01:19:20 2007 +0200
@@ -196,10 +196,14 @@
             jid = JID(node_or_jid=command.jid)
             self.ask_for_subscription(jid)
             
-        if isinstance(command, cmd.RemoveJIDFromRosterCommand):
+        elif isinstance(command, cmd.RemoveJIDFromRosterCommand):
             jid = JID(node_or_jid=command.jid)
             self.remove_subscription(jid)
             
+        elif isinstance(command, cmd.GetPage):
+            msg = u"""Here's the page "%s" that you've requested:\n\n%s"""
+            self.send_message(command.jid, msg % (command.pagename, command.data))
+            
     def ask_for_subscription(self, jid):
         """Sends a <presence/> stanza with type="subscribe"
         
@@ -256,7 +260,7 @@
         if command[0] in self.internal_commands:
             response = self.handle_internal_command(command)
         elif command[0] in self.xmlrpc_commands.keys():
-            response = self.handle_xmlrpc_command(command)
+            response = self.handle_xmlrpc_command(sender, command)
         else:
             response = self.reply_help()
         
@@ -301,8 +305,8 @@
             return help_str % (command, classobj.description, command, classobj.parameter_list)
         
         
-    def handle_xmlrpc_command(self, command):
-        """Creates a command object, and puts it the command queuq
+    def handle_xmlrpc_command(self, sender, command):
+        """Creates a command object, and puts it the command queue
         
         @param command: a valid name of available xmlrpc command
         @type command: list representing a command, name and parameters
@@ -310,15 +314,18 @@
         """
         command_class = self.xmlrpc_commands[command[0]]
         
+        # Add sender's JID to the argument list
+        command.insert(1, sender.as_utf8())
+        
         try:
             instance = command_class.__new__(command_class)
-            instance.__init__(instance, *command[1:])
+            instance.__init__(*command[1:])
             self.from_commands.put_nowait(instance)
             
         # This happens when user specifies wrong parameters
         except TypeError:
-            help = u"You've specified a wrong parameter list. The call should look like:\n\n%s"
-            return help % (command_class.get_invocation_help(), )
+            help = u"You've specified a wrong parameter list. The call should look like:\n\n%s %s"
+            return help % (command[0], command_class.parameter_list)
             
     def handle_unsubscribed_presence(self, stanza):
         """Handles unsubscribed presence stanzas"""