From adc759eb7fd1a953204347cc00791f89d6c85120 Mon Sep 17 00:00:00 2001 From: Colin Patrick Mccabe Date: Tue, 12 Nov 2013 10:46:31 -0800 Subject: [PATCH] snarf_mail_imap.rb: add --since and --before note: go-go-gadget-gmail has replaced most of the uses of this script. Signed-off-by: Colin McCabe --- snarf_mail_imap.rb | 47 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 34 insertions(+), 13 deletions(-) diff --git a/snarf_mail_imap.rb b/snarf_mail_imap.rb index d4d6e93..a20fa32 100755 --- a/snarf_mail_imap.rb +++ b/snarf_mail_imap.rb @@ -14,19 +14,39 @@ require 'net/imap' require 'optparse' require 'ostruct' +# Check that we were passed a date in %e-%b-%Y format. +def check_date(input) + d = Date.parse(input) + reserialized = d.strftime("%e-%b-%Y") + reserialized.strip! + if (reserialized != input) then + raise "Invalid date '" + input + "': reserialized form was " + \ + "'" + reserialized + "', which does not match original." + end +end + class MyOptions def self.parse(args) opts = OpenStruct.new opts.mailboxes = Array.new - opts.delete = "none" + opts.since = nil + opts.before = nil # Fill in $opts values parser = OptionParser.new do |myparser| myparser.banner = "Usage: #{ File.basename($0) } [opts]" myparser.separator("Specific options:") - myparser.on("--delete POLICY", "-d", - "Set delete policy to 'none' or 'old'. Default is 'none'.") do |d| - opts.delete = d + myparser.on("--since [TIME]", "-1", + "Time to fetch email messages since. (example: \ + 1-Dec-2010.)") do |u| + opts.since = u + check_date(opts.since) + end + myparser.on("--before [TIME]", "-2", + "Time to fetch email messages before. (example: \ + 1-Dec-2011.)") do |u| + opts.before = u + check_date(opts.before) end myparser.on("--username [USERNAME]", "-u", "Email account to fetch. (example: \ @@ -58,6 +78,9 @@ multiple mailboxes.") do |a| raise "must specify an action" unless opts.action raise "must give a username" unless opts.username raise "must give a server" unless opts.server + if ((opts.action == :snarf) and opts.mailboxes.empty?()) then + raise "you must specify mailbox(es) with -b in order to snarf" + end return opts end end @@ -115,16 +138,14 @@ def snarf_mailbox(imap, mailbox) first_time = true 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." + if $opts.since != nil then + searchterms << "SINCE" << $opts.since + end + if $opts.before != nil then + searchterms << "BEFORE" << $opts.before end + print "using IMAP search terms: " + searchterms.join(" ") + prequel = "fetched: " while true count = 0 -- 1.6.6.rc1.39.g9a42