changeset 1265:b34b3fad8161

Merge with main.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Sat, 12 Aug 2006 23:57:39 +0200
parents 064778edb38c (current diff) b9c91deadca4 (diff)
children ff08338e67fe
files
diffstat 6 files changed, 93 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/mail/mailimport.py	Sat Aug 12 23:54:14 2006 +0200
+++ b/MoinMoin/mail/mailimport.py	Sat Aug 12 23:57:39 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
@@ -20,7 +21,7 @@
 # python, at least up to 2.4, ships a broken parser for headers
 from MoinMoin.support.HeaderFixed import decode_header
 
-input = sys.stdin
+infile = sys.stdin
 
 debug = False
 
@@ -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,14 +129,19 @@
     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:
-        m = re_subject.match(msg['subject'])
+        subj = msg['subject'].strip()
+        m = re_subject.search(subj)
         if m:
             pagename_tpl = m.group(1)
+            # remove the pagename template from the subject:
+            subj = re_subject.sub('', subj, 1).strip()
+        msg['subject'] = subj
     else:
         # special fix for outlook users :-)
         if pagename_tpl[-1] == pagename_tpl[0] == "'":
@@ -151,11 +176,10 @@
         to the wiki. """
     return import_mail_from_message(request, email.message_from_string(string))
 
-def import_mail_from_file(request, input):
-    """ Reads an RFC 822 compliant message from the file `input` and imports it to
+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(input))
+    return import_mail_from_message(request, email.message_from_file(infile))
 
 def import_mail_from_message(request, message):
     """ Reads a message generated by the email package and imports it
@@ -206,8 +230,7 @@
                 break
 
     # build an attachment link table for the page with the e-mail
-    escape_link = lambda x: x.replace(" ", "%20")
-    attachment_links = [""] + [u"[attachment:%s attachment:%s]" % tuple([escape_link(u"%s/%s" % (pagename, x))] * 2) for x in attachments]
+    attachment_links = [""] + [u'''[attachment:"%s/%s" %s]''' % (pagename, att, att) for att in attachments]
 
     # assemble old page content and new mail body together
     old_content = Page(request, pagename).get_raw_body()
@@ -239,14 +262,15 @@
         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 = 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
+        attach_col = " ".join(attachment_links)
         new_line = u'|| %s || %s || %s || [[DateTime(%s)]] || ["%s"] || %s ||' % (
-            msg['from_addr'][0] or msg['from_addr'][1],
-            msg['to_addr'][0] or msg['to_addr'][1],
-            msg['subject'],
-            msg['date'],
-            pagename,
-            " ".join(attachment_links),
-            )
+                    from_col, to_col, subj_col, date_col, page_col, attach_col)
         if found_table is not None:
             content = "\n".join(old_content[:table_ends] + [new_line] + old_content[table_ends:])
         else:
@@ -264,7 +288,7 @@
     request = RequestCLI(url=url)
 
     try:
-        import_mail_from_file(request, input)
+        import_mail_from_file(request, infile)
     except ProcessingError, e:
         print >>sys.stderr, "An error occured while processing the message:", e.args
 
--- a/MoinMoin/request/__init__.py	Sat Aug 12 23:54:14 2006 +0200
+++ b/MoinMoin/request/__init__.py	Sat Aug 12 23:57:39 2006 +0200
@@ -9,6 +9,12 @@
 
 import os, re, time, sys, cgi, StringIO
 import copy
+
+try:
+    set
+except:
+    from sets import Set as set
+
 from MoinMoin import config, wikiutil, user, caching, error
 from MoinMoin.util import IsWin9x
 
--- a/MoinMoin/user.py	Sat Aug 12 23:54:14 2006 +0200
+++ b/MoinMoin/user.py	Sat Aug 12 23:57:39 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	Sat Aug 12 23:57:39 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	Sat Aug 12 23:57:39 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
+
--- a/setup.py	Sat Aug 12 23:54:14 2006 +0200
+++ b/setup.py	Sat Aug 12 23:57:39 2006 +0200
@@ -241,6 +241,7 @@
         'MoinMoin.script.old',
         'MoinMoin.script.old.migration',
         'MoinMoin.script.old.xmlrpc-tools',
+        'MoinMoin.search',
         'MoinMoin.security',
         'MoinMoin.server',
         'MoinMoin.stats',