#!/usr/bin/perl # # This is one way to get a message-id into the database, although it's # rather crude. Point it at an email message with a 'From_username@host' # message. (Only one message at a time) It looks for Message-ID, Reply-To: and # From:, adding them to the database for the checks. # # The idea is to use it as part of a chain, you should only point it # at _OUTBOUND_ mail. It's like a filter in reverse. # use strict; use DBI; my($DSN) = 'DBI:mysql:database=spamdb;host=sql.example.com'; my($MYSQL_USER) = 'sql_user'; my($MYSQL_PASS) = 'sql_pass'; # SQL statement to insert. my($SQL) = "INSERT INTO mid_history (username,message_id,email) VALUES(?,?,?)"; my($DBH); &main(); sub main { my(@addr,$mid); my($user) = '@GLOBAL'; my($first_line) = 1; while(<>){ chomp; if(! length($_)){ last; } # Try to get user id from From_ line... if($first_line){ if(/From\s([^\@]+)/){ $user = $1; $user =~ s/\s//g; } $first_line = 0; next; } if(/^From:(.+)/i){ push(@addr,&address($1)); next; } if(/^Reply-To:(.+)/i){ push(@addr,&address($1)); next; } if(/^Message-ID:(.+)/i){ $mid = $1; next; } } $mid =~ s/\s//g; &insert($user,$mid,@addr); } sub insert { my($uid,$mid,@addr) = @_; my($dbh) = &dbh(); my($sh) = $dbh->prepare($SQL) || die $dbh->errstr; foreach my $addr (@addr){ $sh->execute($uid,$mid,$addr); } $sh->finish(); } sub dbh { if(! $DBH){ $DBH = DBI->connect($DSN, $MYSQL_USER, $MYSQL_PASS,{}); } return($DBH); } sub address { my($str) = shift; $str =~ s/\(.*?\)//g; my(@addr) = ($str =~ m/([\w.=-]+\@\w+(?:[\w.-]+\.)+\w+)/g); return(@addr); }