Hi all,

I have been trying to set up a small Ruby script as a spam filter. It 
would be used as a content_filter from Postfix, and then pass the 
message to SpamAssassin. I didn't use SA as a direct content filter 
because I wanted the flexibility to be able to do things like pick and 
choose which email addresses get filtered.

Anyway, I set this up and it worked fine. Then towards the end of last 
week people were having problems sending emails with attachments to us. 
Postfix was bouncing them complaining that the content_filter was taking 
too long to run.

I have spent a long time attempting to debug the issue. It basically 
appears to come down to Ruby treating the base64 encoded attachment 
really weirdly when doing IO stuff.

I have this small script (you can ignore the gsub it doesn't make a 
difference):

text = $stdin.read.gsub(/([`\$\\"])/, '\\\\\1')
command = 'echo "' + text + '"'
output = `#{command}`

Then I run:

$ cat message.txt | ruby test.rb

Theoretically it shouldn't output anything. But it does. It turns out 
the output is STDERR, so:

$ cat message.txt | ruby test.rb 2>&1 | head
test.rb:3: command not found: echo 
"691737r3Xvfuvde9+691737r3UWs/wCA7/7D/ex7917oLdwfof8A2P8AvQ9+690Hu2117urU+urY
O71t/wAGyG0x/wAT7chNJoj/AEh/h6bmzDKP6J/wdOX8P/2j/eP+NexB4vz6IfD+XVeHzY7Z7A6y
7P8AihtDau+98bC2t2pvLfeG3/V9bdd4XsvfFbjMLtzHZHGJt3buR627TyVRWQVspLLRYmokaJ3L
qVXUhZb3Tz81W+1y3Eq2B2y7mIjTUxkiXVGcI7BdVA/BQhZmKga1MHt0j5c3DcEhja8TcLKJS7aV
EczSrKMuiliqgpUli4VVDFtLAl1n88s5sD48025u68hjN7dj5X5Mbt6F2mN3122emMhQ0lJmZ3x2
Z+QcUOGjoOnJdv4WKaTIKMK1THTilY0jNNJP7UQbg9xY8jI628e77jtrTTnxaWyPbuFuNMgM9X0v
EREjSapX0KyghVansBBuHOL1mO02UyeAPCLTypPCHtgkZEZZZisjCZ/DQKsvnGqupW/mW42XalHk
sL1Rht47qX5N4P425PF7J7fxe4Nk1tTuakyNdt7fOxOyRs6kx+7sHl6WhHihqKLEujsyyvGFDM7Y
zy7lPy1Bawsr7ibyM+LWPwZrR4lZWoHLxMs8cnjR6qqWMSTDSXbubeOzi5klnmVk26zt7r9Pv8SO
4Vjp7imiZGjkR0NVDKKuKkK+1/8AMYwG3di7gyW8eu8RtjsbB/JnKfF1tsV3Z9HT9dR7rw4gqchv

$ echo "hi" | ruby test.rb
#-> No output

So I think... right, something in that file has to be triggering the 
problem. I cut the file in half to keep narrowing down the problem. I 
take the top off and run it... no output. I cut the bottom off and run 
it... no output. But with top and bottom... I get this weird STDERR 
output.

Also, I'd note that I've tried other methods... Kernel#system works 
okay, but I need the output. %x[] doesn't (that's Kernel#` anyway). 
IO.popen doesn't work. IO.popen then writing to the process doesn't work 
(which is what I was doing originally). Stranger still, if I write to a 
temporary file it doesn't all write correctly. It's stops writing at a 
random point in the string. Is it just me or does this behaviour totally 
not make any sense?

I would be really really seriously grateful if anyone can help me out :)

Thanks very much,
Jon

The files:
http://clients.dunwoodydev.co.uk/jon/message.txt
http://clients.dunwoodydev.co.uk/jon/test.rb

-- 
Posted via http://www.ruby-forum.com/.