1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinMoin/script/message/__init__.py Sun Feb 02 00:16:06 2014 +0100
1.3 @@ -0,0 +1,14 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinMessage Script Package
1.7 +
1.8 + @copyright: 2014 Paul Boddie <paul@boddie.org.uk>
1.9 + @copyright: 2006 MoinMoin:ThomasWaldmann
1.10 + @license: GNU GPL, see COPYING for details.
1.11 +"""
1.12 +
1.13 +from MoinMoin.util import pysupport
1.14 +
1.15 +# create a list of extension scripts from the subpackage directory
1.16 +message_scripts = pysupport.getPackageModules(__file__)
1.17 +modules = message_scripts
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/MoinMoin/script/message/relay.py Sun Feb 02 00:16:06 2014 +0100
2.3 @@ -0,0 +1,182 @@
2.4 +# -*- coding: iso-8859-1 -*-
2.5 +"""
2.6 + MoinMoin - Message relay processor
2.7 +
2.8 + @copyright: 2008, 2009, 2010, 2014 by Paul Boddie <paul@boddie.org.uk>
2.9 + 2005-2007 MoinMoin:AlexanderSchremmer
2.10 + 2006 MoinMoin:ThomasWaldmann
2.11 +
2.12 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.13 +"""
2.14 +
2.15 +from MoinMoin.Page import Page
2.16 +from MoinMoin.script import MoinScript, fatal, log
2.17 +
2.18 +from MoinMessage import GPG, timestamp, sendMessage, MoinMessageError, \
2.19 + MoinMessageTransferError
2.20 +
2.21 +from MoinMessageSupport import get_local_homepage, get_signing_identity, \
2.22 + get_relaying_user, get_user, get_homedir, \
2.23 + get_recipient_details, \
2.24 + MoinMessageRecipientError
2.25 +
2.26 +from ItemSupport import ItemStore
2.27 +
2.28 +from email.parser import Parser
2.29 +
2.30 +# The script's class.
2.31 +
2.32 +class PluginScript(MoinScript):
2.33 +
2.34 + """\
2.35 +Purpose:
2.36 +========
2.37 +This tool processes messages in a message store and relays them to their
2.38 +recipients.
2.39 +
2.40 +Detailed Instructions:
2.41 +======================
2.42 +General syntax: moin [options] message relay [relay-options]
2.43 +
2.44 +[options] usually should be:
2.45 + --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
2.46 +
2.47 +[relay-options] see below:
2.48 + 0. To relay all messages stored in the relaying user's message store
2.49 + moin ... message relay
2.50 +
2.51 + 1. To show all message details
2.52 + moin ... message relay --show
2.53 +
2.54 + 2. To relay the first ten messages stored in the message store
2.55 + moin ... message relay --count=10
2.56 +
2.57 + 3. To relay the ten messages after the first ten stored in the message store
2.58 + moin ... message relay --count=10 --skip=10
2.59 +"""
2.60 +
2.61 + def __init__(self, argv, def_values):
2.62 + MoinScript.__init__(self, argv, def_values)
2.63 + self.parser.add_option(
2.64 + "--count", dest="count", default=None,
2.65 + help="Specify the number of messages to be relayed"
2.66 + )
2.67 + self.parser.add_option(
2.68 + "--skip", dest="skip", default=None,
2.69 + help="Specify the number of messages to skip before selecting messages"
2.70 + )
2.71 + self.parser.add_option(
2.72 + "--show", action="store_true", dest="show", default=False,
2.73 + help="Show details of messages instead of relaying them"
2.74 + )
2.75 +
2.76 + def mainloop(self):
2.77 + self.init_request()
2.78 + count = self.options.count and int(self.options.count)
2.79 + skip = self.options.skip and int(self.options.skip)
2.80 + show = self.options.show
2.81 + self.relay_messages(count, skip, show)
2.82 +
2.83 + def relay_messages(self, count=None, skip=None, show=False):
2.84 +
2.85 + """
2.86 + Read 'count' messages from the message store belonging to the relaying
2.87 + user and attempt to relay them to their recipients. Where 'skip' is
2.88 + specified, the given number of messages will be skipped from the start
2.89 + of the collection. If 'show' is set to a true value, the message details
2.90 + will be shown instead of the messages being relayed to recipients.
2.91 + """
2.92 +
2.93 + request = self.request
2.94 + _ = request.getText
2.95 +
2.96 + relaying_user = get_relaying_user(request)
2.97 + user = relaying_user and get_user(request, relaying_user)
2.98 +
2.99 + if not user:
2.100 + fatal(_("No relaying user has been configured for this wiki."))
2.101 +
2.102 + request.user = user
2.103 +
2.104 + # Initialise GPG.
2.105 +
2.106 + homedir = get_homedir(request)
2.107 + if not homedir:
2.108 + fatal(_("MoinMessage has not been set up: a GPG homedir is not defined."))
2.109 +
2.110 + gpg = GPG(homedir)
2.111 +
2.112 + # Locate the store.
2.113 +
2.114 + relaying_page = relaying_user and get_local_homepage(request, relaying_user)
2.115 + page = Page(request, relaying_page)
2.116 + store = ItemStore(page, "messages", "message-locks")
2.117 +
2.118 + # Read the messages.
2.119 +
2.120 + keys = store.keys()
2.121 + keys.sort()
2.122 + keys = keys[skip or 0:(count is not None and (skip or 0) + count) or count]
2.123 +
2.124 + if show:
2.125 + print keys
2.126 + return
2.127 +
2.128 + for key in keys:
2.129 + message_text = store[key]
2.130 + message = Parser().parsestr(message_text)
2.131 +
2.132 + recipient = message["To"]
2.133 +
2.134 + # Get the recipient details.
2.135 +
2.136 + try:
2.137 + parameters = get_recipient_details(request, recipient)
2.138 + except MoinMessageRecipientError, exc:
2.139 + log(_("Recipient not valid: %s") % recipient)
2.140 + continue
2.141 +
2.142 + type = parameters["type"]
2.143 + location = parameters["location"]
2.144 +
2.145 + # Obtain the actual location if a relay is specified.
2.146 +
2.147 + if type == "relay":
2.148 + relays = get_relays(request)
2.149 + if not relays:
2.150 + log(_("No relays are defined for MoinMessage, but one is specified for the recipient."))
2.151 + continue
2.152 +
2.153 + if not relays.has_key(location):
2.154 + log(_("The relay specified for the recipient is not defined."))
2.155 + continue
2.156 +
2.157 + location = relays[location]
2.158 +
2.159 + # Sign and send the message.
2.160 +
2.161 + try:
2.162 + # Sign the message with the relaying user identity, if appropriate.
2.163 +
2.164 + signer = get_signing_identity(request, type)
2.165 +
2.166 + if signer:
2.167 + gpg.signMessage(message, signer)
2.168 + timestamp(message)
2.169 +
2.170 + sendMessage(message, location)
2.171 +
2.172 + except MoinMessageTransferError, exc:
2.173 + log(_("Message %d to %s could not be sent.") % (key, location))
2.174 + log(str(exc))
2.175 + log(exc.body)
2.176 +
2.177 + except MoinMessageError, exc:
2.178 + log(_("Message %d to %s could not be sent.") % (key, location))
2.179 + log(str(exc))
2.180 +
2.181 + else:
2.182 + log(_("Message %d to %s was successfully sent.") % (key, location))
2.183 + del store[key]
2.184 +
2.185 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/setup.py Sun Feb 02 00:15:45 2014 +0100
3.2 +++ b/setup.py Sun Feb 02 00:16:06 2014 +0100
3.3 @@ -9,5 +9,6 @@
3.4 author_email = "paul@boddie.org.uk",
3.5 url = "http://moinmo.in/ActionMarket/MoinMessage",
3.6 version = "0.1",
3.7 - py_modules = ["MoinMessage", "MoinMessageSupport", "MoinMoin.auth.pgp"]
3.8 + py_modules = ["MoinMessage", "MoinMessageSupport", "MoinMoin.auth.pgp",
3.9 + "MoinMoin.script.message.relay"]
3.10 )