#!/usr/bin/env python
#-*- coding:utf-8 -*-

# import mailboxresource
from mailboxresource import MailboxClient
import argparse


def main():
    argparser = argparse.ArgumentParser(description="Dump a IMAP folder into .eml files")
    argparser.add_argument('-s', dest='host', help="IMAP host, like imap.gmail.com", required=True)
    argparser.add_argument('-u', dest='username', help="IMAP username", required=True)
    argparser.add_argument('-p', dest='password', help="IMAP password", required=True)
    argparser.add_argument('-r', dest='remote_folder', help="Remote folder to download", default='INBOX')
    argparser.add_argument('-l', dest='local_folder', help="Local folder where to create the email folders", default='.')
    args = argparser.parse_args()

    mailbox = MailboxClient(args.host, args.username, args.password, args.remote_folder)
    mailbox.copy_emails(args.local_folder)
    mailbox.cleanup()


if __name__ == '__main__':
    main()

@@ -125,461 +125,1283 @@
 rse%0A
+import ConfigParser, os
 %0A%0A
+%0A
 def 
-main():%0A    argparser = argparse.ArgumentParser(description=%22Dump a IMAP folder into .eml files%22)%0A    argparser.add_argument('-s', dest='host', help=%22IMAP host, like imap.gmail.com%22, required=True)%0A    argparser.add_argument('-u', dest='username', help=%22IMAP username%22
+load_configuration(args):%0A    config = ConfigParser.ConfigParser(allow_no_value=True)%0A    config.read(%5B'/etc/imapbox/config.cfg', os.path.expanduser('~/.config/imapbox/config.cfg')%5D)%0A%0A    options = %7B%0A        'local_folder': '/var/imapbox',%0A        'accounts': %5B%5D%0A    %7D%0A%0A    if (config.has_section('imapbox')):%0A        options%5B'local_folder'%5D = config.get('imapbox', 'local_folder')%0A%0A    for section in config.sections():%0A%0A        if ('imapbox' == section):%0A            continue%0A%0A        account = %7B%0A            'name': section,%0A            'remote_folder': 'INBOX'%0A        %7D%0A%0A        account%5B'host'%5D = config.get(section, 'host')%0A        account%5B'username'%5D = config.get(section, 'username')%0A        account%5B'password'%5D = config.get(section, 'password')%0A%0A        if (config.has_option(section
 , 
+'
 re
-quired=True)%0A    argparser.add_argument('-p', dest='password', help=%22IMAP password%22, required=True)%0A    argparser.add_argument('-r', dest='remote_folder', help=%22Remote
+mote_folder')):%0A            account%5B'remote_folder'%5D = config.get(section, 'remote_folder')%0A%0A        if (None == account%5B'host'%5D or None == account%5B'username'%5D or None == account%5B'password'%5D):%0A            continue%0A%0A        options%5B'accounts'%5D.append(account)%0A%0A    if (args.local_folder):%0A        options%5B'local_folder'%5D = args.local_folder%0A%0A    return options%0A%0A%0A%0A%0Adef main():%0A    argparser = argparse.ArgumentParser(description=%22Dump a IMAP
  folder 
 to d
@@ -1400,37 +1400,24 @@
 der 
+in
 to 
-download%22, default='INBOX'
+.eml files%22
 )%0A  
@@ -1570,17 +1570,100 @@
 _args()%0A
-%0A
+    options = load_configuration(args)%0A%0A    for account in options%5B'accounts'%5D:%0A    
     mail
@@ -1687,31 +1687,41 @@
 nt(a
-rgs.
+ccount%5B'
 host
+'%5D
 , a
-rgs.
+ccount%5B'
 username
 , ar
@@ -1720,23 +1720,29 @@
 name
+'%5D
 , a
-rgs.
+ccount%5B'
 password
 , ar
@@ -1741,15 +1741,21 @@
 word
+'%5D
 , a
-rgs.
+ccount%5B'
 remo
@@ -1759,26 +1759,32 @@
 emote_folder
+'%5D
 )%0A
+    
     mailbox.
@@ -1795,21 +1795,25 @@
 _emails(
-args.
+options%5B'
 local_fo
@@ -1816,18 +1816,24 @@
 l_folder
-)%0A
+'%5D)%0A    
     mail

Load multiple accounts using a config file