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