1.1 --- a/actions/SendMessage.py Mon Jan 27 19:20:33 2014 +0100
1.2 +++ b/actions/SendMessage.py Mon Jan 27 21:57:58 2014 +0100
1.3 @@ -6,52 +6,22 @@
1.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.5 """
1.6
1.7 -from MoinMoin.action import ActionBase, AttachFile
1.8 -from MoinMoin.formatter import text_html
1.9 -from MoinMoin.log import getLogger
1.10 +from MoinMoin.action import ActionBase
1.11 from MoinMoin.Page import Page
1.12 -from MoinMoin import config
1.13 +from MoinMoin.wikiutil import escape, MimeType
1.14 +
1.15 from MoinMessage import GPG, MoinMessageError, Message, sendMessage, timestamp, \
1.16 as_string
1.17 from MoinMessageSupport import get_signing_users, get_recipients, get_relays, \
1.18 - get_recipient_details, MoinMessageRecipientError
1.19 + get_recipient_details, \
1.20 + MoinMessageRecipientError, OutgoingHTMLFormatter
1.21 from MoinSupport import *
1.22 from ItemSupport import ItemStore
1.23 -from MoinMoin.wikiutil import escape, MimeType, parseQueryString, \
1.24 - taintfilename
1.25
1.26 from email.mime.base import MIMEBase
1.27 from email.mime.image import MIMEImage
1.28 from email.mime.multipart import MIMEMultipart
1.29 from email.mime.text import MIMEText
1.30 -from os.path import abspath, exists, join
1.31 -import urllib
1.32 -
1.33 -try:
1.34 - from MoinMoin.web import static
1.35 - htdocs = abspath(join(static.__file__, "htdocs"))
1.36 -except ImportError:
1.37 - htdocs = None
1.38 -
1.39 -Dependencies = []
1.40 -
1.41 -def get_htdocs(request):
1.42 -
1.43 - "Use the 'request' to find the htdocs directory."
1.44 -
1.45 - global htdocs
1.46 -
1.47 - if not htdocs:
1.48 - htdocs_in_cfg = getattr(request.cfg, "moinmessage_static_files", None)
1.49 - if htdocs_in_cfg and exists(htdocs_in_cfg):
1.50 - htdocs = htdocs_in_cfg
1.51 - return htdocs
1.52 - htdocs_in_data = abspath(join(request.cfg.data_dir, "../htdocs"))
1.53 - if exists(htdocs_in_data):
1.54 - htdocs = htdocs_in_data
1.55 - return htdocs
1.56 -
1.57 - return htdocs
1.58
1.59 class SendMessage(ActionBase, ActionSupport):
1.60
1.61 @@ -347,120 +317,6 @@
1.62
1.63 return getattr(self.request.cfg, "moinmessage_gpg_homedir")
1.64
1.65 -# Special message formatters.
1.66 -
1.67 -def unquoteWikinameURL(url, charset=config.charset):
1.68 -
1.69 - """
1.70 - The inverse of wikiutil.quoteWikinameURL, returning the page name referenced
1.71 - by the given 'url', with the page name assumed to be encoded using the given
1.72 - 'charset' (or default charset if omitted).
1.73 - """
1.74 -
1.75 - return unicode(urllib.unquote(url), encoding=charset)
1.76 -
1.77 -def getAttachmentFromURL(url, request):
1.78 -
1.79 - """
1.80 - Return a (full path, attachment filename) tuple for the attachment
1.81 - referenced by the given 'url', using the 'request' to interpret the
1.82 - structure of 'url'.
1.83 -
1.84 - If 'url' does not refer to an attachment on this wiki, None is returned.
1.85 - """
1.86 -
1.87 - # Detect static resources.
1.88 -
1.89 - htdocs_dir = get_htdocs(request)
1.90 -
1.91 - if htdocs_dir:
1.92 - prefix = request.cfg.url_prefix_static
1.93 -
1.94 - # Normalise the
1.95 -
1.96 - if not prefix.endswith("/"):
1.97 - prefix += "/"
1.98 -
1.99 - if url.startswith(prefix):
1.100 - filename = url[len(prefix):]
1.101 -
1.102 - # Obtain the resource path.
1.103 -
1.104 - path = abspath(join(htdocs_dir, filename))
1.105 -
1.106 - if exists(path):
1.107 - return path, taintfilename(filename)
1.108 -
1.109 - # Detect attachments and other resources.
1.110 -
1.111 - script = request.getScriptname()
1.112 -
1.113 - # Normalise the URL.
1.114 -
1.115 - if not script.endswith("/"):
1.116 - script += "/"
1.117 -
1.118 - # Reject URLs outside the wiki.
1.119 -
1.120 - if not url.startswith(script):
1.121 - return None
1.122 -
1.123 - path = url[len(script):].lstrip("/")
1.124 - try:
1.125 - qpagename, qs = path.split("?", 1)
1.126 - except ValueError:
1.127 - qpagename = path
1.128 - qs = None
1.129 -
1.130 - pagename = unquoteWikinameURL(qpagename)
1.131 - qs = qs and parseQueryString(qs) or {}
1.132 -
1.133 - filename = qs.get("target") or qs.get("drawing")
1.134 - filename = taintfilename(filename)
1.135 -
1.136 - # Obtain the attachment path.
1.137 -
1.138 - path = AttachFile.getFilename(request, pagename, filename)
1.139 - return path, filename
1.140 -
1.141 -class OutgoingHTMLFormatter(text_html.Formatter):
1.142 -
1.143 - """
1.144 - Handle outgoing HTML content by identifying attachments and rewriting their
1.145 - locations. References to bundled attachments are done using RFC 2111:
1.146 -
1.147 - https://tools.ietf.org/html/rfc2111
1.148 -
1.149 - Messages employing references between parts are meant to comply with RFC
1.150 - 2387:
1.151 -
1.152 - https://tools.ietf.org/html/rfc2387
1.153 - """
1.154 -
1.155 - def __init__(self, request, **kw):
1.156 - text_html.Formatter.__init__(self, request, **kw)
1.157 - self.attachments = []
1.158 -
1.159 - def add_attachment(self, location):
1.160 - details = getAttachmentFromURL(location, self.request)
1.161 - if details:
1.162 - pos = len(self.attachments)
1.163 - self.attachments.append(details)
1.164 - return "cid:attachment%d" % pos
1.165 - else:
1.166 - return None
1.167 -
1.168 - def image(self, src=None, **kw):
1.169 - src = src or kw.get("src")
1.170 - ref = src and self.add_attachment(src)
1.171 - return text_html.Formatter.image(self, ref or src, **kw)
1.172 -
1.173 - def transclusion(self, on, **kw):
1.174 - if on:
1.175 - data = kw.get("data")
1.176 - kw["data"] = data and self.add_attachment(data)
1.177 - return text_html.Formatter.transclusion(self, on, **kw)
1.178 -
1.179 # Action function.
1.180
1.181 def execute(pagename, request):