forked from mhagander/muttutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap_control.py
executable file
·123 lines (104 loc) · 3.65 KB
/
imap_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
import datetime
import os
import time
import subprocess
import sys
from ConfigParser import ConfigParser
from external import imaplib2
class IdleSpinner(object):
def __init__(self, server, username, password):
self.server = server
self.username = username
self.password = password
self.ignoreidle = False
self.triggered = False
self.imapconn = None
self.connect()
self.start_idle()
def connect(self):
if self.imapconn:
try:
self.imapconn.logout()
except:
pass
self.imapconn = imaplib2.IMAP4_SSL(self.server)
self.imapconn.login(self.username, self.password)
self.imapconn.select('INBOX')
def start_idle(self):
def callback(args):
if self.ignoreidle:
print "Ignored one triggered imap idle event"
return
print "Triggered on one imap idle event"
self.triggered = True
# Whenever we send a new IDLE command, it will trigger a previous
# one for timeout. Thus, we need to ignore new events caused by this.
# There is a race condition here, but let's claim it's tiny.
self.ignoreidle = True
done = False
while not done:
try:
print "Calling imap idle"
self.imapconn.idle(callback=callback, timeout=30)
print "Did call imap idle"
done = True
except Exception, ex:
print "Server closed connection, reopening"
self.connect()
# Loop back up and try again with the idle command
self.ignoreidle = False
self.last_idle_start = datetime.datetime.now()
print "Finished starting idle thread"
def tick(self):
if datetime.datetime.now() - self.last_idle_start > datetime.timedelta(minutes=1):
print "Idle expired, restart just to be on the safe side"
# Complete hang when trying to log out - so just drop the old
# connection and see what happens instead...
self.imapconn = None
self.connect()
self.start_idle()
def clear(self):
self.triggered = False
# Global variables (yuck, but this is a hack..) keeping the time of the last
# full and quick synchronizations done.
last_full = datetime.datetime(2000,1,1,0,0,0)
def full_sync():
global last_full, account_name
print "Running full sync"
subprocess.call(['offlineimap', '-a', account_name, '-o', '-u', 'basic'])
last_full = datetime.datetime.now()
def quick_sync():
# Sync just the inbox (though not in "quick mode" according to
# offlineimap, we want to include everything in the sync. We're
# only syncing a single folder, after all)
global account_name
print "Running quick sync"
subprocess.call(['offlineimap', '-a', account_name, '-o', '-f', 'INBOX', '-u', 'basic'])
# Basic operation: spinner controls inbox pull every <n> seconds, or when IDLE
# indicates something happened.
# Full poll run every 5 minutes.
if __name__=="__main__":
cp = ConfigParser()
cp.read(os.path.expanduser('~/.muttutil'))
account_name = cp.get('offlineimap', 'accountname')
cp = ConfigParser()
cp.read(os.path.expanduser('~/.offlineimaprc'))
remote_repository = cp.get('Account %s' % account_name, 'remoterepository')
server = cp.get('Repository %s' % remote_repository, 'remotehost')
port = cp.get('Repository %s' % remote_repository, 'remoteport')
username = cp.get('Repository %s' % remote_repository, 'remoteuser')
password = cp.get('Repository %s' % remote_repository, 'remotepass')
spinner = IdleSpinner(server, username, password)
# Loop forever polling when it's time
while True:
now = datetime.datetime.now()
print "Time since last full sync: %s, quick sync requested: %s" % (now-last_full, spinner.triggered)
if now - last_full > datetime.timedelta(minutes=5):
full_sync()
elif spinner.triggered:
quick_sync()
spinner.clear()
spinner.start_idle()
spinner.tick()
time.sleep(1)