1.1 --- a/MoinMessageSupport.py Fri Jan 31 18:52:05 2014 +0100
1.2 +++ b/MoinMessageSupport.py Fri Jan 31 18:53:17 2014 +0100
1.3 @@ -6,7 +6,7 @@
1.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.5 """
1.6
1.7 -from MoinMoin import config
1.8 +from MoinMoin import config, user
1.9 from MoinMoin.Page import Page
1.10 from MoinMoin.action import AttachFile
1.11 from MoinMoin.formatter import text_html
1.12 @@ -67,6 +67,8 @@
1.13 # Detect any indicated recipient and change the target page, if
1.14 # appropriate.
1.15
1.16 + new_user = None
1.17 +
1.18 if message.has_key("To"):
1.19 try:
1.20 parameters = get_recipient_details(request, message["To"], main=True)
1.21 @@ -81,15 +83,54 @@
1.22 "Details: %s" % exc.message)
1.23 return
1.24 else:
1.25 + # Recipients with pages can have their messages redirected to
1.26 + # those pages.
1.27 +
1.28 if parameters["type"] == "page":
1.29 self.page = Page(request, parameters["location"])
1.30 self.init_store()
1.31
1.32 - # NOTE: Support "url" for message forwarding.
1.33 + # Recipients with URLs can have their messages forwarded.
1.34 +
1.35 + # Recipients accessible via relays have their messages
1.36 + # forwarded.
1.37 +
1.38 + elif parameters["type"] in ("url", "relay"):
1.39 +
1.40 + # Get the relaying user page and select it.
1.41 +
1.42 + relaying_user = getattr(self.request.cfg, "moinmessage_gpg_relaying_user")
1.43 + relaying_page = relaying_user and get_local_homepage(request, relaying_user)
1.44 +
1.45 + if not relaying_page:
1.46 + writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden")
1.47 + request.write("This site is not able to forward the message to the recipient.")
1.48 + return
1.49 +
1.50 + self.page = Page(request, relaying_page)
1.51 + self.init_store()
1.52 + new_user = relaying_user
1.53 +
1.54 + # The stored messages should then be processed
1.55 + # asynchronously.
1.56 +
1.57 + # NOTE: An action should be able to process stored messages,
1.58 + # NOTE: by invoking code that is also used asynchronously.
1.59 +
1.60 + # Switch to the relaying user if necessary.
1.61 +
1.62 + if new_user:
1.63 + user = request.user
1.64 + request.user = get_user(request, new_user) or user
1.65 + request.user.auth_method = "pgp-overridden"
1.66
1.67 # Handle the parsed message.
1.68
1.69 - self.handle_message(message)
1.70 + try:
1.71 + self.handle_message(message)
1.72 + finally:
1.73 + if new_user:
1.74 + request.user = user
1.75
1.76 def handle_message(self, message):
1.77
1.78 @@ -174,7 +215,7 @@
1.79
1.80 # Accept any message whose sender was authenticated by the PGP method.
1.81
1.82 - if request.user and request.user.valid and request.user.auth_method == "pgp":
1.83 + if request.user and request.user.valid and request.user.auth_method in ("pgp", "pgp-overridden"):
1.84
1.85 # Handle the embedded message.
1.86
1.87 @@ -277,6 +318,21 @@
1.88
1.89 # Utility functions.
1.90
1.91 +def get_user(request, username):
1.92 +
1.93 + "Return the user having the given 'username'."
1.94 +
1.95 + uid = user.getUserId(request, username)
1.96 +
1.97 + # If the user does not exist, just return None.
1.98 +
1.99 + if not uid:
1.100 + return None
1.101 +
1.102 + # Otherwise, return the requested user.
1.103 +
1.104 + return user.User(request, uid)
1.105 +
1.106 def get_homedir(request):
1.107
1.108 "Locate the GPG home directory."
1.109 @@ -321,17 +377,30 @@
1.110 subpage = getattr(request.cfg, "moinmessage_gpg_recipients_page", "MoinMessageRecipientsDict")
1.111
1.112 if not main:
1.113 - homedetails = getInterwikiHomePage(request)
1.114 -
1.115 - if homedetails:
1.116 - homewiki, homepage = homedetails
1.117 - if homewiki == "Self":
1.118 - recipients = getWikiDict("%s/%s" % (homepage, subpage), request)
1.119 - if recipients:
1.120 - return filter_recipients(recipients, sending, fetching)
1.121 + homepage = get_local_homepage(request, None)
1.122 + recipients = getWikiDict("%s/%s" % (homepage, subpage), request)
1.123 + if recipients:
1.124 + return filter_recipients(recipients, sending, fetching)
1.125
1.126 return filter_recipients(getWikiDict(subpage, request), sending, fetching)
1.127
1.128 +def get_local_homepage(request, username):
1.129 +
1.130 + """
1.131 + Using the 'request', return the homepage for the given 'username' or None if
1.132 + no local homepage exists for the user. If 'username' is None, the current
1.133 + user's homepage is located.
1.134 + """
1.135 +
1.136 + homedetails = getInterwikiHomePage(request, username)
1.137 +
1.138 + if homedetails:
1.139 + homewiki, homepage = homedetails
1.140 + if homewiki == "Self":
1.141 + return homepage
1.142 +
1.143 + return None
1.144 +
1.145 def get_username_for_fingerprint(request, fingerprint):
1.146
1.147 """