#!/usr/bin/env python

# oft = Output-From-To, part of the herbrip test suite
#
##########################################################
# Copyright (c) 2001 Philip Hunt. See COPYING for details
# of licensing information.
##########################################################

"""***
Usage:
   oft messageFilename
   
Oft reads the message file, then writes it to a file in the
current directory, with a filename of the form:

   "messo_(f1)(t1)"
   
Where:
   (f1) is converted to the 1st letter of the sender's email address
   (t1) is converted to the 1st letter of the recipient's email address

***"""

#***** python standard libraries:
import sys
import os.path

srcDir = os.path.abspath("../src")
sys.path.append(srcDir)

import utility
import mailheader

debug = 0

#---------------------------------------------------------------------

def main():
   fn = sys.argv[1]
   print "### oft %s ###" % fn
   
   mailStr = utility.readFile(fn)
   if debug: print "************** mailStr = [%s]" % mailStr
   m = mailheader.makeMailFromString(mailStr)
   mh = m.header
   if debug: print "************** mh = [%s]" % mh
   
   # get from
   fromAddr = mh.getaddr("From")[1]
   if debug: print "fromAddr = [%s]" % fromAddr
   f1 = fromAddr[:1]
   if debug: print "f1 = [%s]" % f1
   
   # get to
   toAddr = mh.getaddr("To")[1]
   if debug: print "toAddr = [%s]" % toAddr
   t1 = toAddr[:1]
   if debug: print "t1 = [%s]" % t1
   
   # build output filename
   outFn = "messo_%s%s" % (f1, t1)
   utility.writeFile(outFn, mailStr)
   
main()

#end
