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