Forum: Ruby Exim mail filter in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
374717e4aceb8b92431f3bd9b5673636?d=identicon&s=25 chrisjroos (Guest)
on 2005-11-30 17:43
(Received via mailing list)
Hi,

I have a requirement to create a shell script that accepts data on it's
standard input, calls another shell script (out of my control), passing
an amended copy of the original input, and collects anything sent to
stderr and stdout by the external script.  This shell script is to be
used as a mail filter being called from exim.

I got a basic version working using Kernel.open but got very confused
when trying to trap the output of that external script (stderr and
stdout).

The best looking code was produced when using Session[1] but that
started causing me problems too - it looked as though either my parent
or child process didn't wait until the other finished (i.e. i could see
the logged output not following the expected program flow; I fully
expect this to be something I was doing wrong).

I am now using open3 which is working but feels a little fragile,
although that's probably completely unfounded.

I guess I'm really asking for other peoples approaches to similar
problems.  My concerns really arise from the fact that I'm not able to
fully test the interaction of this process (or at least not easily).
Does anyone have any tips regarding shell scripting with pipes / forks
/ all the other things I don't really understand?

Chris

[1] http://www.codeforpeople.com/lib/ruby/session/
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 hgs (Guest)
on 2005-11-30 18:03
(Received via mailing list)
On Thu, 1 Dec 2005, chrisjroos@gmail.com wrote:

> Hi,
>
> I have a requirement to create a shell script that accepts data on it's
> standard input, calls another shell script (out of my control), passing
> an amended copy of the original input, and collects anything sent to
> stderr and stdout by the external script.  This shell script is to be
> used as a mail filter being called from exim.

I don't find this description very clear.  Which part of exim are
you using to do this?  That might help me understand.
>
> I got a basic version working using Kernel.open but got very confused
> when trying to trap the output of that external script (stderr and
> stdout).

What about using the shell in  popen("/bin/sh command 2>&1", "r+") ?

The stdout and stderr are combined, but you can still write to it.

        Hugh
374717e4aceb8b92431f3bd9b5673636?d=identicon&s=25 chrisjroos (Guest)
on 2005-11-30 18:56
(Received via mailing list)
In exim, I create a transport that uses a pipe to send the email to my
shell script.  I also create a router that sends every mail that exim
processes to my transport (providing I haven't already processed it).

My shell script reads the email from stdin and parses it based on some
custom rules.

If it passes the rule checks it can be sent back into exim.  This is
achieved by writing to the stdin of the exim command with some
additional parameters.  Once I've given the email back to exim, I want
to check for data on it's (exim command) stdout and stderr so that I
can log any problems.

The interaction with exim is straight from the exim book and all works
ok.  The question I have is really about the best way to deal with
piping data to/from different processes within ruby in addition to
trying to understand what's going on better.

I did use IO.popen to start with and can't now remember the problems I
had, probably just ignorance on my part..

Chris
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 hgs (Guest)
on 2005-11-30 21:02
(Received via mailing list)
On Thu, 1 Dec 2005, chrisjroos@gmail.com wrote:

> to check for data on it's (exim command) stdout and stderr so that I
> can log any problems.

OK, sounds pretty std.
>
> The interaction with exim is straight from the exim book and all works
> ok.  The question I have is really about the best way to deal with
> piping data to/from different processes within ruby in addition to
> trying to understand what's going on better.

I needed to understand where the data was flowing: that's why I
asked about how it interacted with exim.
>
> I did use IO.popen to start with and can't now remember the problems I
> had, probably just ignorance on my part..

then I think what I wrote should work.  Maybe give it a spin or
compare it to the version you used unless you threw it out already
:-)
>
> Chris
>

        Hugh
This topic is locked and can not be replied to.