Regex for the optional folder argument and skip shared folders option by default
[imap-mark-seen] / src / mail_iterator.py
index ccd91e2..cd49656 100644 (file)
@@ -34,10 +34,14 @@ class MailIterator:
     mailboxes = None
     # logged in status
     logged_in = None
+    # skip shared folders
+    skip_shared_folders = None
 
-    def __init__(self, server, username, password):
+    def __init__(self, server, username, password, skip_shared_folders = False):
         """Creates a connection and a user session."""
 
+        self.skip_shared_folders = skip_shared_folders
+
         # connect to server
         try:
             self.mail_con = imaplib.IMAP4_SSL(server)
@@ -81,6 +85,10 @@ class MailIterator:
 
         for mailbox in self.mailboxes:
             logging.debug("Checking mailbox %s", mailbox[2])
+            # detect if mailbox is shared and if skip flag is set iterate further
+            if(self.skip_shared_folders and mailbox[2].split(mailbox[1])[0] == '"user'):
+                logging.info("Mailbox %s is shared and therefore skipped.", mailbox[2])
+                continue
             # select mailbox if writable
             try:
                 self.mail_con.select(mailbox[2])
@@ -109,7 +117,7 @@ class MailIterator:
             # Work around unsolicited server responses in imaplib by clearing them
             self.mail_con.response('STORE')
             _result, data = self.mail_con.uid('STORE', mid_range, '+FLAGS', "(\Seen)")
-            logging.info("New flags for messages %s are %s", mid_range, data)
+            logging.debug("New flags for messages %s are %s", mid_range, data)
         except (self.mail_con.error) as ex:
             raise UserWarning("Could not set the flags for some messages: %s", ex)
         self.mail_con.expunge()