* On 25 Nov 2012, Jamie Paul Griffin wrote:
> >
> > In the end the new version of the message is generated by formail, so
> > it's subject to formail's limitations, if it has any. (I don't know of
> > any.) It would be possible to update the python script to handle the
> > rewrite with python core modules instead of depending on formail. Maybe
> > that's worth a try.
> ...
> I am using mbox for all folders currently. I will send the mail to you
> off-list.
I took at look at the messages you sent, and was able to reproduce the
problem with one of them but not the other. It's not clear to me why
it's happening from within mutt. From without, it works fine. The
script runs formail using os.popen2 -- which is deprecated, and causes
an annoying deprecation warning too. This could be entangled in the
problem.
I changed the script to use python's email module instead of calling out
to formail (which was a quick hack when I wrote this last week). It
seems to work fine now.
It also deals with the mbox/maildir (potential) problem, by writing in
mbox format only when the input was in mbox.
--
David Champion • [email protected]
#!/usr/bin/env python
import os
import sys
import time
import email
try:
from parsedatetime.parsedatetime import Calendar
except ImportError:
p = os.path.basename(sys.argv[0])
print >>sys.stderr, '%s: please install the parsedatetime module' % p
sys.exit(255)
def fmtdate(spec):
'''Generate an rfc822 (GMT) time strong for a spec provided in
the arguments.
parsedatetime doesn't know anything about timezones, so the
mktime and gmtime are just to adapt the struct_time value from
c.parse() from local time to GMT, so that the RFC822 address
can assume it. This lets the script work for anyone, without
needing to calculate a zone offset for your locale.
'''
rfc822gmt = '%a, %d %b %Y %H:%M:%S -0000'
c = Calendar()
st, flag = c.parse(spec)
t = time.mktime(st)
tm = time.gmtime(t)
return time.strftime(rfc822gmt, tm)
def main(args):
defaultdate = 'today + 20 days'
tty = open('/dev/tty', 'r+')
tty.write('Expiry date ["%s", "never" to remove]: ' % defaultdate)
response = tty.readline()
tty.close()
if response == '':
# eof, ctrl-D
return 10
spec = response.strip()
if spec == '':
# use default
spec = defaultdate
if len(args):
# if filename given, read from filename
fp = open(args[0], 'r')
m = email.message_from_file(fp)
fp.close()
else:
# else stdin
m = email.message_from_file(sys.stdin)
if spec.lower() == 'never':
# remove header
del m['expires']
else:
date = fmtdate(spec)
m['Expires'] = date
if len(args):
# if filename given, write to filename
fp = open(args[0], 'w')
fp.write(m.as_string())
fp.close()
else:
# write to stdout
showfrom = m.get_unixfrom() and True or False
sys.stdout.write(m.as_string(showfrom))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))