home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Demo / scripts / mboxconvert.py < prev    next >
Encoding:
Python Source  |  1994-09-05  |  2.2 KB  |  114 lines  |  [TEXT/R*ch]

  1. #! /usr/local/bin/python
  2.  
  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)
  6.  
  7. import rfc822
  8. import sys
  9. import time
  10. import os
  11. import stat
  12. import getopt
  13. import regex
  14.  
  15. def main():
  16.     dofile = mmdf
  17.     try:
  18.         opts, args = getopt.getopt(sys.argv[1:], 'f')
  19.     except getopt.error, msg:
  20.         sys.stderr.write('%s\n' % msg)
  21.         sys.exit(2)
  22.     for o, a in opts:
  23.         if o == '-f':
  24.             dofile = message
  25.     if not args:
  26.         args = ['-']
  27.     sts = 0
  28.     for arg in args:
  29.         if arg == '-' or arg == '':
  30.             sts = dofile(sys.stdin) or sts
  31.         elif os.path.isdir(arg):
  32.             sts = mh(arg) or sts
  33.         elif os.path.isfile(arg):
  34.             try:
  35.                 f = open(arg)
  36.             except IOError, msg:
  37.                 sys.stderr.write('%s: %s\n' % (arg, msg))
  38.                 sts = 1
  39.                 continue
  40.             sts = dofile(f) or sts
  41.             f.close()
  42.         else:
  43.             sys.stderr('%s: not found\n' % arg)
  44.             sts = 1
  45.     if sts:
  46.         sys.exit(sts)
  47.  
  48. numeric = regex.compile('[1-9][0-9]*')
  49.  
  50. def mh(dir):
  51.     sts = 0
  52.     msgs = os.listdir(dir)
  53.     for msg in msgs:
  54.         if numeric.match(msg) != len(msg):
  55.             continue
  56.         fn = os.path.join(dir, msg)
  57.         try:
  58.             f = open(fn)
  59.         except IOError, msg:
  60.             sys.stderr.write('%s: %s\n' % (fn, msg))
  61.             sts = 1
  62.             continue
  63.         sts = message(f) or sts
  64.     return sts
  65.  
  66. def mmdf(f):
  67.     sts = 0
  68.     while 1:
  69.         line = f.readline()
  70.         if not line:
  71.             break
  72.         if line == '\1\1\1\1\n':
  73.             sts = message(f, line) or sts
  74.         else:
  75.             sys.stderr.write(
  76.                 'Bad line in MMFD mailbox: %s\n' % `line`)
  77.     return sts
  78.  
  79. def message(f, delimiter = ''):
  80.     sts = 0
  81.     # Parse RFC822 header
  82.     m = rfc822.Message(f)
  83.     # Write unix header line
  84.     fullname, email = m.getaddr('From')
  85.     tt = m.getdate('Date')
  86.     if tt:
  87.         t = time.mktime(tt)
  88.     else:
  89.         sys.stderr.write(
  90.             'Unparseable date: %s\n' % `m.getheader('Date')`)
  91.         t = os.fstat(f.fileno())[stat.ST_MTIME]
  92.     print 'From', email, time.ctime(t)
  93.     # Copy RFC822 header
  94.     for line in m.headers:
  95.         print line,
  96.     print
  97.     # Copy body
  98.     while 1:
  99.         line = f.readline()
  100.         if line == delimiter:
  101.             break
  102.         if not line:
  103.             sys.stderr.write('Unexpected EOF in message\n')
  104.             sts = 1
  105.             break
  106.         if line[:5] == 'From ':
  107.             line = '>' + line
  108.         print line,
  109.     # Print trailing newline
  110.     print
  111.     return sts
  112.  
  113. main()
  114.