Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 98 lines (86 sloc) 1.737 kb
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
#! /usr/local/bin/ruby

require "parsedate"
require "kconv"
require "mailread"

include ParseDate
include Kconv

class String

  def kconv(code = Kconv::EUC)
    Kconv.kconv(self, code, Kconv::AUTO)
  end

  def kjust(len)
    len += 1
    me = self[0, len].ljust(len)
    if me =~ /.$/ and $&.size == 2
      me[-2..-1] = ' '
      me[-2, 2] = ' '
    end
    me.chop!
  end

end

if ARGV[0] == '-w'
  wait = true
  ARGV.shift
end

if ARGV.length == 0
  file = ENV['MAIL']
  user = ENV['USER'] || ENV['USERNAME'] || ENV['LOGNAME']
else
  file = user = ARGV[0]
  ARGV.clear
end

if file == nil or !File.exist? file
  [ENV['SPOOLDIR'], '/usr/spool', '/var/spool', '/usr', '/var'].each do |m|
    if File.exist? f = "#{m}/mail/#{user}"
      file = f
      break
    end
  end
end

$outcount = 0;
def fromout(date, from, subj)
  return if !date
  y, m, d = parsedate(date) if date
  y ||= 0; m ||= 0; d ||= 0
  if from
    from.gsub! /\n/, ""
  else
    from = "sombody@somewhere"
  end
  if subj
    subj.gsub! /\n/, ""
  else
    subj = "(nil)"
  end
  if ENV['LANG'] =~ /sjis/i
    lang = Kconv::SJIS
  else
    lang = Kconv::EUC
  end
  from = from.kconv(lang).kjust(28)
  subj = subj.kconv(lang).kjust(40)
  printf "%02d/%02d/%02d [%s] %s\n",y%100,m,d,from,subj
  $outcount += 1
end

if File.exist?(file)
  atime = File.atime(file)
  mtime = File.mtime(file)
  f = open(file, "r")
  begin
    until f.eof?
      mail = Mail.new(f)
      fromout mail.header['Date'],mail.header['From'],mail.header['Subject']
    end
  ensure
    f.close
    File.utime(atime, mtime, file)
  end
end

if $outcount == 0
  print "You have no mail.\n"
  sleep 2 if wait
elsif wait
  system "stty cbreak -echo"
  getc()
  system "stty cooked echo"
end
Something went wrong with that request. Please try again.