# HG changeset patch # User Paul Boddie # Date 1391296566 -3600 # Node ID 6ecdb56295fe47c2297c3ae559b2a4e3ae3747e1 # Parent 8e31bbf1fcf1d464a7a991db57abe12bc42c38a2 Added a message relaying script. diff -r 8e31bbf1fcf1 -r 6ecdb56295fe MoinMoin/script/message/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MoinMoin/script/message/__init__.py Sun Feb 02 00:16:06 2014 +0100 @@ -0,0 +1,14 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - MoinMessage Script Package + + @copyright: 2014 Paul Boddie + @copyright: 2006 MoinMoin:ThomasWaldmann + @license: GNU GPL, see COPYING for details. +""" + +from MoinMoin.util import pysupport + +# create a list of extension scripts from the subpackage directory +message_scripts = pysupport.getPackageModules(__file__) +modules = message_scripts diff -r 8e31bbf1fcf1 -r 6ecdb56295fe MoinMoin/script/message/relay.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MoinMoin/script/message/relay.py Sun Feb 02 00:16:06 2014 +0100 @@ -0,0 +1,182 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - Message relay processor + + @copyright: 2008, 2009, 2010, 2014 by Paul Boddie + 2005-2007 MoinMoin:AlexanderSchremmer + 2006 MoinMoin:ThomasWaldmann + + @license: GNU GPL (v2 or later), see COPYING.txt for details. +""" + +from MoinMoin.Page import Page +from MoinMoin.script import MoinScript, fatal, log + +from MoinMessage import GPG, timestamp, sendMessage, MoinMessageError, \ + MoinMessageTransferError + +from MoinMessageSupport import get_local_homepage, get_signing_identity, \ + get_relaying_user, get_user, get_homedir, \ + get_recipient_details, \ + MoinMessageRecipientError + +from ItemSupport import ItemStore + +from email.parser import Parser + +# The script's class. + +class PluginScript(MoinScript): + + """\ +Purpose: +======== +This tool processes messages in a message store and relays them to their +recipients. + +Detailed Instructions: +====================== +General syntax: moin [options] message relay [relay-options] + +[options] usually should be: + --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/ + +[relay-options] see below: + 0. To relay all messages stored in the relaying user's message store + moin ... message relay + + 1. To show all message details + moin ... message relay --show + + 2. To relay the first ten messages stored in the message store + moin ... message relay --count=10 + + 3. To relay the ten messages after the first ten stored in the message store + moin ... message relay --count=10 --skip=10 +""" + + def __init__(self, argv, def_values): + MoinScript.__init__(self, argv, def_values) + self.parser.add_option( + "--count", dest="count", default=None, + help="Specify the number of messages to be relayed" + ) + self.parser.add_option( + "--skip", dest="skip", default=None, + help="Specify the number of messages to skip before selecting messages" + ) + self.parser.add_option( + "--show", action="store_true", dest="show", default=False, + help="Show details of messages instead of relaying them" + ) + + def mainloop(self): + self.init_request() + count = self.options.count and int(self.options.count) + skip = self.options.skip and int(self.options.skip) + show = self.options.show + self.relay_messages(count, skip, show) + + def relay_messages(self, count=None, skip=None, show=False): + + """ + Read 'count' messages from the message store belonging to the relaying + user and attempt to relay them to their recipients. Where 'skip' is + specified, the given number of messages will be skipped from the start + of the collection. If 'show' is set to a true value, the message details + will be shown instead of the messages being relayed to recipients. + """ + + request = self.request + _ = request.getText + + relaying_user = get_relaying_user(request) + user = relaying_user and get_user(request, relaying_user) + + if not user: + fatal(_("No relaying user has been configured for this wiki.")) + + request.user = user + + # Initialise GPG. + + homedir = get_homedir(request) + if not homedir: + fatal(_("MoinMessage has not been set up: a GPG homedir is not defined.")) + + gpg = GPG(homedir) + + # Locate the store. + + relaying_page = relaying_user and get_local_homepage(request, relaying_user) + page = Page(request, relaying_page) + store = ItemStore(page, "messages", "message-locks") + + # Read the messages. + + keys = store.keys() + keys.sort() + keys = keys[skip or 0:(count is not None and (skip or 0) + count) or count] + + if show: + print keys + return + + for key in keys: + message_text = store[key] + message = Parser().parsestr(message_text) + + recipient = message["To"] + + # Get the recipient details. + + try: + parameters = get_recipient_details(request, recipient) + except MoinMessageRecipientError, exc: + log(_("Recipient not valid: %s") % recipient) + continue + + type = parameters["type"] + location = parameters["location"] + + # Obtain the actual location if a relay is specified. + + if type == "relay": + relays = get_relays(request) + if not relays: + log(_("No relays are defined for MoinMessage, but one is specified for the recipient.")) + continue + + if not relays.has_key(location): + log(_("The relay specified for the recipient is not defined.")) + continue + + location = relays[location] + + # Sign and send the message. + + try: + # Sign the message with the relaying user identity, if appropriate. + + signer = get_signing_identity(request, type) + + if signer: + gpg.signMessage(message, signer) + timestamp(message) + + sendMessage(message, location) + + except MoinMessageTransferError, exc: + log(_("Message %d to %s could not be sent.") % (key, location)) + log(str(exc)) + log(exc.body) + + except MoinMessageError, exc: + log(_("Message %d to %s could not be sent.") % (key, location)) + log(str(exc)) + + else: + log(_("Message %d to %s was successfully sent.") % (key, location)) + del store[key] + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8e31bbf1fcf1 -r 6ecdb56295fe setup.py --- a/setup.py Sun Feb 02 00:15:45 2014 +0100 +++ b/setup.py Sun Feb 02 00:16:06 2014 +0100 @@ -9,5 +9,6 @@ author_email = "paul@boddie.org.uk", url = "http://moinmo.in/ActionMarket/MoinMessage", version = "0.1", - py_modules = ["MoinMessage", "MoinMessageSupport", "MoinMoin.auth.pgp"] + py_modules = ["MoinMessage", "MoinMessageSupport", "MoinMoin.auth.pgp", + "MoinMoin.script.message.relay"] )