--- /dev/null
+#!/usr/bin/env ruby
+
+#
+# snarf_gmail.rb
+#
+# Copies mail from a gmail account
+# You need ruby 1.9 for this
+# You need the password gem for this
+#
+# Problem: this appears to only download some mails (usually around 383 or
+# so). One workaround is to run this multiple times. Still not sure if there
+# is a better workaround.
+#
+# Colin McCabe
+#
+
+require 'net/pop'
+require 'optparse'
+require 'ostruct'
+require 'password'
+
+class MyOptions
+ def self.parse(args)
+ opts = OpenStruct.new
+
+ # Fill in $opts values
+ parser = OptionParser.new do |myparser|
+ myparser.banner = "Usage: #{ File.basename($0) } [opts]"
+ myparser.separator("Specific options:")
+ myparser.on("--username USERNAME", "-u",
+ "Email account to fetch. (example: \
+RareCactus@gmail.com)") do |u|
+ opts.username = u
+ end
+ myparser.on("--dry-run", "-d",
+ "Dry run. State what would be done without actually \
+doing it.") do |a|
+ opts.dry_run = true
+ end
+ end
+
+ parser.parse!(args)
+ raise "must give a username" unless opts.username
+ return opts
+ end
+end
+
+# MAIN
+begin
+ $opts = MyOptions.parse(ARGV)
+rescue Exception => msg
+ $stderr.print("#{msg}.\nType --help to see usage information.\n")
+ exit 1
+end
+
+puts "type password for #{$opts.username}"
+password = gets
+password.chomp!
+
+Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
+Net::POP3.start("pop.gmail.com", 995, $opts.username, password) do |pop|
+ #pop.reset()
+ mails = pop.mails
+ n_mails = pop.n_mails
+ puts "found #{n_mails} mails."
+ if ($opts.dry_run)
+ puts "successfully connected."
+ exit 0
+ end
+ count = 0
+ mails.each do |mail|
+ fname = mail.unique_id
+ #fname = sprintf("%08d", count)
+ File.open(fname, 'w+') do|f|
+ f.write mail.pop
+ end
+ count = count + 1
+ if ((count % 100) == 0)
+ count = 0
+ print "."
+ STDOUT.flush
+ end
+ end
+end
+puts "done."
+exit 0