-
Notifications
You must be signed in to change notification settings - Fork 0
/
testidle.py
149 lines (122 loc) · 4.76 KB
/
testidle.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from twisted.internet.ssl import ClientContextFactory
from twisted.mail import imap4
from twisted.internet import protocol, defer
from datetime import datetime
import termstyle
Command = imap4.Command
class IMAPFolderListProtocol(imap4.IMAP4Client):
idle_folder = None
_keepalive = None
'''
def lineReceived(self, line):
if 'IDLE' in line or 'DONE' in line: print termstyle.red('DEBUG lineReceived-\n\t%s' % line)
return imap4.IMAP4Client.lineReceived(self, line)
#return super(IMAPFolderListProtocol, self).lineReceived(line)
#return self.__super.lineReceived(line)
def dispatchCommand(self, tag, rest):
print termstyle.red('DEBUG dispatchCommand-\n\ttag: %s\n\trest: %s' % (tag, rest))
return imap4.IMAP4Client.dispatchCommand(self, tag, rest)
#return super(IMAPFolderListProtocol, self).dispatchCommand(tag, rest)
#return self.__super.dispatchCommand(tag, rest)
'''
def serverGreeting(self, capabilities):
login = self.login(self.factory.username, self.factory.password)
login.addCallback(self.__loggedIn)
login.chainDeferred(self.factory.deferred)
def __loggedIn(self, results):
self.state = 'AUTH'
return self.IDLE('INBOX/MIEN/')
def IDLE(self, folder):
self.idle_folder = folder
return self.select(folder).addCallback(self.__initIDLE)
def __initIDLE(self, *args, **kwargs):
print '__initIDLE'
print 'args ', args
print 'kwargs', kwargs
cmd = 'IDLE'
resp = ('IDLE',)
d = self.sendCommand(Command(cmd, wantResponse=resp, continuation=self.__cbIDLE))
d.addCallback(self.__cbterminateIDLE)
return d
def response_IDLE(self, tag, rest):
print termstyle.blue('%s - DEBUG response_IDLE' % datetime.now())
print termstyle.blue('\ttag- %s' % tag)
print termstyle.blue('\trest - %s' % rest)
return tag, rest
def IDLEDone(self):
print termstyle.yellow('running IDLEDone')
self.state = self._prev_state
del self._prev_state
cmd = 'DONE'
cmd = Command(cmd)
cmd.defer = defer.Deferred()
self.sendLine(cmd.command)
return cmd.defer
def __cbterminateIDLE(self, *args, **kwargs):
print termstyle.cyan('__cbterminateIDLE')
print termstyle.cyan(args)
print termstyle.cyan(kwargs)
return self.logout()
def __cbIDLE(self, rest):
print termstyle.green('DEBUG- __cbIDLE\n\trest: %s' % rest)
if self.state != 'IDLE' and 'accepted, awaiting DONE command' in rest or 'idling' in rest:
print termstyle.green('idle start conditions found')
#print self.waiting
#print self._lastCmd
#print self.tags
self._prev_state = self.state
self.state = 'IDLE'
#self.keepAlive()
reactor.callLater( 300, self.IDLEDone)
return
d = reactor.callLater( 30, self.IDLEDone)
return d
else:
print termstyle.green('idle continuation')
return self.response_IDLE('+', rest)
def keepAlive(self):
reactor.callLater(20, self.keepAlive)
return self.noop()
def connectionLost(self, reason):
if not self.factory.deferred.called:
# connection was lost unexpectedly!
self.factory.deferred.errback(reason)
class IMAPFolderListFactory(protocol.ClientFactory):
protocol = IMAPFolderListProtocol
def __init__(self, username, password):
self.username = username
self.password = password
self.deferred = defer.Deferred( )
def stopFactory(self):
print termstyle.magenta(termstyle.bold('stopping factory'))
return
def clientConnectionFailed(self, connection, reason):
self.deferred.errback(reason)
if __name__ == "__main__":
from twisted.internet import reactor
import sys, getpass
termstyle.enable()
def printMailboxList(*args, **kwargs):
print 'DEBUG- printMailboxList'
print args
print kwargs
#list.sort( )
#for box in list:
#print box
#reactor.stop( )
def handleError(error):
print >> sys.stderr, "Error:", error.getErrorMessage( )
reactor.stop( )
if not len(sys.argv) == 3:
print "Usage: %s server login" % sys.argv[0]
sys.exit(1)
server = sys.argv[1]
user = sys.argv[2]
password = getpass.getpass("Password: ")
factory = IMAPFolderListFactory(user, password)
factory.deferred.addCallback(
printMailboxList).addErrback(
handleError)
reactor.connectTCP(server, 143, factory)
#icon = reactor.connectSSL(server, 993, factory, ClientContextFactory())
reactor.run( )