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@21 | 9 | from MoinMoin.action import ActionBase, AttachFile |
paul@21 | 10 | from MoinMoin.formatter import text_html |
paul@12 | 11 | from MoinMoin.log import getLogger |
paul@21 | 12 | from MoinMoin import config |
paul@12 | 13 | from MoinMessage import GPG, MoinMessageError, Message, sendMessage |
paul@12 | 14 | from MoinSupport import * |
paul@21 | 15 | from MoinMoin.wikiutil import escape, MimeType, parseQueryString, taintfilename |
paul@21 | 16 | |
paul@21 | 17 | from email.mime.image import MIMEImage |
paul@21 | 18 | from email.mime.multipart import MIMEMultipart |
paul@12 | 19 | from email.mime.text import MIMEText |
paul@21 | 20 | import urllib |
paul@12 | 21 | |
paul@12 | 22 | Dependencies = [] |
paul@12 | 23 | |
paul@12 | 24 | class SendMessage(ActionBase, ActionSupport): |
paul@12 | 25 | |
paul@12 | 26 | "An action that can send a message to another site." |
paul@12 | 27 | |
paul@12 | 28 | def get_form_html(self, buttons_html): |
paul@12 | 29 | |
paul@12 | 30 | "Present an interface for message sending." |
paul@12 | 31 | |
paul@12 | 32 | _ = self._ |
paul@12 | 33 | request = self.request |
paul@12 | 34 | form = self.get_form() |
paul@12 | 35 | |
paul@12 | 36 | message = form.get("message", [""])[0] |
paul@12 | 37 | recipient = form.get("recipient", [""])[0] |
paul@21 | 38 | preview = form.get("preview", [""])[0] |
paul@12 | 39 | |
paul@12 | 40 | # Get a list of potential recipients. |
paul@12 | 41 | |
paul@12 | 42 | recipients = self.get_recipients() |
paul@12 | 43 | |
paul@12 | 44 | # Prepare the recipients list, selecting the specified recipients. |
paul@12 | 45 | |
paul@12 | 46 | recipients_list = [] |
paul@12 | 47 | |
paul@12 | 48 | if recipients: |
paul@12 | 49 | recipients_list += self.get_option_list(recipient, recipients) or [] |
paul@12 | 50 | |
paul@12 | 51 | recipients_list.sort() |
paul@12 | 52 | |
paul@21 | 53 | # Prepare any preview. |
paul@21 | 54 | |
paul@21 | 55 | request.formatter.setPage(self.page) |
paul@22 | 56 | preview_output = preview and formatText(message, request, request.formatter, inhibit_p=False) or "" |
paul@21 | 57 | |
paul@12 | 58 | # Fill in the fields and labels. |
paul@12 | 59 | |
paul@12 | 60 | d = { |
paul@12 | 61 | "buttons_html" : buttons_html, |
paul@12 | 62 | "recipient_label" : _("Recipient"), |
paul@12 | 63 | "recipients_list" : "\n".join(recipients_list), |
paul@12 | 64 | "message_label" : _("Message text"), |
paul@21 | 65 | "message_default" : escape(message), |
paul@21 | 66 | "preview_label" : _("Preview message"), |
paul@21 | 67 | "preview_output" : preview_output, |
paul@12 | 68 | } |
paul@12 | 69 | |
paul@12 | 70 | # Prepare the output HTML. |
paul@12 | 71 | |
paul@12 | 72 | html = ''' |
paul@12 | 73 | <table> |
paul@12 | 74 | <tr> |
paul@12 | 75 | <td class="label"><label>%(recipient_label)s</label></td> |
paul@12 | 76 | <td> |
paul@12 | 77 | <select name="recipient"> |
paul@12 | 78 | %(recipients_list)s |
paul@12 | 79 | </select> |
paul@12 | 80 | </td> |
paul@12 | 81 | </tr> |
paul@12 | 82 | <tr> |
paul@12 | 83 | <td class="label"><label>%(message_label)s</label></td> |
paul@21 | 84 | <td> |
paul@21 | 85 | <textarea name="message" cols="60" rows="10">%(message_default)s</textarea> |
paul@12 | 86 | </td> |
paul@12 | 87 | </tr> |
paul@12 | 88 | <tr> |
paul@12 | 89 | <td></td> |
paul@21 | 90 | <td class="buttons"> |
paul@21 | 91 | <input name="preview" type="submit" value="%(preview_label)s" /> |
paul@21 | 92 | </td> |
paul@21 | 93 | </tr> |
paul@21 | 94 | <tr> |
paul@21 | 95 | <td></td> |
paul@21 | 96 | <td class="moinmessage-preview"> |
paul@21 | 97 | %(preview_output)s |
paul@21 | 98 | </td> |
paul@21 | 99 | </tr> |
paul@21 | 100 | <tr> |
paul@21 | 101 | <td></td> |
paul@21 | 102 | <td class="buttons"> |
paul@12 | 103 | %(buttons_html)s |
paul@12 | 104 | </td> |
paul@12 | 105 | </tr> |
paul@12 | 106 | </table>''' % d |
paul@12 | 107 | |
paul@12 | 108 | return html |
paul@12 | 109 | |
paul@12 | 110 | def do_action(self): |
paul@12 | 111 | |
paul@12 | 112 | "Attempt to send the message." |
paul@12 | 113 | |
paul@12 | 114 | _ = self._ |
paul@12 | 115 | request = self.request |
paul@12 | 116 | form = self.get_form() |
paul@12 | 117 | |
paul@12 | 118 | text = form.get("message", [None])[0] |
paul@12 | 119 | recipient = form.get("recipient", [None])[0] |
paul@12 | 120 | |
paul@12 | 121 | if not text: |
paul@12 | 122 | return 0, _("A message must be given.") |
paul@12 | 123 | |
paul@12 | 124 | if not recipient: |
paul@12 | 125 | return 0, _("A recipient must be given.") |
paul@12 | 126 | |
paul@12 | 127 | homedir = self.get_homedir() |
paul@12 | 128 | if not homedir: |
paul@12 | 129 | return 0, _("MoinMessage has not been set up: a GPG homedir is not defined.") |
paul@12 | 130 | |
paul@12 | 131 | gpg = GPG(homedir) |
paul@12 | 132 | |
paul@12 | 133 | # Construct a message from the request. |
paul@12 | 134 | |
paul@12 | 135 | message = Message() |
paul@21 | 136 | |
paul@21 | 137 | container = MIMEMultipart("related") |
paul@21 | 138 | container["Update-Action"] = "store" |
paul@21 | 139 | |
paul@21 | 140 | # Add the message body and any attachments. |
paul@21 | 141 | |
paul@21 | 142 | fmt = OutgoingHTMLFormatter(request) |
paul@21 | 143 | fmt.setPage(request.page) |
paul@23 | 144 | body = formatText(text, request, fmt, inhibit_p=False) |
paul@21 | 145 | |
paul@21 | 146 | container.attach(MIMEText(body, "html")) |
paul@21 | 147 | |
paul@21 | 148 | for pos, (pagename, filename) in enumerate(fmt.attachments): |
paul@21 | 149 | |
paul@21 | 150 | # Obtain the attachment path. |
paul@21 | 151 | |
paul@21 | 152 | filename = taintfilename(filename) |
paul@21 | 153 | path = AttachFile.getFilename(request, pagename, filename) |
paul@21 | 154 | |
paul@21 | 155 | # Obtain the attachment content. |
paul@21 | 156 | |
paul@21 | 157 | f = open(path, "rb") |
paul@21 | 158 | try: |
paul@21 | 159 | body = f.read() |
paul@21 | 160 | finally: |
paul@21 | 161 | f.close() |
paul@21 | 162 | |
paul@21 | 163 | # Determine the attachment type. |
paul@21 | 164 | |
paul@21 | 165 | mimetype = MimeType(filename=filename) |
paul@21 | 166 | |
paul@21 | 167 | # NOTE: Support a limited set of explicit part types for now. |
paul@21 | 168 | |
paul@21 | 169 | if mimetype.major == "image": |
paul@21 | 170 | part = MIMEImage(body, mimetype.minor, **mimetype.params) |
paul@21 | 171 | elif mimetype.major == "text": |
paul@21 | 172 | part = MIMEText(body, mimetype.minor, mimetype.charset, **mimetype.params) |
paul@21 | 173 | else: |
paul@21 | 174 | part = MIMEApplication(body, mimetype.minor, **mimetype.params) |
paul@21 | 175 | |
paul@21 | 176 | # Label the attachment and include it in the message. |
paul@21 | 177 | |
paul@21 | 178 | part["Content-ID"] = "attachment%d" % pos |
paul@21 | 179 | container.attach(part) |
paul@21 | 180 | |
paul@21 | 181 | message.add_update(container) |
paul@12 | 182 | |
paul@12 | 183 | # Get the sender details for signing messages. |
paul@12 | 184 | # This is not the same as the details for authenticating users in the |
paul@12 | 185 | # PostMessage action since the fingerprints refer to public keys. |
paul@12 | 186 | |
paul@21 | 187 | signing_users = self.get_signing_users() |
paul@12 | 188 | signer = signing_users and signing_users.get(request.user.name) |
paul@12 | 189 | |
paul@12 | 190 | # Get the recipient details. |
paul@12 | 191 | |
paul@12 | 192 | recipients = self.get_recipients() |
paul@12 | 193 | if not recipients: |
paul@12 | 194 | return 0, _("No recipients page is defined for MoinMessage.") |
paul@12 | 195 | |
paul@12 | 196 | recipient_details = recipients.get(recipient) |
paul@12 | 197 | if not recipient_details: |
paul@12 | 198 | return 0, _("The specified recipient is not present in the list of known contacts.") |
paul@12 | 199 | |
paul@12 | 200 | try: |
paul@12 | 201 | url, fingerprint = recipient_details.split() |
paul@12 | 202 | except ValueError: |
paul@12 | 203 | return 0, _("The recipient details were not in the correct format: url, fingerprint.") |
paul@12 | 204 | |
paul@12 | 205 | # Sign, encrypt and send the message. |
paul@12 | 206 | |
paul@12 | 207 | try: |
paul@12 | 208 | message = message.get_payload() |
paul@12 | 209 | if signer: |
paul@12 | 210 | message = gpg.signMessage(message, signer) |
paul@12 | 211 | |
paul@12 | 212 | message = gpg.encryptMessage(message, fingerprint) |
paul@12 | 213 | sendMessage(message, url) |
paul@12 | 214 | |
paul@12 | 215 | except MoinMessageError, exc: |
paul@12 | 216 | return 0, "%s: %s" % (_("The message could not be prepared and sent:"), exc) |
paul@12 | 217 | |
paul@12 | 218 | return 1, None |
paul@12 | 219 | |
paul@12 | 220 | def get_homedir(self): |
paul@12 | 221 | |
paul@12 | 222 | "Locate the GPG home directory." |
paul@12 | 223 | |
paul@12 | 224 | return getattr(self.request.cfg, "moinmessage_gpg_homedir") |
paul@12 | 225 | |
paul@12 | 226 | def get_recipients(self): |
paul@21 | 227 | return getWikiDict( |
paul@21 | 228 | getattr(self.request.cfg, "moinmessage_gpg_recipients_page", "MoinMessageRecipientsDict"), |
paul@21 | 229 | self.request) |
paul@21 | 230 | |
paul@21 | 231 | def get_signing_users(self): |
paul@21 | 232 | return getWikiDict( |
paul@21 | 233 | getattr(self.request.cfg, "moinmessage_gpg_signing_users_page", "MoinMessageSigningUserDict"), |
paul@21 | 234 | self.request) |
paul@21 | 235 | |
paul@21 | 236 | # Special message formatters. |
paul@21 | 237 | |
paul@21 | 238 | def unquoteWikinameURL(url, charset=config.charset): |
paul@21 | 239 | |
paul@21 | 240 | """ |
paul@21 | 241 | The inverse of wikiutil.quoteWikinameURL, returning the page name referenced |
paul@21 | 242 | by the given 'url', with the page name assumed to be encoded using the given |
paul@21 | 243 | 'charset' (or default charset if omitted). |
paul@21 | 244 | """ |
paul@21 | 245 | |
paul@21 | 246 | return unicode(urllib.unquote(url), encoding=charset) |
paul@21 | 247 | |
paul@21 | 248 | def getAttachmentFromURL(url, request): |
paul@21 | 249 | |
paul@21 | 250 | """ |
paul@21 | 251 | Return a (page name, attachment filename) tuple for the attachment |
paul@21 | 252 | referenced by the given 'url', using the 'request' to interpret the |
paul@21 | 253 | structure of 'url'. |
paul@21 | 254 | |
paul@21 | 255 | If 'url' does not refer to an attachment on this wiki, None is returned. |
paul@21 | 256 | """ |
paul@21 | 257 | |
paul@21 | 258 | script = request.getScriptname() |
paul@21 | 259 | if not url.startswith(script): |
paul@21 | 260 | return None |
paul@21 | 261 | |
paul@21 | 262 | path = url[len(script):].lstrip("/") |
paul@21 | 263 | try: |
paul@21 | 264 | qpagename, qs = path.split("?", 1) |
paul@21 | 265 | except ValueError: |
paul@21 | 266 | qpagename = path |
paul@21 | 267 | qs = None |
paul@21 | 268 | |
paul@21 | 269 | pagename = unquoteWikinameURL(qpagename) |
paul@21 | 270 | qs = qs and parseQueryString(qs) or {} |
paul@21 | 271 | return pagename, qs.get("target") or qs.get("drawing") |
paul@21 | 272 | |
paul@21 | 273 | class OutgoingHTMLFormatter(text_html.Formatter): |
paul@21 | 274 | |
paul@21 | 275 | """ |
paul@21 | 276 | Handle outgoing HTML content by identifying attachments and rewriting their |
paul@21 | 277 | locations. References to bundled attachments are done using RFC 2111: |
paul@21 | 278 | |
paul@21 | 279 | https://tools.ietf.org/html/rfc2111 |
paul@21 | 280 | |
paul@21 | 281 | Messages employing references between parts are meant to comply with RFC |
paul@21 | 282 | 2387: |
paul@21 | 283 | |
paul@21 | 284 | https://tools.ietf.org/html/rfc2387 |
paul@21 | 285 | """ |
paul@21 | 286 | |
paul@21 | 287 | def __init__(self, request, **kw): |
paul@21 | 288 | text_html.Formatter.__init__(self, request, **kw) |
paul@21 | 289 | self.attachments = [] |
paul@21 | 290 | |
paul@21 | 291 | def add_attachment(self, location): |
paul@21 | 292 | details = getAttachmentFromURL(location, self.request) |
paul@21 | 293 | if details: |
paul@21 | 294 | pos = len(self.attachments) |
paul@21 | 295 | self.attachments.append(details) |
paul@21 | 296 | return "cid:attachment%d" % pos |
paul@21 | 297 | else: |
paul@21 | 298 | return None |
paul@21 | 299 | |
paul@21 | 300 | def image(self, src=None, **kw): |
paul@21 | 301 | src = src or kw.get("src") |
paul@21 | 302 | ref = src and self.add_attachment(src) |
paul@21 | 303 | return text_html.Formatter.image(self, ref or src, **kw) |
paul@21 | 304 | |
paul@21 | 305 | def transclusion(self, on, **kw): |
paul@21 | 306 | if on: |
paul@21 | 307 | data = kw.get("data") |
paul@21 | 308 | kw["data"] = data and self.add_attachment(data) |
paul@21 | 309 | return text_html.Formatter.transclusion(self, on, **kw) |
paul@12 | 310 | |
paul@12 | 311 | # Action function. |
paul@12 | 312 | |
paul@12 | 313 | def execute(pagename, request): |
paul@12 | 314 | SendMessage(pagename, request).render() |
paul@12 | 315 | |
paul@12 | 316 | # vim: tabstop=4 expandtab shiftwidth=4 |