#!/usr/bin/python

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

#
# Usage Notes:
#
#  send_yahoo is a fetchmail replacement which, like Perl's fetchyahoo,
#  pulls emails through your Yahoo! webmail account.  It expects this
#  line in ~/.netrc:
#
#   machine mail.yahoo.com login your_username password your_password
#
#  Or you can invoke it with:
#
#        pop_yahoo --user: your_username --passwd: your_password
#
#  Email is pulled as plain text or mime/multipart to the MBOX setting
#  in this script.  If you like HTML email, you will not pop_yahoo will
#  annoy you, because it ruthlessly removes all HTML from incoming
#  messages.
#
#  You are encouraged to send any debug.htmls generated by this script
#  to its author.
#  

import os, sys

try:
	import mechanoid
except:
	print ("""
	pop_yahoo requires mechanoid, a free software 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
	b = mechanoid.Browser()
	y = MailDotYahoo(b, ("--debug" in sys.argv),
					 ("--silent" not in sys.argv),
					 ("--nodelete" not in sys.argv))
	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"))
	raise
