[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

gsub and regexes on binary files...

Kyle Schmitt

1/17/2008 3:33:00 PM

I'm having a few issues with gsubbing a regex on a binary file.
With all text, this seems to work great, but with the binary junk in a
file, only some of the instances are replaced, not all.

What this script is supposed to do, is pull mail message out of
quarantine, change who it's going to, and put it back in postfix's
queue to send it on its way.

This is a super-simplified version of the code in question (just
in-case you're wondering, if you really write something like this, you
need to change the ownership and permission of the resulting file,
otherwise postfix won't pick it up).

#!/usr/bin/ruby
message_path="/var/spool/MailScanner/quarantine/20080111/ABCDEFG"
postfix_outgoing_path="/var/spool/postfix/incoming/A/ABCDEFG"
message=File.open(message_path,"rb"){|f| f.read()}
message.gsub(/[a-z][a-z0-9]*@candycompany.foo/i,"review-spam@candycompany.foo")
outgoing_message=File.open(postfix_outgoing_path,"wb"){|f| f.write(message)}

Err. Since the real version of this script is rather lengthy and a
little ugly, mostly due to the command-line-option parsing, anyone
have a suggestion on a good CLI library to use :) my home-grown
attempt is functional at best...

Thanks,
Kyle

2 Answers

Alex LeDonne

1/17/2008 9:01:00 PM

0

On Jan 17, 2008 10:32 AM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> I'm having a few issues with gsubbing a regex on a binary file.
> With all text, this seems to work great, but with the binary junk in a
> file, only some of the instances are replaced, not all.

Could you send an example where the regex failed? Then we can try to
figure out what's different about the failure case.

-A



> What this script is supposed to do, is pull mail message out of
> quarantine, change who it's going to, and put it back in postfix's
> queue to send it on its way.
>
> This is a super-simplified version of the code in question (just
> in-case you're wondering, if you really write something like this, you
> need to change the ownership and permission of the resulting file,
> otherwise postfix won't pick it up).
>
> #!/usr/bin/ruby
> message_path="/var/spool/MailScanner/quarantine/20080111/ABCDEFG"
> postfix_outgoing_path="/var/spool/postfix/incoming/A/ABCDEFG"
> message=File.open(message_path,"rb"){|f| f.read()}
> message.gsub(/[a-z][a-z0-9]*@candycompany.foo/i,"review-spam@candycompany.foo")
> outgoing_message=File.open(postfix_outgoing_path,"wb"){|f| f.write(message)}
>
> Err. Since the real version of this script is rather lengthy and a
> little ugly, mostly due to the command-line-option parsing, anyone
> have a suggestion on a good CLI library to use :) my home-grown
> attempt is functional at best...
>
> Thanks,
> Kyle

Kyle Schmitt

1/17/2008 11:04:00 PM

0

I'll grab one off of the machine tomorrow. Due to some of the
security stuff, you can only get console access to that box. Ugh.

Thanks
--Kyle

On Jan 17, 2008 3:00 PM, Alex LeDonne <aledonne.listmail@gmail.com> wrote:
> On Jan 17, 2008 10:32 AM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> > I'm having a few issues with gsubbing a regex on a binary file.
> > With all text, this seems to work great, but with the binary junk in a
> > file, only some of the instances are replaced, not all.
>
> Could you send an example where the regex failed? Then we can try to
> figure out what's different about the failure case.
>
> -A
>
>
>
>
> > What this script is supposed to do, is pull mail message out of
> > quarantine, change who it's going to, and put it back in postfix's
> > queue to send it on its way.
> >
> > This is a super-simplified version of the code in question (just
> > in-case you're wondering, if you really write something like this, you
> > need to change the ownership and permission of the resulting file,
> > otherwise postfix won't pick it up).
> >
> > #!/usr/bin/ruby
> > message_path="/var/spool/MailScanner/quarantine/20080111/ABCDEFG"
> > postfix_outgoing_path="/var/spool/postfix/incoming/A/ABCDEFG"
> > message=File.open(message_path,"rb"){|f| f.read()}
> > message.gsub(/[a-z][a-z0-9]*@candycompany.foo/i,"review-spam@candycompany.foo")
> > outgoing_message=File.open(postfix_outgoing_path,"wb"){|f| f.write(message)}
> >
> > Err. Since the real version of this script is rather lengthy and a
> > little ugly, mostly due to the command-line-option parsing, anyone
> > have a suggestion on a good CLI library to use :) my home-grown
> > attempt is functional at best...
> >
> > Thanks,
> > Kyle
>
>