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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
__all__ = [
'DirectoryMailBox',
'IMailBox',
'MailBoxError',
'POP3MailBox',
'TestMailBox',
]
import os
import poplib
import socket
import threading
from zope.interface import (
implements,
Interface,
)
from lp.services.mail import stub
class MailBoxError(Exception):
"""Indicates that some went wrong while interacting with the mail box."""
class IMailBox(Interface):
def open():
"""Opens the mail box.
Raises MailBoxError if the mail box can't be opened.
This method has to be called before any operations on the mail
box is performed.
"""
def items():
"""Returns all the ids and mails in the mail box.
Returns an iterable of (id, mail) tuples.
Raises MailBoxError if there's some error while returning the mails.
"""
def delete(id):
"""Deletes the mail with the given id.
Raises MailBoxError if the mail couldn't be deleted.
"""
def close():
"""Closes the mailbox."""
class TestMailBox:
"""Mail box used for testing.
It operates on stub.test_emails.
"""
implements(IMailBox)
def __init__(self):
self._lock = threading.Lock()
def open(self):
"""See IMailBox."""
if not self._lock.acquire(False):
raise MailBoxError("The mail box is already open.")
def items(self):
"""See IMailBox."""
id = 0
# Loop over a copy of test_emails to avoid infinite loops.
for item in list(stub.test_emails):
if item is not None:
from_addr, to_addr, raw_mail = item
yield id, raw_mail
id += 1
def delete(self, id):
"""See IMailBox."""
if id not in [valid_id for valid_id, mail in self.items()]:
raise MailBoxError("No such id: %s" % id)
# Mark it as deleted. We can't really delete it yet, since the
# ids need to be preserved.
stub.test_emails[id] = None
def close(self):
"""See IMailBox."""
# Clean up test_emails
stub.test_emails = [item for item in stub.test_emails
if item is not None]
self._lock.release()
class POP3MailBox:
"""Mail box which talks to a POP3 server."""
implements(IMailBox)
def __init__(self, host, user, password, ssl=False):
self._host = host
self._user = user
self._password = password
self._ssl = ssl
def open(self):
"""See IMailBox."""
try:
if self._ssl:
popbox = poplib.POP3_SSL(self._host)
else:
popbox = poplib.POP3(self._host)
except socket.error, e:
raise MailBoxError(str(e))
try:
popbox.user(self._user)
popbox.pass_(self._password)
except poplib.error_proto, e:
popbox.quit()
raise MailBoxError(str(e))
self._popbox = popbox
def items(self):
"""See IMailBox."""
popbox = self._popbox
try:
count, size = popbox.stat()
except poplib.error_proto, e:
# This means we lost the connection.
raise MailBoxError(str(e))
for msg_id in range(1, count + 1):
response, msg_lines, size = popbox.retr(msg_id)
yield (msg_id, '\n'.join(msg_lines))
def delete(self, id):
"""See IMailBox."""
try:
self._popbox.dele(id)
except poplib.error_proto, e:
raise MailBoxError(str(e))
def close(self):
"""See IMailBox."""
self._popbox.quit()
class DirectoryMailBox:
"""Mail box which reads files from a directory."""
implements(IMailBox)
def __init__(self, directory):
self.mail_dir = os.path.abspath(directory)
def open(self):
"""See IMailBox."""
# No-op.
def items(self):
"""See IMailBox."""
for name in os.listdir(self.mail_dir):
filename = os.path.join(self.mail_dir, name)
if os.path.isfile(filename):
yield (filename, open(filename).read())
def delete(self, id):
"""See IMailBox."""
if not os.path.isfile(id):
raise MailBoxError("No such id: %s" % id)
if not os.path.abspath(id).startswith(self.mail_dir):
raise MailBoxError("No such id: %s" % id)
os.remove(id)
def close(self):
"""See IMailBox."""
# No-op.
|