| 191 | return s |
| 192 | |
| 193 | class MailFolder(object): |
| 194 | |
| 195 | def __init__(self, parent, name): |
| 196 | """ A folder (inbox, spam, trash, ...) in a mailbox. |
| 197 | E-mail messages can be searched and retrieved (including attachments) from a folder. |
| 198 | """ |
| 199 | self._parent = parent |
| 200 | self._name = name |
| 201 | |
| 202 | @property |
| 203 | def parent(self): |
| 204 | return self._parent |
| 205 | |
| 206 | @property |
| 207 | def name(self): |
| 208 | return _basename(self._name) |
| 209 | |
| 210 | @property |
| 211 | def count(self): |
| 212 | return len(self) |
| 213 | |
| 214 | def search(self, q, field=FROM, cached=False): |
| 215 | """ Returns a list of indices for the given query, latest-first. |
| 216 | The search field can be FROM, DATE or SUBJECT. |
| 217 | """ |
| 218 | id = "mail-%s-%s-%s-%s" % (self.parent._id, self.name, q, field) |
| 219 | if cached and id in cache: |
| 220 | status, response = "OK", [cache[id]] |
| 221 | else: |
| 222 | status, response = self.parent.imap4.select(self._name, readonly=1) |
| 223 | status, response = self.parent.imap4.search(None, field.upper(), q) |
| 224 | if cached: |
| 225 | cache[id] = response[0] |
| 226 | return sorted([int(i)-1 for i in response[0].split()], reverse=True) |
| 227 | |
| 228 | def read(self, i, attachments=False, cached=True): |
| 229 | return self.__getitem__(i, attachments, cached) |
| 230 | |
| 231 | def __getitem__(self, i, attachments=False, cached=True): |
| 232 | """ Returns the mail message with the given index. |
| 233 | Each message is a dictionary with date, from, subject, body, attachments entries. |
| 234 | The attachments entry is a list of (MIME-type, str)-tuples. |
| 235 | """ |
| 236 | i += 1 |
| 237 | id = "mail-%s-%s-%s-%s" % (self.parent._id, self.name, i, attachments) |
| 238 | if cached and id in cache: |
| 239 | m = cache[id] |
| 240 | else: |
| 241 | # Select the current mail folder. |
| 242 | # Get the e-mail header. |
| 243 | # Get the e-mail body, with or without file attachments. |
| 244 | status, response = self.parent.imap4.select(self._name, readonly=1) |
| 245 | status, response1 = self.parent.imap4.fetch(str(i), '(BODY.PEEK[HEADER])') |
| 246 | status, response2 = self.parent.imap4.fetch(str(i), '(BODY.PEEK[%s])' % (not attachments and "TEXT" or "")) |
| 247 | time.sleep(0.1) |
| 248 | m = response1[0][1] + response2[0][1] |
| 249 | # Cache the raw message for faster retrieval. |
| 250 | if cached: |
no outgoing calls
no test coverage detected
searching dependent graphs…