6 # Copies mail from an IMAP account
9 # http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html
19 opts.mailboxes = Array.new
20 opts.delete_after = false
22 # Fill in $opts values
23 parser = OptionParser.new do |myparser|
24 myparser.banner = "Usage: #{ File.basename($0) } [opts]"
25 myparser.separator("Specific options:")
26 myparser.on("--delete-after", "-d",
27 "Delete emails after fetching them.") do |d|
28 opts.delete_after = true
30 myparser.on("--username USERNAME", "-u",
31 "Email account to fetch. (example: \
32 RareCactus@gmail.com)") do |u|
35 myparser.on("--list-folders", "-l",
36 "List the IMAP folders that are present.") do |a|
37 raise "can only specify one action" if (opts.action)
40 myparser.on("--snarf", "-S",
41 "Copy mail to the current directory.") do |a|
42 raise "can only specify one action" if (opts.action)
45 myparser.on("--box [MAILBOX]", "-b",
46 "Act on a given mailbox. You may specify -b more than once for \
47 multiple mailboxes.") do |a|
50 myparser.on("--server [SERVER]", "-s",
51 "Email server to use") do |u|
57 raise "must specify an action" unless opts.action
58 raise "must give a username" unless opts.username
59 raise "must give a server" unless opts.server
64 # Get a password from STDIN without echoing it.
65 # This is kind of ugly, but it does work.
66 def get_password(prompt)
67 shell_cmds = 'stty -echo && read password && echo ${password}'
71 pipe = IO.popen(shell_cmds, "r") do |pipe|
74 echo_status = $?.exitstatus
77 if (echo_status != 0) then
78 raise "get_password: error executing: #{shell_cmds}"
84 # We don't know how to deal with non-numeric UIDs. Best just to leave them
86 return uid if (uid =~ /[^0123456789]/)
88 # Pad numeric uids out to 6 digits
89 return sprintf("%006d", uid)
92 def snarf_mailbox(imap, mailbox)
97 msg_seqnos = Array.new
100 imap.search(["NOT", "DELETED"]).each do |message_id|
101 if (first_time == true) then
102 # Print a dot immediately after making first contact with the server.
103 # It is reassuring to the user.
108 data = imap.fetch(message_id, [ "UID", "RFC822.HEADER", "RFC822.TEXT" ])
110 filename = "#{mailbox}#{format_uid(arr["UID"])}"
111 fp = File.open(filename, 'w')
112 fp.write(arr["RFC822.HEADER"])
113 fp.write(arr["RFC822.TEXT"])
116 full_count = full_count + 1
117 msg_seqnos << data[0].seqno.to_i
118 break if (count > 20)
121 action_str = ($opts.delete_after == true) ?
122 "fetched and deleted" : "fetched"
123 puts "#{action_str} #{full_count} messages from #{mailbox}"
127 # Print out a dot to signify progress
131 # Delete messages if we're supposed to
132 if ($opts.delete_after == true) then
133 imap.store(msg_seqnos, "+FLAGS", [:Deleted])
141 $opts = MyOptions.parse(ARGV)
142 rescue Exception => msg
143 $stderr.print("#{msg}.\nType --help to see usage information.\n")
147 password = get_password("Please enter the password for #{$opts.username}:")
148 imap = Net::IMAP.new($opts.server, 993, true)
149 imap.login($opts.username, password)
152 imap.list("", "*").each do |mbl|
156 $opts.mailboxes.each do |mailbox|
157 snarf_mailbox(imap, mailbox)
160 raise "unknown action #{$opts.action}"