#!/usr/bin/python

# mg::executable for the MailGoose package
# goosequill@users.sourceforge.net
# http://goosequill.sourceforge.net

# Copyright (C) 2004 by Richard Harris
# Released under the GNU General Public License
# (See the included COPYING file)

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import os, sys
import lib_rharris

class mg:
	NAME = "MailGoose"
	AUTHOR = "Richard Harris"
	CPRT = "2003 - 2005"
	CONSTS = {
		"BROWSER":"/usr/local/bin/w3m",
		"DIR_D":os.path.expandvars("$HOME/Mail/goose/drafts"),
		"DIR_G":os.path.expandvars("$HOME/Mail/goose"),
		"EDITOR":"vi",
		"SIGNATURE":os.path.expandvars('$HOME/.signature'),
		"MAILRC":os.path.expandvars('$HOME/.mailrc'),
		"MGRC":os.path.expandvars('$HOME/.mgrc'),
		"SMTP":'localhost',
		"TMPF":"mg",
		}
	USAGE = '''
usage: mg -b [filename]|d|n alias|r [filename]|s 
		  -b [filename]
			  browses URLs in mbox file
			  (/var/spool/mail/$USER by default)
		  -d edits drafts in ~/Mail/goose/drafts
		  -n someone@some.com OR alias
			  write new message to someone|alias
			  alias can be any name OR an alias from ~/.mailrc
		  -r [filename]
			  create reply-draft fo email msg copy
			  (filename defaults to mg)
		  -s send pending geese	  
'''

	def __init__(self):
		self.lib = lib_rharris.lib_rharris()
		self.params, self.options = self.lib.get_args()
		self.debug = ("debug" in self.options)
		if (os.path.exists(self.CONSTS["MGRC"])):
			lines = self.lib.get_lines(self.CONSTS["MGRC"])
			for line in lines:
				tokens = self.lib.tokens(line,":")
				if (len(tokens) == 2):
					if (self.CONSTS.has_key(tokens[0])):
						self.CONSTS[tokens[0]] = tokens[1]
		return

	def __usage(self):
		if ("?" in self.options) or (len(self.options) == 0):
			print self.USAGE
			sys.exit(1)
		return

	def run(self):
		try:
			self.__usage()
			self.lib.entrance()
			opt = self.options[0]
			if (opt == "b"):
				if (self.params == []):
					mbox = os.path.expandvars("/var/spool/mail/$USER")
				else:
					mbox = self.params[0]
				from MailGoose import BrowseGoose
				bg = BrowseGoose.BrowseGoose(self.lib, self.CONSTS, self.debug)
				bg.run(mbox)
			elif (opt == "d"):
				from MailGoose import DraftsGoose
				dg = DraftsGoose.DraftsGoose(self.lib, self.CONSTS, self.debug)
				dg.run()
			elif (opt == "n"):
				if (len(self.params) != 1):
					self.__usage()
				else:	
					from MailGoose import NewGoose
					ng = NewGoose.NewGoose(self.lib, self.CONSTS, self.params, self.options, self.debug)
					ng.run(self.params[0])
			elif (opt == "r"):
				if (self.params == []):
					reply = "mg"
				else:
					reply = self.params[0]
				from MailGoose import ReplyGoose
				rg = ReplyGoose.ReplyGoose(self.lib, self.CONSTS, self.debug)
				rg.run(reply)
			elif (opt == "s"):
				from MailGoose import MailGoose
				g = MailGoose.MailGoose(self.lib, self.CONSTS, self.debug)
				g.run()
			else:
				self.__usage()
			self.lib.credits(self.NAME+" - Byte the Honker", self.AUTHOR, self.CPRT)
			self.lib.exit()
		except SystemExit:
			pass
		except:
			self.lib.exc(self.NAME)	
		return

if __name__ == "__main__":
	obj = mg()
	obj.run()

