- Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathmsg2hypermailarchive.py
executable file
·241 lines (176 loc) · 6.61 KB
/
msg2hypermailarchive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
"""Take one email and put it in the right archive using hypermail. v%(version)s
Example usage:
cat email | %(progname)s [OPTIONS] -L <listname>
OPTIONS:
-h --help print this usage information
-L --listname= (required) archive for this list
-M --month= use this month instead of current, e.g. 4 for April
-v --verbose report actions
-Y --year= use year (e.g. 2001) instead of current (needs -M, too)
This is a Free Software reimplementation of the little programs
living in hypermail/archive/.
Copyright (C) 2002 Bernhard Reiter <bernhard@gnu.org>
This is Free Software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
__version__="0.2"
# initial 3.8.2002 Bernhard Reiter <bernhard@gnu.org>
# 5. 8.2002 Bernhard
# added -M and -Y options; made yearstring and month local variables
# improved error message when list unknown
# added a proper copyright message
importgetopt
importsys
importos
importtime
months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
verbose=0
classlistconfig:
"""Keep configuration options for one list."""
def__init__(self,
listname,archive,label,aboutlink="../",options="",
mailboxdir="",hypermail="/usr/bin/hypermail"):
self.listname=listname
self.archive=archive
ifmailboxdir:
self.mailboxdir=mailboxdir
else:
self.mailboxdir=os.path.join(self.archive,"mailbox")
self.label=label
self.aboutlink=aboutlink
self.options=options
self.hypermail=hypermail
# configuration section
lists={}
l=listconfig( listname="hypermail-test",
archive="../tests/mail-archive",
label="Hypermail Test Archive",
options="-X -L en",
hypermail="../src/hypermail"
)
lists[l.listname]=l
l=listconfig( listname="testmailman",
archive="/var/www/mail-archive",
label="Testmailman Archive",
aboutlink="../",
options="-X -L de"
)
lists[l.listname]=l
# end configuration section
defusage():
sys.stderr.write("\n"+__doc__%
{"progname":sys.argv[0], "version":__version__})
sys.stderr.write("\nThe following mailinglist configurations are known:\n")
listnames=lists.keys()
listnames.sort()
forlinlistnames:
sys.stderr.write(l+" ")
sys.stderr.write("\n")
defopenmailboxfile(list,yearstring,month):
"""Construct mailbox file, open and return it.
To get the old original msg2archive.c behaviour you would
have to replace yearstring with: ("%.2d" % int(yearstring)-1900)
"""
n=list.listname+"."+yearstring+("%.2d"%month)
fn=os.path.join(list.mailboxdir,n)
ifverbose:
sys.stderr.write("Will append message to [%s]...\n"%fn)
f=open(fn,"a")
returnf
defopenhypermailpipe(list,yearstring, month):
"""Construct hypermail command line and open pipe for it."""
d=os.path.join(list.archive,yearstring,months[month-1])
cmd=( "%s -u -i -d \"%s\" -l \"%s\" -b \"%s\" %s\n"%
(list.hypermail,d,list.label,list.aboutlink,list.options))
ifverbose:
sys.stderr.write("Will pipe message into [%s]...\n"%cmd)
p=os.popen(cmd,"w")
returnp
defarchivemail(listname,inputfile, yearstring, month):
"""Write one email to the mailbox and the hypermail pipe.
The email is read from inputfile and the non header "From "s
are quoted for the mboxfile, but not for the hypermail pipe.
"""
mboxfile=openmailboxfile(listname,yearstring,month)
pipe=openhypermailpipe(listname,yearstring,month)
inheader=1
try:
line=inputfile.readline()
whileline:
ifline=="\n"orline=="\r\n":
inheader=0
ifnotinheader:
ifline[:5]=="From ":
mboxfile.write("> ")
mboxfile.write(line)
pipe.write(line)
line=inputfile.readline()
# seperate mails
mboxfile.write("\n")
finally:
mboxfile.close()
piperesult=pipe.close()
ifpiperesult:
sys.stderr.write("Pipe to hypermail failed. Popen() returned "+
repr(piperesult)+"\n")
sys.exit(3)
ifverbose:
sys.stderr.write("done.\n")
defmain():
"Process arguments and call functions doing the real work."
globalverbose
yearstring=""
month=0
try:
opts, args=getopt.getopt(sys.argv[1:], "hL:M:vY:",
["help", "listname=","month=","verbose", "year="])
exceptgetopt.GetoptError:
# print help information and exit:
usage()
sys.exit(2)
listname=None
foro, ainopts:
ifoin ("-h", "--help"):
usage()
sys.exit()
ifoin ("-L", "--listname"):
listname=a
ifoin ("-M","--month"):
month=int(a)
ifmonth<1ormonth>12:
sys.stderr.write("Month [%s] out of range!\n"%a)
sys.exit(1)
ifoin ("-v", "--verbose"):
verbose=1
ifoin ("-Y", "--year"):
ifint(a) >1950andint(a) <3002:
yearstring=a
else:
sys.stderr.write("Year [%s] out of sensible range!\n"%a)
sys.exit(1)
ifyearstringandnotmonth:
sys.stderr.write("Whem specifying year you also must specify month!\n")
usage()
sys.exit(1)
now=time.localtime(time.time())
ifnotyearstring:
yearstring=time.strftime("%Y",now)
ifnotmonth:
month=now[1] # note time() returns 1 .. 12 in this field
ifnotlistnameornotlists.has_key(listname):
sys.stderr.write("List [%s] not known!\n"%listname)
usage()
sys.exit(1)
archivemail(lists[listname],sys.stdin,yearstring, month)
if__name__=="__main__":
main()