1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - SendMessage Action 4 5 @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import ActionBase 10 from MoinMoin.log import getLogger 11 from MoinMessage import GPG, MoinMessageError, Message, sendMessage 12 from MoinSupport import * 13 from MoinMoin.wikiutil import escape 14 from email.mime.text import MIMEText 15 16 Dependencies = [] 17 18 class SendMessage(ActionBase, ActionSupport): 19 20 "An action that can send a message to another site." 21 22 def get_form_html(self, buttons_html): 23 24 "Present an interface for message sending." 25 26 _ = self._ 27 request = self.request 28 form = self.get_form() 29 30 message = form.get("message", [""])[0] 31 recipient = form.get("recipient", [""])[0] 32 33 # Get a list of potential recipients. 34 35 recipients = self.get_recipients() 36 37 # Prepare the recipients list, selecting the specified recipients. 38 39 recipients_list = [] 40 41 if recipients: 42 recipients_list += self.get_option_list(recipient, recipients) or [] 43 44 recipients_list.sort() 45 46 # Fill in the fields and labels. 47 48 d = { 49 "buttons_html" : buttons_html, 50 "recipient_label" : _("Recipient"), 51 "recipients_list" : "\n".join(recipients_list), 52 "message_label" : _("Message text"), 53 "message_default" : escattr(message), 54 } 55 56 # Prepare the output HTML. 57 58 html = ''' 59 <table> 60 <tr> 61 <td class="label"><label>%(recipient_label)s</label></td> 62 <td> 63 <select name="recipient"> 64 %(recipients_list)s 65 </select> 66 </td> 67 </tr> 68 <tr> 69 <td class="label"><label>%(message_label)s</label></td> 70 <td colspan="2"> 71 <input name="message" type="text" size="40" value="%(message_default)s" /> 72 </td> 73 </tr> 74 <tr> 75 <td></td> 76 <td colspan="2" class="buttons"> 77 %(buttons_html)s 78 </td> 79 </tr> 80 </table>''' % d 81 82 return html 83 84 def do_action(self): 85 86 "Attempt to send the message." 87 88 _ = self._ 89 request = self.request 90 form = self.get_form() 91 92 text = form.get("message", [None])[0] 93 recipient = form.get("recipient", [None])[0] 94 95 if not text: 96 return 0, _("A message must be given.") 97 98 if not recipient: 99 return 0, _("A recipient must be given.") 100 101 homedir = self.get_homedir() 102 if not homedir: 103 return 0, _("MoinMessage has not been set up: a GPG homedir is not defined.") 104 105 gpg = GPG(homedir) 106 107 # Construct a message from the request. 108 109 message = Message() 110 message.add_update(MIMEText(text, "moin")) 111 112 # Get the sender details for signing messages. 113 # This is not the same as the details for authenticating users in the 114 # PostMessage action since the fingerprints refer to public keys. 115 116 signing_users = getWikiDict(getattr(request.cfg, "moinmessage_gpg_signing_users_page", "MoinMessageSigningUserDict"), request) 117 signer = signing_users and signing_users.get(request.user.name) 118 119 # Get the recipient details. 120 121 recipients = self.get_recipients() 122 if not recipients: 123 return 0, _("No recipients page is defined for MoinMessage.") 124 125 recipient_details = recipients.get(recipient) 126 if not recipient_details: 127 return 0, _("The specified recipient is not present in the list of known contacts.") 128 129 try: 130 url, fingerprint = recipient_details.split() 131 except ValueError: 132 return 0, _("The recipient details were not in the correct format: url, fingerprint.") 133 134 # Sign, encrypt and send the message. 135 136 try: 137 message = message.get_payload() 138 if signer: 139 message = gpg.signMessage(message, signer) 140 141 message = gpg.encryptMessage(message, fingerprint) 142 sendMessage(message, url) 143 144 except MoinMessageError, exc: 145 return 0, "%s: %s" % (_("The message could not be prepared and sent:"), exc) 146 147 return 1, None 148 149 def get_homedir(self): 150 151 "Locate the GPG home directory." 152 153 return getattr(self.request.cfg, "moinmessage_gpg_homedir") 154 155 def get_recipients(self): 156 return getWikiDict(getattr(self.request.cfg, "moinmessage_gpg_recipients_page", "MoinMessageRecipientsDict"), self.request) 157 158 # Action function. 159 160 def execute(pagename, request): 161 SendMessage(pagename, request).render() 162 163 # vim: tabstop=4 expandtab shiftwidth=4