#!/usr/local/bin/python
# coding: iso-8859-1
# Copyright © 2008 by Amos Newcombe
# 
# 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 3 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 along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''Add email headers to stdin for piping to sendmail.
'''

import sys, os
from commander import commander

class mailer(commander):
	
	# ¤ Metadata
	
	sUsage = 'Add email headers to stdin for piping to sendmail'
	sVersion = '%prog 2008.06.22.0'
	
	# ¤ Creation
	
	pathAddresses = os.path.expanduser('~/Documents/maileraddresses.tsv')
	dAddresses = dict([s.strip().split('\t', 1) for s in open(pathAddresses, 'rU')])
	
	def options(self):
		'Populate the option parser (self.optParser) with available options.'
		# Example:
		sSubjectDefault = 'mailer.py'
		self.optParser.add_option('-s', '--subject',
			action='store', dest='sSubject', type='str', default=sSubjectDefault,
			metavar='"SUBJECT"', 
			help='add "Subject: SUBJECT" (default %s)' % sSubjectDefault,
		)
	
	def ResolveOptions(self):
		'Interpret the command line options.'
		self.lsAddresses = [
			self.dAddresses[s.lower()] 
			for s in self.lsArgs 
			if self.dAddresses.has_key(s.lower())
		]
	
	def main(self):
		print '\n'.join([
			'Subject: %s' % self.opt.sSubject,
			'To: %s' % ', '.join(self.lsAddresses),
			'',
			sys.stdin.read(),
		])

if __name__ == '__main__':
	
	mailer(sys.argv).main()
