use uids to manipulate mails
authorOlivier Matz <olivier.matz@6wind.com>
Tue, 29 Mar 2016 08:38:13 +0000 (10:38 +0200)
committerOlivier Matz <olivier.matz@6wind.com>
Tue, 29 Mar 2016 08:38:13 +0000 (10:38 +0200)
The item identifier can be modified in some conditions, for instance
after an expunge operation. This could leads to manipulate the wrong
mail. Use uids instead, which does not vary for a given mail.

Signed-off-by: Olivier Matz <zer0@droids-corp.org>
imapami/actions.py
imapami/rules.py

index 169588c..8653ea0 100644 (file)
@@ -170,7 +170,7 @@ class ImapamiActionCopy(ImapamiAction):
         imap = ami.imap
         dest = self.evaluate(self.dest, ami, mail.msg)
         imap.create(dest)
-        ret, msg = imap.copy(mail.item, dest)
+        ret, msg = imap.uid("COPY", mail.item, dest)
         if ret != "OK":
             ami.logger.warning(
                 "imap copy returned %s: %s" % (ret, str(msg)))
@@ -205,7 +205,7 @@ class ImapamiActionChangeFlag(ImapamiAction):
             cmd = '-FLAGS'
         flag = '\\' + self.evaluate(self.flag, ami,
                                     mail.msg)
-        ret, msg = imap.store(mail.item, cmd, flag)
+        ret, msg = imap.uid("STORE", mail.item, cmd, flag)
         if ret != "OK":
             ami.logger.warning(
                 "imap store '%s %s' returned %s: %s" % (
@@ -276,12 +276,12 @@ class ImapamiActionMove(ImapamiAction):
         imap = ami.imap
         dest = self.evaluate(self.dest, ami, mail.msg)
         imap.create(dest)
-        ret, msg = imap.copy(mail.item, dest)
+        ret, msg = imap.uid("COPY", mail.item, dest)
         if ret != "OK":
             ami.logger.warning(
                 "imap copy returned %s: %s" % (ret, str(msg)))
             return False
-        ret, msg = imap.store(mail.item, '+FLAGS', '\\Deleted')
+        ret, msg = imap.uid("STORE", mail.item, '+FLAGS', '\\Deleted')
         if ret != "OK":
             ami.logger.warning(
                 "imap delete returned %s: %s" % (ret, str(msg)))
index 840d1aa..9efd6dd 100644 (file)
@@ -166,7 +166,7 @@ class ImapamiRule(object):
         criteria = "(%s)" % self.get_criteria(ami)
         ami.logger.debug("processing rule %s, inbox %s, imap criteria %s",
                          self.name, inbox, criteria)
-        resp, items = ami.imap.search(None, criteria)
+        resp, items = ami.imap.uid("SEARCH", None, criteria)
         if resp != 'OK':
             ami.logger.warning(
                 "search failed: server response = %s, skip rule", resp)
@@ -220,7 +220,8 @@ class ImapamiRule(object):
         for item in item_list:
             mail_data = {'item': item, 'inbox': inbox}
             if parts != []:
-                resp, data = ami.imap.fetch(item, parts_str)
+                resp, data = ami.imap.uid("FETCH", item, parts_str)
+                print resp, data
                 if resp != 'OK':
                     ami.logger.warning(
                         "search failed: server response = %s, skip item",