This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimpleimap.py
161 lines (116 loc) · 3.51 KB
/
simpleimap.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
150
151
152
153
154
155
156
157
158
159
160
161
"""
This code was adapted from iGmail http://butterfat.net/igmail/ Copyright (C)
2006 Brian Muller <[email protected]> which was in turn adapted from
"Twisted Network Programming Essentials" by Abe Fettig (ISBN 0-596-10032-9)
as published by O'Reilly.
All changes and additional code are Copyright (C) 2010 Devon Jones
<[email protected]> and available under the terms of the GPL Version 2.
"""
import sys
from optparse import OptionParser
from twisted.cred import portal, checkers, credentials
from twisted.mail import imap4
from twisted.internet import protocol, reactor
from twisted.python import log, logfile
class Account:
__implements__ = (imap4.IAccount,)
def addMailbox(self, name, mbox=None):
pass
def create(self, pathspec):
pass
def select(self, name, rw=True):
pass
def delete(self, name):
pass
def rename(self, oldname, newname):
pass
def isSubscribed(self, name):
pass
def subscribe(self, name):
pass
def unsubscribe(self, name):
pass
def listMailboxes(self, ref, wildcard):
pass
class Mailbox:
__implements__ = (imap4.IMailbox,)
def getUIDValidity(self):
pass
def getUIDNext(self):
pass
def getUID(self, message):
pass
def getMessageCount(self):
pass
def getRecentCount(self):
pass
def getUnseenCount(self):
pass
def isWriteable(self):
pass
def destroy(self):
pass
def requestStatus(self, names):
pass
def addListener(self, listener):
pass
def removeListener(self, listener):
pass
def addMessage(self, message, flags=(), date=None):
pass
def expunge(self):
pass
def fetch(self, messages, uid):
pass
def store(self, messages, flags, mode, uid):
pass
class IMAPServerProtocol(imap4.IMAP4Server):
"Subclass of imap4.IMAP4Server that adds debugging."
debug = False
def lineReceived(self, line):
if self.debug:
print "CLIENT:", line
imap4.IMAP4Server.lineReceived(self, line)
def sendLine(self, line):
imap4.IMAP4Server.sendLine(self, line)
if self.debug:
print "SERVER:", line
class IMAPFactory(protocol.Factory):
protocol = IMAPServerProtocol
portal = None # placeholder
def buildProtocol(self, address):
p = self.protocol()
p.portal = self.portal
p.factory = self
return p
class NoopCredentialsChecker:
__implements__ = (checkers.ICredentialsChecker,)
credentialInterfaces = (credentials.IUsernamePassword, credentials.IUsernamePassword)
def requestAvatarId(self, credentials):
return checkers.ANONYMOUS
def logout():
pass
# Cleanup logic goes here
class MailUserRealm:
__implements__ = (portal.IRealm,)
def requestAvatar(self, avatarID, mind, *interfaces):
if imap4.IAccount not in interfaces:
raise NotImplementedError
return imap4.IAccount, Account(), logout
def main():
usage = "usage: %prog [options]\n\n"
parser = option_parser(usage)
(options, args) = parser.parse_args()
port = 143
log.startLogging(sys.stdout)
p = portal.Portal(MailUserRealm())
p.registerChecker(NoopCredentialsChecker())
factory = IMAPFactory()
factory.portal = p
reactor.listenTCP(port, factory)
reactor.run()
def option_parser(usage):
parser = OptionParser(usage=usage)
return parser
if __name__ == "__main__":
main()