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@53 | 14 | from MoinMessage import GPG, MoinMessageError, Message, sendMessage, timestamp |
paul@60 | 15 | from MoinMessageSupport import get_signing_users, get_recipients |
paul@12 | 16 | from MoinSupport import * |
paul@37 | 17 | from MoinMoin.wikiutil import escape, MimeType, parseQueryString, \ |
paul@60 | 18 | taintfilename |
paul@21 | 19 | |
paul@41 | 20 | from email.mime.base import MIMEBase |
paul@21 | 21 | from email.mime.image import MIMEImage |
paul@21 | 22 | from email.mime.multipart import MIMEMultipart |
paul@12 | 23 | from email.mime.text import MIMEText |
paul@40 | 24 | from os.path import abspath, exists, join |
paul@21 | 25 | import urllib |
paul@12 | 26 | |
paul@40 | 27 | try: |
paul@40 | 28 | from MoinMoin.web import static |
paul@40 | 29 | htdocs = abspath(join(static.__file__, "htdocs")) |
paul@40 | 30 | except ImportError: |
paul@40 | 31 | htdocs = None |
paul@40 | 32 | |
paul@12 | 33 | Dependencies = [] |
paul@12 | 34 | |
paul@40 | 35 | def get_htdocs(request): |
paul@40 | 36 | |
paul@40 | 37 | "Use the 'request' to find the htdocs directory." |
paul@40 | 38 | |
paul@40 | 39 | global htdocs |
paul@40 | 40 | |
paul@40 | 41 | if not htdocs: |
paul@40 | 42 | htdocs_in_cfg = getattr(request.cfg, "moinmessage_static_files", None) |
paul@40 | 43 | if htdocs_in_cfg and exists(htdocs_in_cfg): |
paul@40 | 44 | htdocs = htdocs_in_cfg |
paul@40 | 45 | return htdocs |
paul@40 | 46 | htdocs_in_data = abspath(join(request.cfg.data_dir, "../htdocs")) |
paul@40 | 47 | if exists(htdocs_in_data): |
paul@40 | 48 | htdocs = htdocs_in_data |
paul@40 | 49 | return htdocs |
paul@40 | 50 | |
paul@40 | 51 | return htdocs |
paul@40 | 52 | |
paul@12 | 53 | class SendMessage(ActionBase, ActionSupport): |
paul@12 | 54 | |
paul@12 | 55 | "An action that can send a message to another site." |
paul@12 | 56 | |
paul@12 | 57 | def get_form_html(self, buttons_html): |
paul@12 | 58 | |
paul@12 | 59 | "Present an interface for message sending." |
paul@12 | 60 | |
paul@12 | 61 | _ = self._ |
paul@12 | 62 | request = self.request |
paul@12 | 63 | form = self.get_form() |
paul@12 | 64 | |
paul@12 | 65 | message = form.get("message", [""])[0] |
paul@12 | 66 | recipient = form.get("recipient", [""])[0] |
paul@41 | 67 | format = form.get("format", ["wiki"])[0] |
paul@25 | 68 | preview = form.get("preview") |
paul@53 | 69 | action = form.get("send-action", ["send"])[0] |
paul@12 | 70 | |
paul@12 | 71 | # Get a list of potential recipients. |
paul@12 | 72 | |
paul@60 | 73 | recipients = get_recipients(request) |
paul@12 | 74 | |
paul@12 | 75 | # Prepare the recipients list, selecting the specified recipients. |
paul@12 | 76 | |
paul@12 | 77 | recipients_list = [] |
paul@12 | 78 | |
paul@12 | 79 | if recipients: |
paul@12 | 80 | recipients_list += self.get_option_list(recipient, recipients) or [] |
paul@12 | 81 | |
paul@12 | 82 | recipients_list.sort() |
paul@12 | 83 | |
paul@21 | 84 | # Prepare any preview. |
paul@21 | 85 | |
paul@41 | 86 | parser_cls = getParserClass(request, format) |
paul@21 | 87 | request.formatter.setPage(self.page) |
paul@41 | 88 | preview_output = preview and formatText(message, request, request.formatter, inhibit_p=False, parser_cls=parser_cls) or "" |
paul@21 | 89 | |
paul@12 | 90 | # Fill in the fields and labels. |
paul@12 | 91 | |
paul@12 | 92 | d = { |
paul@12 | 93 | "buttons_html" : buttons_html, |
paul@41 | 94 | "format_label" : escape(_("Message format")), |
paul@41 | 95 | "format" : escattr(format), |
paul@25 | 96 | "recipient_label" : escape(_("Recipient")), |
paul@12 | 97 | "recipients_list" : "\n".join(recipients_list), |
paul@25 | 98 | "message_label" : escape(_("Message text")), |
paul@21 | 99 | "message_default" : escape(message), |
paul@25 | 100 | "preview_label" : escattr(_("Preview message")), |
paul@21 | 101 | "preview_output" : preview_output, |
paul@53 | 102 | "send_label" : escape(_("Send message immediately")), |
paul@53 | 103 | "send_selected" : self._get_selected("send", action), |
paul@26 | 104 | "queue_label" : escape(_("Queue message for sending")), |
paul@53 | 105 | "queue_selected" : self._get_selected("queue", action), |
paul@53 | 106 | "forward_label" : escape(_("Send message for forwarding")), |
paul@53 | 107 | "forward_selected" : self._get_selected("forward", action), |
paul@12 | 108 | } |
paul@12 | 109 | |
paul@12 | 110 | # Prepare the output HTML. |
paul@12 | 111 | |
paul@12 | 112 | html = ''' |
paul@12 | 113 | <table> |
paul@12 | 114 | <tr> |
paul@12 | 115 | <td class="label"><label>%(recipient_label)s</label></td> |
paul@12 | 116 | <td> |
paul@12 | 117 | <select name="recipient"> |
paul@12 | 118 | %(recipients_list)s |
paul@12 | 119 | </select> |
paul@12 | 120 | </td> |
paul@12 | 121 | </tr> |
paul@12 | 122 | <tr> |
paul@41 | 123 | <td class="label"><label>%(format_label)s</label></td> |
paul@41 | 124 | <td> |
paul@41 | 125 | <input name="format" type="text" value="%(format)s" size="20" /> |
paul@41 | 126 | </td> |
paul@41 | 127 | </tr> |
paul@41 | 128 | <tr> |
paul@12 | 129 | <td class="label"><label>%(message_label)s</label></td> |
paul@21 | 130 | <td> |
paul@21 | 131 | <textarea name="message" cols="60" rows="10">%(message_default)s</textarea> |
paul@12 | 132 | </td> |
paul@12 | 133 | </tr> |
paul@12 | 134 | <tr> |
paul@12 | 135 | <td></td> |
paul@21 | 136 | <td class="buttons"> |
paul@21 | 137 | <input name="preview" type="submit" value="%(preview_label)s" /> |
paul@21 | 138 | </td> |
paul@21 | 139 | </tr> |
paul@21 | 140 | <tr> |
paul@21 | 141 | <td></td> |
paul@21 | 142 | <td class="moinmessage-preview"> |
paul@21 | 143 | %(preview_output)s |
paul@21 | 144 | </td> |
paul@21 | 145 | </tr> |
paul@21 | 146 | <tr> |
paul@53 | 147 | <td></td> |
paul@26 | 148 | <td> |
paul@53 | 149 | <select name="send-action"> |
paul@53 | 150 | <option value="send" %(send_selected)s>%(send_label)s</option> |
paul@53 | 151 | <option value="queue" %(queue_selected)s>%(queue_label)s</option> |
paul@53 | 152 | <option value="forward" %(forward_selected)s>%(forward_label)s</option> |
paul@53 | 153 | </select> |
paul@26 | 154 | </td> |
paul@52 | 155 | </tr> |
paul@26 | 156 | <tr> |
paul@21 | 157 | <td></td> |
paul@21 | 158 | <td class="buttons"> |
paul@12 | 159 | %(buttons_html)s |
paul@12 | 160 | </td> |
paul@12 | 161 | </tr> |
paul@12 | 162 | </table>''' % d |
paul@12 | 163 | |
paul@12 | 164 | return html |
paul@12 | 165 | |
paul@12 | 166 | def do_action(self): |
paul@12 | 167 | |
paul@12 | 168 | "Attempt to send the message." |
paul@12 | 169 | |
paul@12 | 170 | _ = self._ |
paul@12 | 171 | request = self.request |
paul@12 | 172 | form = self.get_form() |
paul@12 | 173 | |
paul@12 | 174 | text = form.get("message", [None])[0] |
paul@12 | 175 | recipient = form.get("recipient", [None])[0] |
paul@41 | 176 | format = form.get("format", ["wiki"])[0] |
paul@53 | 177 | action = form.get("send-action", ["send"])[0] |
paul@53 | 178 | |
paul@53 | 179 | queue = action == "queue" |
paul@53 | 180 | forward = action == "forward" |
paul@12 | 181 | |
paul@12 | 182 | if not text: |
paul@12 | 183 | return 0, _("A message must be given.") |
paul@12 | 184 | |
paul@12 | 185 | if not recipient: |
paul@12 | 186 | return 0, _("A recipient must be given.") |
paul@12 | 187 | |
paul@12 | 188 | homedir = self.get_homedir() |
paul@12 | 189 | if not homedir: |
paul@12 | 190 | return 0, _("MoinMessage has not been set up: a GPG homedir is not defined.") |
paul@12 | 191 | |
paul@12 | 192 | gpg = GPG(homedir) |
paul@12 | 193 | |
paul@12 | 194 | # Construct a message from the request. |
paul@12 | 195 | |
paul@12 | 196 | message = Message() |
paul@21 | 197 | |
paul@21 | 198 | container = MIMEMultipart("related") |
paul@21 | 199 | container["Update-Action"] = "store" |
paul@26 | 200 | container["To"] = recipient |
paul@21 | 201 | |
paul@21 | 202 | # Add the message body and any attachments. |
paul@21 | 203 | |
paul@41 | 204 | parser_cls = getParserClass(request, format) |
paul@41 | 205 | |
paul@41 | 206 | # Determine whether alternative output types are produced and, if so, |
paul@41 | 207 | # bundle them in a multipart/alternative part. |
paul@41 | 208 | |
paul@41 | 209 | output_types = getParserOutputTypes(parser_cls) |
paul@41 | 210 | |
paul@41 | 211 | if len(output_types) > 1: |
paul@41 | 212 | alternatives = MIMEMultipart("alternative") |
paul@41 | 213 | container.attach(alternatives) |
paul@41 | 214 | else: |
paul@41 | 215 | alternatives = container |
paul@41 | 216 | |
paul@41 | 217 | # Produce each of the representations. |
paul@41 | 218 | |
paul@41 | 219 | for output_type in output_types: |
paul@21 | 220 | |
paul@41 | 221 | # HTML must be processed to identify attachments. |
paul@41 | 222 | |
paul@41 | 223 | if output_type == "text/html": |
paul@41 | 224 | fmt = OutgoingHTMLFormatter(request) |
paul@41 | 225 | fmt.setPage(request.page) |
paul@41 | 226 | body = formatText(text, request, fmt, inhibit_p=False, parser_cls=parser_cls) |
paul@41 | 227 | else: |
paul@41 | 228 | body = formatTextForOutputType(text, request, parser_cls, output_type) |
paul@41 | 229 | |
paul@41 | 230 | maintype, subtype = output_type.split("/", 1) |
paul@41 | 231 | if maintype == "text": |
paul@41 | 232 | part = MIMEText(body.encode("utf-8"), subtype, "utf-8") |
paul@41 | 233 | else: |
paul@41 | 234 | part = MIMEBase(maintype, subtype) |
paul@41 | 235 | part.set_payload(body) |
paul@41 | 236 | |
paul@41 | 237 | alternatives.attach(part) |
paul@41 | 238 | |
paul@41 | 239 | # Produce any identified attachments. |
paul@21 | 240 | |
paul@40 | 241 | for pos, (path, filename) in enumerate(fmt.attachments): |
paul@21 | 242 | |
paul@21 | 243 | # Obtain the attachment content. |
paul@21 | 244 | |
paul@21 | 245 | f = open(path, "rb") |
paul@21 | 246 | try: |
paul@21 | 247 | body = f.read() |
paul@21 | 248 | finally: |
paul@21 | 249 | f.close() |
paul@21 | 250 | |
paul@21 | 251 | # Determine the attachment type. |
paul@21 | 252 | |
paul@21 | 253 | mimetype = MimeType(filename=filename) |
paul@21 | 254 | |
paul@21 | 255 | # NOTE: Support a limited set of explicit part types for now. |
paul@21 | 256 | |
paul@21 | 257 | if mimetype.major == "image": |
paul@21 | 258 | part = MIMEImage(body, mimetype.minor, **mimetype.params) |
paul@21 | 259 | elif mimetype.major == "text": |
paul@21 | 260 | part = MIMEText(body, mimetype.minor, mimetype.charset, **mimetype.params) |
paul@21 | 261 | else: |
paul@21 | 262 | part = MIMEApplication(body, mimetype.minor, **mimetype.params) |
paul@21 | 263 | |
paul@21 | 264 | # Label the attachment and include it in the message. |
paul@21 | 265 | |
paul@21 | 266 | part["Content-ID"] = "attachment%d" % pos |
paul@21 | 267 | container.attach(part) |
paul@21 | 268 | |
paul@21 | 269 | message.add_update(container) |
paul@12 | 270 | |
paul@12 | 271 | # Get the sender details for signing messages. |
paul@12 | 272 | # This is not the same as the details for authenticating users in the |
paul@12 | 273 | # PostMessage action since the fingerprints refer to public keys. |
paul@12 | 274 | |
paul@60 | 275 | signing_users = get_signing_users(request) |
paul@12 | 276 | signer = signing_users and signing_users.get(request.user.name) |
paul@12 | 277 | |
paul@12 | 278 | # Get the recipient details. |
paul@12 | 279 | |
paul@60 | 280 | recipients = get_recipients(request) |
paul@12 | 281 | if not recipients: |
paul@12 | 282 | return 0, _("No recipients page is defined for MoinMessage.") |
paul@12 | 283 | |
paul@12 | 284 | recipient_details = recipients.get(recipient) |
paul@12 | 285 | if not recipient_details: |
paul@12 | 286 | return 0, _("The specified recipient is not present in the list of known contacts.") |
paul@12 | 287 | |
paul@43 | 288 | parameters = parseDictEntry(recipient_details, ("type", "location", "fingerprint",)) |
paul@27 | 289 | |
paul@43 | 290 | if not parameters.has_key("type"): |
paul@43 | 291 | return 0, _("The recipient details are missing a destination type.") |
paul@43 | 292 | |
paul@43 | 293 | if not parameters.has_key("location"): |
paul@27 | 294 | return 0, _("The recipient details are missing a location for sent messages.") |
paul@27 | 295 | |
paul@43 | 296 | if parameters.get("type") == "url" and not parameters.has_key("fingerprint"): |
paul@27 | 297 | return 0, _("The recipient details are missing a fingerprint for sending messages.") |
paul@12 | 298 | |
paul@12 | 299 | # Sign, encrypt and send the message. |
paul@12 | 300 | |
paul@26 | 301 | message = message.get_payload() |
paul@43 | 302 | type = parameters["type"] |
paul@26 | 303 | |
paul@43 | 304 | if not queue and type == "url": |
paul@26 | 305 | try: |
paul@26 | 306 | if signer: |
paul@26 | 307 | message = gpg.signMessage(message, signer) |
paul@12 | 308 | |
paul@27 | 309 | message = gpg.encryptMessage(message, parameters["fingerprint"]) |
paul@53 | 310 | |
paul@53 | 311 | # Add signing for forwarded messages. |
paul@53 | 312 | # NOTE: Signing with the same identity. |
paul@53 | 313 | |
paul@53 | 314 | if forward and signer: |
paul@53 | 315 | timestamp(message) |
paul@53 | 316 | message["Update-Action"] = "store" |
paul@53 | 317 | message = gpg.signMessage(message, signer) |
paul@53 | 318 | |
paul@43 | 319 | sendMessage(message, parameters["location"]) |
paul@26 | 320 | |
paul@26 | 321 | except MoinMessageError, exc: |
paul@39 | 322 | return 0, "%s: %s" % (_("The message could not be prepared and sent"), exc) |
paul@12 | 323 | |
paul@27 | 324 | # Or queue the message on the specified page. |
paul@27 | 325 | |
paul@43 | 326 | elif type == "page": |
paul@43 | 327 | page = Page(request, parameters["location"]) |
paul@27 | 328 | outbox = ItemStore(page, "messages", "message-locks") |
paul@27 | 329 | outbox.append(message.as_string()) |
paul@27 | 330 | |
paul@27 | 331 | # Or queue the message in a special outbox. |
paul@26 | 332 | |
paul@26 | 333 | else: |
paul@26 | 334 | outbox = ItemStore(request.page, "outgoing-messages", "outgoing-message-locks") |
paul@26 | 335 | outbox.append(message.as_string()) |
paul@12 | 336 | |
paul@31 | 337 | return 1, _("Message sent!") |
paul@12 | 338 | |
paul@12 | 339 | def get_homedir(self): |
paul@12 | 340 | |
paul@12 | 341 | "Locate the GPG home directory." |
paul@12 | 342 | |
paul@12 | 343 | return getattr(self.request.cfg, "moinmessage_gpg_homedir") |
paul@12 | 344 | |
paul@21 | 345 | # Special message formatters. |
paul@21 | 346 | |
paul@21 | 347 | def unquoteWikinameURL(url, charset=config.charset): |
paul@21 | 348 | |
paul@21 | 349 | """ |
paul@21 | 350 | The inverse of wikiutil.quoteWikinameURL, returning the page name referenced |
paul@21 | 351 | by the given 'url', with the page name assumed to be encoded using the given |
paul@21 | 352 | 'charset' (or default charset if omitted). |
paul@21 | 353 | """ |
paul@21 | 354 | |
paul@21 | 355 | return unicode(urllib.unquote(url), encoding=charset) |
paul@21 | 356 | |
paul@21 | 357 | def getAttachmentFromURL(url, request): |
paul@21 | 358 | |
paul@21 | 359 | """ |
paul@40 | 360 | Return a (full path, attachment filename) tuple for the attachment |
paul@21 | 361 | referenced by the given 'url', using the 'request' to interpret the |
paul@21 | 362 | structure of 'url'. |
paul@21 | 363 | |
paul@21 | 364 | If 'url' does not refer to an attachment on this wiki, None is returned. |
paul@21 | 365 | """ |
paul@21 | 366 | |
paul@40 | 367 | # Detect static resources. |
paul@40 | 368 | |
paul@40 | 369 | htdocs_dir = get_htdocs(request) |
paul@40 | 370 | |
paul@40 | 371 | if htdocs_dir: |
paul@40 | 372 | prefix = request.cfg.url_prefix_static |
paul@40 | 373 | |
paul@40 | 374 | # Normalise the |
paul@40 | 375 | |
paul@40 | 376 | if not prefix.endswith("/"): |
paul@40 | 377 | prefix += "/" |
paul@40 | 378 | |
paul@40 | 379 | if url.startswith(prefix): |
paul@40 | 380 | filename = url[len(prefix):] |
paul@40 | 381 | |
paul@40 | 382 | # Obtain the resource path. |
paul@40 | 383 | |
paul@40 | 384 | path = abspath(join(htdocs_dir, filename)) |
paul@40 | 385 | |
paul@40 | 386 | if exists(path): |
paul@40 | 387 | return path, taintfilename(filename) |
paul@40 | 388 | |
paul@40 | 389 | # Detect attachments and other resources. |
paul@40 | 390 | |
paul@21 | 391 | script = request.getScriptname() |
paul@39 | 392 | |
paul@39 | 393 | # Normalise the URL. |
paul@39 | 394 | |
paul@39 | 395 | if not script.endswith("/"): |
paul@39 | 396 | script += "/" |
paul@39 | 397 | |
paul@39 | 398 | # Reject URLs outside the wiki. |
paul@39 | 399 | |
paul@21 | 400 | if not url.startswith(script): |
paul@21 | 401 | return None |
paul@21 | 402 | |
paul@21 | 403 | path = url[len(script):].lstrip("/") |
paul@21 | 404 | try: |
paul@21 | 405 | qpagename, qs = path.split("?", 1) |
paul@21 | 406 | except ValueError: |
paul@21 | 407 | qpagename = path |
paul@21 | 408 | qs = None |
paul@21 | 409 | |
paul@21 | 410 | pagename = unquoteWikinameURL(qpagename) |
paul@21 | 411 | qs = qs and parseQueryString(qs) or {} |
paul@40 | 412 | |
paul@40 | 413 | filename = qs.get("target") or qs.get("drawing") |
paul@40 | 414 | filename = taintfilename(filename) |
paul@40 | 415 | |
paul@40 | 416 | # Obtain the attachment path. |
paul@40 | 417 | |
paul@40 | 418 | path = AttachFile.getFilename(request, pagename, filename) |
paul@40 | 419 | return path, filename |
paul@21 | 420 | |
paul@21 | 421 | class OutgoingHTMLFormatter(text_html.Formatter): |
paul@21 | 422 | |
paul@21 | 423 | """ |
paul@21 | 424 | Handle outgoing HTML content by identifying attachments and rewriting their |
paul@21 | 425 | locations. References to bundled attachments are done using RFC 2111: |
paul@21 | 426 | |
paul@21 | 427 | https://tools.ietf.org/html/rfc2111 |
paul@21 | 428 | |
paul@21 | 429 | Messages employing references between parts are meant to comply with RFC |
paul@21 | 430 | 2387: |
paul@21 | 431 | |
paul@21 | 432 | https://tools.ietf.org/html/rfc2387 |
paul@21 | 433 | """ |
paul@21 | 434 | |
paul@21 | 435 | def __init__(self, request, **kw): |
paul@21 | 436 | text_html.Formatter.__init__(self, request, **kw) |
paul@21 | 437 | self.attachments = [] |
paul@21 | 438 | |
paul@21 | 439 | def add_attachment(self, location): |
paul@21 | 440 | details = getAttachmentFromURL(location, self.request) |
paul@21 | 441 | if details: |
paul@21 | 442 | pos = len(self.attachments) |
paul@21 | 443 | self.attachments.append(details) |
paul@21 | 444 | return "cid:attachment%d" % pos |
paul@21 | 445 | else: |
paul@21 | 446 | return None |
paul@21 | 447 | |
paul@21 | 448 | def image(self, src=None, **kw): |
paul@21 | 449 | src = src or kw.get("src") |
paul@21 | 450 | ref = src and self.add_attachment(src) |
paul@21 | 451 | return text_html.Formatter.image(self, ref or src, **kw) |
paul@21 | 452 | |
paul@21 | 453 | def transclusion(self, on, **kw): |
paul@21 | 454 | if on: |
paul@21 | 455 | data = kw.get("data") |
paul@21 | 456 | kw["data"] = data and self.add_attachment(data) |
paul@21 | 457 | return text_html.Formatter.transclusion(self, on, **kw) |
paul@12 | 458 | |
paul@12 | 459 | # Action function. |
paul@12 | 460 | |
paul@12 | 461 | def execute(pagename, request): |
paul@12 | 462 | SendMessage(pagename, request).render() |
paul@12 | 463 | |
paul@12 | 464 | # vim: tabstop=4 expandtab shiftwidth=4 |