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