| 1 | package Mail::Abuse::Reader::GoogleGroups; | ||
| 2 | |||
| 3 | require 5.005_62; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Date::Manip; | ||
| 9 | use WWW::Google::Groups; | ||
| 10 | |||
| 11 | use base 'Mail::Abuse::Reader'; | ||
| 12 | |||
| 13 | # The code below should be in a single line | ||
| 14 | our $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r }; | ||
| 15 | |||
| 16 | # This contraption is required to include | ||
| 17 | # $VERSION in the BLURB. | ||
| 18 | |||
| 19 | BEGIN { $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r } }; | ||
| 20 | |||
| 21 | use constant GROUP => 'news.admin.net-abuse.sightings'; | ||
| 22 | use constant BLURB => <<EOF; | ||
| 23 | |||
| 24 | The message below has been submitted to the &{GROUP} news group | ||
| 25 | and fetched using Mail::Abuse::Reader::GoogleGroups $VERSION | ||
| 26 | |||
| 27 | EOF | ||
| 28 | ; | ||
| 29 | =pod | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Mail::Abuse::Reader::GoogleGroups - Reads Mail::Abuse::Report from NANAS via Google Groups | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use Mail::Abuse::Report; | ||
| 38 | use Mail::Abuse::Reader::GoogleGroups; | ||
| 39 | my $r = new Mail::Abuse::Reader::GoogleGroups; | ||
| 40 | my $report = new Mail::Abuse::Report (reader => $r); | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | THIS CODE SHOULD NOT BE USED IN PRODUCTION YET. SEE bin/google-gather | ||
| 45 | INSTEAD, FOR AN ALTERNATE MECHANISM OF FETCHING REPORTS FROM NANAS | ||
| 46 | VIA GOOGLE GROUPS. | ||
| 47 | |||
| 48 | This module uses the news.admin.net-abuse.sightings archive kindly | ||
| 49 | provided by the Google(tm) Groups service to feed public complaints | ||
| 50 | into the C<Mail::Abuse> package. | ||
| 51 | |||
| 52 | The general idea is that a search is going to be performed and each | ||
| 53 | result will be fed into the C<Mail::Abuse> engine. The parameters of | ||
| 54 | the process can be configured by the following keys in the | ||
| 55 | configuration file. | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item B<google groups search> | ||
| 60 | |||
| 61 | Regexp to search for in the text of each article. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | use constant QUERY => 'google groups search'; | ||
| 66 | |||
| 67 | =pod | ||
| 68 | |||
| 69 | =item B<google groups max messages> | ||
| 70 | |||
| 71 | Maximum number of messages to read in a single run. Defaults to 50. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | use constant MAX => 'google groups max messages'; | ||
| 76 | |||
| 77 | =pod | ||
| 78 | |||
| 79 | =item B<google groups oldest message> | ||
| 80 | |||
| 81 | Controls the maximum age of a message. Messages that are older | ||
| 82 | (accorging to its Date: header) will be skipped and not counted. This | ||
| 83 | defaults to five days. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | use constant OLDEST => 'google groups oldest message'; | ||
| 88 | |||
| 89 | =pod | ||
| 90 | |||
| 91 | =item B<google groups server> | ||
| 92 | |||
| 93 | The name of the Google(tm) Groups server to use. Defaults to | ||
| 94 | C<groups.google.com>. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | use constant SERVER => 'google groups server'; | ||
| 99 | |||
| 100 | =pod | ||
| 101 | |||
| 102 | =item B<google groups proxy> | ||
| 103 | |||
| 104 | The proxy server to use. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | use constant PROXY => 'google groups proxy'; | ||
| 109 | |||
| 110 | =pod | ||
| 111 | |||
| 112 | =item B<debug google groups> | ||
| 113 | |||
| 114 | When set to a true value, causes debug information to be sent to | ||
| 115 | STDERR. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | use constant DEBUG => 'debug google groups'; | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | The following methods are implemented within this class. | ||
| 126 | |||
| 127 | =over | ||
| 128 | |||
| 129 | =item C<read($report)> | ||
| 130 | |||
| 131 | Populates the text of the given C<$report> using the C<-E<gt>text> | ||
| 132 | method. Must return true if succesful or false otherwise. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub read | ||
| 137 | { | ||
| 138 | my $self = shift; | ||
| 139 | my $rep = shift; | ||
| 140 | |||
| 141 | my $config = $rep->config; | ||
| 142 | |||
| 143 | unless ($config->{&QUERY}) | ||
| 144 | { | ||
| 145 | carp "Not enough config info for GoogleGroups reader"; | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | my $search = qr/$config->{&QUERY}/; | ||
| 150 | |||
| 151 | unless ($self->agent) | ||
| 152 | { | ||
| 153 | $self->agent( new WWW::Google::Groups | ||
| 154 | ( | ||
| 155 | server => $config->{&SERVER} || 'groups.google.com', | ||
| 156 | $config->{&PROXY} ? (proxy => $config->{&PROXY}) : (), | ||
| 157 | ) | ||
| 158 | ); | ||
| 159 | |||
| 160 | $self->count(0); | ||
| 161 | $self->group($self->agent->select_group(&GROUP)); | ||
| 162 | # $self->group($self->agent->search(query => "group:" . | ||
| 163 | # &GROUP . " " . $config->{&QUERY}, | ||
| 164 | # limit => 100)); | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | unless ($self->before) | ||
| 169 | { | ||
| 170 | my $date_before; | ||
| 171 | |||
| 172 | Date_Init('TZ=UTC'); | ||
| 173 | |||
| 174 | eval | ||
| 175 | { | ||
| 176 | if (ref $config->{&OLDEST} eq 'ARRAY') | ||
| 177 | { | ||
| 178 | $date_before = ParseDate(join(' ', @{$config->{&OLDEST}})); | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | $date_before = ParseDate($rep->config->{&OLDEST} | ||
| 182 | || "5 days ago"); | ||
| 183 | } | ||
| 184 | }; | ||
| 185 | |||
| 186 | warn "M::A::R::GoogleGroups parser said $@" | ||
| 187 | if $@ and $config->{&DEBUG}; | ||
| 188 | die "M::A::R::GoogleGroups: Cannot parse '" . &OLDEST . "\n" | ||
| 189 | unless $date_before; | ||
| 190 | $self->before(UnixDate($date_before, '%s')); | ||
| 191 | die "M::A::R::GoogleGroups: Times before the epoch are not supported" | ||
| 192 | if $self->before < 0; | ||
| 193 | warn "M::A::R::GoogleGroups: Ignoring posts older than ", | ||
| 194 | $self->before, "\n" if $config->{&DEBUG}; | ||
| 195 | } | ||
| 196 | |||
| 197 | while ($self->thread($self->group->next_thread), $self->thread) | ||
| 198 | { | ||
| 199 | warn "M::A::R::GoogleGroups: Mining thread '", | ||
| 200 | $self->thread->title, "'\n" | ||
| 201 | if $config->{&DEBUG}; | ||
| 202 | |||
| 203 | my $mid = undef; | ||
| 204 | |||
| 205 | while ($self->article($self->thread->next_article), $self->article) | ||
| 206 | { | ||
| 207 | if ($mid and $mid eq $self->article->header('Message-Id')) | ||
| 208 | { | ||
| 209 | warn "M::A::R::GoogleGroups: Looks like we pulled the " | ||
| 210 | . "same message twice\n" | ||
| 211 | if $config->{&DEBUG}; | ||
| 212 | last; | ||
| 213 | } | ||
| 214 | |||
| 215 | $mid = $self->article->header('Message-Id'); | ||
| 216 | |||
| 217 | return unless $self->count < ($config->{&MAX} || 50); | ||
| 218 | |||
| 219 | my $date = $self->article->header('Date'); | ||
| 220 | my $article_date = undef; | ||
| 221 | |||
| 222 | warn "M::A::R::GoogleGroups: Fetched article ", | ||
| 223 | $self->article->header('Message-Id'), "/$date\n" | ||
| 224 | if $config->{&DEBUG}; | ||
| 225 | |||
| 226 | eval | ||
| 227 | { | ||
| 228 | $article_date = UnixDate(ParseDate($date), '%s'); | ||
| 229 | }; | ||
| 230 | # Only if parsing succeeded, will we filter | ||
| 231 | # this article | ||
| 232 | unless ($@) | ||
| 233 | { | ||
| 234 | if ($article_date < $self->before) | ||
| 235 | { | ||
| 236 | warn "M::A::R::GoogleGroups filtered article ", | ||
| 237 | "($date/$article_date)\n" | ||
| 238 | if $config->{&DEBUG}; | ||
| 239 | last; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | if ($self->article->body =~ m/$search/i) | ||
| 244 | { | ||
| 245 | $self->count($self->count + 1); | ||
| 246 | my $buffer = 'From: ' . $self->article->header('From') . "\n"; | ||
| 247 | $buffer .= 'Id: ' . $self->article->header('Message-ID'). "\n"; | ||
| 248 | $buffer .= 'Date: ' . $self->article->header('Date') . "\n"; | ||
| 249 | $buffer .= $self->article->header('Subject') . "\n\n"; | ||
| 250 | $buffer .= &BLURB; | ||
| 251 | $buffer .= $self->article->body; | ||
| 252 | $rep->text(\$buffer); | ||
| 253 | return 1; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | warn "M::A::R::GoogleGroups: Thread exhausted. Going to the next one\n" | ||
| 257 | if $config->{&DEBUG}; | ||
| 258 | } | ||
| 259 | |||
| 260 | warn "M::A::R::GoogleGroups: Group exhausted.\n" | ||
| 261 | if $config->{&DEBUG}; | ||
| 262 | |||
| 263 | return; | ||
| 264 | } | ||
| 265 | |||
| 266 | __END__ |