3 # Convert MH directories (1 message per file) or MMDF mailboxes (4x^A
4 # delimited) to unix mailbox (From ... delimited) on stdout.
5 # If -f is given, files contain one message per file (e.g. MH messages)
18 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'f')
19 except getopt
.error
, msg
:
20 sys
.stderr
.write('%s\n' % msg
)
29 if arg
== '-' or arg
== '':
30 sts
= dofile(sys
.stdin
) or sts
31 elif os
.path
.isdir(arg
):
33 elif os
.path
.isfile(arg
):
37 sys
.stderr
.write('%s: %s\n' % (arg
, msg
))
40 sts
= dofile(f
) or sts
43 sys
.stderr
.write('%s: not found\n' % arg
)
48 numeric
= re
.compile('[1-9][0-9]*')
52 msgs
= os
.listdir(dir)
54 if numeric
.match(msg
) != len(msg
):
56 fn
= os
.path
.join(dir, msg
)
60 sys
.stderr
.write('%s: %s\n' % (fn
, msg
))
63 sts
= message(f
) or sts
72 if line
== '\1\1\1\1\n':
73 sts
= message(f
, line
) or sts
76 'Bad line in MMFD mailbox: %r\n' % (line
,))
79 counter
= 0 # for generating unique Message-ID headers
81 def message(f
, delimiter
= ''):
85 # Write unix header line
86 fullname
, email
= m
.getaddr('From')
87 tt
= m
.getdate('Date')
92 'Unparseable date: %r\n' % (m
.getheader('Date'),))
93 t
= os
.fstat(f
.fileno())[stat
.ST_MTIME
]
94 print 'From', email
, time
.ctime(t
)
96 for line
in m
.headers
:
98 # Invent Message-ID header if none is present
99 if not m
.has_key('message-id'):
101 counter
= counter
+ 1
102 msgid
= "<%s.%d>" % (hex(t
), counter
)
103 sys
.stderr
.write("Adding Message-ID %s (From %s)\n" %
105 print "Message-ID:", msgid
110 if line
== delimiter
:
113 sys
.stderr
.write('Unexpected EOF in message\n')
116 if line
[:5] == 'From ':
119 # Print trailing newline
123 if __name__
== "__main__":