File: chronicle-spam-test

package info (click to toggle)
chronicle 4.4-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 656 kB
  • ctags: 108
  • sloc: perl: 2,323; makefile: 132; sh: 9
file content (192 lines) | stat: -rwxr-xr-x 3,620 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/perl -w
#
#  This script is a simple example which will submit comments for testing
# against the blogspam.net API.
#
#  The files are assumed to be those written by the comments.cgi script
# included in the chronicle distribution - and it is expected you'll
# test the comments via this script, or manual inspection prior to making
# them live.
#
#  Sample output:
#
# $ chronicle-spam-test *
# i_bet_you_re_a_real_tiger_in_disguise_.html.5-July-2009-18:27:09 - OK
# i_bet_you_re_a_real_tiger_in_disguise_.html.6-July-2009-07:17:25 - OK
# i_bet_you_re_a_real_tiger_in_disguise_.html.6-July-2009-16:54:26 - OK
# i_bet_you_re_a_real_tiger_in_disguise_.html.6-July-2009-17:07:05 - OK
# $
#
#
# Steve
# --
#


use strict;
use warnings;
use Getopt::Long;

require RPC::XML;
require RPC::XML::Client;


#
#  Config
#
my %CONFIG;

#
#  Default server
#
$CONFIG{ 'server' }  = "http://test.blogspam.net:8888/";
$CONFIG{ 'options' } = "exclude=bayasian";

#
#  Parse our options
#
exit
  if (
       !GetOptions( "server=s",  \$CONFIG{ 'server' },
                    "options=s", \$CONFIG{ 'options' } ) );




#
#  Make sure the server is valid
#
if ( $CONFIG{ 'server' } !~ /^http:\/\// )
{
    $CONFIG{ 'server' } = "http://" . $CONFIG{ 'server' };
}
if ( $CONFIG{ 'server' } !~ /:([0-9]+)/ )
{
    $CONFIG{ 'server' } .= ":8888/";
}



#
#  If we're using a file then test that, otherwise
# test all files in the current directory.
#
while ( defined( my $file = shift ) )
{
    testTheFile($file);
}



#
#  All done
#
exit;




=begin doc

   Make a request against the RPC server with the given file providing
 both input and the expected result - the latter coming from the filename.

=end doc

=cut

sub testTheFile
{
    my ($file) = (@_);

    if ( !-e $file )
    {
        print "File not found: $file\n";
        return;
    }


    #
    #  Params we send to the server.
    #
    #  Note we send "test" so that the spam isn't logged.
    #
    my %params = ( 'test'  => '1',
                   options => $CONFIG{ 'options' } );


    #
    #  Read the file.
    #
    open( my $handle, "<", $file ) or
      die "Failed to open $file - $!";

    while ( my $line = <$handle> )
    {
        if ( $line =~ /^IP.*: (.*)/i )
        {
            $params{ 'ip' } = $1;
        }
        elsif ( $line =~ /^User-Agent: (.*)/i )
        {
            $params{ 'agent' } = $1;
        }
        elsif ( $line =~ /^mail: (.*)/i )
        {
            $params{ 'email' } = $1;
        }
        elsif ( $line =~ /^Name: (.*)/i )
        {
            $params{ 'name' } = $1;
        }
        elsif ( $line =~ /^Subject: (.*)/i )
        {
            $params{ 'subject' } = $1;
        }
        else
        {
            $params{ 'comment' } .= $line;
        }

    }
    close($handle);

    #
    #  The result we obtained
    #
    my $result = undef;

    #
    #  Make the result
    #
    eval {
        my $client = RPC::XML::Client->new( $CONFIG{ 'server' } );
        my $req    = RPC::XML::request->new( 'testComment', \%params );
        my $res    = $client->send_request($req);
        $result = $res->value();
    };
    if ($@)
    {
        print "Connection failed to $CONFIG{'server'}\n";
        print "Or there was some other error.\n";
        print "Aborting.\n";
        exit;
    }


    #
    #  Did we get the result we wanted?
    #
    my $literal = $result;
    $result = $1 if ( $result =~ /^([^:]+):(.*)/ );
    $result = uc($result);

    if ( $result =~ /ok/i )
    {
        print "$file - OK\n";
    }
    else
    {
        print "$file - SPAM [$literal]\n";
    }
}