''' fix_imap_internaldate.py - Fix the INTERNALDATE field on IMAP servers Copyright (c) 2012 Intra2net AG Author: Plamen Dimitrov This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ''' import sys import csv import argparse # python version handling try: import configparser except ImportError: print("This module needs python version 3 or later.") sys.exit() import logging from mail_date_parser import MailDateParser from mail_iterator import MailIterator from caching_data import CachingData def main(): """Interprets command arguments and initializes configuration and logger. Then begins mail synchronization.""" # parse arguments parser = argparse.ArgumentParser(description="Fix the INTERNALDATE field on IMAP servers. " "Small tool to fix the IMAP internaldate " "in case it's too much off compared to the last date " "stored in the received lines.") parser.add_argument('-u', '--update', dest='test_mode', action='store_false', default=True, help='update all e-mails and exit test mode') # config and logging setup config = load_configuration() prepare_logger(config) args = parser.parse_args() if(args.test_mode): logging.info("Testing mode initiated. No message will be modified on the server.") else: logging.info("Update mode initiated. Messages will be modified.") # proceed to main functionality try: synchronize_csv(config, args.test_mode) except KeyboardInterrupt: logging.info("Script was interrupted by the user.") return def load_configuration(): """Loads the script configuration from a file or creates such.""" config = configparser.RawConfigParser() success = config.read('confscript.cfg') if(len(success)==0): config.add_section('basic_settings') config.set('basic_settings', 'file_log_level', logging.INFO) config.set('basic_settings', 'console_log_level', logging.INFO) config.set('basic_settings', 'imap_server', 'imap.company.com') config.set('basic_settings', 'tolerance', 30) with open('confscript.cfg', 'w') as configfile: config.write(configfile) return config def prepare_logger(config): """Sets up the logging functionality""" # reset the log with open('fix_imap_internaldate.log', 'w'): pass # add basic configuration logging.basicConfig(filename='fix_imap_internaldate.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=config.getint('basic_settings', 'file_log_level')) # add a handler for a console output console = logging.StreamHandler() console.setLevel(config.getint('basic_settings', 'console_log_level')) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) return def synchronize_csv(config, test_mode): """Iterates through csv list of users and synchronizes their messages.""" # initialize loop permanent data caching_data = CachingData() date_parser = MailDateParser() server = config.get('basic_settings', 'imap_server') tolerance = config.getint('basic_settings', 'tolerance') * 60 # iterate through the users in the csv data user_reader = csv.DictReader(open("userdata.csv", "r"), delimiter=',') for user in user_reader: try: session = MailIterator(server, user['username'], user['password']) except UserWarning as ex: logging.error(ex) continue for mailbox in session: try: box = caching_data.retrieve_cached_mailbox(mailbox[0], mailbox[1], user['username']) mail_ids = session.fetch_messages() new_ids = box.synchronize(mail_ids, tolerance) logging.info("%s new messages out of %s found in %s.", len(new_ids), len(mail_ids), box.name) except UserWarning as ex: logging.error(ex) continue for mid in new_ids: try: fetched_internal_date = session.fetch_internal_date(mid) internal_date = date_parser.extract_internal_date(fetched_internal_date) fetched_received_date = session.fetch_received_date(mid) received_date = date_parser.extract_received_date(fetched_received_date) if(received_date==""): logging.debug("No received date could be found in message uid: %s - mailbox: %s - user: %s.", mid.decode('iso-8859-1'), box.name, box.owner) box.no_received_field += 1 continue except UserWarning as ex: logging.error(ex) continue if(date_parser.compare_dates(received_date, internal_date, tolerance)): logging.warning("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.", mid.decode('iso-8859-1'), box.name, box.owner, internal_date.strftime("%d %b %Y %H:%M:%S"), received_date.strftime("%d %b %Y %H:%M:%S"), fetched_received_date.split("Received:")[1]) if(test_mode==0): try: session.update_message(mid, box.name, received_date) except UserWarning as ex: logging.error(ex) continue # count total emails for every user and mailbox box.date_conflicts += 1 # if all messages were successfully fixed confirm caching if(not test_mode): box.confirm_change() # final report on date conflicts caching_data.report_conflicts() return if(__name__ == "__main__"): main()