2 fix_imap_internaldate.py - Fix the INTERNALDATE field on IMAP servers
4 Copyright (c) 2012 Intra2net AG
5 Author: Plamen Dimitrov
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.
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.
17 Add '-t' argument when running the module for a test mode.
18 For a detailed list of each message with a date conflict change
19 the 'log_level' in the configuration file from '30' to '20'.
26 from date_interpreter import DateInterpreter
27 from mail_iterator import MailIterator
28 from caching_data import CachingData
31 """Iterates through csv list of users and their mailboxes"""
32 if (len(sys.argv) > 1 and sys.argv[1]=="-t"):
37 config = load_configuration()
38 logging.basicConfig(filename='mailscript.log',
39 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
40 level=config.getint('basic_settings', 'log_level'))
42 date_interp = DateInterpreter()
43 cashing_data = CachingData()
44 logging.warning(cashing_data)
45 user_reader = csv.DictReader(open("userdata.csv", "r"), delimiter=',')
47 server = config.get('basic_settings', 'imap_server')
48 tolerance = config.getint('basic_settings', 'tolerance')
51 for user in user_reader:
53 session = MailIterator(server, user['username'], user['password'])
54 except UserWarning as ex:
57 for mailbox in session:
59 #special key to ensure better mailbox uniqueness
60 mailbox_key = mailbox[0].strip('"') + mailbox[1]
61 mail_ids = session.fetch_messages()
62 new_ids = cashing_data.sync_cached_mailbox(user['username'], mailbox_key, mail_ids)
63 #print(len(new_ids), "new out of", len(mail_ids), "in", mailbox)
64 except UserWarning as ex:
69 fetched_internal_date = session.fetch_internal_date(mid)
70 internal_date = date_interp.extract_internal_date(fetched_internal_date)
71 fetched_received_date = session.fetch_received_date(mid)
72 received_date = date_interp.extract_received_date(fetched_received_date)
73 if(received_date==""):
74 logging.warning("No received date could be found in message uid: %s - mailbox: %s - user: %s.\n",
75 mid.decode("utf-8"), mailbox[0], user['username'])
77 except UserWarning as ex:
80 if(date_interp.compare_dates(received_date, internal_date, tolerance)):
81 #print(received_date, internal_date)
84 session.update_message(mid, mailbox[0], received_date)
85 except UserWarning as ex:
89 logging.info("Date conflict found in message uid: %s - mailbox: %s - user: %s.\nInternal date %s is different from received date %s from RECEIVED header:\n%s.",
90 mid.decode("utf-8"), mailbox[0], user['username'],
91 internal_date.strftime("%d %b %Y %H:%M:%S"),
92 received_date.strftime("%d %b %Y %H:%M:%S"),
93 fetched_received_date[0][1].decode("utf-8").split("Received:")[1])
94 # count total emails for every user and mailbox
95 user_key = user['username']+'|'+mailbox[0].strip('"')
96 total_per_box[user_key] = 1 + total_per_box.get(user_key, 0)
97 # if all messages were successfully fixed confirm caching
98 cashing_data.commit_cached_mailbox(user['username'], mailbox_key)
99 # final report on date conflicts
101 for warning in total_per_box:
102 total_per_user += total_per_box[warning]
103 logging.warning("Total date conflicts to be corrected in a mailbox %s are %s.",
104 warning.split('|')[1], total_per_box[warning])
105 logging.warning("Total date conflicts to be corrected for user %s are %s.\n",
106 user['username'], total_per_user)
108 def load_configuration():
109 """Loads the script configuration from a file or creates such."""
110 config = configparser.RawConfigParser()
112 config.read('confscript.cfg')
114 config.add_section('basic_settings')
115 config.set('basic_settings', 'log_level', logging.DEBUG)
116 #config.set('Basic settings', 'bool', 'true')
117 with open('confscript.cfg', 'w') as configfile:
118 config.write(configfile)
121 if(__name__ == "__main__"):