Hey!

Done some work with the WikiMail bits. Wrote a python script to get email's off a pop server and hand them off to the mailin.py external method. Also updated the mailin.py external method to auto-create new wiki-pages (if needed).

I'm currently using Zwiki at work to support the tech department. Am interested in getting more involved in its development.

mail me - jos at trapeze.com


NB Zwiki's Extensions/mailin.py is the latest code

mailin.py :

    # zwiki mailin - post an email message to a wiki page
    #
    # This method expects at least one argument, msg, an RFC822 email
    # message. This will be formatted as a wiki comment and appended to
    # the appropriate zwiki page.  The page is selected as follows:
    #
    # - if called in a page context, use that page
    # - if called in a folder context, look for
    #   1. a zwiki page name appearing before @ in a recipient hdr (To,Cc,etc)
    #   2. or, the first page name appearing in the subject 
    #   3. or, the defaultpage argument
    #   4. or, defaultpage hardcoded below (FrontPage)
    #
    # 1 & 2 may be turned off if you don't want them
    #
    # This is intended to receive email messages from a procmail recipe
    # like the following:
    #
    # # forward messages to a zwiki web
    # # assumes an entry for site in ~/.netrc
    # # processing only messages whose recipient contains 'wiki':
    # * ^TO.*wiki
    # * !^FROM_MAILER
    # :0 i
    # | curl -n -F 'msg=<-' http://site/wiki_folder/mailin
    #
    # todo:
    # in procmail, forward message+error to a human if post fails
    # support checking of sender against a member list
    # support size limits

    from types import *
    import re, string
    from DocumentTemplate.DT_Util import html_quote
    from Products.ZWiki.Regexps import wikiname1,wikiname2,bracketedexpr

    #wikinameexp = r'(%s|%s|%s)' % (wikiname1,wikiname2,bracketedexpr)
    wikinameexp = r'(%s|%s)' % (wikiname1,wikiname2)
    defaultpage = 'FrontPage'

    def mailin(self, msg, pagenameexp=wikinameexp, defaultpage=defaultpage, separator='\n\n', checkrecipient=1, checksubject=1):

        # extract essential fields

        # could use rfc822 ?
        #    mo=rfc822.Message(mfile)
        #
        #    hd={}
        #    hd['to']=[]
        #    for header in (mo.getaddrlist('to'),
        #                   mo.getaddrlist('cc'),
        #                   mo.getaddrlist('bcc')):
        #        if not header: continue
        #        for name, addr in header:
        #            hd['to'].append(addr)
        #    
        #    hd['from']=mo.getaddr('from')[1]
        #    hd['subject']=mo.getheader('subject') or ''

        author = re.search(r'(?m)^From: (.*)',msg)
        if author: author = author.group(1)
        else: author = ''

        date = re.search(r'(?m)^Date: (.*)',msg)
        if date: date = date.group(1)
        else: date = ''

        text = re.search(r'(?s)\n\n(.*)',msg)
        if text: text = text.group(1)
        else: text = ''

        # & do just a little html-prettification for web display.
        # If this comment gets sent out again to subscribers, zwiki
        # will try to strip this again.
        #comment = "<hr><b>%s, %s (via mail):</b><br>\n%s" % \
        #          (html_quote(author), html_quote(date), text)
        #comment = re.sub(r'(?m)^>(.*)',r'<br>><i>\1</i>',comment)
        # the comment() method takes care of this now

        # figure out the best page to use
        if self.meta_type is 'ZWiki Page':
            # if we are called on a page, use that
            page = self
        else:
            # if we are called on a folder,

            pagename = None
            if (not pagename) and checkrecipient:
                # look in recipient(s)
                pagename = re.search(
                    # simple recipient regexp:
                    # r'(?m)^(To|Cc|Bcc):.*(%s).*'
                    # using procmail's TO regexp, search all recipients
                    #r'(?m)(^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope|Apparently(-Resent)?)-To):.*(?P<pagename>%s).*)' \
                    #% (pagenameexp), msg)
                    # same, but look only at the mail acct. name before the @
                    r'(?m)(^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope|Apparently(-Resent)?)-To):.*(?P<pagename>%s)@.*)' \
                    % (pagenameexp), msg)
                if pagename:
                    pagename = pagename.group('pagename')

            if (not pagename) and checksubject:
                # look in subject
                subject = re.search(r'(?m)^Subject: (.*)',msg)
                if subject:
                    subject = subject.group(1)
                    pagename = re.search(pagenameexp,subject)
                    if pagename:
                        pagename = pagename.group(1)

            if not pagename:
                # use default page
                pagename = defaultpage

            try:
                addnewpage=0
                page = getattr(self,pagename)
            except:
                parentpage = getattr(self,defaultpage)
                addnewpage = 1

        # temp hack until we get tracker@zwiki.org - if "(tracker)" appears in
        # one of the recipients, create a tracker issue instead
        if re.search(r'(?m)(^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope|Apparently(-Resent)?)-To):.*\(tracker\).*)', msg):
            # create tracker issue
            subject = re.search(r'(?m)^Subject: (.*)',msg)
            subject = (subject and string.strip(subject.group(1))) or 'no subject'
            self.REQUEST.set('newtitle', subject)
            self.REQUEST.set('newtext', text)
            self.REQUEST.set('submitted', 1)
            return page.aq_parent.AddIssue(REQUEST=self.REQUEST)

        # add comment
        # use the time of sending, or
        #page.comment(use_heading=1, text=text, username=author, time=date, note=' (via mail)')
        # use the time of posting to the wiki - see how this works out
        if addnewpage:
            the_text = """New page: %s\n\n%s""" % (pagename,text)
            parentpage.comment(use_heading=1,text=the_text,username=author, note=' (new page via mail)')
            parentpage.create(pagename,text='',REQUEST=self.REQUEST)
            page = getattr(self,pagename)

        page.comment(use_heading=1, text=text, username=author, note=' (via mail)')


getWikiMail.py (now writes to a log file) :

    import poplib
    import urllib
    import time

    while 1:
        logstring = ""
        print "\nGetting New Messages"
        M = poplib.POP3('email.trapeze.com')
        M.user('<email login>')
        M.pass_('<email password>')
        numMessages = len(M.list()[1])
        if numMessages:        
            logstring = "\n\n**** Getting New Messages ****\n\n"  
        for i in range(numMessages):
            theMessage = ""
            for j in M.retr(i+1)[1]:
                theMessage += j + "\n"

            params = urllib.urlencode({'msg':theMessage,'defaultpage':'GeneralDiscussion'})
            f = urllib.urlopen("http://<yourURL>:8080/<zwiki directory>/mailin", params)
            M.dele(i + 1)
            logstring += "\nMessage: %s\n%s" % (i,theMessage)
            logstring += "\nServer: %s" % (f.read())
            #print "\nMessage: %s\n%s" % (i,theMessage)
            #print "\nServer: %s" % (f.read())

        M.quit()  
        if logstring != "":
            logfd = open("email.log","a")
            logfd.write(logstring)
            logfd.close()
        print "\nWaiting..."
        time.sleep(60)

JosToDoList

subtopics: