No need for reverse sort of folder list
[imap-mark-seen] / src / mail_iterator.py
index 6e3afdd..0b67bdd 100644 (file)
@@ -2,7 +2,7 @@
 The module contains the MailIterator class.
 
 Copyright (c) 2012 Intra2net AG
-Author: Plamen Dimitrov and Thomas Jarosch
+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
@@ -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)
@@ -60,12 +64,12 @@ class MailIterator:
         try:
             _result, mailboxes = self.mail_con.list()
         except self.mail_con.error as ex:
-            logging.warning("Could not retrieve mailboxes for user %s: %s", username, ex)
+            logging.error("Could not retrieve mailboxes for user %s: %s", username, ex)
         self.mailboxes = []
         for mailbox in mailboxes:
             mailbox = MAILBOX_RESP.match(mailbox.decode('iso-8859-1')).groups()
             self.mailboxes.append(mailbox)
-        self.mailboxes = sorted(self.mailboxes, key=lambda box: box[2], reverse=True)
+        self.mailboxes = sorted(self.mailboxes, key=lambda box: box[2], reverse=False)
 
         return
 
@@ -81,6 +85,11 @@ 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
+            imap_delimiter = mailbox[1]
+            if(self.skip_shared_folders and mailbox[2].split(imap_delimiter)[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])
@@ -104,12 +113,12 @@ class MailIterator:
 
     def set_seen_messages(self, mid_range):
         """Sets the \\Seen flag for all messages with the respective mids."""
-        
+
         try:
             # 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)
+            _result, data = self.mail_con.uid('STORE', mid_range, '+FLAGS', "(\\Seen)")
+            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()