# HG changeset patch # User Paul Boddie # Date 1371166004 -7200 # Node ID aadbea4d62ef59bbb4292d38c6a5e0cad930491f # Parent 8a25c75665f5c1be3977d121ee6cf4d92daa8954 Added the ability to choose message formats and produce alternative representations of message content. diff -r 8a25c75665f5 -r aadbea4d62ef actions/SendMessage.py --- a/actions/SendMessage.py Thu Jun 13 19:44:56 2013 +0200 +++ b/actions/SendMessage.py Fri Jun 14 01:26:44 2013 +0200 @@ -16,6 +16,7 @@ from MoinMoin.wikiutil import escape, MimeType, parseQueryString, \ taintfilename, getInterwikiHomePage +from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText @@ -62,6 +63,7 @@ message = form.get("message", [""])[0] recipient = form.get("recipient", [""])[0] + format = form.get("format", ["wiki"])[0] preview = form.get("preview") queue = form.get("queue") @@ -80,13 +82,16 @@ # Prepare any preview. + parser_cls = getParserClass(request, format) request.formatter.setPage(self.page) - preview_output = preview and formatText(message, request, request.formatter, inhibit_p=False) or "" + preview_output = preview and formatText(message, request, request.formatter, inhibit_p=False, parser_cls=parser_cls) or "" # Fill in the fields and labels. d = { "buttons_html" : buttons_html, + "format_label" : escape(_("Message format")), + "format" : escattr(format), "recipient_label" : escape(_("Recipient")), "recipients_list" : "\n".join(recipients_list), "message_label" : escape(_("Message text")), @@ -110,6 +115,12 @@ + + + + + + @@ -152,6 +163,7 @@ text = form.get("message", [None])[0] recipient = form.get("recipient", [None])[0] + format = form.get("format", ["wiki"])[0] queue = form.get("queue") if not text: @@ -176,11 +188,42 @@ # Add the message body and any attachments. - fmt = OutgoingHTMLFormatter(request) - fmt.setPage(request.page) - body = formatText(text, request, fmt, inhibit_p=False) + parser_cls = getParserClass(request, format) + + # Determine whether alternative output types are produced and, if so, + # bundle them in a multipart/alternative part. + + output_types = getParserOutputTypes(parser_cls) + + if len(output_types) > 1: + alternatives = MIMEMultipart("alternative") + container.attach(alternatives) + else: + alternatives = container + + # Produce each of the representations. + + for output_type in output_types: - container.attach(MIMEText(body, "html")) + # HTML must be processed to identify attachments. + + if output_type == "text/html": + fmt = OutgoingHTMLFormatter(request) + fmt.setPage(request.page) + body = formatText(text, request, fmt, inhibit_p=False, parser_cls=parser_cls) + else: + body = formatTextForOutputType(text, request, parser_cls, output_type) + + maintype, subtype = output_type.split("/", 1) + if maintype == "text": + part = MIMEText(body.encode("utf-8"), subtype, "utf-8") + else: + part = MIMEBase(maintype, subtype) + part.set_payload(body) + + alternatives.attach(part) + + # Produce any identified attachments. for pos, (path, filename) in enumerate(fmt.attachments):