Fix snarf_mail.rb fetch-without-del functionality
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Jun 2010 07:03:22 +0000 (00:03 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Jun 2010 07:03:22 +0000 (00:03 -0700)
snarf_mail.rb

index 304c5ad..a22e72f 100755 (executable)
@@ -89,9 +89,55 @@ def format_uid(uid)
   return sprintf("%006d", uid)
 end
 
+def get_sanitized_email_name(mailbox, uid)
+  msn = mailbox.dup
+  msn.gsub!(' ', '_')
+  msn.gsub!('/', '.')
+  return "#{msn}#{format_uid(uid)}"
+end
+
+def write_email_to_disk(mailbox, data)
+  arr = data[0].attr
+  filename = get_sanitized_email_name(mailbox, arr["UID"])
+  fp = File.open(filename, 'w')
+  fp.write(arr["RFC822.HEADER"])
+  fp.write(arr["RFC822.TEXT"])
+  fp.close
+end
+
 def snarf_mailbox(imap, mailbox)
   full_count = 0
   first_time = true
+  count = 0
+
+  imap.select(mailbox)
+  imap.search(["NOT", "DELETED"]).each do |message_id|
+    if (first_time == true) then
+      # Print a dot immediately after making first contact with the server.
+      # It is reassuring to the user.
+      printf(".")
+      STDOUT.flush()
+      first_time = false
+    end
+    data = imap.fetch(message_id, [ "UID", "RFC822.HEADER", "RFC822.TEXT" ])
+    write_email_to_disk(mailbox, data)
+
+    count = count + 1
+    full_count = full_count + 1
+    if (count > 20)
+      # Print out a dot to signify progress
+      printf(".")
+      STDOUT.flush()
+      count = 0
+    end
+  end
+
+  puts "fetched: #{full_count} messages from #{mailbox}"
+end
+
+def snarf_and_delete_mailbox(imap, mailbox)
+  full_count = 0
+  first_time = true
   while true
     count = 0
     msg_seqnos = Array.new
@@ -106,21 +152,14 @@ def snarf_mailbox(imap, mailbox)
         first_time = false
       end
       data = imap.fetch(message_id, [ "UID", "RFC822.HEADER", "RFC822.TEXT" ])
-      arr = data[0].attr
-      filename = "#{mailbox}#{format_uid(arr["UID"])}"
-      fp = File.open(filename, 'w')
-      fp.write(arr["RFC822.HEADER"])
-      fp.write(arr["RFC822.TEXT"])
-      fp.close
+      write_email_to_disk(mailbox, data)
       count = count + 1
       full_count = full_count + 1
       msg_seqnos << data[0].seqno.to_i
-      break if (count > 20)
+      #break if (count > 20)
     end
     if (count == 0) then
-      action_str = ($opts.delete_after == true) ?
-        "fetched and deleted" : "fetched"
-      puts "#{action_str} #{full_count} messages from #{mailbox}"
+      puts "fetched and deleted: #{full_count} messages from #{mailbox}"
       return
     end
 
@@ -128,11 +167,9 @@ def snarf_mailbox(imap, mailbox)
     printf(".")
     STDOUT.flush()
 
-    # Delete messages if we're supposed to
-    if ($opts.delete_after == true) then
-      imap.store(msg_seqnos, "+FLAGS", [:Deleted])
-      imap.expunge
-    end
+    # Delete messages
+    imap.store(msg_seqnos, "+FLAGS", [:Deleted])
+    imap.expunge
   end
 end
 
@@ -154,7 +191,11 @@ when :list
   end
 when :snarf
   $opts.mailboxes.each do |mailbox|
-    snarf_mailbox(imap, mailbox)
+    if ($opts.delete_after == true)
+      snarf_and_delete_mailbox(imap, mailbox)
+    else
+      snarf_mailbox(imap, mailbox)
+    end
   end
 else
   raise "unknown action #{$opts.action}"