1998-01-16 07:13:05 -05:00
|
|
|
#! /usr/local/bin/ruby
|
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
require "time"
|
1998-01-16 07:13:05 -05:00
|
|
|
require "kconv"
|
|
|
|
|
|
|
|
class String
|
|
|
|
def kjust(len)
|
2007-12-24 22:23:15 -05:00
|
|
|
res = ''
|
|
|
|
rlen = 0
|
|
|
|
self.each_char do |char|
|
|
|
|
delta = char.bytesize > 1 ? 2 : 1
|
|
|
|
break if rlen + delta > len
|
|
|
|
rlen += delta
|
|
|
|
res += char
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2007-12-24 22:23:15 -05:00
|
|
|
res += ' ' * (len - rlen) if rlen < len
|
|
|
|
res
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
def fromout(date, from, subj)
|
|
|
|
return 0 if !date
|
|
|
|
y, m, d = Time.parse(date).to_a.reverse[4, 3] if date
|
|
|
|
y ||= 0; m ||= 0; d ||= 0
|
|
|
|
from ||= "sombody@somewhere"
|
|
|
|
from.delete!("\r\n")
|
|
|
|
from = from.kconv(Encoding.default_external).kjust(28)
|
|
|
|
subj ||= "(nil)"
|
|
|
|
subj.delete!("\r\n")
|
|
|
|
subj = subj.kconv(Encoding.default_external).kjust(40)
|
|
|
|
printf "%02d/%02d/%02d [%s] %s\n", y%100, m, d, from, subj
|
|
|
|
return 1
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
def get_mailfile(user)
|
|
|
|
file = user
|
|
|
|
unless user
|
|
|
|
file = ENV['MAIL']
|
2011-05-15 07:55:52 -04:00
|
|
|
user = ENV['USER'] || ENV['USERNAME'] || ENV['LOGNAME']
|
2007-12-24 22:23:15 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
if file == nil or !File.exist?(file)
|
|
|
|
[ENV['SPOOLDIR'], '/usr/spool', '/var/spool', '/usr', '/var'].each do |m|
|
|
|
|
path = "#{m}/mail/#{user}"
|
|
|
|
if File.exist?(path)
|
2013-07-20 09:41:02 -04:00
|
|
|
file = path
|
|
|
|
break
|
2007-12-24 22:23:15 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-24 22:23:15 -05:00
|
|
|
file
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
def from_main
|
|
|
|
if ARGV[0] == '-w'
|
|
|
|
wait = true
|
|
|
|
ARGV.shift
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2007-12-24 22:23:15 -05:00
|
|
|
file = get_mailfile(ARGV[0])
|
|
|
|
|
|
|
|
outcount = 0
|
|
|
|
if File.exist?(file)
|
|
|
|
atime = File.atime(file)
|
|
|
|
mtime = File.mtime(file)
|
|
|
|
open(file, "r") do |f|
|
|
|
|
until f.eof?
|
2013-07-20 09:41:02 -04:00
|
|
|
header = {}
|
|
|
|
f.each_line do |line|
|
|
|
|
next if /^From / =~ line # skip From-line
|
|
|
|
break if /^$/ =~ line # end of header
|
|
|
|
|
|
|
|
if /^(?<attr>\S+?):\s*(?<value>.*)/ =~ line
|
|
|
|
attr.capitalize!
|
|
|
|
header[attr] = value
|
|
|
|
elsif attr
|
|
|
|
header[attr] += "\n" + line.lstrip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
f.each_line do |line|
|
|
|
|
break if /^From / =~ line
|
|
|
|
end
|
|
|
|
outcount += fromout(header['Date'], header['From'], header['Subject'])
|
2007-12-24 22:23:15 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
File.utime(atime, mtime, file)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2007-12-24 22:23:15 -05:00
|
|
|
|
|
|
|
if outcount == 0
|
|
|
|
print "You have no mail.\n"
|
|
|
|
sleep 2 if wait
|
|
|
|
elsif wait
|
|
|
|
system "stty cbreak -echo"
|
|
|
|
$stdin.getc
|
|
|
|
system "stty cooked echo"
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2007-12-24 22:23:15 -05:00
|
|
|
if __FILE__ == $0
|
|
|
|
from_main
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2007-12-24 22:23:15 -05:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
= from.rb
|
|
|
|
|
|
|
|
== USAGE
|
|
|
|
|
|
|
|
ruby from.rb [-w] [username_or_filename]
|
|
|
|
|
|
|
|
=end
|