Back to index

moin  1.9.0~rc2
emailnotify.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - email notification plugin from event system
00004 
00005     This code sends email notifications about page changes.
00006     TODO: refactor it to handle separate events for page changes, creations, etc
00007 
00008     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 
00012 from MoinMoin import user
00013 from MoinMoin.Page import Page
00014 from MoinMoin.mail import sendmail
00015 from MoinMoin.support.python_compatibility import set
00016 from MoinMoin.user import User, getUserList
00017 from MoinMoin.action.AttachFile import getAttachUrl
00018 
00019 import MoinMoin.events as ev
00020 import MoinMoin.events.notification as notification
00021 
00022 
00023 def prep_page_changed_mail(request, page, comment, email_lang, revisions, trivial):
00024     """ Prepare information required for email notification about page change
00025 
00026     @param page: the modified page instance
00027     @param comment: editor's comment given when saving the page
00028     @param email_lang: language of email
00029     @param revisions: revisions of this page (newest first!)
00030     @param trivial: the change is marked as trivial
00031     @return: dict with email title and body
00032     @rtype: dict
00033 
00034     """
00035     change = notification.page_change_message("page_changed", request, page, email_lang, comment=comment, revisions=revisions)
00036     _ = lambda text: request.getText(text, lang=email_lang)
00037 
00038     if len(revisions) >= 2:
00039         querystr = {'action': 'diff',
00040                     'rev2': str(revisions[0]),
00041                     'rev1': str(revisions[1])}
00042     else:
00043         querystr = {}
00044 
00045     pagelink = "%(link)s\n\n" % {'link': notification.page_link(request, page, querystr)}
00046 
00047     subject = _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s') % {
00048             'trivial': (trivial and _("Trivial ")) or "",
00049             'sitename': page.cfg.sitename or "Wiki",
00050             'pagename': page.page_name,
00051             'username': page.uid_override or user.getUserIdentification(request),
00052         }
00053 
00054     if change.has_key('comment'):
00055         comment = _("Comment:") + "\n" + change['comment'] + "\n\n"
00056     else:
00057         comment = ''
00058 
00059     return {'subject': subject, 'text': change['text'] + pagelink + comment + change['diff']}
00060 
00061 
00062 def send_notification(request, from_address, emails, data):
00063     """ Send notification email
00064 
00065     @param emails: list of email addresses
00066     @return: sendmail result
00067     @rtype int
00068 
00069     """
00070     return sendmail.sendmail(request, emails, data['subject'], data['text'], mail_from=from_address)
00071 
00072 
00073 def handle_page_change(event):
00074     """ Send email to all subscribers of given page.
00075 
00076     @param event: event to notify about
00077     @rtype: string
00078     @return: message, indicating success or errors.
00079 
00080     """
00081     comment = event.comment
00082     page = event.page
00083     request = event.request
00084     trivial = isinstance(event, ev.TrivialPageChangedEvent)
00085     subscribers = page.getSubscribers(request, return_users=1)
00086     mail_from = page.cfg.mail_from
00087 
00088     if subscribers:
00089         recipients = set()
00090 
00091         # get a list of old revisions, and append a diff
00092         revisions = page.getRevList()
00093 
00094         # send email to all subscribers
00095         for lang in subscribers:
00096             users = [u for u in subscribers[lang]
00097                      if event.name in u.email_subscribed_events]
00098             emails = [u.email for u in users]
00099             names = [u.name for u in users]
00100             data = prep_page_changed_mail(request, page, comment, lang, revisions, trivial)
00101 
00102             if send_notification(request, mail_from, emails, data):
00103                 recipients.update(names)
00104 
00105         if recipients:
00106             return notification.Success(recipients)
00107 
00108 
00109 def handle_user_created(event):
00110     """Sends an email to super users that have subscribed to this event type"""
00111 
00112     request = event.request
00113     sitename = request.cfg.sitename
00114     from_address = request.cfg.mail_from
00115     event_name = event.name
00116     email = event.user.email or u"NOT SET"
00117     username = event.user.name
00118 
00119     user_ids = getUserList(request)
00120     for usr_id in user_ids:
00121         usr = User(request, id=usr_id)
00122         # Currently send this only to super users
00123         if usr.isSuperUser() and event_name in usr.email_subscribed_events:
00124             _ = lambda text: request.getText(text, lang=usr.language or 'en')
00125             data = notification.user_created_message(request, _, sitename, username, email)
00126             send_notification(request, from_address, [usr.email], data)
00127 
00128 
00129 def handle_file_attached(event):
00130     """Sends an email to users that have subscribed to this event type"""
00131 
00132     names = set()
00133     from_address = event.request.cfg.mail_from
00134     request = event.request
00135     page = Page(request, event.pagename)
00136 
00137     subscribers = page.getSubscribers(request, return_users=1)
00138     notification.filter_subscriber_list(event, subscribers, False)
00139     recipients = []
00140 
00141     for lang in subscribers:
00142         recipients.extend(subscribers[lang])
00143 
00144     attachlink = request.getQualifiedURL(getAttachUrl(event.pagename, event.filename, request))
00145     pagelink = request.getQualifiedURL(page.url(request, {}))
00146 
00147     for lang in subscribers:
00148         emails = []
00149         _ = lambda text: request.getText(text, lang=lang)
00150 
00151         links = _("Attachment link: %(attach)s\n" \
00152                   "Page link: %(page)s\n") % {'attach': attachlink, 'page': pagelink}
00153 
00154         data = notification.attachment_added(request, _, event.pagename, event.filename, event.size)
00155         data['text'] = data['text'] + links
00156 
00157         emails = [usr.email for usr in subscribers[lang]]
00158 
00159         if send_notification(request, from_address, emails, data):
00160             names.update(recipients)
00161 
00162     return notification.Success(names)
00163 
00164 
00165 def handle_file_removed(event):
00166     """Sends an email to users that have subscribed to this event type"""
00167 
00168     names = set()
00169     from_address = event.request.cfg.mail_from
00170     request = event.request
00171     page = Page(request, event.pagename)
00172 
00173     subscribers = page.getSubscribers(request, return_users=1)
00174     notification.filter_subscriber_list(event, subscribers, False)
00175     recipients = []
00176 
00177     for lang in subscribers:
00178         recipients.extend(subscribers[lang])
00179 
00180     attachlink = request.getQualifiedURL(getAttachUrl(event.pagename, event.filename, request))
00181     pagelink = request.getQualifiedURL(page.url(request, {}))
00182 
00183     for lang in subscribers:
00184         emails = []
00185         _ = lambda text: request.getText(text, lang=lang)
00186 
00187         links = _("Attachment link: %(attach)s\n" \
00188                   "Page link: %(page)s\n") % {'attach': attachlink, 'page': pagelink}
00189 
00190         data = notification.attachment_removed(request, _, event.pagename, event.filename, event.size)
00191         data['text'] = data['text'] + links
00192 
00193         emails = [usr.email for usr in subscribers[lang]]
00194 
00195         if send_notification(request, from_address, emails, data):
00196             names.update(recipients)
00197 
00198     return notification.Success(names)
00199 
00200 
00201 def handle(event):
00202     """An event handler"""
00203 
00204     if not event.request.cfg.mail_enabled:
00205         return
00206 
00207     if isinstance(event, (ev.PageChangedEvent, ev.TrivialPageChangedEvent)):
00208         return handle_page_change(event)
00209     elif isinstance(event, ev.UserCreatedEvent):
00210         return handle_user_created(event)
00211     elif isinstance(event, ev.FileAttachedEvent):
00212         return handle_file_attached(event)
00213     elif isinstance(event, ev.FileRemovedEvent):
00214         return handle_file_removed(event)
00215