summaryrefslogtreecommitdiff
path: root/spelling-rbead
blob: 4e92938ebafa51d74f470f79f6429f60ef4f49ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/ruby
%w[net/imap readline optparse]
  .each &Kernel.method(:require)

$file = nil
$subject = "Spelling:"

OptionParser.new do |opts|
  opts.banner = "Usage: spelling-rbead [options]"
  opts.on("-uUSER", "--user=USER", "Log in as USER") 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| $server = 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!

unless $help
  unless $user && $server
    raise "#{$user ? "Server" : "User"} not given"
  end

  $stderr.print "Password: "
  $password = Readline.readline
  $stderr.puts

  #Net::IMAP.debug = true
  imap = Net::IMAP.new $server
  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