#!/usr/bin/env python

"""CouchMail.py imports your mail from an IMAP server into a CouchDB database

License: Apache 2.0 - http://opensource.org/licenses/Apache-2.0
"""

import calendar
import ConfigParser
from datetime import datetime
import os
import email
import mimetypes
import getpass

import easyimap
from dateutil.parser import parse
from couchdb import Server, ResourceConflict, ResourceNotFound
from couchmail import archive_msg, headers, truly_unique_id

config = ConfigParser.RawConfigParser()
config.read('config.ini')

# CouchDB/Cloudant setup
server = Server(config.get('couch', 'server'))
couch = server[config.get('couch', 'db')]

# IMAP setup
host = config.get('imap', 'host')
user = config.get('imap', 'user')
try:
    password = config.get('imap', 'password')
except ConfigParser.NoOptionError:
    password = getpass.getpass("Password for %s on %s: " % (user, host))
mailbox = config.get('imap', 'mailbox')

if __name__ == '__main__':
    print 'Connecting...'
    imapper = easyimap.connect(host, user, password, mailbox, read_only=True)
    print 'Connected. Couple more questions...'
    amount = int(raw_input('How many mail items would you like to archive? '))
    should_skip = raw_input('Skip mail already archived (y/n)? ')
    if should_skip.lower() in ["y", "yes", "yea", "si", "go", "aye", "sure"]:
      should_skip = True
    else:
      should_skip = False
    # Loop through the messages and add them to CouchDB
    ids = imapper.listids(amount)
    for id in ids:
        msg = imapper.mail(id, include_raw=True)
        unique_id = truly_unique_id(headers(msg))
        try:
            current_doc = couch[unique_id]
            if should_skip:
                print 'Skipping ' + unique_id + ' (already archived)'
                continue
            else:
                # archive document exists, but user wants to overwrite it
                archive_msg(couch, msg)
        except ResourceNotFound:
            # add the msg to the archive
            archive_msg(couch, msg)

@@ -164,16 +164,32 @@
 .0%0A%22%22%22%0A%0A
+import argparse%0A
 import c
@@ -476,16 +476,201 @@
 que_id%0A%0A
+argparser = argparse.ArgumentParser()%0Aargparser.add_argument('config_file', type=file,%0A        help=%22Config INI file. See %60config.sample.ini%60 for info.%22)%0Aargs = argparser.parse_args()%0A%0A
 config =
@@ -716,21 +716,27 @@
 read
-('
+fp(args.
 config
-.ini'
+_file
 )%0A%0A#

Add config_file param; require it