#!/usr/bin/env ruby # Some info: # - correctly handles charset/encoding in Subject # - correctly handles various from formats # - correctly strips message to 160 (bit less, though) chars, if requested # - correctly(?) strips quoted lines and replied/forwarded parts # - incorrectly handles content of message - doesn't attempt to fix/recognize # multiparts, content encodings, content charsets # This is serious bug. But I'm not going to implement whole MIME module # now, just to fix it. Maybe later. $conf = Hash.new #----------------=[ Config section # Recipient of the announce? $conf['to'] = 'to@example.com' # Prefix before 'from' address $conf['from'] = 'B' # Envelope sender $conf['fromeml'] = 'from@example.com' # Flag file to disable sms sending $conf['flagfile'] = '/home/wejn/.no.nmsms' # Don't limit sms to 160 chars $conf['unlimited'] = false # Path to recoding program $conf['recodeby'] = '/home/wejn/bin/cconv' # Path to sending program #$conf['sendby'] = "/var/qmail/bin/qmail-inject -h -f%%FROM%%" $conf['sendby'] = "/usr/sbin/sendmail -t -f%%FROM%%" #----------------=[ End of config section # Commandline parsing def usage puts <:]/ $contentadd = false if x =~ /^---?\s*$/ if x =~ /^-*\s*Forwarded\s*mess.*-*\s*$/ || x =~ /^-*\s*Original\s*Message\s*-/ || x =~ /^On\s*.*\s*wrote:\s*$/ $contentadd = false next end $content += x end next end # Non body part here ... find from and subject x.chomp! case x when /^From:/; $from = x.sub(/^From:\s*/, '') if $from.nil? when /^Subject:/; $subject = x.sub(/^Subject:\s*/, '') if $subject.nil? end } # -- # Perform some parsing # -- # Get email from: 'email (name)' format $from.sub!(/^([^@]*@[^@ ]*)\s*\(.*\)\s*$/, '\1') # Get email from: 'name ' format $from.sub!(/^[^<]*<([^@]*@[^@]*)>\s*$/, '\1') # Parse && recode subject -- this should be MIME compatible if $subject =~ /=\?[^?]*\?.\?.*\?=/ def wTranslate(x) x = x.gsub!(/(^=\?|\?=$)/, '') x = x.split("?", -1) case x[1].downcase when 'q'; x[2] = x[2].gsub(/_/, ' ').unpack('M')[0] when 'b'; x[2] = x[2].unpack('m')[0] end case x[0].downcase #when 'windows-1252'; ie = '1252' when 'windows-1250'; ie = '1250' when 'iso-8859-2'; ie = 'il2' when 'iso-8859-1'; ie = 'il2' # glitch, i know else; ie = nil end x[2] = `#{$conf['recodeby']} #{ie} ascii '#{x[2]}'` unless ie.nil? x[2].chomp end $subject.gsub!(/=\?.*?\?=/) { |x| wTranslate(x) } end # -- # Determine whether we will sending it # -- case $from when /^mailer-daemon@/i; $send = false when /^postmaster@/i; $send = false else; $send = true end $send = false if test ?f, $conf['flagfile'] # -- # Send it ... # -- if $send open("|#{$conf['sendby'].gsub(/%%FROM%%/, $conf['fromeml'])}", 'w') { |x| $bsent = 12 # Magic, don't change ('trash' chars we can't get rid of) realfrom = "#{$conf['from']}=#$from" x.puts("From: #{realfrom}") $bsent += realfrom.length x.puts("To: #{$conf['to']}") x.puts("\n") tosend = "#$subject;#$content" tosend = tosend[Range.new(0, 160 - $bsent)] unless $conf['unlimited'] x.puts(tosend) } end # -- # Be happy now # -- exit 0 # vim: set ft=ruby ts=4 sw=4 :