changeset 1282:6d46d6627b2f

merge with main (?)
author Franz Pletz <fpletz AT franz-pletz DOT org>
date Tue, 15 Aug 2006 00:01:43 +0200
parents 7df93b9b320b (current diff) cf88789ac3f0 (diff)
children e26799c574b0
files
diffstat 4 files changed, 70 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/mail/mailimport.py	Mon Aug 14 23:56:48 2006 +0200
+++ b/MoinMoin/mail/mailimport.py	Tue Aug 15 00:01:43 2006 +0200
@@ -4,13 +4,14 @@
     Just call this script with the URL of the wiki as a single argument
     and feed the mail into stdin.
 
-    @copyright: 2006 by MoinMoin:AlexanderSchremmer
+    @copyright: 2006 by MoinMoin:AlexanderSchremmer,
+                2006 by MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
 import os, sys, re, time
 import email
-from email.Utils import parseaddr, parsedate_tz, mktime_tz
+from email.Utils import getaddresses, parseaddr, parsedate_tz, mktime_tz
 
 from MoinMoin import user, wikiutil, config
 from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists
@@ -53,16 +54,34 @@
         chunks_decoded.append(i[0].decode(i[1] or 'ascii'))
     return u''.join(chunks_decoded).strip()
 
+def email_to_markup(request, email):
+    """ transform the (realname, mailaddr) tuple we get in email argument to
+        some string usable as wiki markup, that represents that person (either
+        HomePage link for a wiki user, or just the realname of the person). """
+    realname, mailaddr = email
+    u = user.get_by_email_address(request, mailaddr)
+    if u:
+        markup = u.wikiHomeLink()
+    else:
+        markup = realname or mailaddr
+    return markup
+
+def get_addrs(message, header):
+    """ get a list of tuples (realname, mailaddr) from the specified header """
+    dec_hdr = [decode_2044(hdr) for hdr in message.get_all(header, [])]
+    return getaddresses(dec_hdr)
+
 def process_message(message):
     """ Processes the read message and decodes attachments. """
     attachments = []
     html_data = []
     text_data = []
 
-    to_addr = parseaddr(decode_2044(message['To']))
-    from_addr = parseaddr(decode_2044(message['From']))
-    cc_addr = parseaddr(decode_2044(message['Cc']))
-    bcc_addr = parseaddr(decode_2044(message['Bcc']))
+    to_addr = get_addrs(message, 'To')[0]
+    from_addr = get_addrs(message, 'From')[0]
+    cc_addrs = get_addrs(message, 'Cc')
+    bcc_addrs = get_addrs(message, 'Bcc')
+    target_addrs = [to_addr] + cc_addrs + bcc_addrs
 
     subject = decode_2044(message['Subject'])
     date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mktime_tz(parsedate_tz(message['Date']))))
@@ -98,7 +117,8 @@
 
     return {'text': u"".join(text_data), 'html': u"".join(html_data),
             'attachments': attachments,
-            'to_addr': to_addr, 'from_addr': from_addr, 'cc_addr': cc_addr, 'bcc_addr': bcc_addr,
+            'target_addrs': target_addrs, 'to_addr': to_addr, 'cc_addrs': cc_addrs, 'bcc_addrs': bcc_addrs,
+            'from_addr': from_addr,
             'subject': subject, 'date': date}
 
 def get_pagename_content(msg, email_subpage_template, wiki_address):
@@ -109,9 +129,10 @@
     choose_html = True
 
     pagename_tpl = ""
-    for addr in ('to_addr', 'cc_addr', 'bcc_addr'):
-        if msg[addr][1].strip().lower() == wiki_address:
-            pagename_tpl = msg[addr][0]
+    for addr in msg['target_addrs']:
+        if addr[1].strip().lower() == wiki_address:
+            pagename_tpl = addr[0]
+            break
 
     if not pagename_tpl:
         subj = msg['subject'].strip()
@@ -158,7 +179,6 @@
 def import_mail_from_file(request, infile):
     """ Reads an RFC 822 compliant message from the file `infile` and imports it to
         the wiki. """
-
     return import_mail_from_message(request, email.message_from_file(infile))
 
 def import_mail_from_message(request, message):
@@ -242,8 +262,9 @@
         table_header = (u"\n\n## mail_overview (don't delete this line)\n" +
                         u"|| '''[[GetText(From)]] ''' || '''[[GetText(To)]] ''' || '''[[GetText(Subject)]] ''' || '''[[GetText(Date)]] ''' || '''[[GetText(Link)]] ''' || '''[[GetText(Attachments)]] ''' ||\n"
                        )
-        from_col = msg['from_addr'][0] or msg['from_addr'][1]
-        to_col = msg['to_addr'][0] or msg['to_addr'][1]
+
+        from_col = email_to_markup(request, msg['from_addr'])
+        to_col = ' '.join([email_to_markup(request, (realname, mailaddr)) for realname, mailaddr in msg['target_addrs'] if mailaddr != wiki_address])
         subj_col = msg['subject']
         date_col = msg['date']
         page_col = pagename
--- a/MoinMoin/user.py	Mon Aug 14 23:56:48 2006 +0200
+++ b/MoinMoin/user.py	Tue Aug 15 00:01:43 2006 +0200
@@ -914,31 +914,35 @@
         host = self.isCurrentUser() and self._cfg.show_hosts and self._request.remote_addr
         return host or _("<unknown>")
 
+    def wikiHomeLink(self):
+        """ return wiki markup usable as a link to the user homepage,
+            it doesn't matter whether it already exists or not.
+        """
+        wikiname, pagename = wikiutil.getInterwikiHomePage(self._request, self.name)
+        if wikiname == 'Self':
+            if wikiutil.isStrictWikiname(self.name):
+                markup = pagename
+            else:
+                markup = '["%s"]' % pagename
+        else:
+            markup = '%s:%s' % (wikiname, pagename) # TODO: support spaces in pagename
+        return markup
+
     def signature(self):
-        """ Return user signature using markup
+        """ Return user signature using wiki markup
         
-        Users sign with a link to their homepage, or with text if they
-        don't have one. The text may be parsed as a link if it's using
-        CamelCase. Visitors return their host address.
+        Users sign with a link to their homepage.
+        Visitors return their host address.
         
         TODO: The signature use wiki format only, for example, it will
         not create a link when using rst format. It will also break if
         we change wiki syntax.
         """
-        if not self.name:
+        if self.name:
+            return self.wikiHomeLink()
+        else:
             return self.host()
 
-        wikiname, pagename = wikiutil.getInterwikiHomePage(self._request,
-                                                           self.name)
-        if wikiname == 'Self':
-            if not wikiutil.isStrictWikiname(self.name):
-                markup = '["%s"]' % pagename
-            else:
-                markup = pagename
-        else:
-            markup = '%s:%s' % (wikiname, pagename)
-        return markup
-
     def mailAccountData(self, cleartext_passwd=None):
         from MoinMoin.mail import sendmail
         from MoinMoin.wikiutil import getSysPage
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/mailimport/.procmailrc	Tue Aug 15 00:01:43 2006 +0200
@@ -0,0 +1,8 @@
+# Add this to your .procmailrc rules:
+
+# Pipe all mails directed to the wiki mail address to the wikimailimport script.
+# Make sure wikimailimport script is in your PATH.
+:0 w:
+* ^(To|Cc):.*wiki@example.org
+| wikimailimport
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/mailimport/wikimailimport	Tue Aug 15 00:01:43 2006 +0200
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# add the directories were your mailimport configuration and your MoinMoin code is located to the python path:
+export PYTHONPATH='/path_to_MoinMoin:/path_to_mailimportconfig'
+
+# call the xmlrpc client code
+/path_to_MoinMoin/MoinMoin/script/moin.py xmlrpc mailimport
+