summaryrefslogtreecommitdiff
path: root/sample/tkfrom.rb
blob: b0ef8995ca7b5cee47d54e66cf2e5cf340f7bc1b (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /usr/local/bin/ruby

require "parsedate"
require "base64"

include ParseDate

class Mail
  def Mail.new(f)
    if !f.kind_of?(IO)
      f = open(f, "r")
      me = super(f)
      f.close
    else
      me = super
    end
    return me
  end

  def initialize(f)
    @header = {}
    @body = []
    while f.gets()
      $_.chop!
      next if /^From /		# skip From-line  
      break if /^$/		# end of header
      if /^(\S+):\s*(.*)/
	@header[attr = $1.capitalize] = $2
      elsif attr
	sub(/^\s*/, '')
	@header[attr] += "\n" + $_
      end
    end

    return if ! $_

    while f.gets()
      break if /^From /
      @body.push($_)
    end
  end

  def header
    return @header
  end

  def body
    return @body
  end

end

if ARGV.length == 0
  if ENV['MAIL']
    ARGV[0] = ENV['MAIL']
  elsif ENV['USER']
    ARGV[0] = '/usr/spool/mail/' + ENV['USER']
  elsif ENV['LOGNAME']
    ARGV[0] = '/usr/spool/mail/' + ENV['LOGNAME']
  end
end

require "tk"
list = scroll = nil
TkFrame.new{|f|
  list = TkListbox.new(f) {
    yscroll proc{|idx|
	scroll.set *idx
    }
    relief 'raised'
#    geometry "80x5"
    width 80
    height 5
    setgrid 'yes'
    pack('side'=>'left','fill'=>'both','expand'=>'yes')
  }
  scroll = TkScrollbar.new(f) {
    command proc{|idx|
      list.yview *idx
    }
    pack('side'=>'right','fill'=>'y')
  }
  pack
}
root = Tk.root
TkButton.new(root) {
  text 'Dismiss'
  command proc {exit}
  pack('fill'=>'both','expand'=>'yes')
}
root.bind "Control-c", proc{exit}
root.bind "Control-q", proc{exit}
root.bind "space", proc{exit}

$outcount = 0;
for file in ARGV
  next if !File.exist?(file)
  f = open(file, "r")
  while !f.eof
    mail = Mail.new(f)
    date =  mail.header['Date']
    next if !date
    from =  mail.header['From']
    subj =  mail.header['Subject']
    y = m = d = 0
    y, m, d = parsedate(date) if date
    from = "sombody@somewhere" if ! from
    subj = "(nil)" if ! subj
    from = decode_b(from)
    subj = decode_b(subj)
    list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj)
    $outcount += 1
  end
  f.close
  list.see 'end'
end

limit = 10000
if $outcount == 0
  list.insert 'end', "You have no mail."
  limit = 2000
end
Tk.after limit, proc{
  exit
}
Tk.mainloop