#!/usr/bin/perl -w
use HTML::Form;
use LWP;
use HTTP::Cookies;
sub get_all_links( $ );
sub get_login_cookie( $$$$ );
sub report_all_spam( $$ );
if ($ARGV[0] eq "--help" ) {
print 'usage: spamcop {EMAIL-FOLDER}'."\n";
print 'usage: spamcop /home/heinz/mail/Inbox/.SpamCop'."\n";
exit;
}
# Configuration settings
my $spam_cop_user = 'gunnarwrobel@yahoo.de';
my $spam_cop_pass = 'k8FHdADl';
# Main routine
my $folder = $ARGV[0]; # the folder with spam cop answers
my $ua = LWP::UserAgent->new();
$ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
my @link_batch = get_all_links( $folder );
if (scalar( @link_batch ) > 0)
{
get_login_cookie( $ua,
$link_batch[0],
$spam_cop_user,
$spam_cop_pass
);
report_all_spam( $ua,
\@link_batch
);
}
else
{
die "Unable to extract any links from folder $folder!";
}
## Function get_all_links
##
## Retrieves all the SpamCop report links from the
## mails in one folder
##
## Parameters:
##
## folder: the path to the folder to check for links
sub get_all_links ( $ )
{
my $folder = shift;
opendir (DIR, $folder . "/cur");
my @files = grep { $_ ne '.' and $_ ne '..'} readdir( DIR );
my @links = ();
my $link;
if (scalar @files > 0)
{
foreach my $file (@files)
{
open(FILE, $folder . "/cur/" . $file);
while ()
{
if (($link) = ($_ =~ /(.*www.spamcop.net.sc.id=.*)/))
{
push @links, {'LINK' => $link, 'FILE' => $folder . "/cur/" . $file};
}
}
close(FILE);
}
}
return @links;
}
## Function get_login_cookie
##
## Logs the user into spamcop
## and returns the session cookie
##
## Parameters:
##
## ua: LWP user agent
## link: one of the extracted report links
## user: SpamCop user
## pass: SpamCop password
sub get_login_cookie ( $$$$ )
{
my $ua = shift;
my $link = shift;
my $user = shift;
my $pass = shift;
my $form = $ua->get($link->{'LINK'})
or
die "Couldn't fetch $link";
if ( $form->is_error() )
{
die $form->message();
}
my $formurl = "http://www.spamcop.net/mcgi";
my $resp = $ua->post
(
$formurl,
[
'username' => $user,
'password' => $pass,
'duration' => '+12h',
'action' => 'cookielogin',
'returnurl' => '/mcgi?action=verifylogin',
'submit' => 'Login'
]
);
if ( $resp->is_error() )
{
die $resp->message();
}
}
## Function report_all_spam
##
## Reports every spam link
##
## Parameters:
##
## ua: LWP user agent
## linklist: The list of report pages
sub report_all_spam ( $$ )
{
my $ua = shift;
my $link_list = shift;
my $form;
my @forms;
my $response;
foreach my $link (@{$link_list})
{
$form = $ua->get($link->{'LINK'})
or
die "Couldn't fetch $link";
@forms = HTML::Form->parse( $form );
foreach my $sendform (@forms)
{
if (defined $sendform->attr( 'name' )
and $sendform->attr( 'name' ) eq 'sendreport')
{
$response = $ua->request($sendform->click());
if ( $response->is_error() )
{
die "Failed to report spam:\n\n" . $response->message();
}
else
{
unlink $link->{'FILE'}
}
}
}
}
}