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