Improve IMAP server connection error handling
[imap-fix-internaldate] / src / mail_iterator.py
1 '''
2 mail_iterator.py - The module contains the MailIterator class.
3
4 Copyright (c) 2012 Intra2net AG
5 Author: Plamen Dimitrov
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 '''
17
18 import imaplib
19 import re
20 import time
21 import logging
22
23 MAILBOX_RESP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
24 UIDVAL_RESP = re.compile(r'(?P<name>.*) \(UIDVALIDITY (?P<uidval>.*)\)')
25
26 #imaplib.Debug = 4
27
28 class MailIterator:
29     """This class communicates with the e-mail server."""
30
31     # class attributes
32     # IMAP4_SSL for connection with an IMAP server
33     mail_con = None
34     # list of tuples (uidvalidity, mailboxname) for the retrieved mailboxes
35     mailboxes = None
36     # logged in status
37     logged_in = None
38     # skip shared folders
39     skip_shared_folders = None
40
41     def __init__(self, server, username, password, skip_shared_folders = False):
42         """Creates a connection and a user session."""
43         self.skip_shared_folders = skip_shared_folders
44
45         # connect to server
46         try:
47             self.mail_con = imaplib.IMAP4_SSL(server)
48         except Exception as ex:
49             raise UserWarning("Could not connect to host %s: %s" % (server, ex))
50
51         # log in
52         try:
53             self.mail_con.login(username, password)
54             logging.info("Logged in as %s.", username)
55         except:
56             self.logged_in = False
57             raise UserWarning("Could not log in as user " + username)
58         self.logged_in = True
59
60         # list mailboxes
61         try:
62             _result, self.mailboxes = self.mail_con.list()
63         except (self.mail_con.error):
64             raise UserWarning("Could not retrieve mailboxes for user " + username)
65
66     def __del__(self):
67         """Closes the connection and the user session."""
68         if(self.logged_in):
69             self.mail_con.close()
70             self.mail_con.logout()
71
72     def __iter__(self):
73         """Iterates through all mailboxes, returns (uidval,name)."""
74         for mailbox in self.mailboxes:
75             logging.debug("Checking mailbox %s.", mailbox)
76             mailbox = MAILBOX_RESP.match(mailbox.decode('iso-8859-1')).groups()
77             # detect if mailbox is shared and if skip flag is set iterate further
78             if(self.skip_shared_folders and mailbox[2].split(mailbox[1])[0] == '"user'):
79                 logging.info("Mailbox %s is shared and therefore skipped.", mailbox[2])
80                 continue
81             # retrieve uidvalidity
82             try:
83                 _result, data = self.mail_con.status(mailbox[2], '(UIDVALIDITY)')
84             except (self.mail_con.error):
85                 raise UserWarning("Could not retrieve mailbox uidvalidity.")
86             uidval = UIDVAL_RESP.match(data[0].decode('iso-8859-1')).groups()
87             logging.debug("Extracted mailbox info is %s %s.", data[0], uidval)
88             # select mailbox if writable
89             try:
90                 self.mail_con.select(mailbox[2])
91             except self.mail_con.readonly:
92                 logging.warning("Mailbox %s is not writable and therefore skipped.", mailbox[2])
93                 continue
94             yield (mailbox[2], uidval[1])
95
96     def fetch_messages(self):
97         """Fetches the messages from the current mailbox, return list of uids."""
98         try:
99             _result, data = self.mail_con.uid('search', None, "ALL")
100         except (self.mail_con.error):
101             raise UserWarning("Could not fetch messages.")
102         mailid_list = data[0].split()
103         return mailid_list
104
105     def fetch_internal_date(self, mid):
106         """Fetches the internal date of a message, returns a time tuple."""
107         try:
108             _result, data = self.mail_con.uid('fetch', mid, '(INTERNALDATE)')
109         except (self.mail_con.error):
110             raise UserWarning("Could not fetch the internal date of message " + mid.decode('iso-8859-1') + ".")
111         internal_date = imaplib.Internaldate2tuple(data[0])
112         return internal_date
113
114     def fetch_received_date(self, mid):
115         """Fetches the received date of a message, returns bytes reponse."""
116         try:
117             _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (RECEIVED)])')
118         except (self.mail_con.error):
119             raise UserWarning("Could not fetch the received header of message " + mid.decode('iso-8859-1') + ".")
120         return data[0][1].decode('iso-8859-1')
121
122     def fetch_basic_date(self, mid):
123         """Fetches the basic date of a message, returns bytes reponse."""
124         try:
125             _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (DATE)])')
126         except (self.mail_con.error):
127             raise UserWarning("Could not fetch the date header of message " + mid.decode('iso-8859-1') + ".")
128         return data[0][1].decode('iso-8859-1')
129
130     def update_message(self, mid, mailbox, internal_date):
131         """Replaces a message with one with correct internal date."""
132         internal_date_sec = time.mktime(internal_date.timetuple())
133         try:
134             result, data = self.mail_con.uid('fetch', mid, '(RFC822)')
135             #logging.debug("Entire e-mail is: %s", data[0][1])
136
137             # retrieve and select flags to upload
138             fetched_flags = self.mail_con.uid('fetch', mid, '(FLAGS)')[1][0]
139             parsed_flags = imaplib.ParseFlags(fetched_flags)
140             selected_flags = ()
141             for flag in parsed_flags:
142                 if(flag != b'\\Recent'):
143                     selected_flags += (flag,)
144             logging.debug("Selected flags %s from parsed flags %s.", selected_flags, parsed_flags)                    
145             flags_str = " ".join(flag.decode('iso-8859-1') for flag in selected_flags)
146
147             # upload message copy and delete old one
148             result, data = self.mail_con.append(mailbox, flags_str,
149                                                 internal_date_sec, data[0][1])
150             logging.debug("Adding corrected copy of the message reponse: %s %s", result, data)
151         except (self.mail_con.error):
152             raise UserWarning("Could not replace the e-mail " + mid.decode('iso-8859-1') + ".")
153         try:
154             result, data = self.mail_con.uid('STORE', mid, '+FLAGS', r'(\Deleted)')
155             logging.debug("Removing old copy of the message reponse: %s %s", result, data)
156         except (self.mail_con.error):
157             raise UserWarning("Could not delete the e-mail " + mid.decode('iso-8859-1') + ".")
158         self.mail_con.expunge()
159         return