#!/usr/bin/python # BeOS to mbox converter # # Written by: Colin Stewart Copyright 2003 # # WARNING: This software might eat your email - use after taking backups # of the relevant files! import sys, os, os.path, re # Get a list of all files from the given directory # Read in each file # Retrieve the X-From_: line for use as the 'From ' seperator # In a loop check for a match of the regex '^>*From '. If present then escape. # If your mail is already mostly escaped (mine was) then leave as is, otherwise # use the first regex and comment out the second one. #ESCAPE_REGEX=re.compile ('^>*From ') ESCAPE_REGEX=re.compile ('^From ') XFROM_LINE_REGEX=re.compile ('^X-From_: ') DATE_LINE_REGEX=re.compile ('^Date: ') RECIEVED_DATE_REGEX=re.compile ('Received: [^;]*; ') class BeMailToMbox: def __init__ (self, sourceDir, destFile): self.sourceDir = sourceDir self.destFile = destFile if (not os.path.isdir (sourceDir)): raise Exception ("Provided source directory is not a directory!") def convert (self): print "Starting conversion process." candidateFiles = os.listdir (self.sourceDir) self.outputFile = open (self.destFile, 'w') count = 0 for candidate in candidateFiles: beosMailFile = os.path.join (self.sourceDir, candidate) if (candidate == "Mail-It.cache"): print "Skipping cache file." elif (not os.path.isfile (beosMailFile)): print "WARNING: %s is not a file - skipping." % beosMailFile else: self.process (beosMailFile) count += 1 self.outputFile.close() print "Finished - wrote %s messages to mbox" % str (count) def process (self, mailFilename): mailFile = open (mailFilename, 'r') firstLine = 1 mailLines = [] fromLine = None for line in mailFile.readlines(): if (firstLine): match = XFROM_LINE_REGEX.match (line) if (match is not None): fromLine = line[match.end():] firstLine = 0 elif (fromLine is None): match = DATE_LINE_REGEX.match (line) if (match is not None): fromLine = 'BeosMailToMbox@localhost %s' % line[match.end():] else: match = RECIEVED_DATE_REGEX.match (line) if (match is not None): fromLine = 'BeosMailToMbox@localhost %s' % line[match.end():] if (ESCAPE_REGEX.match (line)): print "Escaping line: %s" % line mailLines.append ('>%s' % line) else: mailLines.append (line) # Finished this mail if (fromLine is None): raise Exception ("No from line found in email %s" % mailFilename) self.outputFile.write ('From %s' % fromLine) self.outputFile.write (''.join (mailLines)) mailFile.close() # Usage: BeosMailToMbox.py dir mboxfile if (len (sys.argv) != 3): print "Usage: BeosMailToMbox.py dir mboxfile" sys.exit (0) try: conv = BeMailToMbox (sys.argv[1], sys.argv[2]) conv.convert() except Exception, e: print "Error during conversion: %s" % str (e)