Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with code

Status
Not open for further replies.

cnw40007

Technical User
Mar 4, 2003
35
IE
Hi the code below is supposed to get the email address from the mbox compare it to a database with spam email address.If a match is found then relabel the subject line of the email message with subject:{SPAM}.But this is not the case. The program runs but nothing happens. Can anybody help me please its driving me mad!!

#! usr/bin/perl -w

use DBI;

open (HEADER,"+>/root/mbox") or die "Could not open the file";

my $username = 'me';
my $password = '';
my $dbh = DBI->connect("DBI:mysql:spamdb",$username,$password,);
my $sql = "SELECT subject, address from addsubj";

my $sth = $dbh->prepare($sql) || die "Error preparing DBI:errstr";
my $result = $sth->execute || die "Error executing: $DBI::errstr";

while(my $rowref = $sth->fetchrow_hashref)
{
$row=1;
$row = "$rowref->{address}\n";
$row = "$rowref->{subject}\n";

while(<HEADER>)
{

$subject = $_;


#if((/From:([^\@]+)/) eq $rowref->{address})

if((/^From:/) eq $rowref->{address})
{
$subject=~ s/Subject:/Subject:{SPAM!}/;
print &quot;$subject&quot;;

}
if( (/^Subject:/) eq $rowref->{subject})
{

$subject=~ s/Subject:/Subject:{SPAM!}/;
print &quot;$subject&quot;;
}


}
}
$sth->finish;

$dbh->disconnect;

 
Your problem is in the loop! A little judicious indentation should help
[tt]
....
while(my $rowref = $sth->fetchrow_hashref) {
$row=1;
$row = &quot;$rowref->{address}\n&quot;;
$row = &quot;$rowref->{subject}\n&quot;;

while(<HEADER>) {
$subject = $_;
....
[/tt]
so you read a record from the database, read the entire mailbox, read the next record from the database and, as <HEADER> is at EOF, do nothing with it. You then do nothing with every other record in the database.

You're trying to do a two-dimensional match so you need to either: (i) read the whole database for each message in the mailbox; (ii) read the whole mailbox for each record in the database; or (iii) digest the essential elements of each (or either) into memory and work there. Which is appropriate will depend on the sizes of the objects being considered.

Yours,

&quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Ok thanks a mill, that cleared it up some what!im fairly new to perl so would you be able to give me some sample code??
 
Another problem is you seem to be reading and writing the same file in completely haphazard mode. Open a temp file and copy across line by line, then copy the whole lot back. Look at file locking to make this safe.

You'd want something like
Code:
my( @BadAddresses, @BadSubjects );
while(my $rowref = $sth->fetchrow_hashref) {
   push @BadAddresses, $rowref->{address};
   push @BadSubjects, $rowref->{subject};
}
$sth->finish; # whole db now in core

....
open( TMPFILE, &quot;>........ # somewhere
while(<HEADER>) {
    my $line = $_;
    /From:([^\@]+)/ && do {
        my $ad = $1;
        foreach my $ba (@BadAddresses) {
            if ( $ba eq $ad ) {
                $line =~ s/: /: !SPAM!/;
                last;
            }
        }
    /Subject:([^\@]+)/ && do {
        my $sub = $1;
        foreach my $bs (@BadSubjects) {
            if ( $bs eq $sub ) {
                $line =~ s/: /: !SPAM!/;
                last;
            }
        }
    print TMPFILE $line;
}
# now copy tempfile over your input and delete it...
[code]

Can't really spend much longer on this.  Good luck, &quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought.  Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
         --Maurice Wilk
 
Since replying earlier today, I've discovered the -i command line switch to perl. From the perlrun manual:

&quot;-i[extension]
specifies that files processed by the &quot;<>&quot; construct are to be edited in-place. It does this by renaming the input file, opening the output file by the origi­nal name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy, following these rules:
If no extension is supplied, no backup is made and the current file is overwritten.
If the extension doesn't contain a &quot;*&quot;, then it is appended to the end of the current filename as a suf­fix. If the extension does contain one or more &quot;*&quot; characters, then each &quot;*&quot; is replaced with the cur­rent filename. In Perl terms, you could think of this as:
($backup = $extension) =~ s/\*/$file_name/g;

This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix:
$ perl -pi 'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'

Or even....


There's lots more and it looks like it's exactly what you need! &quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top