#!%__python
#
#    imap2mbox.py sample script
#
#    Copyright (C) 2008/2009 Michele <o-zone@zerozone.it> Pinassi
#
#    This software is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This software 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
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

#
# This is a sample script (fully usable) to show you how to use imap2mbox class :-)
#

import getpass, imaplib, os, email, sys, getopt, re
from optparse import OptionParser
from imap2mbox import imap2mbox

if __name__ == '__main__':
    # Parse commandline options
    parser = OptionParser()
    parser.add_option("-m", "--mailsrv", default=None,help="Mailserver to connect to")
    parser.add_option("-p", "--port", type="int",help="Port to connect to")
    parser.add_option("-u", "--username", default=getpass.getuser(), help="Username (defaults to current user)")
    parser.add_option("-w", "--password", default=None, help="Specify Password (less secure)")
    parser.add_option("-f", "--folder", default="*", help="Select folders to download (wildcards supported)")
    parser.add_option("-s", "--ssl", action="store_true", help="Enable SSL")		  
    parser.add_option("-v", "--verbose", action="store_true", help="Be verbose (debug)")		  
    parser.add_option("-r", "--readonly", action="store_true", help="Do not mark messages as read")		  
    parser.add_option("-t", "--test", action="store_true", help="Test Mode")
    parser.add_option("-o", "--output", default="mbox", help="How to save mails ? mbox (default) or html (save text and mime attachments)")
    parser.add_option("", "--fromdate", default=None, help="Start date (format: YYYY-MM-DD)")
    parser.add_option("", "--todate", default=None, help="To date (format: YYYY-MM-DD)")
    parser.add_option("", "--markdelete", action="store_true", help="Mark as deleted all saved messages")		  

    (options, args) = parser.parse_args()

    if options.mailsrv == None:
	parser.error("Argument 'mailsrv' missing: -m [your_mail_server]")

    if options.username == None:
	parser.error("Argument 'username' missing: -u [your_username]")

    if options.password == None:
	options.password = getpass.getpass()
    
    i2m = imap2mbox.imap2mbox()
    
    i2m.imapHost = options.mailsrv
    if options.port:
	i2m.imapPort = options.port

    i2m.imapUser = options.username	
    i2m.imapPass = options.password

    if options.fromdate:
	userDate = options.fromdate.split('-')
	i2m.imapFromDate = datetime(int(userDate[0]),int(userDate[1]),int(userDate[2]))

    if options.todate:
	userDate = options.todate.split('-')
	i2m.imapToDate = datetime(int(userDate[0]),int(userDate[1]),int(userDate[2]))

    if options.output:
	i2m.imapOutput = options.output

    if options.markdelete:
	i2m.isMarkDelete = True

    if options.ssl:
	i2m.isSsl = True

    if options.verbose:
	i2m.isVerbose = True

    if options.readonly:
	i2m.isReadonly = True

    if i2m.doLogin():
	if options.test:
	    i2m.printSummary()
	else:
	    i2m.doSave()	
	    
	i2m.doLogout()
    else:
	print i2m.imapError
    
    
    