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: \
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
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