1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinMoin/script/message/relay.py Sun Feb 02 00:16:06 2014 +0100
1.3 @@ -0,0 +1,182 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - Message relay processor
1.7 +
1.8 + @copyright: 2008, 2009, 2010, 2014 by Paul Boddie <paul@boddie.org.uk>
1.9 + 2005-2007 MoinMoin:AlexanderSchremmer
1.10 + 2006 MoinMoin:ThomasWaldmann
1.11 +
1.12 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.13 +"""
1.14 +
1.15 +from MoinMoin.Page import Page
1.16 +from MoinMoin.script import MoinScript, fatal, log
1.17 +
1.18 +from MoinMessage import GPG, timestamp, sendMessage, MoinMessageError, \
1.19 + MoinMessageTransferError
1.20 +
1.21 +from MoinMessageSupport import get_local_homepage, get_signing_identity, \
1.22 + get_relaying_user, get_user, get_homedir, \
1.23 + get_recipient_details, \
1.24 + MoinMessageRecipientError
1.25 +
1.26 +from ItemSupport import ItemStore
1.27 +
1.28 +from email.parser import Parser
1.29 +
1.30 +# The script's class.
1.31 +
1.32 +class PluginScript(MoinScript):
1.33 +
1.34 + """\
1.35 +Purpose:
1.36 +========
1.37 +This tool processes messages in a message store and relays them to their
1.38 +recipients.
1.39 +
1.40 +Detailed Instructions:
1.41 +======================
1.42 +General syntax: moin [options] message relay [relay-options]
1.43 +
1.44 +[options] usually should be:
1.45 + --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
1.46 +
1.47 +[relay-options] see below:
1.48 + 0. To relay all messages stored in the relaying user's message store
1.49 + moin ... message relay
1.50 +
1.51 + 1. To show all message details
1.52 + moin ... message relay --show
1.53 +
1.54 + 2. To relay the first ten messages stored in the message store
1.55 + moin ... message relay --count=10
1.56 +
1.57 + 3. To relay the ten messages after the first ten stored in the message store
1.58 + moin ... message relay --count=10 --skip=10
1.59 +"""
1.60 +
1.61 + def __init__(self, argv, def_values):
1.62 + MoinScript.__init__(self, argv, def_values)
1.63 + self.parser.add_option(
1.64 + "--count", dest="count", default=None,
1.65 + help="Specify the number of messages to be relayed"
1.66 + )
1.67 + self.parser.add_option(
1.68 + "--skip", dest="skip", default=None,
1.69 + help="Specify the number of messages to skip before selecting messages"
1.70 + )
1.71 + self.parser.add_option(
1.72 + "--show", action="store_true", dest="show", default=False,
1.73 + help="Show details of messages instead of relaying them"
1.74 + )
1.75 +
1.76 + def mainloop(self):
1.77 + self.init_request()
1.78 + count = self.options.count and int(self.options.count)
1.79 + skip = self.options.skip and int(self.options.skip)
1.80 + show = self.options.show
1.81 + self.relay_messages(count, skip, show)
1.82 +
1.83 + def relay_messages(self, count=None, skip=None, show=False):
1.84 +
1.85 + """
1.86 + Read 'count' messages from the message store belonging to the relaying
1.87 + user and attempt to relay them to their recipients. Where 'skip' is
1.88 + specified, the given number of messages will be skipped from the start
1.89 + of the collection. If 'show' is set to a true value, the message details
1.90 + will be shown instead of the messages being relayed to recipients.
1.91 + """
1.92 +
1.93 + request = self.request
1.94 + _ = request.getText
1.95 +
1.96 + relaying_user = get_relaying_user(request)
1.97 + user = relaying_user and get_user(request, relaying_user)
1.98 +
1.99 + if not user:
1.100 + fatal(_("No relaying user has been configured for this wiki."))
1.101 +
1.102 + request.user = user
1.103 +
1.104 + # Initialise GPG.
1.105 +
1.106 + homedir = get_homedir(request)
1.107 + if not homedir:
1.108 + fatal(_("MoinMessage has not been set up: a GPG homedir is not defined."))
1.109 +
1.110 + gpg = GPG(homedir)
1.111 +
1.112 + # Locate the store.
1.113 +
1.114 + relaying_page = relaying_user and get_local_homepage(request, relaying_user)
1.115 + page = Page(request, relaying_page)
1.116 + store = ItemStore(page, "messages", "message-locks")
1.117 +
1.118 + # Read the messages.
1.119 +
1.120 + keys = store.keys()
1.121 + keys.sort()
1.122 + keys = keys[skip or 0:(count is not None and (skip or 0) + count) or count]
1.123 +
1.124 + if show:
1.125 + print keys
1.126 + return
1.127 +
1.128 + for key in keys:
1.129 + message_text = store[key]
1.130 + message = Parser().parsestr(message_text)
1.131 +
1.132 + recipient = message["To"]
1.133 +
1.134 + # Get the recipient details.
1.135 +
1.136 + try:
1.137 + parameters = get_recipient_details(request, recipient)
1.138 + except MoinMessageRecipientError, exc:
1.139 + log(_("Recipient not valid: %s") % recipient)
1.140 + continue
1.141 +
1.142 + type = parameters["type"]
1.143 + location = parameters["location"]
1.144 +
1.145 + # Obtain the actual location if a relay is specified.
1.146 +
1.147 + if type == "relay":
1.148 + relays = get_relays(request)
1.149 + if not relays:
1.150 + log(_("No relays are defined for MoinMessage, but one is specified for the recipient."))
1.151 + continue
1.152 +
1.153 + if not relays.has_key(location):
1.154 + log(_("The relay specified for the recipient is not defined."))
1.155 + continue
1.156 +
1.157 + location = relays[location]
1.158 +
1.159 + # Sign and send the message.
1.160 +
1.161 + try:
1.162 + # Sign the message with the relaying user identity, if appropriate.
1.163 +
1.164 + signer = get_signing_identity(request, type)
1.165 +
1.166 + if signer:
1.167 + gpg.signMessage(message, signer)
1.168 + timestamp(message)
1.169 +
1.170 + sendMessage(message, location)
1.171 +
1.172 + except MoinMessageTransferError, exc:
1.173 + log(_("Message %d to %s could not be sent.") % (key, location))
1.174 + log(str(exc))
1.175 + log(exc.body)
1.176 +
1.177 + except MoinMessageError, exc:
1.178 + log(_("Message %d to %s could not be sent.") % (key, location))
1.179 + log(str(exc))
1.180 +
1.181 + else:
1.182 + log(_("Message %d to %s was successfully sent.") % (key, location))
1.183 + del store[key]
1.184 +
1.185 +# vim: tabstop=4 expandtab shiftwidth=4