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