From 7633a30066bac0bc100875ba41dfad69478c1506 Mon Sep 17 00:00:00 2001 From: Sean Whalen Date: Thu, 12 Sep 2024 14:48:26 -0400 Subject: [PATCH] maildir fixes --- parsedmarc/cli.py | 10 +++++----- parsedmarc/mail/maildir.py | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/parsedmarc/cli.py b/parsedmarc/cli.py index efa32480..5dec5127 100644 --- a/parsedmarc/cli.py +++ b/parsedmarc/cli.py @@ -20,8 +20,8 @@ parse_report_file, get_dmarc_reports_from_mbox, elastic, opensearch, \ kafkaclient, splunk, save_output, email_results, ParserError, \ __version__, InvalidDMARCReport, s3, syslog, loganalytics, gelf -from parsedmarc.mail import IMAPConnection, MSGraphConnection, GmailConnection, \ - MaildirConnection +from parsedmarc.mail import IMAPConnection, MSGraphConnection, \ + GmailConnection, MaildirConnection from parsedmarc.mail.graph import AuthMethod from parsedmarc.log import logger @@ -505,8 +505,8 @@ def process_reports(reports_): gmail_api_paginate_messages=True, gmail_api_scopes=[], gmail_api_oauth2_port=8080, - maildir_path = None, - maildir_create = False, + maildir_path=None, + maildir_create=False, log_file=args.log_file, n_procs=1, ip_db_path=None, @@ -1016,7 +1016,7 @@ def process_reports(reports_): opts.maildir_path = \ maildir_api_config.get("maildir_path") opts.maildir_create = \ - maildir_api_config.get("maildir_create") + maildir_api_config.get("maildir_create") if "log_analytics" in config.sections(): log_analytics_config = config["log_analytics"] diff --git a/parsedmarc/mail/maildir.py b/parsedmarc/mail/maildir.py index f8c70c28..17d3f54d 100644 --- a/parsedmarc/mail/maildir.py +++ b/parsedmarc/mail/maildir.py @@ -1,15 +1,11 @@ from time import sleep -#from imapclient.exceptions import IMAPClientError -#from mailsuite.imap import IMAPClient -from socket import timeout - from parsedmarc.log import logger from parsedmarc.mail.mailbox_connection import MailboxConnection -#import email import mailbox import os + class MaildirConnection(MailboxConnection): def __init__(self, maildir_path=None, @@ -17,18 +13,22 @@ def __init__(self, ): self._maildir_path = maildir_path self._maildir_create = maildir_create - maildir_owner=os.stat(maildir_path).st_uid + maildir_owner = os.stat(maildir_path).st_uid if os.getuid() != maildir_owner: if os.getuid() == 0: - logger.warning("Switching uid to {} to access Maildir".format(maildir_owner)) + logger.warning("Switching uid to {} to access Maildir".format( + maildir_owner)) os.setuid(maildir_owner) else: - raise Exception('runtime uid {} differ from maildir {} owner {}'.format(os.getuid().maildir_path,maildir_owner)) + ex = 'runtime uid {} differ from maildir {} owner {}'.format( + os.getuid(), maildir_path, maildir_owner) + raise Exception(ex) self._client = mailbox.Maildir(maildir_path, create=maildir_create) - self._subfolder_client={} + self._subfolder_client = {} def create_folder(self, folder_name: str): - self._subfolder_client[folder_name]=self._client.add_folder(folder_name) + self._subfolder_client[folder_name] = self._client.add_folder( + folder_name) self._client.add_folder(folder_name) def fetch_messages(self, reports_folder: str, **kwargs): @@ -41,9 +41,10 @@ def delete_message(self, message_id: str): self._client.remove(message_id) def move_message(self, message_id: str, folder_name: str): - message_data=self._client.get(message_id) + message_data = self._client.get(message_id) if folder_name not in self._subfolder_client.keys(): - self._subfolder_client = mailbox.Maildir(os.join(maildir_path,folder_name), create=maildir_create) + self._subfolder_client = mailbox.Maildir(os.join( + self.maildir_path, folder_name), create=self.maildir_create) self._subfolder_client[folder_name].add(message_data) self._client.remove(message_id) @@ -57,4 +58,3 @@ def watch(self, check_callback, check_timeout): except Exception as e: logger.warning("Maildir init error. {0}".format(e)) sleep(check_timeout) - \ No newline at end of file