News/Article/Ref.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 405 CC 76 SCAR 59.7
CC
76
Cov
0%
valid_header
756.0
valid_control
600.0
valid_headers
156.0
CRAP 393.1
1 $VERSION = "0.2";
2 package News::Article::Ref;
3 our $VERSION = "0.2";
4  
5 # -*- Perl -*-
6 #############################################################################
7 # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2000-2002, Tim
8 # Skirvin. Redistribution terms are below.
9 #############################################################################
10  
11 =head1 NAME
12  
13 News::Article::Ref - reference functions for news articles
14  
15 =head1 SYNOPSIS
16  
17 use News::Article::Ref;
18  
19 my $date = "Wed, 06 Mar 2002 11:23:10 -0600";
20 my $gooddate = News::Article::Ref->valid_date($date);
21  
22 my $messageid = '<godwin-20020306172310$31f9@news.killfile.org>';
23 my $goodmid = News::Article::Ref->valid_messageid($messageid);
24  
25 Further functions are below.
26  
27 =head1 DESCRIPTION
28  
29 News::Article::Ref is a module for determining if a news article is
30 technically suited for Usenet - ie, it checks to see if it follows all of
31 Usenet's technical rules, as set down in the RFCs. This is useful for
32 moderation 'bots and other news processing.
33  
34 The current specifications are based on a combination of RFC1036 and
35 RFC1036bis. This probably isn't the best idea, but it works for now.
36  
37 News::Article::Ref exports nothing.
38  
39 =head1 USAGE
40  
41 =cut
42  
43 ###############################################################################
44 ### main() ####################################################################
45 ###############################################################################
46  
47 use Net::Domain qw(hostfqdn);
48 use strict;
49 use vars qw($VERSION $DEBUG);
50 $VERSION = "0.1a";
51 $DEBUG = "0";
52  
53 ###############################################################################
54 ### VARIABLES #################################################################
55 ###############################################################################
56 ## There are lots of variables here. Most of them are for use in regular
57 ## expressions later down the line.
58  
59 use vars qw( $GROUP_CHARS $TAG_CHAR $CODE_CHAR $CHARSET $ENCODING $CODES
60 $ENCODED_WORD $UNQUOTED_CHAR $QUOTED_CHAR $PAREN_CHAR $UNQUOTED_WORD
61 $QUOTED_WORD $PLAIN_WORD $PAREN_PHRASE $PLAIN_PHRASE $LOCAL_PART $DOMAIN
62 $ADDRESS $RELAYER $NONBLANK $NAMECOMPONENT );
63  
64 $GROUP_CHARS = '[a-zA-Z0-9_+-]';
65 $TAG_CHAR = '[^!\(\)<>@,\;:\\"\[\]\/\?=]+';
66 $ENCODING = $TAG_CHAR;
67 $CODES = '[^\?]+';
68 $ENCODED_WORD = join('', '=\?',$CHARSET,'\?',$ENCODING,'\?',$CODES,'\?=');
69 $UNQUOTED_CHAR = '[^!\(\)<>@,\;:\\\"\.\[\]]';
70 $QUOTED_CHAR = '[^"\(\)\\<>]';
71 $PAREN_CHAR = '[^\(\)\\<>]';
72 $UNQUOTED_WORD = $UNQUOTED_CHAR . '+';
73 $QUOTED_WORD = '"' . $QUOTED_CHAR . '+"';
74 $PLAIN_WORD = join('', '(?:', $UNQUOTED_WORD, '|', $QUOTED_WORD,
75 '|', $ENCODED_WORD, ')');
76 $PLAIN_PHRASE = $PLAIN_WORD . '(?: ' . $PLAIN_WORD . ')*';
77 $PAREN_PHRASE = join('', '(?:', $ENCODED_WORD, '|\s|', $PAREN_CHAR, ')+');
78 $LOCAL_PART = $UNQUOTED_WORD . '(?:\.' . $UNQUOTED_WORD . ')*';
79 $DOMAIN = $UNQUOTED_WORD . '(?:\.' . $UNQUOTED_WORD . ')*';
80 $ADDRESS = join('@', $LOCAL_PART, $DOMAIN);
81 $RELAYER = '[a-zA-Z0-9=.-_]+';
82 $NONBLANK = '\s*\S.*';
83 $NAMECOMPONENT = '[a-zA-Z0-9][a-zA-Z0-9_\+-]+';
84  
85 ###############################################################################
86 ### METHODS ###################################################################
87 ###############################################################################
88  
89 =head2 Validation Methods
90  
91 The following methods validate the information already in a header - ie,
92 they check to see if it's valid with current Usenet specifications. This
93 may be more or less restrictive than any given news server will require, but
94 it's a good general rule to follow the rules regardless.
95  
96 =over 4
97  
98 =item valid_header ( HEADER, CONTENTS )
99  
100 Verifies that the B<CONTENTS> of B<HEADER> are valid. Checks
101 From, Subject, Newsgroups, Message-ID, Path, Date, Followup-to, Expires,
102 Reply-To, Sender, References, Control, Distribution, Summary, Approved,
103 Lines, Organization, and Supersedes; all other headers are assumed to be
104 unnecessary but okay.
105  
106 Note that many of these functions are available below.
107  
108 =cut
109  
110 sub valid_header {
111 my ($self, $header, $contents) = @_;
112 return 0 unless ($header && $contents);
113 if (lc $header eq 'from') { $self->valid_from($contents) }
114 elsif (lc $header eq 'subject') { $self->valid_subject($contents) }
115 elsif (lc $header eq 'newsgroups') { $self->valid_newsgroups($contents) }
116 elsif (lc $header eq 'message-id') { $self->valid_messageid($contents) }
117 elsif (lc $header eq 'path') { $self->valid_path($contents) }
118 elsif (lc $header eq 'date') { $self->valid_date($contents) }
119 elsif (lc $header eq 'followup-to') {
120 return 1 if $contents eq 'poster';
121 $self->valid_header('newsgroups', $contents);
122 }
123 elsif (lc $header eq 'expires') { $self->valid_header('date', $contents) }
124 elsif (lc $header eq 'reply-to') { $self->valid_header('from', $contents) }
125 elsif (lc $header eq 'sender') { $self->valid_header('from', $contents) }
126 elsif (lc $header eq 'references') {
127 foreach (split(/\s+/, $contents)) {
128 return 0 unless $self->valid_header('message-id', $_);
129 }
130 1;
131 }
132 elsif (lc $header eq 'control') { $self->valid_control($contents) }
133 elsif (lc $header eq 'distribution' || lc $header eq 'keywords') {
134 foreach (split(',', $contents)) {
135 return 0 unless ($contents =~ /^$NAMECOMPONENT$/);
136 }
137 1;
138 }
139 elsif (lc $header eq 'summary') { $contents =~ /^$NONBLANK$/s ? 1 : 0 }
140 elsif (lc $header eq 'approved') {
141 foreach (split(',', $contents)) {
142 return 0 unless $self->valid_header('from', $_);
143 }
144 1;
145 }
146 elsif (lc $header eq 'lines') { $contents =~ /^\d+$/ ? 1 : 0 }
147 elsif (lc $header eq 'organization') { $contents =~ /^$NONBLANK$/ ? 1 : 0 }
148 elsif (lc $header eq 'supersedes') {
149 $self->valid_header('message-id', $contents)
150 }
151 else { 1 } # We don't mess with other headers
152 }
153  
154 =item valid_headers ( HEADERS )
155  
156 Takes an array of headers B<HEADERS>, and verifies that together they make
157 up a valid set of headers for a news article. This means, in general, that
158 each header is valid, and that enough headers are there to be posted. Takes
159 advantage of B<valid_headers()>. Returns 1 if valid, 0 otherwise.
160  
161 =cut
162  
163 sub valid_headers {
164 my ($self, @headers) = @_;
165
166 my (%headers, $prev);
167 foreach (@headers) {
168 chomp;
169 return 0 unless ($_ =~ /^(?:(\S+):\s*(.*)|\s+(.*))$/);
170 my $header = lc $1 || $prev; return 0 unless $header;
171 my $contents = $2 || $3;
172 return 0 if $headers{$header} && defined $2;
173 $headers{$header} =
174 $headers{$header} ? join("\n ", $headers{$header}, $contents)
175 : $contents;
176 $prev = $header;
177 }
178
179 # Need newsgroups, subject, from, message-id, date
180 return 0 unless $headers{'newsgroups'};
181 return 0 unless $headers{'subject'};
182 return 0 unless $headers{'from'};
183 return 0 unless $headers{'message-id'};
184 return 0 unless $headers{'date'};
185 # return 0 unless $headers{'path'};
186  
187 # Can't have both a Supersedes: and Control:
188 return 0 if $headers{'control'} && $headers{'supersedes'};
189  
190 foreach (keys %headers) {
191 return 0 unless $self->valid_header($_, $headers{$_});
192 }
193
194 1;
195 }
196  
197 =item valid_body ( BODY )
198  
199 Verifies that B<BODY> is a valid message body for the article. Currently
200 just checks to make sure that there *is* a body; this may change later.
201 Returns 1 if valid, 0 otherwise.
202  
203 =cut
204  
205 sub valid_body {
206 my ($self, @lines) = @_;
207 return 0 unless scalar @lines;
208 1;
209 }
210  
211 =item valid_article ( ARTICLE )
212  
213 Takes a whole B<ARTICLE> as input, and does both B<verify_headers()> and
214 B<verify_body()> on it. Returns 1 if the article is valid, 0 otherwise.
215  
216 =cut
217  
218 sub valid_article {
219 my ($self, @lines) = @_;
220 my ($count, @headers, @body);
221 foreach (@lines) {
222 chomp;
223 if (/^$/) { $count++; next; }
224 $count ? push @body, $_ : push @headers, $_;
225 }
226 $self->valid_headers(@headers) && $self->valid_body(@body);
227 }
228  
229  
230 =item valid_messageid ( ID )
231  
232 Determines whether B<ID> is a valid Message-ID, which is of the general form
233 '<unique.string4159@site.com.invalid>'. Returns 1 if yes, 0 otherwise.
234  
235 =cut
236  
237 sub valid_messageid { $_[1] =~ /^<$LOCAL_PART\@$DOMAIN>$/ ? 1 : 0; }
238  
239 =item valid_date ( DATE )
240  
241 Determines whether B<DATE> is a valid Date header, which is of the general
242 form 'Wed, 06 Mar 2002 11:23:10 -0600'. Returns 1 if yes, 0 otherwise.
243  
244 =cut
245  
246 sub valid_date {
247 my ($self, $date) = @_;
248 return 0 unless $date;
249 $date =~ m/^
250 (\w{3},?\s*)? # Day of Week
251 ((\d{1,2})\s*(\w{3})| (\w{3})\s*(\d{1,2}))\s* # Day and Month
252 (\d{2,5})?\s* # Year, maybe.
253 (\d\d):(\d\d):(\d\d)\s* # H,M,S
254 ([^\d\s]\S+)?\s* # Timezone
255 (\d{2,5})?\s*(.*)?\s* # Year+TZ
256 /sx ? 1 : 0;
257 }
258  
259  
260  
261 =item valid_from ( ADDRESS )
262  
263 Verifies that the email address is "valid" - not that it delivers, but that
264 it follows the proper form. B<ADDRESS> can take one of three forms:
265  
266 tskirvin@killfile.org
267 Tim Skirvin <tskirvin@killfile.org>
268 tskirvin@killfile.org (Tim Skirvin)
269  
270 Returns 1 if valid, 0 otherwise.
271  
272 =cut
273  
274 sub valid_from {
275 my ($self, $address) = @_;
276 $address =~ /^(?:\"?($PLAIN_PHRASE)?\"?\s*<($ADDRESS)>|
277 ($ADDRESS)\s*(?:\(($PAREN_PHRASE)\))?)$/sx ? 1 : 0;
278 }
279  
280 =item valid_path ( PATH )
281  
282 Determines if B<PATH> is valid for the Path: header. Takes the form
283 'news.meat.net!news.killfile.org!local-form'. Returns 1 if valid, 0
284 otherwise.
285  
286 =cut
287  
288 sub valid_path {
289 my ($self, $path) = @_;
290 my @contents = split('!', $path);
291 my $local = pop @contents; return 0 unless ($local =~ /^$LOCAL_PART$/);
292 foreach (@contents) { return 0 unless /^$RELAYER$/ }
293 1;
294 }
295  
296 =item valid_groupname ( GROUPNAME )
297  
298 Determines if the given B<GROUPNAME> is a valid newsgroup name - letters and
299 numbers only, with '.' as a separator. Returns 1 if valid, 0 otherwise.
300  
301 =cut
302  
303 sub valid_groupname { $_[1] =~ /^$GROUP_CHARS+(\.$GROUP_CHARS+)*$/ ? 1 : 0; }
304  
305 =item valid_newsgroups ( NEWSGROUPS )
306  
307 Determines of B<NEWSGROUPS> is a valid Newsgroups: header - each group name
308 must be separated by only a comma, and new groups can be repeated. Returns
309 1 if valid, 0 otherwise.
310  
311 =cut
312  
313 sub valid_newsgroups {
314 my ($self, $groups) = @_;
315 return 0 unless $groups;
316 my %groups;
317 foreach my $group (split(',', $groups)) {
318 return 0 unless $self->valid_groupname($group);
319 return 0 if $groups{$group}; # Can't repeat newsgroup names
320 $groups{$group}++;
321 }
322 1;
323 }
324  
325 =item valid_subject ( SUBJECT )
326  
327 Determines if B<SUBJECT> is a valid subject header. This isn't too tough -
328 it just has to be not blank. Returns 1 if valid, 0 otherwise.
329  
330 =cut
331  
332 sub valid_subject { $_[1] =~ /^$NONBLANK$/ ? 1 : 0 }
333  
334 =item valid_control ( LINE )
335  
336 Determines if B<LINE> is a valid Control: header. This is fairly tricky,
337 because there are many types of control headers:
338
339 cancel MESSAGEID
340 ihave MESSAGEID [HOST]
341 sendme MESSAGEID [HOST]
342 newgroup GROUPNAME [moderated|unmoderated]
343 rmgroup GROUPNAME
344 sendsys
345 version
346 checkgroups
347  
348 Returns 1 if valid, 0 otherwise.
349
350 =cut
351  
352 sub valid_control {
353 my ($self, $line) = @_;
354 if ($line =~ /^([a-zA-Z0-9]+)((?:\s+\S+)*)\s*$/) {
355 my $verb = lc $1; my $args = $2;
356 if ($verb eq 'cancel') { $self->valid_messageid($2) ? 1 : 0 }
357 elsif ($verb eq 'ihave') {
358 my @args = split(/\s+/, $args);
359 return 0 unless $self->valid_messageid($args[0]);
360 return 0 unless (!$args[1] || $args[1] =~ /^$RELAYER$/);
361 return 0 if (scalar @args > 1);
362 return 1;
363 }
364 elsif ($verb eq 'sendme') {
365 my @args = split(/\s+/, $args);
366 return 0 unless $self->valid_messageid($args[0]);
367 return 0 unless (!$args[1] || $args[1] =~ /^$RELAYER$/);
368 return 0 if (scalar @args > 1);
369 return 1;
370 }
371 elsif ($verb eq 'newgroup') {
372 my @args = split(/\s+/, $args);
373 return 0 unless $self->valid_groupname($args[0]);
374 return 0 if ($args[1] && $args[1] !~ /^(moderated|unmoderated)$/);
375 return 0 if (scalar @args > 1);
376 1;
377 }
378 elsif ($verb eq 'rmgroup') { $self->valid_groupname($args) ? 1 : 0 }
379 elsif ($verb eq 'sendsys') { $args ? 0 : 1 }
380 elsif ($verb eq 'version') { $args ? 0 : 1 }
381 elsif ($verb eq 'checkgroups') { $args ? 0 : 1 }
382 else { 0 }
383 }
384 }
385  
386 =back
387  
388 =head2 Create-New-Entry Methods
389  
390 The following methods can be used to create new data suitable for using in
391 article headers.
392  
393 =over 4
394  
395 =item create_messageid ( [ PREFIX [, DOMAIN ]] )
396  
397 Creates a valid message-ID based on B<PREFIX>, B<DOMAIN>, and the
398 current time. Based on B<add_message_id()> from News::Article.
399  
400 =cut
401  
402 sub create_messageid {
403 my ($self, $prefix, $domain) = @_;
404 $prefix ||= "";
405 $domain ||= hostfqdn() || 'broken-configuration';
406  
407 my ($sec,$min,$hr,$mday,$mon,$year) = gmtime(time); ++$mon;
408 sprintf('<%s%04d%02d%02d%02d%02d%02d$%04x@%s>', $prefix, $year+1900,
409 $mon, $mday, $hr, $min, $sec, 0xFFFF & (rand(32768) ^ $$), $domain);
410 }
411  
412 =item create_date ( [TIME] )
413  
414 Creates a valid Date: header from B<TIME> (seconds since the epoch), or
415 the current time if not offered. Based on B<add_date()> from News::Article.
416  
417 =cut
418  
419 sub create_date {
420 my ($self, $time) = @_; $time ||= time;
421 my ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
422 my ($gsec,$gmin,$ghr,$gmday) = gmtime($time);
423  
424 # mystic incantations to calculate zone offset from difference
425 # between UTC and local time. Assumes that difference is not more
426 # than a full day (saves having to take months into consideration).
427 # ANSI is apparently going to add a spec to strftime() to do this,
428 # but that isn't yet commonly available.
429  
430 use integer;
431 $gmday = $mday + ($mday <=> $gmday) if (abs($mday-$gmday) > 1);
432 my $tzdiff = 24*60*($mday-$gmday) + 60*($hr-$ghr) + ($min-$gmin);
433 my $tz = sprintf("%+04.4d", $tzdiff + ($tzdiff/60*40));
434  
435 $mon = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
436 $wday = (qw(Sun Mon Tue Wed Thu Fri Sat Sun))[$wday];
437 $year += 1900;
438 sprintf("%s, %02d %s %d %02d:%02d:%02d %s",
439 $wday,$mday,$mon,$year,$hr,$min,$sec,$tz);
440 }
441  
442 =back
443  
444 =head1 TODO
445  
446 Put some clean_* functions somewhere - ie B<clean_date()>, which would make
447 a canonical date header for the article based on whatever it's offered.
448 This wouldn't necessarily go in this module, though.
449  
450 Include some debugging information, so that the user can determine *how*
451 there were problems. This will involve some major re-writes.
452  
453 Choose which RFC to follow.
454  
455 =head1 AUTHOR
456  
457 Tim Skirvin <tskirvin@killfile.org>
458  
459 =head1 COPYRIGHT
460  
461 This code may be distributed under the same terms as Perl itself.
462  
463 =cut
464  
465 ###############################################################################
466 ### Version History ###########################################################
467 ###############################################################################
468 # v0.1a Thu Mar 7 17:07:20 CST 2002
469 ### First commented version.
470 # v0.2 Thu Apr 22 11:40:48 CDT 2004
471 ### No real changes, just version numbers.