7 confdir = ENV['HOME'] + '/settings/mailvisa'
8 sockpath = 'mailvisad.sock'
10 pidfile = 'mailvisad.pid'
11 logfile = 'mailvisad.log'
24 def most_interesting words
25 ## Sort words by extremity
26 interesting = words.sort do |x, y|
27 (0.5 - get_score(y)).abs <=> (0.5 - get_score(x)).abs
29 # Return the most interesting words
33 def spam_probability scores
35 scores.each { |x| prod = prod * x }
37 scores.each { |x| div = div * (1 - x) }
41 usage = 'USAGE: ' + $0 + ' [options]'
46 -c <path> Look for files in <path> (default: $HOME/settings/mailvisa)
47 -f <path> Load scores from <path> (default: scores)
48 -l <path> Log to <path> (default: mailvisad.log)
49 -p <path> Use <path> as pidfile (default: mailvisad.pid)
50 -s <path> Use <path> as socket (default: mailvisad.sock)
55 ## Process command line
79 $stderr.puts 'Invalid option: ' + ARGV[i]
81 $stderr.puts 'Use "' + $0 + ' -h" for help'
87 sockpath = confdir + '/' + sockpath if sockpath.index('/') == nil
88 scorefile = confdir + '/' + scorefile if scorefile.index('/') == nil
89 pidfile = confdir + '/' + pidfile if pidfile.index('/') == nil
90 logfile = confdir + '/' + logfile if logfile.index('/') == nil
92 ## Define procedure to load configuration
93 load_config = lambda do
94 db = load_wordlist open(scorefile)
99 trap('SIGHUP') { load_config.call }
102 scorefh = open scorefile
106 sock = UNIXServer.open sockpath
107 rescue Errno::EADDRINUSE
108 ## Figure out if the socket is actually live
110 sock = UNIXSocket.new sockpath
111 # Aye, inform user and exit
112 $stderr.puts 'mailvisad already bound to ' + sockpath
115 # Nope, remove and try again
116 if File.exists?(sockpath) && File.ftype(sockpath) == 'socket'
118 sock = UNIXServer.open sockpath
124 $stderr = open logfile, 'a'
127 # Fork child, exit parent
128 exit if fork # Fork off a child, exit parent
130 # Set at_exit handler
133 File.unlink sockpath if
134 File.exists?(sockpath) && File.ftype(sockpath) == 'socket'
137 ## Detach from terminal
142 fh = open pidfile, 'w'
147 $scores = load_wordlist(scorefh)[:words]
153 message = conn.read 16384
154 words = most_interesting tokenize(message)
155 scores = words.map { |x| get_score x }
156 probability = (spam_probability(scores) * 100).to_i / 100.0
158 conn.puts probability