[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A Ruby script to add email disclaimers

Johann Spies

6/6/2005 10:07:00 AM

A few years after my first contact with ruby (and not using it) I am
like a newbie again.

I want to use a ruby script to add a disclaimer to a message passed to
it by exim.

So far I can add a disclaimer to a message file by the following
script:
=============================
#!/usr/bin/ruby

require 'tmail'

mail = TMail::Mail.load( ARGV[0])

disklymer = "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"

if not mail.key?("X-Disclaimer") then
mail.[]=("X-Disclaimer","added")
if mail.multipart? then
mail.preamble += disklymer

else
mail.body += disklymer
end
end
puts mail.encoded
==============================

But when exim calls the script the message is not saved in a temporary
file. The script should thus be able to handle a message passed to it
like in the following command:

cat <email-message> | disclaimerscript.rb

I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
did not have success.

I would appreciate some help with this one please.

Regards
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"Therefore being justified by faith, we have peace with
God through our Lord Jesus Christ." Romans 5:1


3 Answers

Robert Klemme

6/6/2005 10:28:00 AM

0

Johann Spies wrote:
> A few years after my first contact with ruby (and not using it) I am
> like a newbie again.
>
> I want to use a ruby script to add a disclaimer to a message passed to
> it by exim.
>
> So far I can add a disclaimer to a message file by the following
> script:
> =============================
> #!/usr/bin/ruby
>
> require 'tmail'
>
> mail = TMail::Mail.load( ARGV[0])
>
> disklymer = "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"
>
> if not mail.key?("X-Disclaimer") then
> mail.[]=("X-Disclaimer","added")
> if mail.multipart? then
> mail.preamble += disklymer
>
> else
> mail.body += disklymer
> end
> end
> puts mail.encoded
> ==============================
>
> But when exim calls the script the message is not saved in a temporary
> file. The script should thus be able to handle a message passed to it
> like in the following command:
>
> cat <email-message> | disclaimerscript.rb
>
> I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
> did not have success.
>
> I would appreciate some help with this one please.

I'm not 100% sure about your use case. If your script is invoked like
this:

add-disclaimer.rb mail-file

then you can use option "-i.bak" which does in place modification and
saves the original in a backup file (or leave the ".bak" in that case you
get just the modified file).

If you want to use it in a pipe context, I'd try

mail = TMail::Mail.load( ARGF )

or

mail = TMail::Mail.load( STDIN )

Dunno which of the two TMail can handle. A quick glance at the sources
reveales that maybe you have to implement a sub class of Port to handle
reading from streams.

Kind regards

robert

Brian Schröder

6/6/2005 10:28:00 AM

0

On 06/06/05, Johann Spies <jspies@sun.ac.za> wrote:
> A few years after my first contact with ruby (and not using it) I am
> like a newbie again.
>
> I want to use a ruby script to add a disclaimer to a message passed to
> it by exim.
>
> So far I can add a disclaimer to a message file by the following
> script:
> =============================
> #!/usr/bin/ruby
>
> require 'tmail'
>
> mail = TMail::Mail.load( ARGV[0])
>
> disklymer = "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"
>
> if not mail.key?("X-Disclaimer") then
> mail.[]=("X-Disclaimer","added")
> if mail.multipart? then
> mail.preamble += disklymer
>
> else
> mail.body += disklymer
> end
> end
> puts mail.encoded
> ==============================
>
> But when exim calls the script the message is not saved in a temporary
> file. The script should thus be able to handle a message passed to it
> like in the following command:
>
> cat <email-message> | disclaimerscript.rb
>
> I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
> did not have success.
>
> I would appreciate some help with this one please.
>
> Regards
> Johann
> --
> Johann Spies Telefoon: 021-808 4036
> Informasietegnologie, Universiteit van Stellenbosch
>
> "Therefore being justified by faith, we have peace with
> God through our Lord Jesus Christ." Romans 5:1
>
>

$ echo "this is a test" | ruby -e 'puts $stdin.read; puts "Disclaimer"'
this is a test
Disclaimer

regards,

Brian

--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


Johann Spies

6/6/2005 10:39:00 AM

0

On Mon, Jun 06, 2005 at 07:28:22PM +0900, Brian Schröder wrote:

>
> $ echo "this is a test" | ruby -e 'puts $stdin.read; puts "Disclaimer"'
> this is a test
> Disclaimer

Thanks. Also thanks to Robert Klemme.

The use of $stdin.read was wat I was looking for:

mail = TMail::Mail.parse( $stdin.read )

does the job.

Regards

Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"Therefore being justified by faith, we have peace with
God through our Lord Jesus Christ." Romans 5:1