home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / from.rb < prev    next >
Text File  |  1999-08-13  |  2KB  |  99 lines

  1. #! /usr/local/bin/ruby
  2.  
  3. require "parsedate"
  4. require "kconv"
  5. require "mailread"
  6.  
  7. include ParseDate
  8. include Kconv
  9.  
  10. class String
  11.  
  12.   def kconv(code = Kconv::EUC)
  13.     Kconv.kconv(self, code, Kconv::AUTO)
  14.   end
  15.  
  16.   def kjust(len)
  17.     len += 1
  18.     me = self[0, len].ljust(len)
  19.     if me =~ /.$/ and $&.size == 2
  20.       me[-2..-1] = '  '
  21.       me[-2, 2] = '  '
  22.     end
  23.     me.chop!
  24.   end
  25.  
  26. end
  27.  
  28. if ARGV[0] == '-w'
  29.   wait = TRUE
  30.   ARGV.shift
  31. end
  32.  
  33. if ARGV.length == 0
  34.   file = ENV['MAIL']
  35.   user = ENV['USER'] || ENV['USERNAME'] || ENV['LOGNAME'] 
  36. else
  37.   file = user = ARGV[0]
  38.   ARGV.clear
  39. end
  40.  
  41. if file == nil or !File.exist? file
  42.   [ENV['SPOOLDIR'], '/usr/spool', '/var/spool', '/usr', '/var'].each do |m|
  43.     if File.exist? f = "#{m}/mail/#{user}"
  44.       file = f
  45.       break 
  46.     end
  47.   end
  48. end
  49.  
  50. $outcount = 0;
  51. def fromout(date, from, subj)
  52.   return if !date
  53.   y = m = d = 0
  54.   y, m, d = parsedate(date) if date
  55.   if from
  56.     from.gsub! /\n/, ""
  57.   else
  58.     from = "sombody@somewhere"
  59.   end
  60.   if subj
  61.     subj.gsub! /\n/, ""
  62.   else
  63.     subj = "(nil)"
  64.   end
  65.   if ENV['LANG'] =~ /sjis/i
  66.     lang = Kconv::SJIS
  67.   else
  68.     lang = Kconv::EUC
  69.   end
  70.   from = from.kconv(lang).kjust(28)
  71.   subj = subj.kconv(lang).kjust(40)
  72.   printf "%02d/%02d/%02d [%s] %s\n",y%100,m,d,from,subj
  73.   $outcount += 1
  74. end
  75.  
  76. if File.exist?(file)
  77.   atime = File.atime(file)
  78.   mtime = File.mtime(file)
  79.   f = open(file, "r")
  80.   begin
  81.     until f.eof?
  82.       mail = Mail.new(f)
  83.       fromout mail.header['Date'],mail.header['From'],mail.header['Subject']
  84.     end
  85.   ensure
  86.     f.close
  87.     File.utime(atime, mtime, file)
  88.   end
  89. end
  90.  
  91. if $outcount == 0
  92.   print "You have no mail.\n"
  93.   sleep 2 if wait
  94. elsif wait
  95.   system "stty cbreak -echo"
  96.   getc()
  97.   system "stty cooked echo"
  98. end
  99.