#!/usr/bin/ruby %w[net/imap readline optparse].each do |m| require m end $file = nil OptionParser.new do |opts| opts.banner = "Usage: spelling-rbead [options]" opts.on("-uUSER", "--user=USER", "User to log in as") do |u| $user = u; end opts.on("-fADDR", "--from=ADDR", "Search from ADDR") do |a| $from = a; end opts.on("-sSUBJ", "--subject=SUBJ", "Search for SUBJ") do |s| $subject = s; end opts.on("-mSERVER", "--mailserver=SERVER", "Connect to SERVER") do |m| $mailbox = m; end opts.on("-o", "--output=FILE", "Output to FILE") do |f| $file = f; end opts.on("-h", "--help", "Get help") do puts opts; $help = true; end end.parse! if !$help $password = Readline.readline "Password: " $subject ||= "Spelling:" #Net::IMAP.debug = true imap = Net::IMAP.new($mailbox) imap.login($user, $password) imap.select "inbox" n = imap.search("FROM \"#{$from}\" SUBJECT \"#{$subject}\"").last if n msgs = imap.fetch n, "body[1.text]" c = msgs[0].attr["BODY[1.TEXT]"] words = c.split("\n") end imap.logout if $file open $file, "w" do |f| f.puts words end else puts words end end