annotate MoinMoin/mailimport.py @ 729:8e8130a1f46b

some cosmetic changes to config/multiconfig
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 27 May 2006 23:06:04 +0200
parents 7df204e270ff
children
rev   line source
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
1 """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
2 MoinMoin - E-Mail Import
694
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
3
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
4 Just call this script with the URL of the wiki as a single argument
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
5 and feed the mail into stdin.
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
6
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
7 @copyright: 2006 by MoinMoin:AlexanderSchremmer
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
9 """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
10
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
11 import os, sys, re, time
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
12 import email
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
13 from email.Utils import parseaddr, parsedate_tz, mktime_tz
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
14
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
15 from MoinMoin import user, wikiutil, config
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
16 from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
17 from MoinMoin.Page import Page
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
18 from MoinMoin.PageEditor import PageEditor
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
19 from MoinMoin.request.CLI import Request as RequestCLI
693
dd62645b4219 Commented out debug lines.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 692
diff changeset
20 # python, at least up to 2.4, ships a broken parser for headers
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
21 from MoinMoin.support.HeaderFixed import decode_header
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
22
693
dd62645b4219 Commented out debug lines.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 692
diff changeset
23 input = sys.stdin
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
24
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
25 debug = False
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
26
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
27 re_subject = re.compile(r"\[([^\]]*)\]")
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
28 re_sigstrip = re.compile("\r?\n-- \r?\n.*$", re.S)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
29
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
30 class attachment(object):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
31 """ Represents an attachment of a mail. """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
32 def __init__(self, filename, mimetype, data):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
33 self.filename = filename
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
34 self.mimetype = mimetype
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
35 self.data = data
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
36
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
37 def __repr__(self):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
38 return "<attachment filename=%r mimetype=%r size=%i bytes>" % (
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
39 self.filename, self.mimetype, len(self.data))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
40
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
41 class ProcessingError(Exception):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
42 pass
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
43
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
44 def log(text):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
45 if debug:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
46 print >>sys.stderr, text
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
47
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
48 def decode_2044(header):
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
49 """ Decodes header field. See RFC 2044. """
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
50 chunks = decode_header(header)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
51 chunks_decoded = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
52 for i in chunks:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
53 chunks_decoded.append(i[0].decode(i[1] or 'ascii'))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
54 return u''.join(chunks_decoded).strip()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
55
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
56 def process_message(message):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
57 """ Processes the read message and decodes attachments. """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
58 attachments = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
59 html_data = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
60 text_data = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
61
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
62 to_addr = parseaddr(decode_2044(message['To']))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
63 from_addr = parseaddr(decode_2044(message['From']))
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
64 cc_addr = parseaddr(decode_2044(message['Cc']))
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
65 bcc_addr = parseaddr(decode_2044(message['Bcc']))
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
66
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
67 subject = decode_2044(message['Subject'])
718
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
68 date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mktime_tz(parsedate_tz(message['Date']))))
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
69
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
70 log("Processing mail:\n To: %r\n From: %r\n Subject: %r" % (to_addr, from_addr, subject))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
71
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
72 for part in message.walk():
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
73 log(" Part " + repr((part.get_charsets(), part.get_content_charset(), part.get_content_type(), part.is_multipart(), )))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
74 ct = part.get_content_type()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
75 cs = part.get_content_charset() or "latin1"
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
76 payload = part.get_payload(None, True)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
77
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
78 fn = part.get_filename()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
79 if fn is not None and fn.startswith("=?"): # heuristics ...
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
80 fn = decode_2044(fn)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
81
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
82 if fn is None and part["Content-Disposition"] is not None and "attachment" in part["Content-Disposition"]:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
83 # this doesn't catch the case where there is no content-disposition but there is a file to offer to the user
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
84 # i hope that this can be only found in mails that are older than 10 years,
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
85 # so I won't care about it here
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
86 fn = part["Content-Description"] or "NoName"
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
87 if fn:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
88 a = attachment(fn, ct, payload)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
89 attachments.append(a)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
90 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
91 if ct == 'text/plain':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
92 text_data.append(payload.decode(cs))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
93 log(repr(payload.decode(cs)))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
94 elif ct == 'text/html':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
95 html_data.append(payload.decode(cs))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
96 elif not part.is_multipart():
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
97 log("Unknown mail part " + repr((part.get_charsets(), part.get_content_charset(), part.get_content_type(), part.is_multipart(), )))
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
98
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
99 return {'text': u"".join(text_data), 'html': u"".join(html_data),
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
100 'attachments': attachments,
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
101 'to_addr': to_addr, 'from_addr': from_addr, 'cc_addr': cc_addr, 'bcc_addr': bcc_addr,
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
102 'subject': subject, 'date': date}
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
103
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
104 def get_pagename_content(msg, email_subpage_template, wiki_address):
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
105 """ Generates pagename and content according to the specification
701
276079601342 Fixed table search.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 700
diff changeset
106 that can be found on MoinMoin:FeatureRequests/WikiEmailintegration """
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
107
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
108 generate_summary = False
695
82e482f6eb13 Enabled HTML mails, forgot to remove a code line.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 694
diff changeset
109 choose_html = True
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
110
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
111 pagename_tpl = ""
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
112 for addr in ('to_addr', 'cc_addr', 'bcc_addr'):
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
113 if msg[addr][1].strip().lower() == wiki_address:
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
114 pagename_tpl = msg[addr][0]
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
115
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
116 if not pagename_tpl:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
117 m = re_subject.match(msg['subject'])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
118 if m:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
119 pagename_tpl = m.group(1)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
120 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
121 # special fix for outlook users :-)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
122 if pagename_tpl[-1] == pagename_tpl[0] == "'":
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
123 pagename_tpl = pagename_tpl[1:-1]
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
124
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
125 if pagename_tpl.endswith("/"):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
126 pagename_tpl += email_subpage_template
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
127
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
128 # last resort
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
129 if not pagename_tpl:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
130 pagename_tpl = email_subpage_template
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
131
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
132 # rewrite using string.formatter when python 2.4 is mandatory
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
133 pagename = (pagename_tpl.replace("$from", msg['from_addr'][0]).
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
134 replace("$date", msg['date']).
715
eaf76d312ea5 mailimport: use , not
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 707
diff changeset
135 replace("$subject", msg['subject']))
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
136
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
137 if pagename.startswith("+ ") and "/" in pagename:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
138 generate_summary = True
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
139 pagename = pagename[1:].lstrip()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
140
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
141 if choose_html and msg['html']:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
142 content = "{{{#!html\n%s\n}}}" % msg['html'].replace("}}}", "} } }")
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
143 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
144 # strip signatures ...
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
145 content = re_sigstrip.sub("", msg['text'])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
146
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
147 return {'pagename': pagename, 'content': content, 'generate_summary': generate_summary}
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
148
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
149 def import_mail_from_string(request, string):
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
150 """ Reads an RFC 822 compliant message from a string and imports it
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
151 to the wiki. """
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
152 return import_mail_from_message(request, email.message_from_string(string))
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
153
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
154 def import_mail_from_file(request, input):
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
155 """ Reads an RFC 822 compliant message from the file `input` and imports it to
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
156 the wiki. """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
157
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
158 return import_mail_from_message(request, email.message_from_file(input))
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
159
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
160 def import_mail_from_message(request, message):
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
161 """ Reads a message generated by the email package and imports it
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
162 to the wiki. """
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
163 msg = process_message(message)
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
164
718
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
165 email_subpage_template = request.cfg.mail_import_subpage_template
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
166 wiki_address = request.cfg.mail_import_wiki_address or request.cfg.mail_from
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
167
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
168 request.user = user.get_by_email_address(request, msg['from_addr'][1])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
169
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
170 if not request.user:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
171 raise ProcessingError("No suitable user found for mail address %r" % (msg['from_addr'][1], ))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
172
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
173 d = get_pagename_content(msg, email_subpage_template, wiki_address)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
174 pagename = d['pagename']
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
175 generate_summary = d['generate_summary']
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
176
718
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
177 comment = u"Mail: '%s'" % (msg['subject'], )
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
178
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
179 page = PageEditor(request, pagename, do_editor_backup=0)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
180
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
181 if not request.user.may.save(page, "", 0):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
182 raise ProcessingError("Access denied for page %r" % pagename)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
183
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
184 attachments = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
185
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
186 for att in msg['attachments']:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
187 i = 0
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
188 while 1:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
189 if i == 0:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
190 fname = att.filename
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
191 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
192 components = att.filename.split(".")
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
193 new_suffix = "-" + str(i)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
194 # add the counter before the file extension
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
195 if len(components) > 1:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
196 fname = u"%s%s.%s" % (u".".join(components[:-1]), new_suffix, components[-1])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
197 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
198 fname = att.filename + new_suffix
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
199 try:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
200 # get the fname again, it might have changed
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
201 fname = add_attachment(request, pagename, fname, att.data)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
202 attachments.append(fname)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
203 except AttachmentAlreadyExists:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
204 i += 1
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
205 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
206 break
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
207
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
208 # build an attachment link table for the page with the e-mail
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
209 escape_link = lambda x: x.replace(" ", "%20")
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
210 attachment_links = [""] + [u"[attachment:%s attachment:%s]" % tuple([escape_link(u"%s/%s" % (pagename, x))] * 2) for x in attachments]
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
211
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
212 # assemble old page content and new mail body together
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
213 old_content = Page(request, pagename).get_raw_body()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
214 if old_content:
696
0d16d47c6d02 Small clean ups
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 695
diff changeset
215 new_content = u"%s\n-----\n%s" % (old_content, d['content'], )
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
216 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
217 new_content = d['content']
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
218 new_content += "\n" + u"\n * ".join(attachment_links)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
219
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
220 try:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
221 page.saveText(new_content, 0, comment=comment)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
222 except page.AccessDenied:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
223 raise ProcessingError("Access denied for page %r" % pagename)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
224
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
225 if generate_summary and "/" in pagename:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
226 parent_page = u"/".join(pagename.split("/")[:-1])
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
227 old_content = Page(request, parent_page).get_raw_body().splitlines()
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
228
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
229 found_table = None
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
230 table_ends = None
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
231 for lineno, line in enumerate(old_content):
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
232 if line.startswith("## mail_overview") and old_content[lineno+1].startswith("||"):
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
233 found_table = lineno
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
234 elif found_table is not None and line.startswith("||"):
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
235 table_ends = lineno + 1
701
276079601342 Fixed table search.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 700
diff changeset
236 elif table_ends is not None and not line.startswith("||"):
276079601342 Fixed table search.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 700
diff changeset
237 break
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
238
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
239 table_header = (u"\n\n## mail_overview (don't delete this line)\n" +
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
240 u"|| '''[[GetText(From)]] ''' || '''[[GetText(To)]] ''' || '''[[GetText(Subject)]] ''' || '''[[GetText(Date)]] ''' || '''[[GetText(Link)]] ''' || '''[[GetText(Attachments)]] ''' ||\n"
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
241 )
718
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
242 new_line = u'|| %s || %s || %s || [[DateTime(%s)]] || ["%s"] || %s ||' % (
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
243 msg['from_addr'][0] or msg['from_addr'][1],
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
244 msg['to_addr'][0] or msg['to_addr'][1],
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
245 msg['subject'],
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
246 msg['date'],
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
247 pagename,
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
248 " ".join(attachment_links),
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
249 )
718
7df204e270ff Mail import - Added support for 8 bit mails, renamed config fields from email_ to mail_import_, added support for DateTime macro, fixed table recognition.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 715
diff changeset
250 if found_table is not None:
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
251 content = "\n".join(old_content[:table_ends] + [new_line] + old_content[table_ends:])
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
252 else:
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
253 content = "\n".join(old_content) + table_header + new_line
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
254
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
255 page = PageEditor(request, parent_page, do_editor_backup=0)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
256 page.saveText(content, 0, comment=comment)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
257
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
258 if __name__ == "__main__":
694
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
259 if len(sys.argv) > 1:
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
260 url = sys.argv[1]
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
261 else:
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
262 url = 'localhost/'
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
263
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
264 request = RequestCLI(url=url)
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
265
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
266 try:
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
267 import_mail_from_file(request, input)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
268 except ProcessingError, e:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
269 print >>sys.stderr, "An error occured while processing the message:", e.args
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
270