annotate MoinMoin/mail/mailimport.py @ 6133:a6283e189869 tip

fixup: remove nonexisting passlib.utils._blowfish this was removed by the passlib 1.7.1 upgrade.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Jun 2017 18:10:19 +0200
parents c788f228845e
children
rev   line source
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
1 """
750
f158c4e8fea2 moved mail related functions to MoinMoin.mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 718
diff changeset
2 MoinMoin - E-Mail Import into wiki
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
3
694
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
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
7 @copyright: 2006 MoinMoin:AlexanderSchremmer,
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
8 2006 MoinMoin:ThomasWaldmann
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
9 @license: GNU GPL, see COPYING for details.
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
10 """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
11
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1697
diff changeset
12 import sys, re, time
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
13 import email
6108
c788f228845e from email.lowercase import ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6107
diff changeset
14 from email.utils import getaddresses, parsedate_tz, mktime_tz
c788f228845e from email.lowercase import ...
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6107
diff changeset
15 from email.header import decode_header
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
16
5519
ace26ca9c562 mailimport: fix missing wikiutil import for normalize_pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5144
diff changeset
17 from MoinMoin import wikiutil, user
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
18 from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
19 from MoinMoin.Page import Page
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
20 from MoinMoin.PageEditor import PageEditor
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
21
1152
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
22 infile = sys.stdin
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
23
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
24 debug = False
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
25
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
26 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
27
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
28 class attachment(object):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
29 """ Represents an attachment of a mail. """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
30 def __init__(self, filename, mimetype, data):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
31 self.filename = filename
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
32 self.mimetype = mimetype
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
33 self.data = data
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
34
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
35 def __repr__(self):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
36 return "<attachment filename=%r mimetype=%r size=%i bytes>" % (
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
37 self.filename, self.mimetype, len(self.data))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
38
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
39 class ProcessingError(Exception):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
40 pass
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
41
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
42 def log(text):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
43 if debug:
2450
3d08f1cb4429 pep8 whitespace fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
44 print >> sys.stderr, text
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
45
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
46 def decode_2044(header):
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
47 """ Decodes header field. See RFC 2044. """
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
48 chunks = decode_header(header)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
49 chunks_decoded = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
50 for i in chunks:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
51 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
52 return u''.join(chunks_decoded).strip()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
53
1154
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
54 def email_to_markup(request, email):
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
55 """ transform the (realname, mailaddr) tuple we get in email argument to
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
56 some string usable as wiki markup, that represents that person (either
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
57 HomePage link for a wiki user, or just the realname of the person). """
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
58 realname, mailaddr = email
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
59 u = user.get_by_email_address(request, mailaddr)
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
60 if u:
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
61 markup = u.wikiHomeLink()
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
62 else:
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
63 markup = realname or mailaddr
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
64 return markup
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
65
1155
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
66 def get_addrs(message, header):
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
67 """ get a list of tuples (realname, mailaddr) from the specified header """
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
68 dec_hdr = [decode_2044(hdr) for hdr in message.get_all(header, [])]
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
69 return getaddresses(dec_hdr)
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
70
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
71 def process_message(message):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
72 """ Processes the read message and decodes attachments. """
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
73 attachments = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
74 html_data = []
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
75 text_data = []
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
76
1155
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
77 from_addr = get_addrs(message, 'From')[0]
1286
2c27897f5c98 mailimport: process all To: addresses, link mail page from subject
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1155
diff changeset
78 to_addrs = get_addrs(message, 'To')
1155
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
79 cc_addrs = get_addrs(message, 'Cc')
1591
3c59e4f9bf42 also look at some headers where mailservers deposit the envelope to-address
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1579
diff changeset
80 bcc_addrs = get_addrs(message, 'Bcc') # depending on sending MTA, this can be present or not
3c59e4f9bf42 also look at some headers where mailservers deposit the envelope to-address
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1579
diff changeset
81 envelope_to_addrs = get_addrs(message, 'X-Original-To') + get_addrs(message, 'X-Envelope-To') # Postfix / Sendmail does this
3c59e4f9bf42 also look at some headers where mailservers deposit the envelope to-address
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1579
diff changeset
82 target_addrs = to_addrs + cc_addrs + bcc_addrs + envelope_to_addrs
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
83
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
84 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
85 date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(mktime_tz(parsedate_tz(message['Date']))))
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
86
1286
2c27897f5c98 mailimport: process all To: addresses, link mail page from subject
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1155
diff changeset
87 log("Processing mail:\n To: %r\n From: %r\n Subject: %r" % (to_addrs[0], from_addr, subject))
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
88
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
89 for part in message.walk():
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
90 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
91 ct = part.get_content_type()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
92 cs = part.get_content_charset() or "latin1"
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
93 payload = part.get_payload(None, True)
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
94
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
95 fn = part.get_filename()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
96 if fn is not None and fn.startswith("=?"): # heuristics ...
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
97 fn = decode_2044(fn)
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
98
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
99 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
100 # 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
101 # 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
102 # so I won't care about it here
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
103 fn = part["Content-Description"] or "NoName"
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
104 if fn:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
105 a = attachment(fn, ct, payload)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
106 attachments.append(a)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
107 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
108 if ct == 'text/plain':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
109 text_data.append(payload.decode(cs))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
110 log(repr(payload.decode(cs)))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
111 elif ct == 'text/html':
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
112 html_data.append(payload.decode(cs))
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
113 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
114 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
115
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
116 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
117 'attachments': attachments,
1591
3c59e4f9bf42 also look at some headers where mailservers deposit the envelope to-address
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1579
diff changeset
118 'target_addrs': target_addrs,
3c59e4f9bf42 also look at some headers where mailservers deposit the envelope to-address
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1579
diff changeset
119 'to_addrs': to_addrs, 'cc_addrs': cc_addrs, 'bcc_addrs': bcc_addrs, 'envelope_to_addrs': envelope_to_addrs,
1155
bb4758056a46 mailimport: list multiple to/cc recipients in To: column
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1154
diff changeset
120 'from_addr': from_addr,
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
121 'subject': subject, 'date': date}
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
122
1686
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
123 def get_pagename_content(request, msg):
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
124 """ Generates pagename and content according to the specification
701
276079601342 Fixed table search.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 700
diff changeset
125 that can be found on MoinMoin:FeatureRequests/WikiEmailintegration """
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
126 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
127 choose_html = True
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
128
1686
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
129 cfg = request.cfg
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
130 email_subpage_template = cfg.mail_import_subpage_template
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
131 email_pagename_envelope = cfg.mail_import_pagename_envelope
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
132 wiki_addrs = cfg.mail_import_wiki_addrs
1687
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
133 search_list = cfg.mail_import_pagename_search
1688
07da298b129a mail import: mail_import_pagename_regex determines how to find/extract the pagename from the subject
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1687
diff changeset
134 re_subject = re.compile(cfg.mail_import_pagename_regex)
700
45cf56e067c5 Added support for looking in cc and bcc.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 699
diff changeset
135
1687
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
136 subj = msg['subject'].strip()
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
137 pagename_tpl = ""
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
138 for method in search_list:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
139 if method == 'to':
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
140 for addr in msg['target_addrs']:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
141 if addr[1].strip().lower() in wiki_addrs:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
142 pagename_tpl = addr[0]
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
143 # special fix for outlook users :-)
1696
70e505db8728 mailimport: fix backtrace for empty target addr realname, loop in target addrs until some realname was found
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1688
diff changeset
144 if pagename_tpl and pagename_tpl[-1] == pagename_tpl[0] == "'":
1687
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
145 pagename_tpl = pagename_tpl[1:-1]
1696
70e505db8728 mailimport: fix backtrace for empty target addr realname, loop in target addrs until some realname was found
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1688
diff changeset
146 if pagename_tpl:
70e505db8728 mailimport: fix backtrace for empty target addr realname, loop in target addrs until some realname was found
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1688
diff changeset
147 break
1697
7c58e8af1a97 mailimport: fix wrong comparison value
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1696
diff changeset
148 elif method == 'subject':
1687
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
149 m = re_subject.search(subj)
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
150 if m:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
151 pagename_tpl = m.group(1)
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
152 # remove the pagename template from the subject:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
153 subj = re_subject.sub('', subj, 1).strip()
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
154 if pagename_tpl:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
155 break
1289
01d6d28d5039 mailimport: treating empty subjects better, normalize pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1288
diff changeset
156
1287
210f96eeda09 mailimport: strip blanks from pagename_tpl, add TODO
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1286
diff changeset
157 pagename_tpl = pagename_tpl.strip()
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
158 # last resort
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
159 if not pagename_tpl:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
160 pagename_tpl = email_subpage_template
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
161
1687
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
162 if not subj:
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
163 subj = '(...)' # we need non-empty subject
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
164 msg['subject'] = subj
1d358ccc0f2f mail_import_pagename_search determines where and in which order to look for target pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1686
diff changeset
165
1615
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
166 # for normal use, email_pagename_envelope is just u"%s" - so nothing changes.
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
167 # for special use, you can use u"+ %s/" - so you don't need to enter "+"
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
168 # and "/" in every email, but you get the result as if you did.
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
169 pagename_tpl = email_pagename_envelope % pagename_tpl
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
170
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
171 if pagename_tpl.endswith("/"):
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
172 pagename_tpl += email_subpage_template
9fa2ec252d1f mailimport: add envelope feature for target page spec
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1614
diff changeset
173
1616
d1c37c39f8ad mailimport: replace / in subject by \ because it is invalid in (sub)page names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1615
diff changeset
174 subject = msg['subject'].replace('/', '\\') # we can't use / in pagenames
d1c37c39f8ad mailimport: replace / in subject by \ because it is invalid in (sub)page names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1615
diff changeset
175
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
176 # 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
177 pagename = (pagename_tpl.replace("$from", msg['from_addr'][0]).
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
178 replace("$date", msg['date']).
1616
d1c37c39f8ad mailimport: replace / in subject by \ because it is invalid in (sub)page names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1615
diff changeset
179 replace("$subject", subject))
692
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 pagename.startswith("+ ") and "/" in pagename:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
182 generate_summary = True
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
183 pagename = pagename[1:].lstrip()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
184
4146
3b36f0cfc656 (Re)moved normalizePagename from Request into wikiutil library
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 3175
diff changeset
185 pagename = wikiutil.normalize_pagename(pagename, request.cfg)
1289
01d6d28d5039 mailimport: treating empty subjects better, normalize pagename
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1288
diff changeset
186
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
187 if choose_html and msg['html']:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
188 content = "{{{#!html\n%s\n}}}" % msg['html'].replace("}}}", "} } }")
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
189 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
190 # strip signatures ...
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
191 content = re_sigstrip.sub("", msg['text'])
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
192
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
193 return {'pagename': pagename, 'content': content, 'generate_summary': generate_summary}
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
194
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
195 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
196 """ 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
197 to the wiki. """
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
198 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
199
1152
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
200 def import_mail_from_file(request, infile):
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
201 """ Reads an RFC 822 compliant message from the file `infile` and imports it to
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
202 the wiki. """
1152
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
203 return import_mail_from_message(request, email.message_from_file(infile))
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
204
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
205 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
206 """ 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
207 to the wiki. """
1544
b9e4f2a88b64 mailimport: define _ to avoid crash
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1381
diff changeset
208 _ = request.getText
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
209 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
210
1658
0dee8ba737af mailimport: allow multiple wiki email addrs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1616
diff changeset
211 wiki_addrs = request.cfg.mail_import_wiki_addrs
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
212
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
213 request.user = user.get_by_email_address(request, msg['from_addr'][1])
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
214
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
215 if not request.user:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
216 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
217
1686
b010f7260ab6 mailimport: minor refactoring
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1658
diff changeset
218 d = get_pagename_content(request, msg)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
219 pagename = d['pagename']
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
220 generate_summary = d['generate_summary']
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
221
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
222 comment = u"Mail: '%s'" % (msg['subject'], )
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
223
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
224 page = PageEditor(request, pagename, do_editor_backup=0)
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
225
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
226 if not request.user.may.save(page, "", 0):
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
227 raise ProcessingError("Access denied for page %r" % pagename)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
228
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
229 attachments = []
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
230
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
231 for att in msg['attachments']:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
232 i = 0
5132
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
233 while i < 1000: # do not create a gazillion attachments if something
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
234 # strange happens, give up after 1000.
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
235 if i == 0:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
236 fname = att.filename
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
237 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
238 components = att.filename.split(".")
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
239 new_suffix = "-" + str(i)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
240 # add the counter before the file extension
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
241 if len(components) > 1:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
242 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
243 else:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
244 fname = att.filename + new_suffix
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
245 try:
5132
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
246 # att.data can be None for forwarded message content - we can
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
247 # just ignore it, the forwarded message's text will be present
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
248 # nevertheless
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
249 if att.data is not None:
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
250 # get the fname again, it might have changed
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
251 fname, fsize = add_attachment(request, pagename, fname, att.data)
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
252 attachments.append(fname)
df8f5b4546ca mailimport: fix endless looping while trying to import a forwarded mail
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4070
diff changeset
253 break
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
254 except AttachmentAlreadyExists:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
255 i += 1
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
256
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
257 # build an attachment link table for the page with the e-mail
6020
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
258 # use relative attachment link markup here, so the page can be easily
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
259 # renamed and the links don't break
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
260 attachment_links = [""] + [u'''[[attachment:%s|%s]]''' % (att, att) for att in attachments]
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
261
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
262 # assemble old page content and new mail body together
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
263 old_content = Page(request, pagename).get_raw_body()
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
264 if old_content:
1290
655978c9f6c3 mailimport: generate subj/from/date header if we have no overview table
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1289
diff changeset
265 new_content = u"%s\n-----\n" % old_content
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
266 else:
1290
655978c9f6c3 mailimport: generate subj/from/date header if we have no overview table
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1289
diff changeset
267 new_content = ''
655978c9f6c3 mailimport: generate subj/from/date header if we have no overview table
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1289
diff changeset
268
1614
f89eb93324e2 mailimport: generate header above mail content in any case
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1591
diff changeset
269 #if not (generate_summary and "/" in pagename):
f89eb93324e2 mailimport: generate header above mail content in any case
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1591
diff changeset
270 #generate header in any case:
2725
388204baf00f change [[macro]] to <<macro>> in all built-in markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2450
diff changeset
271 new_content += u"'''Mail: %s (%s, <<DateTime(%s)>>)'''\n\n" % (msg['subject'], email_to_markup(request, msg['from_addr']), msg['date'])
1290
655978c9f6c3 mailimport: generate subj/from/date header if we have no overview table
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1289
diff changeset
272
655978c9f6c3 mailimport: generate subj/from/date header if we have no overview table
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1289
diff changeset
273 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
274 new_content += "\n" + u"\n * ".join(attachment_links)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
275
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
276 try:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
277 page.saveText(new_content, 0, comment=comment)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
278 except page.AccessDenied:
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
279 raise ProcessingError("Access denied for page %r" % pagename)
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
280
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
281 if generate_summary and "/" in pagename:
6020
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
282 splitted = pagename.split("/")
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
283 parent_page = u"/".join(splitted[:-1])
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
284 child_page = splitted[-1]
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
285 # here, use relative links also, but we need to include the child_page
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
286 # name in the relative link as the markup gets put onto the parent_page
b59cef3f6e09 mail import: use relative attachment link markup instead of absolute links (enables some renaming operations without changing the markup)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5887
diff changeset
287 attachment_links = [u'''[[attachment:%s|%s]]''' % ("/%s/%s" % (child_page, att), att) for att in attachments]
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
288 old_content = Page(request, parent_page).get_raw_body().splitlines()
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
289
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
290 found_table = None
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
291 table_ends = None
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
292 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
293 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
294 found_table = lineno
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
295 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
296 table_ends = lineno + 1
701
276079601342 Fixed table search.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 700
diff changeset
297 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
298 break
993
1547a168325a mail package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 750
diff changeset
299
2725
388204baf00f change [[macro]] to <<macro>> in all built-in markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2450
diff changeset
300 # in order to let the gettext system recognise the <<GetText>> calls used below,
1320
a36c70e5f0dd Reworked i18n and logging support, from now on, the log is attached to the job page.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1290
diff changeset
301 # we must repeat them here:
1579
134062b3d442 mailimport: move date column to the left
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1544
diff changeset
302 [_("Date"), _("From"), _("To"), _("Content"), _("Attachments")]
1320
a36c70e5f0dd Reworked i18n and logging support, from now on, the log is attached to the job page.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1290
diff changeset
303
699
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
304 table_header = (u"\n\n## mail_overview (don't delete this line)\n" +
2725
388204baf00f change [[macro]] to <<macro>> in all built-in markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2450
diff changeset
305 u"|| '''<<GetText(Date)>> ''' || '''<<GetText(From)>> ''' || '''<<GetText(To)>> ''' || '''<<GetText(Content)>> ''' || '''<<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
306 )
1154
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
307
44632345fbfb mailimport: use link to homepage or realname in from/to column, user.wikiHomeLink() to generate markup for user's homepage link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1152
diff changeset
308 from_col = email_to_markup(request, msg['from_addr'])
1286
2c27897f5c98 mailimport: process all To: addresses, link mail page from subject
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1155
diff changeset
309 to_col = ' '.join([email_to_markup(request, (realname, mailaddr))
1658
0dee8ba737af mailimport: allow multiple wiki email addrs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1616
diff changeset
310 for realname, mailaddr in msg['target_addrs'] if not mailaddr in wiki_addrs])
2728
59b3d8b8971f remove (un)quoteName, new functions split_interwiki() and resolve_interwiki(), cleanup interwiki mess, fix some link markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2725
diff changeset
311 subj_col = '[[%s|%s]]' % (pagename, msg['subject'])
1152
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
312 date_col = msg['date']
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
313 attach_col = " ".join(attachment_links)
2725
388204baf00f change [[macro]] to <<macro>> in all built-in markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2450
diff changeset
314 new_line = u'|| <<DateTime(%s)>> || %s || %s || %s || %s ||' % (date_col, from_col, to_col, subj_col, attach_col)
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
315 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
316 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
317 else:
ea6dab2f43f7 Write a table on the parent page in +-mode.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 697
diff changeset
318 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
319
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
320 page = PageEditor(request, parent_page, do_editor_backup=0)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
321 page.saveText(content, 0, comment=comment)
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
322
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
323 if __name__ == "__main__":
694
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
324 if len(sys.argv) > 1:
4258
3f8be25b79cc Replaced use of request_cli with ScriptContext (all occurrences)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4146
diff changeset
325 request_url = sys.argv[1]
694
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
326 else:
4594
d706f5d4f4ec fix ScriptContext URL default value
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4327
diff changeset
327 request_url = None
694
0c32fb700c3c Added URL support, added comment.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 693
diff changeset
328
4258
3f8be25b79cc Replaced use of request_cli with ScriptContext (all occurrences)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4146
diff changeset
329 from MoinMoin.web.contexts import ScriptContext
3f8be25b79cc Replaced use of request_cli with ScriptContext (all occurrences)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4146
diff changeset
330 request = ScriptContext(url=request_url)
704
38aa322f35c9 Added two-tier XMLRPC mail import client/server system.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 701
diff changeset
331
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
332 try:
1152
acf14d68b7c8 mailimport: if template is given in subject, use first we find there (can be at beginning or end), src cosmetics
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1151
diff changeset
333 import_mail_from_file(request, infile)
692
7a6e256dd074 Added mail import feature.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
diff changeset
334 except ProcessingError, e:
5887
b397401dda7f fix typos, correct is: 'occurred'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5519
diff changeset
335 print >> sys.stderr, "An error occurred while processing the message:", e.args
702
433a0ee1635d some cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 701
diff changeset
336