comparison MoinMoin/mail/mailimport.py @ 1155:bb4758056a46

mailimport: list multiple to/cc recipients in To: column
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 10 Aug 2006 22:55:59 +0200
parents 44632345fbfb
children 2c27897f5c98
comparison
equal deleted inserted replaced
1154:44632345fbfb 1155:bb4758056a46
9 @license: GNU GPL, see COPYING for details. 9 @license: GNU GPL, see COPYING for details.
10 """ 10 """
11 11
12 import os, sys, re, time 12 import os, sys, re, time
13 import email 13 import email
14 from email.Utils import parseaddr, parsedate_tz, mktime_tz 14 from email.Utils import getaddresses, parseaddr, parsedate_tz, mktime_tz
15 15
16 from MoinMoin import user, wikiutil, config 16 from MoinMoin import user, wikiutil, config
17 from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists 17 from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists
18 from MoinMoin.Page import Page 18 from MoinMoin.Page import Page
19 from MoinMoin.PageEditor import PageEditor 19 from MoinMoin.PageEditor import PageEditor
64 markup = u.wikiHomeLink() 64 markup = u.wikiHomeLink()
65 else: 65 else:
66 markup = realname or mailaddr 66 markup = realname or mailaddr
67 return markup 67 return markup
68 68
69 def get_addrs(message, header):
70 """ get a list of tuples (realname, mailaddr) from the specified header """
71 dec_hdr = [decode_2044(hdr) for hdr in message.get_all(header, [])]
72 return getaddresses(dec_hdr)
73
69 def process_message(message): 74 def process_message(message):
70 """ Processes the read message and decodes attachments. """ 75 """ Processes the read message and decodes attachments. """
71 attachments = [] 76 attachments = []
72 html_data = [] 77 html_data = []
73 text_data = [] 78 text_data = []
74 79
75 to_addr = parseaddr(decode_2044(message['To'])) 80 to_addr = get_addrs(message, 'To')[0]
76 from_addr = parseaddr(decode_2044(message['From'])) 81 from_addr = get_addrs(message, 'From')[0]
77 cc_addr = parseaddr(decode_2044(message['Cc'])) 82 cc_addrs = get_addrs(message, 'Cc')
78 bcc_addr = parseaddr(decode_2044(message['Bcc'])) 83 bcc_addrs = get_addrs(message, 'Bcc')
84 target_addrs = [to_addr] + cc_addrs + bcc_addrs
79 85
80 subject = decode_2044(message['Subject']) 86 subject = decode_2044(message['Subject'])
81 date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mktime_tz(parsedate_tz(message['Date'])))) 87 date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mktime_tz(parsedate_tz(message['Date']))))
82 88
83 log("Processing mail:\n To: %r\n From: %r\n Subject: %r" % (to_addr, from_addr, subject)) 89 log("Processing mail:\n To: %r\n From: %r\n Subject: %r" % (to_addr, from_addr, subject))
109 elif not part.is_multipart(): 115 elif not part.is_multipart():
110 log("Unknown mail part " + repr((part.get_charsets(), part.get_content_charset(), part.get_content_type(), part.is_multipart(), ))) 116 log("Unknown mail part " + repr((part.get_charsets(), part.get_content_charset(), part.get_content_type(), part.is_multipart(), )))
111 117
112 return {'text': u"".join(text_data), 'html': u"".join(html_data), 118 return {'text': u"".join(text_data), 'html': u"".join(html_data),
113 'attachments': attachments, 119 'attachments': attachments,
114 'to_addr': to_addr, 'from_addr': from_addr, 'cc_addr': cc_addr, 'bcc_addr': bcc_addr, 120 'target_addrs': target_addrs, 'to_addr': to_addr, 'cc_addrs': cc_addrs, 'bcc_addrs': bcc_addrs,
121 'from_addr': from_addr,
115 'subject': subject, 'date': date} 122 'subject': subject, 'date': date}
116 123
117 def get_pagename_content(msg, email_subpage_template, wiki_address): 124 def get_pagename_content(msg, email_subpage_template, wiki_address):
118 """ Generates pagename and content according to the specification 125 """ Generates pagename and content according to the specification
119 that can be found on MoinMoin:FeatureRequests/WikiEmailintegration """ 126 that can be found on MoinMoin:FeatureRequests/WikiEmailintegration """
120 127
121 generate_summary = False 128 generate_summary = False
122 choose_html = True 129 choose_html = True
123 130
124 pagename_tpl = "" 131 pagename_tpl = ""
125 for addr in ('to_addr', 'cc_addr', 'bcc_addr'): 132 for addr in msg['target_addrs']:
126 if msg[addr][1].strip().lower() == wiki_address: 133 if addr[1].strip().lower() == wiki_address:
127 pagename_tpl = msg[addr][0] 134 pagename_tpl = addr[0]
135 break
128 136
129 if not pagename_tpl: 137 if not pagename_tpl:
130 subj = msg['subject'].strip() 138 subj = msg['subject'].strip()
131 m = re_subject.search(subj) 139 m = re_subject.search(subj)
132 if m: 140 if m:
254 table_header = (u"\n\n## mail_overview (don't delete this line)\n" + 262 table_header = (u"\n\n## mail_overview (don't delete this line)\n" +
255 u"|| '''[[GetText(From)]] ''' || '''[[GetText(To)]] ''' || '''[[GetText(Subject)]] ''' || '''[[GetText(Date)]] ''' || '''[[GetText(Link)]] ''' || '''[[GetText(Attachments)]] ''' ||\n" 263 u"|| '''[[GetText(From)]] ''' || '''[[GetText(To)]] ''' || '''[[GetText(Subject)]] ''' || '''[[GetText(Date)]] ''' || '''[[GetText(Link)]] ''' || '''[[GetText(Attachments)]] ''' ||\n"
256 ) 264 )
257 265
258 from_col = email_to_markup(request, msg['from_addr']) 266 from_col = email_to_markup(request, msg['from_addr'])
259 to_col = email_to_markup(request, msg['to_addr']) 267 to_col = ' '.join([email_to_markup(request, (realname, mailaddr)) for realname, mailaddr in msg['target_addrs'] if mailaddr != wiki_address])
260 subj_col = msg['subject'] 268 subj_col = msg['subject']
261 date_col = msg['date'] 269 date_col = msg['date']
262 page_col = pagename 270 page_col = pagename
263 attach_col = " ".join(attachment_links) 271 attach_col = " ".join(attachment_links)
264 new_line = u'|| %s || %s || %s || [[DateTime(%s)]] || ["%s"] || %s ||' % ( 272 new_line = u'|| %s || %s || %s || [[DateTime(%s)]] || ["%s"] || %s ||' % (