#!/usr/bin/python

# Copyright (c) 2014, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses

from __future__ import unicode_literals


from getpass import getpass
from optparse import OptionParser

from six import iteritems

from .config import parse_config_file, create_client_from_config, get_config_defaults


def command_line():
    p = OptionParser()
    p.add_option('-H', '--host', dest='host', action='store',
                 help='IMAP host connect to')
    p.add_option('-u', '--username', dest='username', action='store',
                 help='Username to login with')
    p.add_option('-p', '--password', dest='password', action='store',
                 help='Password to login with')
    p.add_option('-P', '--port', dest='port', action='store', type=int,
                 default=None,
                 help='IMAP port to use (default is 143, or 993 for SSL)')
    p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False,
                 help='Use SSL connection')
    p.add_option('-f', '--file', dest='file', action='store', default=None,
                 help='Config file (same as livetest)')

    opts, args = p.parse_args()
    if args:
        p.error('unexpected arguments %s' % ' '.join(args))

    if opts.file:
        if opts.host or opts.username or opts.password or opts.port or opts.ssl:
            p.error('If -f/--file is given no other options can be used')
        # Use the options in the config file
        opts = parse_config_file(opts.file)
    else:
        # Scan through options, filling in defaults and prompting when
        # a compulsory option wasn't provided.
        compulsory_opts = ('host', 'username', 'password')
        for name, default_value in iteritems(get_config_defaults()):
            value = getattr(opts, name, default_value)
            if name in compulsory_opts and value is None:
                value = getpass(name + ': ')
            setattr(opts, name, value)

    return opts


def main():
    opts = command_line()
    print('Connecting...')
    client = create_client_from_config(opts)
    print('Connected.')
    banner = '\nIMAPClient instance is "c"'

    def ipython_011(c):
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
        ipshell = InteractiveShellEmbed(banner1=banner)
        ipshell('')

    def ipython_010(c):
        from IPython.Shell import IPShellEmbed
        IPShellEmbed('', banner=banner)()

    def builtin(c):
        import code
        code.interact(banner, local=dict(c=c))

    for shell_attempt in (ipython_011, ipython_010, builtin):
        try:
            shell_attempt(client)
            break
        except ImportError:
            pass

if __name__ == '__main__':
    main()

@@ -2205,16 +2205,182 @@
 s %22c%22'%0A%0A
+    def ipython_400(c):%0A        from IPython.terminal.embed import InteractiveShellEmbed%0A        ipshell = InteractiveShellEmbed(banner1=banner)%0A        ipshell('')%0A%0A
     def 
@@ -2752,20 +2752,16 @@
 ))%0A%0A    
-for 
 shell_at
@@ -2769,13 +2769,43 @@
 empt
- in (
+s = (%0A        ipython_400,%0A        
 ipyt
@@ -2812,16 +2812,24 @@
 hon_011,
+%0A       
  ipython
@@ -2837,18 +2837,8 @@
 010,
- builtin):
 %0A   
@@ -2846,24 +2846,39 @@
     
-try:
+builtin,
 %0A    
+)%0A
     
-   
+for shell in
  she
@@ -2891,34 +2891,49 @@
 empt
-(client)%0A            break
+s:%0A        try:%0A            shell(client)
 %0A   
@@ -2973,16 +2973,48 @@
     pass
+%0A        else:%0A            break
 %0A%0Aif __n

Use correct "interact" imports for IPython 4