Back to index

moin  1.9.0~rc2
commands.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - inter-thread communication commands
00004 
00005     This file defines command objects used by notification
00006     bot's threads to communicate among each other.
00007 
00008     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 
00012 from pyxmpp.jid import JID
00013 
00014 # First, XML RPC -> XMPP commands
00015 class NotificationCommand:
00016     """Class representing a notification request"""
00017     def __init__(self, jids, notification, msg_type=u"normal", async=True):
00018         """A constructor
00019 
00020         @param jids: a list of jids to sent this message to
00021         @param notification: dictionary with notification data
00022         @param async: async notifications get queued if contact is DnD
00023         @type jids: list
00024 
00025         """
00026         if type(jids) != list:
00027             raise Exception("jids argument must be a list!")
00028 
00029         self.notification = notification
00030         self.jids = jids
00031         self.async = async
00032         self.msg_type = msg_type
00033 
00034 class NotificationCommandI18n(NotificationCommand):
00035     """Notification request that should be translated by the XMPP bot"""
00036     def __init__(self, jids, notification, msg_type="normal", async=True):
00037         """A constructor
00038 
00039         Params as in NotificationCommand.
00040 
00041         """
00042         NotificationCommand.__init__(self, jids, notification, msg_type, async)
00043 
00044     def translate(self, gettext_func):
00045         """Translate the message using a provided gettext function
00046 
00047         @param gettext_func: a unary gettext function
00048         @return: translated message and subject
00049         @rtype: tuple
00050         """
00051         if self.notification.has_key('data'):
00052             msg =  gettext_func(self.notification['text']) % self.notification['data']
00053         else:
00054             msg = gettext_func(self.notification['text'])
00055 
00056         return (msg, gettext_func(self.notification.get('subject', '')))
00057 
00058 class AddJIDToRosterCommand:
00059     """Class representing a request to add a new jid to roster"""
00060     def __init__(self, jid):
00061         self.jid = jid
00062 
00063 class RemoveJIDFromRosterCommand:
00064     """Class representing a request to remove a jid from roster"""
00065     def __init__(self, jid):
00066         self.jid = jid
00067 
00068 # XMPP <-> XML RPC commands
00069 # These commands are passed in both directions, with added data
00070 # payload when they return to the XMPP code. Naming convention
00071 # follows method names defined by the Wiki RPC Interface v2.
00072 
00073 class BaseDataCommand(object):
00074     """Base class for all commands used by the XMPP component.
00075 
00076     It has to support an optional data payload and store JID the
00077     request has come from and provide a help string for its parameters.
00078     """
00079 
00080     # Description of what the command does
00081     description = u""
00082 
00083     # Parameter list in a human-readable format
00084     parameter_list = u""
00085 
00086     def __init__(self, jid, presentation=u"text"):
00087         """A constructor
00088 
00089         @param jid: Jabber ID to send the reply to
00090         @param presentation: how to display results; "text" or "dataforms"
00091         @type jid: unicode
00092         @type presentation: unicode
00093 
00094         """
00095         self.jid = jid
00096         self.data = None
00097         self.presentation = presentation
00098 
00099 class GetPage(BaseDataCommand):
00100 
00101     description = u"retrieve raw content of a named page"
00102     parameter_list = u"pagename"
00103 
00104     def __init__(self, jid, pagename):
00105         BaseDataCommand.__init__(self, jid)
00106         self.pagename = pagename
00107 
00108 class GetPageHTML(BaseDataCommand):
00109 
00110     description = u"retrieve HTML-formatted content of a named page"
00111     parameter_list = u"pagename"
00112 
00113     def __init__(self, jid, pagename):
00114         BaseDataCommand.__init__(self, jid)
00115         self.pagename = pagename
00116 
00117 class GetPageList(BaseDataCommand):
00118 
00119     description = u"get a list of accesible pages"
00120     parameter_list = u""
00121 
00122     def __init__(self, jid):
00123         BaseDataCommand.__init__(self, jid)
00124 
00125 class GetPageInfo(BaseDataCommand):
00126 
00127     description = u"show detailed information about a page"
00128     parameter_list = u"pagename"
00129 
00130     def __init__(self, jid, pagename, presentation=u"text"):
00131         BaseDataCommand.__init__(self, jid, presentation)
00132         self.pagename = pagename
00133 
00134 class Search(BaseDataCommand):
00135 
00136     description = u"perform a wiki search"
00137     parameter_list = u"{title|text} term"
00138 
00139     def __init__(self, jid, search_type, *args, **kwargs):
00140         BaseDataCommand.__init__(self, jid)
00141 
00142         if not JID(jid).resource:
00143             raise ValueError("The jid argument must be a full jabber id!")
00144 
00145         self.term = ' '.join(args)
00146         self.search_type = search_type
00147         self.presentation = kwargs.get('presentation', 'text') # "text" or "dataforms"
00148         self.case = kwargs.get('case', False)
00149         self.mtime = None
00150         self.regexp = kwargs.get('regexp', False)
00151 
00152 
00153 class RevertPage(BaseDataCommand):
00154 
00155     description = u"revert a page to previous revision"
00156     parameter_list = u"page_name revision"
00157 
00158     def __init__(self, jid, pagename, revision):
00159         BaseDataCommand.__init__(self, jid)
00160         self.pagename = pagename
00161         self.revision = revision
00162 
00163 
00164 class GetUserLanguage:
00165     """Request user's language information from wiki"""
00166 
00167     def __init__(self, jid):
00168         """
00169         @param jid: user's (bare) Jabber ID
00170         """
00171         self.jid = jid
00172         self.language = None
00173