changeset 2074:088edb461832

Use the same logic to generate email and jabber notifications. Both are easily i18n-ised now.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 01 Jun 2007 01:43:38 +0200
parents afa784d8d58d
children d6ab26230e23
files MoinMoin/events/EmailNotification.py MoinMoin/events/JabberNotification.py MoinMoin/events/notification_common.py
diffstat 3 files changed, 125 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/events/EmailNotification.py	Fri Jun 01 01:42:38 2007 +0200
+++ b/MoinMoin/events/EmailNotification.py	Fri Jun 01 01:43:38 2007 +0200
@@ -13,54 +13,21 @@
 from MoinMoin.Page import Page
 from MoinMoin.mail import sendmail
 from MoinMoin.events import *
+from MoinMoin.events.notification_common import page_changed_notification
 
 def sendNotification(request, page, comment, emails, email_lang, revisions, trivial):
     """ Send notification email for a single language.
 
     @param comment: editor's comment given when saving the page
     @param emails: list of email addresses
-    @param email_lang: language of emails
+    @param email_lang: language of email
     @param revisions: revisions of this page (newest first!)
     @param trivial: the change is marked as trivial
     @rtype: int
     @return: sendmail result
     """
     _ = request.getText
-    page._ = lambda s, formatted=True, r=request, l=email_lang: r.getText(s, formatted=formatted, lang=l)
-
-    if len(revisions) >= 2:
-        querystr = {'action': 'diff',
-                    'rev2': str(revisions[0]),
-                    'rev1': str(revisions[1])}
-    else:
-        querystr = {}
-    pagelink = request.getQualifiedURL(page.url(request, querystr, relative=False))
-
-    mailBody = _("Dear Wiki user,\n\n"
-        'You have subscribed to a wiki page or wiki category on "%(sitename)s" for change notification.\n\n'
-        "The following page has been changed by %(editor)s:\n"
-        "%(pagelink)s\n\n", formatted=False) % {
-            'editor': page.uid_override or user.getUserIdentification(request),
-            'pagelink': pagelink,
-            'sitename': page.cfg.sitename or request.getBaseURL(),
-    }
-
-    if comment:
-        mailBody = mailBody + \
-            _("The comment on the change is:\n%(comment)s\n\n", formatted=False) % {'comment': comment}
-
-    # append a diff (or append full page text if there is no diff)
-    if len(revisions) < 2:
-        mailBody = mailBody + \
-            _("New page:\n", formatted=False) + \
-            page.get_raw_body()
-    else:
-        lines = wikiutil.pagediff(request, page.page_name, revisions[1],
-                                  page.page_name, revisions[0])
-        if lines:
-            mailBody = mailBody + "%s\n%s\n" % (("-" * 78), '\n'.join(lines))
-        else:
-            mailBody = mailBody + _("No differences found!\n", formatted=False)
+    mailBody = page_changed_notification(request, page, comment, email_lang, revisions, trivial)
 
     return sendmail.sendmail(request, emails,
         _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s', formatted=False) % {
@@ -82,6 +49,7 @@
     """
     _ = request.getText
     subscribers = page.getSubscribers(request, return_users=1, trivial=trivial)
+    
     if subscribers:
         # get a list of old revisions, and append a diff
         revisions = page.getRevList()
--- a/MoinMoin/events/JabberNotification.py	Fri Jun 01 01:42:38 2007 +0200
+++ b/MoinMoin/events/JabberNotification.py	Fri Jun 01 01:43:38 2007 +0200
@@ -1,6 +1,6 @@
 # -*- coding: iso-8859-1 -*-
 """
-    MoinMoin - jabber notification plugin from event system
+    MoinMoin - jabber notification plugin for event system
 
     This code sends notifications using a separate daemon.
 
@@ -10,28 +10,72 @@
 
 import xmlrpclib
 from MoinMoin.events import PageEvent, PageChangedEvent
+from MoinMoin.events.notification_common import page_changed_notification
 
+# XML RPC Server object used to communicate with notification bot
 server = None
 
 def handle(event):
     global server
+
+    request = event.request
+    trivial = event.trivial
+    comment = event.comment
+    page = event.page
+    cfg = request.cfg
     
-    if not isinstance(event, PageEvent) or event.request.cfg.bot_host is None:
+    _ = request.getText
+    
+    # Check for desired event type and if notification bot is configured
+    if not isinstance(event, PageEvent) or cfg.jabber_enabled is None:
         return
     
     # Create an XML RPC server object only if it doesn't exist
     if server is None:
-        server = xmlrpclib.Server("http://" + event.request.cfg.bot_host)
-    
-    msg = u"Page %(page_name)s has been modified!\n" % ( {'page_name': event.page.page_name} )
-    
-    if event.comment:
-        msg = msg + u"The comment is: %(comment)s\n" % ( {'comment': event.comment} )
+        server = xmlrpclib.Server("http://" + cfg.bot_host)
     
-    if event.trivial:
-        msg = msg + u"This change has been marked as TRIVIAL.\n"
+    subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)
+    
+    if subscribers:
+        # get a list of old revisions, and append a diff
+        revisions = page.getRevList()
         
-    try:
-        server.send_notification(u"grzyw@jabber.org", msg)
-    except Exception, desc:
-        print "XML RPC error:", desc
+        # send notifications to all subscribers
+        results = [_('Status of sending notifications:')]
+        for lang in subscribers:
+            jids = [u.jid for u in subscribers[lang]]
+            names = [u.name for u in subscribers[lang]]
+            jabberok, status = sendNotification(request, page, comment, jids, lang, revisions, trivial)
+            recipients = ", ".join(names)
+            results.append(_('[%(lang)s] %(recipients)s: %(status)s') % {
+                'lang': lang, 'recipients': recipients, 'status': status})
+
+        # Return notifications sent results. Ignore trivial - we don't have
+        # to lie. If notification was sent, just tell about it.
+        return '<p>\n%s\n</p> ' % '<br>'.join(results)
+
+    # No notifications sent, no message.
+    return ''
+
+
+def sendNotification(request, page, comment, jids, message_lang, revisions, trivial):
+    """ Send notifications for a single language.
+
+    @param comment: editor's comment given when saving the page
+    @param jids: list of Jabber IDs
+    @param message_lang: language of notification
+    @param revisions: revisions of this page (newest first!)
+    @param trivial: the change is marked as trivial
+    """
+    _ = request.getText
+    msg = page_changed_notification(request, page, comment, message_lang, revisions, trivial)
+    
+    for jid in jids:
+        # FIXME: for now, stop sending notifications on first error
+        try:
+            server.send_notification(jid, msg)
+        except Exception, desc:
+            print "XML RPC error:", desc
+            return (0, _("Notifications not sent"))
+        
+    return (1, _("Notifications sent OK"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/events/notification_common.py	Fri Jun 01 01:43:38 2007 +0200
@@ -0,0 +1,63 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - common functions for notification framework
+
+    Code for building messages informing about events (changes)
+    happening in the wiki.
+
+    @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin import user, wikiutil
+from MoinMoin.Page import Page
+
+def page_changed_notification(request, page, comment, lang, revisions, trivial):
+    """ Prepare a notification text for a single language
+
+    @param comment: editor's comment given when saving the page
+    @param lang: language of notifications
+    @param revisions: revisions of this page (newest first!)
+    @param trivial: the change is marked as trivial
+    @rtype: int
+    @return: composed string message
+    """
+    _ = request.getText
+    page._ = lambda s, formatted=True, r=request, l=lang: r.getText(s, formatted=formatted, lang=l)
+
+    if len(revisions) >= 2:
+        querystr = {'action': 'diff',
+                    'rev2': str(revisions[0]),
+                    'rev1': str(revisions[1])}
+    else:
+        querystr = {}
+        
+    pagelink = request.getQualifiedURL(page.url(request, querystr, relative=False))
+
+    messageBody = _("Dear Wiki user,\n\n"
+        'You have subscribed to a wiki page or wiki category on "%(sitename)s" for change notification.\n\n'
+        "The following page has been changed by %(editor)s:\n"
+        "%(pagelink)s\n\n", formatted=False) % {
+            'editor': page.uid_override or user.getUserIdentification(request),
+            'pagelink': pagelink,
+            'sitename': page.cfg.sitename or request.getBaseURL(),
+    }
+
+    if comment:
+        messageBody = messageBody + \
+            _("The comment on the change is:\n%(comment)s\n\n", formatted=False) % {'comment': comment}
+
+    # append a diff (or append full page text if there is no diff)
+    if len(revisions) < 2:
+        messageBody = messageBody + \
+            _("New page:\n", formatted=False) + \
+            page.get_raw_body()
+    else:
+        lines = wikiutil.pagediff(request, page.page_name, revisions[1],
+                                  page.page_name, revisions[0])
+        if lines:
+            messageBody = messageBody + "%s\n%s\n" % (("-" * 78), '\n'.join(lines))
+        else:
+            messageBody = messageBody + _("No differences found!\n", formatted=False)
+            
+    return messageBody