aboutsummaryrefslogtreecommitdiffstats
path: root/climl_imap_bridge.py
diff options
context:
space:
mode:
Diffstat (limited to 'climl_imap_bridge.py')
-rw-r--r--climl_imap_bridge.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/climl_imap_bridge.py b/climl_imap_bridge.py
index c0ddb82..be0864a 100644
--- a/climl_imap_bridge.py
+++ b/climl_imap_bridge.py
@@ -4,7 +4,7 @@ import os
import subprocess
import confparser
import imapclient
-
+import contextlib
import backports.ssl
def conf_boolean_postprocess(src):
@@ -23,6 +23,7 @@ def conf_postprocess(conf):
return {key: d[key](value) if key in d else value
for key, value in conf.items()}
+@contextlib.contextmanager
def connect_to_imap(conf, password):
cafile = conf.get('imap.tls_ca', None)
@@ -48,7 +49,16 @@ def connect_to_imap(conf, password):
print('connection succeed')
connection.login(username=conf.get('imap.username'), password=password)
print('successfuly logged')
- return connection
+
+ try:
+ yield connection
+ except IMAPClient.AbortError, socket.error, socket.timeout, \
+ backports.ssl.SSLError, backports.ssl.CertificateError:
+ raise
+ else:
+ connection.logout()
+ #finally:
+ # connection.shutdown()
def main():
confpath = os.path.expanduser('~/temp/conf.cfg')
@@ -59,8 +69,16 @@ def main():
password = subprocess.check_output(password_command, shell=True)
password = password.rstrip().decode('utf8')
print('got pasword:', password)
- connection = connect_to_imap(conf, password)
+ #connection = connect_to_imap(conf, password)
+ with connect_to_imap(conf, password) as connection:
+ print('entering idle mode')
+ connection.idle()
+ print('waiting for an event')
+ result = connection.idle_check(timeout=10)
+ print('result:', result)
+
+ print('end of connection')
main() # dev mode for now