ext/tk/sample/tkfrom.rb


DEFINITIONS

This source file includes following functions.


   1  #! /usr/local/bin/ruby
   2  
   3  require "parsedate"
   4  require "base64"
   5  
   6  include ParseDate
   7  
   8  class Mail
   9    def Mail.new(f)
  10      if !f.kind_of?(IO)
  11        f = open(f, "r")
  12        me = super(f)
  13        f.close
  14      else
  15        me = super
  16      end
  17      return me
  18    end
  19  
  20    def initialize(f)
  21      @header = {}
  22      @body = []
  23      while line = f.gets()
  24        $_.chop!
  25        next if /^From / =~ line  # skip From-line  
  26        break if /^$/ =~ line     # end of header
  27        if /^(\S+):\s*(.*)/ =~ line
  28          @header[attr = $1.capitalize] = $2
  29        elsif attr
  30          sub(/^\s*/, '')
  31          @header[attr] += "\n" + $_
  32        end
  33      end
  34  
  35      return unless $_
  36  
  37      while line = f.gets()
  38        break if /^From / =~ line
  39        @body.push($_)
  40      end
  41    end
  42  
  43    def header
  44      return @header
  45    end
  46  
  47    def body
  48      return @body
  49    end
  50  
  51  end
  52  
  53  if ARGV.length == 0
  54    if ENV['MAIL']
  55      ARGV[0] = ENV['MAIL']
  56    elsif ENV['USER']
  57      ARGV[0] = '/usr/spool/mail/' + ENV['USER']
  58    elsif ENV['LOGNAME']
  59      ARGV[0] = '/usr/spool/mail/' + ENV['LOGNAME']
  60    end
  61  end
  62  
  63  require "tk"
  64  list = scroll = nil
  65  TkFrame.new{|f|
  66    list = TkListbox.new(f) {
  67      yscroll proc{|idx|
  68          scroll.set *idx
  69      }
  70      relief 'raised'
  71  #    geometry "80x5"
  72      width 80
  73      height 5
  74      setgrid 'yes'
  75      pack('side'=>'left','fill'=>'both','expand'=>'yes')
  76    }
  77    scroll = TkScrollbar.new(f) {
  78      command proc{|idx|
  79        list.yview *idx
  80      }
  81      pack('side'=>'right','fill'=>'y')
  82    }
  83    pack
  84  }
  85  root = Tk.root
  86  TkButton.new(root) {
  87    text 'Dismiss'
  88    command proc {exit}
  89    pack('fill'=>'both','expand'=>'yes')
  90  }
  91  root.bind "Control-c", proc{exit}
  92  root.bind "Control-q", proc{exit}
  93  root.bind "space", proc{exit}
  94  
  95  $outcount = 0;
  96  for file in ARGV
  97    next if File.exist?(file)
  98    atime = File.atime(file)
  99    mtime = File.mtime(file)
 100    f = open(file, "r")
 101    begin
 102      until f.eof
 103        mail = Mail.new(f)
 104        date = mail.header['Date']
 105        next unless date
 106        from = mail.header['From']
 107        subj = mail.header['Subject']
 108        y = m = d = 0
 109        y, m, d = parsedate(date) if date
 110        from = "sombody@somewhere" unless from
 111        subj = "(nil)" unless subj
 112        from = decode_b(from)
 113        subj = decode_b(subj)
 114        list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj)
 115        $outcount += 1
 116      end
 117    ensure
 118      f.close
 119      File.utime(atime, mtime, file)
 120      list.see 'end'
 121    end
 122  end
 123  
 124  limit = 10000
 125  if $outcount == 0
 126    list.insert 'end', "You have no mail."
 127    limit = 2000
 128  end
 129  Tk.after limit, proc{
 130    exit
 131  }
 132  Tk.mainloop