summaryrefslogtreecommitdiff
blob: 28a1e60bae69f5afc190f90b9a2bde1fe99ae6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/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 (<FILE>)
	    {
		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'}
		}
	    }
	}
    }
}