Attachment 'email-submit.py'
Download 1 #!/usr/bin/python
2 import email, sys, os, re, glob
3 from email import Message
4 from email import Header
5
6 pagesdir = "/var/www/wiki/data/pages/"
7 prefix = "Email(2f)"
8
9 def handle_message(mail):
10 subject = ""
11 text = ""
12 attachments = []
13 if mail.has_key("Subject"):
14 [(s,e)] = Header.decode_header(mail["Subject"])
15 if e:
16 subject = unicode(s, e).encode("utf8")
17 else:
18 subject = s
19 if mail.has_key("Date"):
20 [(d,e)] = Header.decode_header(mail["Date"])
21 if e:
22 text = text + "## Date: %s\n" % unicode(d, e).encode("utf8")
23 else:
24 text = text + "## Date: %s\n" % d
25 for part in mail.walk():
26 # skip multipart parts...
27 if part.is_multipart():
28 continue
29 # get content and filename if present...
30 ct = part.get_content_type()
31 fn = part.get_filename()
32 # handle plaintext content
33 if ct == "text/plain":
34 # get and decode
35 t = part.get_payload(decode=True)
36 if part.get_charset():
37 t = unicode(t, part.get_charset()).encode("utf8")
38 else:
39 t = unicode(t, "latin1").encode("utf8")
40 # remove quotation signs
41 t = re.compile("^( *> *)+", re.M).sub("", t)
42 # strip proper signatures...
43 t = re.compile("\r?\n-- \r?\n.*$", re.S).sub("", t)
44 # repeat linewraps
45 t = re.compile("\r?\n").sub("\n\n", t)
46 text = text + t
47 # ignore HTML content
48 elif ct == "text/html":
49 pass
50 # handle proper attachments
51 elif fn:
52 print "Attachment of type %s" % (part.get_content_type())
53 print "Filename is %s" % fn
54 attachments.append( (fn, part.get_payload(decode=True)) )
55 else:
56 print "Unknown mimetype '%s', no filename" % (mail.get_content_type())
57 return subject, text, attachments
58
59 # get last ID
60 nextid = 1
61 fn = os.path.join(pagesdir, prefix)
62 r = re.compile( re.escape(fn) + "([0-9]+)(?![0-9])" )
63 for f in glob.glob( fn + "*" ):
64 nums = r.search( f )
65 num = int(nums.group(1))
66 if nextid <= num:
67 nextid = num + 1
68
69 # Load email
70 mail = email.message_from_file(sys.stdin)
71
72 # this is quoteWikinameFS from moinmoin
73 # with some modifications...
74 STRIP = re.compile(r'[\?\\\/\.]+')
75 UNSAFE = re.compile(r'[^a-zA-Z0-9_]+')
76 def quoteWikinameFS(wikiname, charset='utf-8'):
77 """ Return file system representation of a Unicode WikiName.
78
79 Warning: will raise UnicodeError if wikiname can not be encoded using
80 charset. The default value of config.charset, 'utf-8' can encode any
81 character.
82
83 @param wikiname: Unicode string possibly containing non-ascii characters
84 @param charset: charset to encode string
85 @rtype: string
86 @return: quoted name, safe for any file system
87 """
88 filename = wikiname.replace(' ', '_') # " " -> "_"
89 filename = STRIP.sub('', filename)
90
91 quoted = []
92 location = 0
93 for needle in UNSAFE.finditer(filename):
94 # append leading safe stuff
95 quoted.append(filename[location:needle.start()])
96 location = needle.end()
97 # Quote and append unsafe stuff
98 quoted.append('(')
99 for character in needle.group():
100 quoted.append('%02x' % ord(character))
101 quoted.append(')')
102
103 # append rest of string
104 quoted.append(filename[location:])
105 return ''.join(quoted)
106
107 if mail.__class__ is Message.Message:
108 (s, t, a) = handle_message(mail)
109 #fs = re.compile("[^A-Za-z0-9]").sub("", s)
110 fs = prefix + quoteWikinameFS("%06d_%s" % (nextid,s))
111
112 # make the output directory
113 os.mkdir(os.path.join(pagesdir, fs ))
114 os.mkdir(os.path.join(pagesdir, fs, "revisions" ))
115 if a:
116 os.mkdir(os.path.join(pagesdir, fs, "attachments" ))
117
118 t = ("= %s =\n" % s) + t
119 for pair in a:
120 f = file(os.path.join(pagesdir, fs, "attachments", pair[0]), "w")
121 f.write(pair[1])
122 f.close()
123 print pair[0]
124 #t = t + "\n\n" + "attachment:%s" % pair[0]
125 if a:
126 t = t + "\n\n" + "[[AttachList]]"
127
128 # write revision
129 file(os.path.join(pagesdir, fs, "current"), "w").write("00000001")
130 file(os.path.join(pagesdir, fs, "revisions", "00000001"), "w").write(t)
131 else:
132 raise "Unknown class returned by parser: %s" % (mail.__class__)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.