Minor fix for compatibility with Ruby versions that don't understand
[mailvisa.git] / remove_messages.rb
blobc453c027463a5b637dd5064990345f7510fb89e0
1 require 'wordlist'
2 require 'tokenize'
4 ### Defaults
5 confdir = ENV['HOME'] + '/settings/mailvisa'
6 $ignorespamheaders = true
7 filename = nil
8 messages = []
10 ### Functions
11 def remove_stream words, stream
12         stream.each do |line|
13                 next if line[0,6] == 'X-Spam'
14                 tokenize(line).each do |word|
15                         next if word.length > 40
16                         count = words[word]
17                         case count
18                         when nil
19                                 nil
20                         when 1
21                                 words[word] = nil
22                         else
23                                 words[word] = count - 1
24                         end
25                 end
26         end
27 end
29 usage = 'USAGE: ' + $0 + ' [options] <wordlist> [<message> ...]'
31 help = <<EOT
32 Valid options are:
34 -c <path>       Look for configuration files in <path>
35                         (default: $HOME/settings/mailvisa)
36 -i              Include X-Spam headers in analysis
37 EOT
39 ### Main program
41 ## Process command line
42 i = 0
43 while i < ARGV.length
44         case ARGV[i]
45         when '-h'
46                 puts usage
47                 print "\n" + help
48                 exit
49         when '-c'
50                 i = i + 1
51                 confdir = ARGV[i]
52         when /^-/
53                 $stderr.puts 'Invalid option: ' + ARGV[i]
54                 $stderr.puts usage
55                 exit 0x80
56         else
57                 filename = ARGV[i]
58                 i = i + 1
59                 messages = ARGV[i..-1]
60                 break
61         end
62         i = i + 1
63 end
65 if filename == nil
66         $stderr.puts 'No wordlist specified'
67         $stderr.puts usage
68         exit 0x80
69 end
71 filename = confdir + '/' + filename if filename.index('/') == nil
73 begin
74         fh = open filename
75 rescue
76         print filename + ' not found, will create new file'
77         fh = false
78 end
80 if fh
81         $stderr.print "Loading #{filename}..."
82         wordlist = load_wordlist fh
83         fh.close
84         words = wordlist[:words]
85         message_count = wordlist[:messages]
86         $stderr.puts words.length.to_s + ' words loaded'
87 else
88         words = {}
89         message_count = 0
90 end
92 if messages.length > 0
93         messages.each do |x|
94                 $stderr.puts "Removing #{x}"
95                 fh = open x
96                 remove_stream words, fh
97                 fh.close
98                 message_count = message_count - 1
99         end
100 else
101         remove_stream words, $stdin
102         message_count = message_count - 1
106 message_count = 0 if message_count < 0
108 wordlist = {
109         :messages => message_count,
110         :words => words
113 $stderr.print "Writing #{filename}..."
114 fh = open filename, 'w'
115 dump_wordlist wordlist, fh
116 fh.close
117 $stderr.puts 'done'