ext/tk/sample/tkbiff.rb


DEFINITIONS

This source file includes following functions.


   1  #! /usr/local/bin/ruby
   2  
   3  if ARGV[0] != '-d'
   4    unless $DEBUG
   5      exit if fork
   6    end
   7  else
   8    ARGV.shift
   9  end
  10  
  11  if ARGV.length == 0
  12    if ENV['MAIL']
  13      $spool = ENV['MAIL']
  14    else  
  15      $spool = '/usr/spool/mail/' + ENV['USER']
  16    end
  17  else 
  18    $spool = ARGV[0]
  19  end
  20  
  21  require "parsedate"
  22  require "base64"
  23  
  24  include ParseDate
  25  
  26  class 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  
  69  end
  70  
  71  require "tkscrollbox"
  72  
  73  $top = TkRoot.new
  74  $top.withdraw
  75  $list = TkScrollbox.new($top) {
  76    relief 'raised'
  77    width 80
  78    height 8
  79    setgrid 'yes'
  80    pack
  81  }
  82  TkButton.new($top) {
  83    text 'Dismiss'
  84    command proc {$top.withdraw}
  85    pack('fill'=>'both','expand'=>'yes')
  86  }
  87  $top.bind "Control-c", proc{exit}
  88  $top.bind "Control-q", proc{exit}
  89  $top.bind "space", proc{exit}
  90  
  91  $spool_size = 0
  92  $check_time = Time.now
  93  
  94  def check
  95    $check_time = Time.now
  96    size = File.size($spool)
  97    if size and size != $spool_size
  98      $spool_size = size
  99      pop_up if size > 0
 100    end
 101    Tk.after 5000, proc{check}
 102  end
 103  
 104  if defined? Thread
 105    Thread.start do
 106      loop do
 107        sleep 600
 108        if Time.now - $check_time > 200
 109          Tk.after 5000, proc{check}
 110        end
 111      end
 112    end
 113  end
 114  
 115  def pop_up
 116    outcount = 0;
 117    $list.delete 0, 'end'
 118    f = open($spool, "r")
 119    while !f.eof?
 120      mail = Mail.new(f)
 121      date, from, subj =  mail.header['Date'], mail.header['From'], mail.header['Subject']
 122      next if !date
 123      y = m = d = 0
 124      y, m, d = parsedate(date) if date
 125      from = "sombody@somewhere" if ! from
 126      subj = "(nil)" if ! subj
 127      from = decode_b(from)
 128      subj = decode_b(subj)
 129      $list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj)
 130      outcount += 1
 131    end
 132    f.close
 133    if outcount == 0
 134      $list.insert 'end', "You have no mail."
 135    else
 136      $list.see 'end'
 137    end
 138    $top.deiconify
 139    Tk.after 2000, proc{$top.withdraw}
 140  end
 141  
 142  $list.insert 'end', "You have no mail."
 143  check
 144  Tk.after 2000, proc{$top.withdraw}
 145  begin
 146    Tk.mainloop
 147  rescue
 148    `echo #$! > /tmp/tkbiff`
 149  end