1#!/usr/bin/env ruby 2 3if ARGV[0] != '-d' 4 unless $DEBUG 5 exit if fork 6 end 7else 8 ARGV.shift 9end 10 11if ARGV.length == 0 12 if ENV['MAIL'] 13 $spool = ENV['MAIL'] 14 else 15 $spool = '/var/spool/mail/' + ENV['USER'] 16 end 17else 18 $spool = ARGV[0] 19end 20 21require "parsedate" 22require "base64" 23 24include ParseDate 25 26class Mail 27 def Mail.new(f) 28 if !f.kind_of?(IO) 29 f = open(f, "r") 30 me = super 31 f.close 32 else 33 me = super 34 end 35 return me 36 end 37 38 def initialize(f) 39 @header = {} 40 @body = [] 41 while line = f.gets() 42 line.chop! 43 next if /^From / =~ line # skip From-line 44 break if /^$/ =~ line # end of header 45 if /^(\S+):\s*(.*)/ =~ line 46 @header[attr = $1.capitalize] = $2 47 elsif attr 48 sub(/^\s*/, '') 49 @header[attr] += "\n" + $_ 50 end 51 end 52 53 return unless $_ 54 55 while line = f.gets() 56 break if /^From / =~ line 57 @body.push($_) 58 end 59 end 60 61 def header 62 return @header 63 end 64 65 def body 66 return @body 67 end 68 69end 70 71require "tkscrollbox" 72 73my_appname = Tk.appname('tkbiff') 74$top = TkRoot.new 75if ((TkWinfo.interps($top) - [my_appname]).find{|ip| ip =~ /^tkbiff/}) 76 STDERR.print("Probably other 'tkbiff's are running. Bye.\n") 77 exit 78end 79 80$top.withdraw 81$list = TkScrollbox.new($top) { 82 relief 'raised' 83 width 80 84 height 8 85 setgrid 'yes' 86 pack 87} 88TkButton.new($top) { 89 text 'Dismiss' 90 command proc {$top.withdraw} 91 pack('fill'=>'both','expand'=>'yes') 92} 93$top.bind "Control-c", proc{exit} 94$top.bind "Control-q", proc{exit} 95$top.bind "space", proc{exit} 96 97$spool_size = 0 98$check_time = Time.now 99 100def check 101 $check_time = Time.now 102 size = File.size($spool) 103 if size and size != $spool_size 104 $spool_size = size 105 pop_up if size > 0 106 end 107 Tk.after 5000, proc{check} 108end 109 110if defined? Thread 111 Thread.start do 112 loop do 113 sleep 600 114 if Time.now - $check_time > 200 115 Tk.after 5000, proc{check} 116 end 117 end 118 end 119end 120 121def pop_up 122 outcount = 0; 123 $list.delete 0, 'end' 124 f = open($spool, "r") 125 while !f.eof? 126 mail = Mail.new(f) 127 date, from, subj = mail.header['Date'], mail.header['From'], mail.header['Subject'] 128 next if !date 129 y = m = d = 0 130 y, m, d = parsedate(date) if date 131 from = "sombody@somewhere" if ! from 132 subj = "(nil)" if ! subj 133 from = decode_b(from) 134 subj = decode_b(subj) 135 $list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj) 136 outcount += 1 137 end 138 f.close 139 if outcount == 0 140 $list.insert 'end', "You have no mail." 141 else 142 $list.see 'end' 143 end 144 $top.deiconify 145 Tk.after 2000, proc{$top.iconify} 146end 147 148$list.insert 'end', "You have no mail." 149check 150Tk.after 2000, proc{$top.iconify} 151begin 152 Tk.mainloop 153rescue 154 `echo #$! > /tmp/tkbiff` 155end 156