Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (96 sloc) 2.132 kb
3db12e8 Initial revision
matz authored
1 #! /usr/local/bin/ruby
2
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
3 require "time"
3db12e8 Initial revision
matz authored
4 require "kconv"
5
6 class String
7 def kjust(len)
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
8 res = ''
9 rlen = 0
10 self.each_char do |char|
11 delta = char.bytesize > 1 ? 2 : 1
12 break if rlen + delta > len
13 rlen += delta
14 res += char
3db12e8 Initial revision
matz authored
15 end
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
16 res += ' ' * (len - rlen) if rlen < len
17 res
3db12e8 Initial revision
matz authored
18 end
19 end
20
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
21 def fromout(date, from, subj)
22 return 0 if !date
23 y, m, d = Time.parse(date).to_a.reverse[4, 3] if date
24 y ||= 0; m ||= 0; d ||= 0
25 from ||= "sombody@somewhere"
26 from.delete!("\r\n")
27 from = from.kconv(Encoding.default_external).kjust(28)
28 subj ||= "(nil)"
29 subj.delete!("\r\n")
30 subj = subj.kconv(Encoding.default_external).kjust(40)
31 printf "%02d/%02d/%02d [%s] %s\n", y%100, m, d, from, subj
32 return 1
3db12e8 Initial revision
matz authored
33 end
34
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
35 def get_mailfile(user)
36 file = user
37 unless user
38 file = ENV['MAIL']
3a47cf3 @nobu * remove trailing spaces.
nobu authored
39 user = ENV['USER'] || ENV['USERNAME'] || ENV['LOGNAME']
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
40 end
3db12e8 Initial revision
matz authored
41
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
42 if file == nil or !File.exist?(file)
43 [ENV['SPOOLDIR'], '/usr/spool', '/var/spool', '/usr', '/var'].each do |m|
44 path = "#{m}/mail/#{user}"
45 if File.exist?(path)
46 file = path
47 break
48 end
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
49 end
50 end
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
51 file
3db12e8 Initial revision
matz authored
52 end
53
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
54 def from_main
55 if ARGV[0] == '-w'
56 wait = true
57 ARGV.shift
3db12e8 Initial revision
matz authored
58 end
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
59 file = get_mailfile(ARGV[0])
60
61 outcount = 0
62 if File.exist?(file)
63 atime = File.atime(file)
64 mtime = File.mtime(file)
65 open(file, "r") do |f|
66 until f.eof?
67 header = {}
68 f.each_line do |line|
69 next if /^From / =~ line # skip From-line
70 break if /^$/ =~ line # end of header
3db12e8 Initial revision
matz authored
71
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
72 if /^(?<attr>\S+?):\s*(?<value>.*)/ =~ line
73 attr.capitalize!
74 header[attr] = value
75 elsif attr
76 header[attr] += "\n" + line.lstrip
77 end
78 end
79
80 f.each_line do |line|
81 break if /^From / =~ line
82 end
83 outcount += fromout(header['Date'], header['From'], header['Subject'])
84 end
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
85 end
86 File.utime(atime, mtime, file)
3db12e8 Initial revision
matz authored
87 end
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
88
89 if outcount == 0
90 print "You have no mail.\n"
91 sleep 2 if wait
92 elsif wait
93 system "stty cbreak -echo"
94 $stdin.getc
95 system "stty cooked echo"
96 end
3db12e8 Initial revision
matz authored
97 end
98
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
99 if __FILE__ == $0
100 from_main
3db12e8 Initial revision
matz authored
101 end
f2c3391 @nurse * sample/from.rb: follow Ruby 1.9 libraries.
nurse authored
102
103 __END__
104
105 =begin
106
107 = from.rb
108
109 == USAGE
110
111 ruby from.rb [-w] [username_or_filename]
112
113 =end
Something went wrong with that request. Please try again.