1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/actions/SendMessage.py Sun Oct 21 18:41:38 2012 +0200
1.3 @@ -0,0 +1,163 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - SendMessage Action
1.7 +
1.8 + @copyright: 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.action import ActionBase
1.13 +from MoinMoin.log import getLogger
1.14 +from MoinMessage import GPG, MoinMessageError, Message, sendMessage
1.15 +from MoinSupport import *
1.16 +from MoinMoin.wikiutil import escape
1.17 +from email.mime.text import MIMEText
1.18 +
1.19 +Dependencies = []
1.20 +
1.21 +class SendMessage(ActionBase, ActionSupport):
1.22 +
1.23 + "An action that can send a message to another site."
1.24 +
1.25 + def get_form_html(self, buttons_html):
1.26 +
1.27 + "Present an interface for message sending."
1.28 +
1.29 + _ = self._
1.30 + request = self.request
1.31 + form = self.get_form()
1.32 +
1.33 + message = form.get("message", [""])[0]
1.34 + recipient = form.get("recipient", [""])[0]
1.35 +
1.36 + # Get a list of potential recipients.
1.37 +
1.38 + recipients = self.get_recipients()
1.39 +
1.40 + # Prepare the recipients list, selecting the specified recipients.
1.41 +
1.42 + recipients_list = []
1.43 +
1.44 + if recipients:
1.45 + recipients_list += self.get_option_list(recipient, recipients) or []
1.46 +
1.47 + recipients_list.sort()
1.48 +
1.49 + # Fill in the fields and labels.
1.50 +
1.51 + d = {
1.52 + "buttons_html" : buttons_html,
1.53 + "recipient_label" : _("Recipient"),
1.54 + "recipients_list" : "\n".join(recipients_list),
1.55 + "message_label" : _("Message text"),
1.56 + "message_default" : escattr(message),
1.57 + }
1.58 +
1.59 + # Prepare the output HTML.
1.60 +
1.61 + html = '''
1.62 +<table>
1.63 + <tr>
1.64 + <td class="label"><label>%(recipient_label)s</label></td>
1.65 + <td>
1.66 + <select name="recipient">
1.67 + %(recipients_list)s
1.68 + </select>
1.69 + </td>
1.70 + </tr>
1.71 + <tr>
1.72 + <td class="label"><label>%(message_label)s</label></td>
1.73 + <td colspan="2">
1.74 + <input name="message" type="text" size="40" value="%(message_default)s" />
1.75 + </td>
1.76 + </tr>
1.77 + <tr>
1.78 + <td></td>
1.79 + <td colspan="2" class="buttons">
1.80 + %(buttons_html)s
1.81 + </td>
1.82 + </tr>
1.83 +</table>''' % d
1.84 +
1.85 + return html
1.86 +
1.87 + def do_action(self):
1.88 +
1.89 + "Attempt to send the message."
1.90 +
1.91 + _ = self._
1.92 + request = self.request
1.93 + form = self.get_form()
1.94 +
1.95 + text = form.get("message", [None])[0]
1.96 + recipient = form.get("recipient", [None])[0]
1.97 +
1.98 + if not text:
1.99 + return 0, _("A message must be given.")
1.100 +
1.101 + if not recipient:
1.102 + return 0, _("A recipient must be given.")
1.103 +
1.104 + homedir = self.get_homedir()
1.105 + if not homedir:
1.106 + return 0, _("MoinMessage has not been set up: a GPG homedir is not defined.")
1.107 +
1.108 + gpg = GPG(homedir)
1.109 +
1.110 + # Construct a message from the request.
1.111 +
1.112 + message = Message()
1.113 + message.add_update([MIMEText(text, "moin")])
1.114 +
1.115 + # Get the sender details for signing messages.
1.116 + # This is not the same as the details for authenticating users in the
1.117 + # PostMessage action since the fingerprints refer to public keys.
1.118 +
1.119 + signing_users = getWikiDict(getattr(request.cfg, "moinmessage_gpg_signing_users_page", "MoinMessageSigningUserDict"), request)
1.120 + signer = signing_users and signing_users.get(request.user.name)
1.121 +
1.122 + # Get the recipient details.
1.123 +
1.124 + recipients = self.get_recipients()
1.125 + if not recipients:
1.126 + return 0, _("No recipients page is defined for MoinMessage.")
1.127 +
1.128 + recipient_details = recipients.get(recipient)
1.129 + if not recipient_details:
1.130 + return 0, _("The specified recipient is not present in the list of known contacts.")
1.131 +
1.132 + try:
1.133 + url, fingerprint = recipient_details.split()
1.134 + except ValueError:
1.135 + return 0, _("The recipient details were not in the correct format: url, fingerprint.")
1.136 +
1.137 + # Sign, encrypt and send the message.
1.138 +
1.139 + try:
1.140 + message = message.get_payload()
1.141 + if signer:
1.142 + message = gpg.signMessage(message, signer)
1.143 +
1.144 + message = gpg.encryptMessage(message, fingerprint)
1.145 + sendMessage(message, url)
1.146 +
1.147 + except MoinMessageError, exc:
1.148 + return 0, "%s: %s" % (_("The message could not be prepared and sent:"), exc)
1.149 +
1.150 + return 1, None
1.151 +
1.152 + def get_homedir(self):
1.153 +
1.154 + "Locate the GPG home directory."
1.155 +
1.156 + return getattr(self.request.cfg, "moinmessage_gpg_homedir")
1.157 +
1.158 + def get_recipients(self):
1.159 + return getWikiDict(getattr(self.request.cfg, "moinmessage_gpg_recipients_page", "MoinMessageRecipientsDict"), self.request)
1.160 +
1.161 +# Action function.
1.162 +
1.163 +def execute(pagename, request):
1.164 + SendMessage(pagename, request).render()
1.165 +
1.166 +# vim: tabstop=4 expandtab shiftwidth=4