From 63101d4aa1521a3ced6c3bc2e02a548f933bf4e3 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Sun, 11 Sep 2011 22:55:25 -0700 Subject: [PATCH] snarf_mail: add time-based delete strategy Signed-off-by: Colin McCabe --- snarf_mail.rb | 66 ++++++++++++++++++++------------------------------------ 1 files changed, 24 insertions(+), 42 deletions(-) diff --git a/snarf_mail.rb b/snarf_mail.rb index a22e72f..fc51b57 100755 --- a/snarf_mail.rb +++ b/snarf_mail.rb @@ -9,6 +9,7 @@ # http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html # +require 'date' require 'net/imap' require 'optparse' require 'ostruct' @@ -17,15 +18,15 @@ class MyOptions def self.parse(args) opts = OpenStruct.new opts.mailboxes = Array.new - opts.delete_after = false + opts.delete = "none" # Fill in $opts values parser = OptionParser.new do |myparser| myparser.banner = "Usage: #{ File.basename($0) } [opts]" myparser.separator("Specific options:") - myparser.on("--delete-after", "-d", - "Delete emails after fetching them.") do |d| - opts.delete_after = true + myparser.on("--delete POLICY", "-d", + "Set delete policy to 'none' or 'old'. Default is 'none'.") do |d| + opts.delete = d end myparser.on("--username USERNAME", "-u", "Email account to fetch. (example: \ @@ -108,42 +109,25 @@ 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 + searchterms = [ "NOT", "DELETED" ] + if $opts.delete == "old" + t = Date.today() - 365 + time_str = t.strftime("%e-%b-%Y") + searchterms << "BEFORE" << time_str + prequel = "fetched and deleted: " + elsif $opts.delete == "none" + prequel = "fetched: " + else + raise "expected one of 'old', 'none' for delete argument." + end -def snarf_and_delete_mailbox(imap, mailbox) - full_count = 0 - first_time = true while true count = 0 msg_seqnos = Array.new imap.select(mailbox) - imap.search(["NOT", "DELETED"]).each do |message_id| + imap.search(searchterms).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. @@ -159,7 +143,7 @@ def snarf_and_delete_mailbox(imap, mailbox) #break if (count > 20) end if (count == 0) then - puts "fetched and deleted: #{full_count} messages from #{mailbox}" + puts "#{prequel} #{full_count} messages from #{mailbox}" return end @@ -167,9 +151,11 @@ def snarf_and_delete_mailbox(imap, mailbox) printf(".") STDOUT.flush() - # Delete messages - imap.store(msg_seqnos, "+FLAGS", [:Deleted]) - imap.expunge + if ($opts.delete != "none"): + # Delete messages + imap.store(msg_seqnos, "+FLAGS", [:Deleted]) + imap.expunge + end end end @@ -191,11 +177,7 @@ when :list end when :snarf $opts.mailboxes.each do |mailbox| - if ($opts.delete_after == true) - snarf_and_delete_mailbox(imap, mailbox) - else - snarf_mailbox(imap, mailbox) - end + snarf_mailbox(imap, mailbox) end else raise "unknown action #{$opts.action}" -- 1.6.6.rc1.39.g9a42