paul@130 | 1 | # -*- coding: iso-8859-1 -*- |
paul@130 | 2 | """ |
paul@130 | 3 | MoinMoin - Message relay processor |
paul@130 | 4 | |
paul@130 | 5 | @copyright: 2008, 2009, 2010, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@130 | 6 | 2005-2007 MoinMoin:AlexanderSchremmer |
paul@130 | 7 | 2006 MoinMoin:ThomasWaldmann |
paul@130 | 8 | |
paul@130 | 9 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@130 | 10 | """ |
paul@130 | 11 | |
paul@130 | 12 | from MoinMoin.Page import Page |
paul@130 | 13 | from MoinMoin.script import MoinScript, fatal, log |
paul@130 | 14 | |
paul@130 | 15 | from MoinMessage import GPG, timestamp, sendMessage, MoinMessageError, \ |
paul@130 | 16 | MoinMessageTransferError |
paul@130 | 17 | |
paul@130 | 18 | from MoinMessageSupport import get_local_homepage, get_signing_identity, \ |
paul@130 | 19 | get_relaying_user, get_user, get_homedir, \ |
paul@130 | 20 | get_recipient_details, \ |
paul@130 | 21 | MoinMessageRecipientError |
paul@130 | 22 | |
paul@130 | 23 | from ItemSupport import ItemStore |
paul@130 | 24 | |
paul@130 | 25 | from email.parser import Parser |
paul@130 | 26 | |
paul@130 | 27 | # The script's class. |
paul@130 | 28 | |
paul@130 | 29 | class PluginScript(MoinScript): |
paul@130 | 30 | |
paul@130 | 31 | """\ |
paul@130 | 32 | Purpose: |
paul@130 | 33 | ======== |
paul@130 | 34 | This tool processes messages in a message store and relays them to their |
paul@130 | 35 | recipients. |
paul@130 | 36 | |
paul@130 | 37 | Detailed Instructions: |
paul@130 | 38 | ====================== |
paul@130 | 39 | General syntax: moin [options] message relay [relay-options] |
paul@130 | 40 | |
paul@130 | 41 | [options] usually should be: |
paul@130 | 42 | --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/ |
paul@130 | 43 | |
paul@130 | 44 | [relay-options] see below: |
paul@130 | 45 | 0. To relay all messages stored in the relaying user's message store |
paul@130 | 46 | moin ... message relay |
paul@130 | 47 | |
paul@130 | 48 | 1. To show all message details |
paul@130 | 49 | moin ... message relay --show |
paul@130 | 50 | |
paul@130 | 51 | 2. To relay the first ten messages stored in the message store |
paul@130 | 52 | moin ... message relay --count=10 |
paul@130 | 53 | |
paul@130 | 54 | 3. To relay the ten messages after the first ten stored in the message store |
paul@130 | 55 | moin ... message relay --count=10 --skip=10 |
paul@130 | 56 | """ |
paul@130 | 57 | |
paul@130 | 58 | def __init__(self, argv, def_values): |
paul@130 | 59 | MoinScript.__init__(self, argv, def_values) |
paul@130 | 60 | self.parser.add_option( |
paul@130 | 61 | "--count", dest="count", default=None, |
paul@130 | 62 | help="Specify the number of messages to be relayed" |
paul@130 | 63 | ) |
paul@130 | 64 | self.parser.add_option( |
paul@130 | 65 | "--skip", dest="skip", default=None, |
paul@130 | 66 | help="Specify the number of messages to skip before selecting messages" |
paul@130 | 67 | ) |
paul@130 | 68 | self.parser.add_option( |
paul@130 | 69 | "--show", action="store_true", dest="show", default=False, |
paul@130 | 70 | help="Show details of messages instead of relaying them" |
paul@130 | 71 | ) |
paul@130 | 72 | |
paul@130 | 73 | def mainloop(self): |
paul@130 | 74 | self.init_request() |
paul@130 | 75 | count = self.options.count and int(self.options.count) |
paul@130 | 76 | skip = self.options.skip and int(self.options.skip) |
paul@130 | 77 | show = self.options.show |
paul@130 | 78 | self.relay_messages(count, skip, show) |
paul@130 | 79 | |
paul@130 | 80 | def relay_messages(self, count=None, skip=None, show=False): |
paul@130 | 81 | |
paul@130 | 82 | """ |
paul@130 | 83 | Read 'count' messages from the message store belonging to the relaying |
paul@130 | 84 | user and attempt to relay them to their recipients. Where 'skip' is |
paul@130 | 85 | specified, the given number of messages will be skipped from the start |
paul@130 | 86 | of the collection. If 'show' is set to a true value, the message details |
paul@130 | 87 | will be shown instead of the messages being relayed to recipients. |
paul@130 | 88 | """ |
paul@130 | 89 | |
paul@130 | 90 | request = self.request |
paul@130 | 91 | _ = request.getText |
paul@130 | 92 | |
paul@130 | 93 | relaying_user = get_relaying_user(request) |
paul@130 | 94 | user = relaying_user and get_user(request, relaying_user) |
paul@130 | 95 | |
paul@130 | 96 | if not user: |
paul@130 | 97 | fatal(_("No relaying user has been configured for this wiki.")) |
paul@130 | 98 | |
paul@130 | 99 | request.user = user |
paul@130 | 100 | |
paul@130 | 101 | # Initialise GPG. |
paul@130 | 102 | |
paul@130 | 103 | homedir = get_homedir(request) |
paul@130 | 104 | if not homedir: |
paul@130 | 105 | fatal(_("MoinMessage has not been set up: a GPG homedir is not defined.")) |
paul@130 | 106 | |
paul@130 | 107 | gpg = GPG(homedir) |
paul@130 | 108 | |
paul@130 | 109 | # Locate the store. |
paul@130 | 110 | |
paul@130 | 111 | relaying_page = relaying_user and get_local_homepage(request, relaying_user) |
paul@130 | 112 | page = Page(request, relaying_page) |
paul@130 | 113 | store = ItemStore(page, "messages", "message-locks") |
paul@130 | 114 | |
paul@130 | 115 | # Read the messages. |
paul@130 | 116 | |
paul@130 | 117 | keys = store.keys() |
paul@130 | 118 | keys.sort() |
paul@130 | 119 | keys = keys[skip or 0:(count is not None and (skip or 0) + count) or count] |
paul@130 | 120 | |
paul@130 | 121 | if show: |
paul@130 | 122 | print keys |
paul@130 | 123 | return |
paul@130 | 124 | |
paul@130 | 125 | for key in keys: |
paul@130 | 126 | message_text = store[key] |
paul@130 | 127 | message = Parser().parsestr(message_text) |
paul@130 | 128 | |
paul@130 | 129 | recipient = message["To"] |
paul@130 | 130 | |
paul@130 | 131 | # Get the recipient details. |
paul@130 | 132 | |
paul@130 | 133 | try: |
paul@130 | 134 | parameters = get_recipient_details(request, recipient) |
paul@130 | 135 | except MoinMessageRecipientError, exc: |
paul@130 | 136 | log(_("Recipient not valid: %s") % recipient) |
paul@130 | 137 | continue |
paul@130 | 138 | |
paul@130 | 139 | type = parameters["type"] |
paul@130 | 140 | location = parameters["location"] |
paul@130 | 141 | |
paul@130 | 142 | # Obtain the actual location if a relay is specified. |
paul@130 | 143 | |
paul@130 | 144 | if type == "relay": |
paul@130 | 145 | relays = get_relays(request) |
paul@130 | 146 | if not relays: |
paul@130 | 147 | log(_("No relays are defined for MoinMessage, but one is specified for the recipient.")) |
paul@130 | 148 | continue |
paul@130 | 149 | |
paul@130 | 150 | if not relays.has_key(location): |
paul@130 | 151 | log(_("The relay specified for the recipient is not defined.")) |
paul@130 | 152 | continue |
paul@130 | 153 | |
paul@130 | 154 | location = relays[location] |
paul@130 | 155 | |
paul@130 | 156 | # Sign and send the message. |
paul@130 | 157 | |
paul@130 | 158 | try: |
paul@130 | 159 | # Sign the message with the relaying user identity, if appropriate. |
paul@130 | 160 | |
paul@130 | 161 | signer = get_signing_identity(request, type) |
paul@130 | 162 | |
paul@130 | 163 | if signer: |
paul@130 | 164 | gpg.signMessage(message, signer) |
paul@130 | 165 | timestamp(message) |
paul@130 | 166 | |
paul@130 | 167 | sendMessage(message, location) |
paul@130 | 168 | |
paul@130 | 169 | except MoinMessageTransferError, exc: |
paul@130 | 170 | log(_("Message %d to %s could not be sent.") % (key, location)) |
paul@130 | 171 | log(str(exc)) |
paul@130 | 172 | log(exc.body) |
paul@130 | 173 | |
paul@130 | 174 | except MoinMessageError, exc: |
paul@130 | 175 | log(_("Message %d to %s could not be sent.") % (key, location)) |
paul@130 | 176 | log(str(exc)) |
paul@130 | 177 | |
paul@130 | 178 | else: |
paul@130 | 179 | log(_("Message %d to %s was successfully sent.") % (key, location)) |
paul@130 | 180 | del store[key] |
paul@130 | 181 | |
paul@130 | 182 | # vim: tabstop=4 expandtab shiftwidth=4 |