[tor-commits] [flashproxy/master] Move messages to Trash first thing.

dcf at torproject.org dcf at torproject.org
Thu Oct 4 00:34:59 UTC 2012


commit a1a3f4bb238d0dd5bf47272d9ccc659a682cf503
Author: David Fifield <david at bamsoftware.com>
Date:   Wed Oct 3 15:22:31 2012 -0700

    Move messages to Trash first thing.
    
    Instead of processing messages one by one in All Mail, then deleting
    from Trash en masse, copy from All Mail to Trash en mass, then delete
    from Trash one by one. Trash acts more like a real mailbox, in that
    deleting a message from there really deletes the message immediately. If
    something goes wrong and we are able to process the messages from Trash,
    Gmail will delete them anyway after 30 days.
---
 facilitator/facilitator-email-poller |   33 ++++++++++++++-------------------
 1 files changed, 14 insertions(+), 19 deletions(-)

diff --git a/facilitator/facilitator-email-poller b/facilitator/facilitator-email-poller
index 9c9b7d4..ee44cf9 100755
--- a/facilitator/facilitator-email-poller
+++ b/facilitator/facilitator-email-poller
@@ -251,11 +251,6 @@ def imap_get_uid(imap, index):
     assert m is not None
     return m.group(1)
 
-def imap_empty_trash(imap):
-    typ, data = imap.select("[Gmail]/Trash")
-    imap.store("1:*", "+FLAGS", "\\Deleted")
-    imap.expunge()
-
 # Gmail's IMAP folders are funny: they are not real folders, but actually views
 # of messages having a particular label. INBOX consists of messages having the
 # INBOX label, for example. Deleting a message from a folder just removes its
@@ -266,14 +261,17 @@ def imap_empty_trash(imap):
 # delete it from there. Messages in Trash are deleted automatically after 30
 # days, but we do it immediately.
 def imap_loop(imap):
-    # Empty the trash first thing, in case there are messages there from a
-    # previous failed run of this program or something.
-    imap_empty_trash(imap)
-
     while True:
+        # Copy all messages to Trash, and work on them from there. This is a
+        # failsafe so that messages will eventually be deleted if we are not
+        # able to retrieve them. This act of copying also deletes from All Mail.
         typ, data = imap.select("[Gmail]/All Mail")
-        exists = int(data[0])
+        assert typ == "OK"
+        imap.copy("1:*", "[Gmail]/Trash")
 
+        typ, data = imap.select("[Gmail]/Trash")
+        assert typ == "OK"
+        exists = int(data[0])
         if exists > 0:
             while True:
                 # Grab message 1 on each iteration; remaining messages shift down so
@@ -284,21 +282,18 @@ def imap_loop(imap):
 
                 typ, data = imap.uid("FETCH", uid, "(BODY[])")
                 assert typ == "OK"
+                msg_text = data[0][1]
+                typ, data = imap.uid("STORE", uid, "+FLAGS", "\\Deleted")
+                assert typ == "OK"
+                typ, data = imap.expunge()
+                assert typ == "OK"
 
                 try:
-                    msg = email.message_from_string(data[0][1])
+                    msg = email.message_from_string(msg_text)
                     handle_message(msg)
                 except Exception, e:
                     log("Error processing message, deleting anyway: %s" % str(e))
 
-                imap.uid("COPY", uid, "[Gmail]/Trash")
-                # Attempting to delete from All Mail here would have no effect.
-                # Messages disappear from All Mail when they are deleted from Trash
-                # in the next step.
-
-            # Now delete all messages from Trash to really delete them.
-            imap_empty_trash(imap)
-
         time.sleep(POLL_INTERVAL)
 
 def imap_login():





More information about the tor-commits mailing list