1.1 --- a/MoinMessageSupport.py Thu Nov 14 18:40:34 2013 +0100
1.2 +++ b/MoinMessageSupport.py Thu Nov 14 18:43:10 2013 +0100
1.3 @@ -10,7 +10,8 @@
1.4 from MoinMoin.log import getLogger
1.5 from MoinMoin.user import User
1.6 from MoinMoin import wikiutil
1.7 -from MoinSupport import getHeader, getMetadata, getWikiDict, writeHeaders
1.8 +from MoinSupport import getHeader, getMetadata, getWikiDict, writeHeaders, \
1.9 + parseDictEntry
1.10 from ItemSupport import ItemStore
1.11 from MoinMessage import GPG, Message, MoinMessageError, \
1.12 MoinMessageMissingPart, MoinMessageBadContent, \
1.13 @@ -39,6 +40,9 @@
1.14 self.pagename = pagename
1.15 self.request = request
1.16 self.page = Page(request, pagename)
1.17 + self.init_store()
1.18 +
1.19 + def init_store(self):
1.20 self.store = ItemStore(self.page, "messages", "message-locks")
1.21
1.22 def do_action(self):
1.23 @@ -53,7 +57,29 @@
1.24
1.25 "Handle the given 'message_text'."
1.26
1.27 + request = self.request
1.28 message = Parser().parse(StringIO(message_text))
1.29 +
1.30 + # Detect any indicated recipient and change the target page, if
1.31 + # appropriate.
1.32 +
1.33 + if message.has_key("To"):
1.34 + try:
1.35 + parameters = get_recipient_details(request, message["To"], main=True)
1.36 + except MoinMessageRecipientError, exc:
1.37 + writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden")
1.38 + request.write("The recipient indicated in the message is not known to this site. "
1.39 + "Details: %s" % exc.message)
1.40 + return
1.41 + else:
1.42 + if parameters["type"] == "page":
1.43 + self.page = Page(request, parameters["location"])
1.44 + self.init_store()
1.45 +
1.46 + # NOTE: Support "url".
1.47 +
1.48 + # Handle the parsed message.
1.49 +
1.50 self.handle_message(message)
1.51
1.52 def handle_message(self, message):
1.53 @@ -200,6 +226,18 @@
1.54
1.55 return homedir
1.56
1.57 +class MoinMessageRecipientError(MoinMessageError):
1.58 + pass
1.59 +
1.60 +class MoinMessageNoRecipients(MoinMessageRecipientError):
1.61 + pass
1.62 +
1.63 +class MoinMessageUnknownRecipient(MoinMessageRecipientError):
1.64 + pass
1.65 +
1.66 +class MoinMessageBadRecipient(MoinMessageRecipientError):
1.67 + pass
1.68 +
1.69 def get_homedir(request):
1.70
1.71 "Locate the GPG home directory."
1.72 @@ -222,27 +260,32 @@
1.73 getattr(request.cfg, "moinmessage_gpg_relays_page", "MoinMessageRelayDict"),
1.74 request)
1.75
1.76 -def get_recipients(request):
1.77 +def get_recipients(request, main=False):
1.78
1.79 """
1.80 Return the recipients dictionary by first obtaining the page in which it
1.81 is stored. This page may either be a subpage of the user's home page, if
1.82 stored on this wiki, or it may be relative to the site root.
1.83
1.84 + When 'main' is specified and set to a true value, only a dictionary under
1.85 + the site root is consulted.
1.86 +
1.87 The name of the subpage is defined by the configuration setting
1.88 'moinmessage_gpg_recipients_page', which if absent is set to
1.89 "MoinMessageRecipientsDict".
1.90 """
1.91
1.92 subpage = getattr(request.cfg, "moinmessage_gpg_recipients_page", "MoinMessageRecipientsDict")
1.93 - homedetails = wikiutil.getInterwikiHomePage(request)
1.94 +
1.95 + if not main:
1.96 + homedetails = wikiutil.getInterwikiHomePage(request)
1.97
1.98 - if homedetails:
1.99 - homewiki, homepage = homedetails
1.100 - if homewiki == "Self":
1.101 - recipients = getWikiDict("%s/%s" % (homepage, subpage), request)
1.102 - if recipients:
1.103 - return recipients
1.104 + if homedetails:
1.105 + homewiki, homepage = homedetails
1.106 + if homewiki == "Self":
1.107 + recipients = getWikiDict("%s/%s" % (homepage, subpage), request)
1.108 + if recipients:
1.109 + return recipients
1.110
1.111 return getWikiDict(subpage, request)
1.112
1.113 @@ -267,4 +310,38 @@
1.114 else:
1.115 return None
1.116
1.117 +def get_recipient_details(request, recipient, main=False):
1.118 +
1.119 + """
1.120 + Using the 'request', return a dictionary of details for the specified
1.121 + 'recipient'. If no details exist, raise a MoinMessageRecipientError
1.122 + exception.
1.123 +
1.124 + When 'main' is specified and set to a true value, only the recipients
1.125 + dictionary under the site root is consulted.
1.126 + """
1.127 +
1.128 + _ = request.getText
1.129 +
1.130 + recipients = get_recipients(request, main)
1.131 + if not recipients:
1.132 + raise MoinMessageNoRecipients, _("No recipients page is defined for MoinMessage.")
1.133 +
1.134 + recipient_details = recipients.get(recipient)
1.135 + if not recipient_details:
1.136 + raise MoinMessageUnknownRecipient, _("The specified recipient is not present in the list of known contacts.")
1.137 +
1.138 + parameters = parseDictEntry(recipient_details, ("type", "location", "fingerprint",))
1.139 +
1.140 + if not parameters.has_key("type"):
1.141 + raise MoinMessageBadRecipient, _("The recipient details are missing a destination type.")
1.142 +
1.143 + if not parameters.has_key("location"):
1.144 + raise MoinMessageBadRecipient, _("The recipient details are missing a location for sent messages.")
1.145 +
1.146 + if parameters["type"] in ("url", "relay") and not parameters.has_key("fingerprint"):
1.147 + raise MoinMessageBadRecipient, _("The recipient details are missing a fingerprint for sending messages.")
1.148 +
1.149 + return parameters
1.150 +
1.151 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/actions/SendMessage.py Thu Nov 14 18:40:34 2013 +0100
3.2 +++ b/actions/SendMessage.py Thu Nov 14 18:43:10 2013 +0100
3.3 @@ -12,7 +12,8 @@
3.4 from MoinMoin.Page import Page
3.5 from MoinMoin import config
3.6 from MoinMessage import GPG, MoinMessageError, Message, sendMessage, timestamp
3.7 -from MoinMessageSupport import get_signing_users, get_recipients, get_relays
3.8 +from MoinMessageSupport import get_signing_users, get_recipients, get_relays, \
3.9 + get_recipient_details, MoinMessageRecipientError
3.10 from MoinSupport import *
3.11 from ItemSupport import ItemStore
3.12 from MoinMoin.wikiutil import escape, MimeType, parseQueryString, \
3.13 @@ -274,24 +275,10 @@
3.14
3.15 # Get the recipient details.
3.16
3.17 - recipients = get_recipients(request)
3.18 - if not recipients:
3.19 - return 0, _("No recipients page is defined for MoinMessage.")
3.20 -
3.21 - recipient_details = recipients.get(recipient)
3.22 - if not recipient_details:
3.23 - return 0, _("The specified recipient is not present in the list of known contacts.")
3.24 -
3.25 - parameters = parseDictEntry(recipient_details, ("type", "location", "fingerprint",))
3.26 -
3.27 - if not parameters.has_key("type"):
3.28 - return 0, _("The recipient details are missing a destination type.")
3.29 -
3.30 - if not parameters.has_key("location"):
3.31 - return 0, _("The recipient details are missing a location for sent messages.")
3.32 -
3.33 - if parameters["type"] in ("url", "relay") and not parameters.has_key("fingerprint"):
3.34 - return 0, _("The recipient details are missing a fingerprint for sending messages.")
3.35 + try:
3.36 + parameters = get_recipient_details(request, recipient)
3.37 + except MoinMessageRecipientError, exc:
3.38 + return 0, exc.message
3.39
3.40 type = parameters["type"]
3.41 location = parameters["location"]