#!/usr/bin/env python
# 
# notifyxenomail - The notify script for xenomail
# Copyright (C) 2005 Jason Chu <jason@archlinux.org>
# 
#   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
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 

import xenopy.config
import xenopy.log
import xenopy.jabber
import xenopy.rpc
import email
import sys
import time
import os
import os.path

done = False

def main(argv=None):
	global notes, account

	if argv is None:
		argv = sys.argv

	xenopy.log.init('notifyxenomail')

	account = xenopy.config.getAccount()
	if account == None:
		xenopy.log.error("Error getting account info.  Remember to fill in ~/.xenophile/account")
		return 1

	# account[0] = username
	xenopy.jabber.init(account[0])

	# account[1] = password
	xenopy.jabber.connect(account[1], True, False)
	xenopy.rpc.registerRPC()

	sp = xenopy.rpc.ServerProxy(xenopy.jabber.jid.getStripped() + '/xenophile/mail')
	em = email.message_from_file(sys.stdin)
	try:
		sp.emailNotify(sys.argv[1], em["to"], em["from"], em["subject"])
	except:
		# Don't let an exception ruin your day
		pass

	return 0

if __name__ == "__main__":
	sys.exit(main())
