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