changeset 2125:9d5f4ba23a54

Add a GetPageList command. Some code duplication to be refactored later.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 15 Jun 2007 17:36:00 +0200
parents 005938143378
children 8b7d6d70fc95
files jabberbot/commands.py jabberbot/xmlrpcbot.py jabberbot/xmppbot.py
diffstat 3 files changed, 73 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/jabberbot/commands.py	Fri Jun 15 17:35:16 2007 +0200
+++ b/jabberbot/commands.py	Fri Jun 15 17:36:00 2007 +0200
@@ -64,4 +64,12 @@
     
     def __init__(self, jid, pagename):
         BaseDataCommand.__init__(self, jid)
-        self.pagename = pagename
\ No newline at end of file
+        self.pagename = pagename
+        
+class GetPageList(BaseDataCommand):
+    
+    description = u"get a list of accesible pages"
+    parameter_list = u""
+    
+    def __init__(self, jid):
+        BaseDataCommand.__init__(self, jid)
--- a/jabberbot/xmlrpcbot.py	Fri Jun 15 17:35:16 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Fri Jun 15 17:36:00 2007 +0200
@@ -55,6 +55,8 @@
             self.get_page(command)
         elif isinstance(command, cmd.GetPageHTML):
             self.get_page_html(command)
+        elif isinstance(command, cmd.GetPageList):
+            self.get_page_list(command)
     
     def get_auth_token(self, jid):
         token = self.connection.getAuthToken(jid, self.config.secret)
@@ -86,9 +88,62 @@
         
         del self.multicall
         del self.token
-    
+        
     def get_page_html(self, command):
-        pass
+        self.token = None
+        self.multicall = MultiCall(self.connection)
+        self.get_auth_token(command.jid)
+            
+        if not self.token:
+            # FIXME: notify the user that he may not have full rights on the wiki
+            self.multicall.getPageHTML(command.pagename)
+            getpage_result = self.multicall()
+        else:
+            self.multicall.applyAuthToken(self.token)
+            self.multicall.getPageHTML(command.pagename)
+            token_result, getpage_result = self.multicall()
+
+        # We get a dict only when Fault happens
+        if isinstance(getpage_result[0], dict):
+            error_str = u"""The page couldn't be retrieved. The reason is: "%s"."""
+            command.data = error_str % getpage_result[0]["faultString"]
+        else:
+            command.data = getpage_result[0]
+     
+        self.commands_out.put_nowait(command)
+        
+        del self.multicall
+        del self.token
+        
+    def get_page_list(self, command):
+        self.token = None
+        self.multicall = MultiCall(self.connection)
+        self.get_auth_token(command.jid)
+        
+        txt = u"""This command may take a while to complete, please be patient..."""
+        info = cmd.NotificationCommand(command.jid, txt)
+        self.commands_out.put_nowait(info)
+        
+        if not self.token:
+            # FIXME: notify the user that he may not have full rights on the wiki
+            self.multicall.getAllPages()
+            getpage_result = self.multicall()
+        else:
+            self.multicall.applyAuthToken(self.token)
+            self.multicall.getAllPages()
+            token_result, getpage_result = self.multicall()
+
+        # We get a dict only when Fault happens
+        if isinstance(getpage_result[0], dict):
+            error_str = u"""List couldn't be retrieved. The reason is: "%s"."""
+            command.data = error_str % getpage_result[0]["faultString"]
+        else:
+            command.data = getpage_result[0]
+     
+        self.commands_out.put_nowait(command)
+        
+        del self.multicall
+        del self.token
 
 
 class XMLRPCServer(Thread):
--- a/jabberbot/xmppbot.py	Fri Jun 15 17:35:16 2007 +0200
+++ b/jabberbot/xmppbot.py	Fri Jun 15 17:36:00 2007 +0200
@@ -120,7 +120,7 @@
         # A dictionary of contact objects, ordered by bare JID
         self.contacts = { }
 
-        self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML] 
+        self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML, cmd.GetPageList] 
         self.internal_commands = ["ping", "help"]
         
         self.xmlrpc_commands = {}
@@ -200,9 +200,14 @@
             jid = JID(node_or_jid=command.jid)
             self.remove_subscription(jid)
             
-        elif isinstance(command, cmd.GetPage):
+        elif isinstance(command, cmd.GetPage) or isinstance(command, cmd.GetPageHTML):
             msg = u"""Here's the page "%s" that you've requested:\n\n%s"""
             self.send_message(command.jid, msg % (command.pagename, command.data))
+        
+        elif isinstance(command, cmd.GetPageList):
+            msg = u"""That's the list of pages accesible to you:\n\n%s"""
+            list = "\n".join(command.data)
+            self.send_message(command.jid, msg % (list, ))
             
     def ask_for_subscription(self, jid):
         """Sends a <presence/> stanza with type="subscribe"