#!/usr/bin/python

# pop_yahoo
# Copyright (C) 2005 by Richard Harris
# truenolejano@yahoo.com 
# 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

DEBUG = 0
VERBOSE = 1

import os, sys

try:
	import mechanoid
except:
	print ("""
	pop_yahoo requires mechanoid, an Open Source programmatic
	browser written in Python.  Download is under 200kb.
	See http://python.org/pypi/mechanoid
	""")
	sys.exit()

try:
	from mechanoid.sites import MailDotYahoo
except:
	print ("""
	Your mechanoid version is too old and does not have the sites
	package. 	See http://python.org/pypi/mechanoid
	""")
	sys.exit()


## -------------------
MBOX = os.path.expandvars("$HOME/Mail/pop_yahoo")
DIR = "/tmp"

try:
	response = None
	if ("--silent" in sys.argv): VERBOSE = 0
	b = mechanoid.Browser()
	y = MailDotYahoo(b, DEBUG, VERBOSE)
	response = y.go_to()
	response = y.log_in()
	response = y.inbox()
	y.fetch_mail(MBOX, DIR)
except SystemExit:
	pass
except:
	if (response != None):
		import time
		fname = hex(int(time.time()))[2]
		b.debug_response(response, os.path.expandvars("$HOME/"+fname+".html"))
	else:	
		raise
