-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchers.py
53 lines (43 loc) · 1.25 KB
/
fetchers.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
import sys
import poplib
import email
class NothingMore(Exception):
pass
class UnknownType(Exception):
pass
class StdInFetcher(object):
def __init__(self):
self.read = False
def fetch(self):
if self.read:
raise NothingMore
else:
val = sys.stdin.read()
self.read = True
return email.message_from_string(val)
class Pop3Fetcher(object):
def __init__(self, host, user, password, ssl=False):
if ssl:
self.pop = poplib.POP3_SSL(host)
else:
self.pop = poplib.POP3(host)
self.pop.user(user)
self.pop.pass_(password)
self.mails = [line.split(" ")[0] for line in self.pop.list()[1]]
def fetch(self):
try:
mailno = self.mails.pop(0)
except IndexError:
raise NothingMore
mail = self.pop.retr(mailno)[1]
self.pop.dele(mailno)
return email.message_from_string("\n".join(mail))
def __del__(self):
self.pop.quit()
def UniversalFetcher(options):
type = options['fetcher']
del(options['fetcher'])
if type.upper() == 'POP3':
return Pop3Fetcher(**options)
else:
raise UnknownType, "%s distributor is unknown" % (type, )