| 1 | $VERSION = "1.02"; | ||
| 2 | package News::Article; | ||
| 3 | our $VERSION = "1.02"; | ||
| 4 | |||
| 5 | # -*- Perl -*- # Mon Mar 22 11:34:43 CST 2004 | ||
| 6 | ############################################################################### | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org> | ||
| 8 | # Copyright 1996-2004 Tim Skirvin. Redistribution terms are in the | ||
| 9 | # documentation, and I'm sure you can find them. | ||
| 10 | ############################################################################### | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | News::Article::Clean - subroutines to clean news article headers | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use News::Article::Clean; | ||
| 19 | my $article = new News::Article; | ||
| 20 | $article->set_headers('References', | ||
| 21 | $article->clean_header( 'References' ); | ||
| 22 | |||
| 23 | my $references = News::Article->clean_references($refstring); | ||
| 24 | |||
| 25 | See below for more subroutines. | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | News::Article::Clean is a package that helps clean up news articles for | ||
| 30 | future posting. It can be used as part of a pre-posting script for local | ||
| 31 | users, or as part of a moderation suite. It is intended as an add-on for | ||
| 32 | News::Article. | ||
| 33 | |||
| 34 | =head1 USAGE | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | |||
| 39 | ############################################################################### | ||
| 40 | ### main() #################################################################### | ||
| 41 | ############################################################################### | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use Date::Parse; | ||
| 45 | use News::Article; | ||
| 46 | use Net::Domain qw(hostfqdn); | ||
| 47 | use vars qw($MY_PREFIX $MY_DOMAIN $MAX_REFERENCES); | ||
| 48 | |||
| 49 | $MAX_REFERENCES = 10; | ||
| 50 | $MY_DOMAIN = $ENV{'HOSTNAME'} || hostfqdn() || 'broken-configuration'; | ||
| 51 | $MY_PREFIX = ""; | ||
| 52 | |||
| 53 | use vars qw( $GROUP_CHARS $TAG_CHAR $CODE_CHAR $CHARSET $ENCODING $CODES | ||
| 54 | $ENCODED_WORD $UNQUOTED_CHAR $QUOTED_CHAR $PAREN_CHAR $UNQUOTED_WORD | ||
| 55 | $QUOTED_WORD $PLAIN_WORD $PAREN_PHRASE $PLAIN_PHRASE $LOCAL_PART $DOMAIN | ||
| 56 | $ADDRESS $FROM_CONTENT ); | ||
| 57 | |||
| 58 | # Define the various character strings that are allowed in Usenet headers. | ||
| 59 | # All of this was found in RFC1036bis, which was probably fairly | ||
| 60 | # accurate a few years ago; future versions may want to fix this up. | ||
| 61 | # Also, it might be nice to use '' instead of "", to get rid of some of | ||
| 62 | # the escaping. | ||
| 63 | |||
| 64 | $GROUP_CHARS = "[a-zA-Z0-9_+-]"; | ||
| 65 | $TAG_CHAR = "[^!\\\(\\\)<>\\\@,:\\\;\\\"\\\[\\\]\\\/\\\?\\\\\\\\\=]"; | ||
| 66 | $CODE_CHAR = "[^\\\?]"; | ||
| 67 | $CHARSET = $TAG_CHAR . "+"; | ||
| 68 | $ENCODING = $TAG_CHAR . "+"; | ||
| 69 | $CODES = $CODE_CHAR . "+"; | ||
| 70 | $ENCODED_WORD = "\"=\\\?" . $CHARSET . "\\\?" . $ENCODING . "\\\?" . | ||
| 71 | $CODES . "?=\\\""; | ||
| 72 | $UNQUOTED_CHAR = "[^!\\\(\\\)<>\\\@,\\\;:\\\\\\\\\"\\\.\\\[\\\]]"; | ||
| 73 | $QUOTED_CHAR = "[^\\\"\\\(\\\)\\\\\\<>]"; # needs a \? Find out later. | ||
| 74 | $PAREN_CHAR = "[^\\\(\\\)<>\\\\]"; | ||
| 75 | $UNQUOTED_WORD = $UNQUOTED_CHAR . "+"; | ||
| 76 | $QUOTED_WORD = "\"" . $QUOTED_CHAR . "+\""; | ||
| 77 | $PLAIN_WORD = "(?:" . $UNQUOTED_WORD . "|" . $QUOTED_WORD . "|" . | ||
| 78 | $ENCODED_WORD . ")"; | ||
| 79 | $PAREN_PHRASE = '(?:' . $PAREN_CHAR . '|\s|' . $ENCODED_WORD . ')+'; | ||
| 80 | $PLAIN_PHRASE = $PLAIN_WORD . "(?: " . $PLAIN_WORD . ")*"; | ||
| 81 | $LOCAL_PART = $UNQUOTED_WORD . "(?:\." . $UNQUOTED_WORD . ")*"; | ||
| 82 | $DOMAIN = $UNQUOTED_WORD . "(?:\." . $UNQUOTED_WORD . ")*"; | ||
| 83 | $ADDRESS = $LOCAL_PART . "\@" . $DOMAIN; | ||
| 84 | |||
| 85 | =head2 Subroutines | ||
| 86 | |||
| 87 | This package offers the following subroutines within News::Article: | ||
| 88 | |||
| 89 | =over 4 | ||
| 90 | |||
| 91 | =item clean_newsgroups ( STRING [, STRING [, STRING [...]]] ) | ||
| 92 | |||
| 93 | Takes an array of strings containing newsgroup names (separated by commas, | ||
| 94 | as per standard Newsgroups: format), and returns either an array of valid | ||
| 95 | newsgroup names or (in scalar context) a string with these names | ||
| 96 | concatenated with ',' - ie, a proper Newsgroups: header. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub clean_newsgroups { | ||
| 101 | my ($self, @groups) = @_; | ||
| 102 | |||
| 103 | my %groups = (); | ||
| 104 | foreach ( split(/\s*,\s*|\s+/, join(',', @groups) ) ) { | ||
| 105 | s/\s+//g; # Trim whitespace | ||
| 106 | if (/^$GROUP_CHARS+(\.$GROUP_CHARS+)*$/) { $groups{$_}++ } | ||
| 107 | } | ||
| 108 | wantarray ? keys %groups : join(',', keys %groups); | ||
| 109 | } | ||
| 110 | |||
| 111 | =item clean_followupto ( STRING [, STRING [, STRING [...]]] ) | ||
| 112 | |||
| 113 | Same as clean_newsgroups, except that, if any of the strings are "poster", | ||
| 114 | then it just returns "poster". | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub clean_followupto { | ||
| 119 | my ($self, @groups) = @_; | ||
| 120 | @groups = $self->clean_newsgroups(@groups); | ||
| 121 | return "poster" if grep { $_ eq 'poster' } @groups; | ||
| 122 | wantarray ? @groups : join (',', @groups); | ||
| 123 | } | ||
| 124 | |||
| 125 | =item clean_references ( MAXREF, STRING [, STRING [, STRING [...]]] ) | ||
| 126 | |||
| 127 | Takes an array of strings containing message-IDs, and tries to manage them | ||
| 128 | into a reasonable References: line. Message-IDs that don't patch RFC | ||
| 129 | standards are trimmed; also only keeps C<MAXREF> references (defaults to | ||
| 130 | $News::Article::MAX_REFERENCES), trimming the extra to a single ID of the | ||
| 131 | format <trimmed-C<COUNT>@C<HOSTNAME>> (C<COUNT> is the total number of | ||
| 132 | trimmed messages, and C<HOSTNAME> is taken from $News::Article::MY_DOMAIN). | ||
| 133 | Returns an array of complete References or (in scalar context) a string | ||
| 134 | formatted for 80 columns, useful in the References: header. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub clean_references { | ||
| 139 | my ($self, $maxref, @refs) = @_; | ||
| 140 | $maxref ||= $MAX_REFERENCES; | ||
| 141 | my $value = join(' ', @refs) || $self->header('references'); | ||
| 142 | |||
| 143 | my ($trimmed, @refs, %refs); | ||
| 144 | foreach (split('\s+|\s*,\s*|>\s*<', $value)) { | ||
| 145 | s/\s//g; # Wipe the whitespace. | ||
| 146 | s/^/</ unless /^</; # Adds in the <'s and >'s if necessary. | ||
| 147 | s/$/>/ unless />$/; | ||
| 148 | next if $refs{$_}++; | ||
| 149 | |||
| 150 | if (/^<trimmed[- ]?(\d*).*>$/) { $trimmed += $1; next; } | ||
| 151 | my $clean = $self->clean_messageid($_); | ||
| 152 | $clean eq $_ ? push (@refs, $_) : $trimmed++; | ||
| 153 | } | ||
| 154 | |||
| 155 | if (scalar(@refs) > $maxref ) { # There's too many references | ||
| 156 | my $difference = scalar(@refs) - $maxref; | ||
| 157 | $trimmed += $difference; | ||
| 158 | @refs = ($refs[0], splice(@refs, $difference + 1)); | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($trimmed) { | ||
| 162 | @refs = ($refs[0], "<trimmed-$trimmed\@$MY_DOMAIN>", splice(@refs, 1)); | ||
| 163 | } | ||
| 164 | wantarray ? @refs : _format_refs(80, @refs); | ||
| 165 | } | ||
| 166 | |||
| 167 | =item clean_messageid ( STRING ) | ||
| 168 | |||
| 169 | Takes a Message-ID in C<STRING>; if the ID is not formatted correctly, it | ||
| 170 | will make a new one using the same algorithm as News::Article's | ||
| 171 | C<add_messageid()>, with the prefix C<$News::Cleanheader::PREFIX> and the | ||
| 172 | domain C<$News::Cleanheader::MY_DOMAIN>. There's nothing here to try to | ||
| 173 | actually clean up the header yet. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub clean_messageid { | ||
| 178 | my ($self, $id) = @_; | ||
| 179 | $id =~ s/\s+//g; | ||
| 180 | $id =~ s/^/</ unless $id =~ /^</; | ||
| 181 | $id =~ s/$/>/ unless $id =~ />$/; | ||
| 182 | |||
| 183 | # Is the ID valid? If so, return it and we're done. | ||
| 184 | return $id if $id =~ m/^<$ADDRESS>$/x; | ||
| 185 | |||
| 186 | # Rewrite bad IDs here, if possible - that'll take some thinking later. | ||
| 187 | |||
| 188 | # Make a new ID if necessary (code from Andrew Gierth's News::Article) | ||
| 189 | my ($sec,$min,$hr,$mday,$mon,$year) = gmtime(time); | ||
| 190 | ++$mon; | ||
| 191 | $id = sprintf('<%s%04d%02d%02d%02d%02d%02d$%04x@%s>', | ||
| 192 | $MY_PREFIX || "" , $year+1900, $mon, $mday, $hr, $min, $sec, | ||
| 193 | 0xFFFF & (rand(32768) ^ $$), $MY_DOMAIN || "" ); | ||
| 194 | return $id; | ||
| 195 | } | ||
| 196 | |||
| 197 | =item clean_date ( STRING ) | ||
| 198 | |||
| 199 | Takes a Date string from just about any known format and converts it to | ||
| 200 | standard 1036-based time. Returns undef if it can't parse the format; | ||
| 201 | but given that we're using Date::Parse, this shouldn't be much of a | ||
| 202 | problem. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub clean_date { | ||
| 207 | my ($self, $date) = @_; | ||
| 208 | my $time = str2time($date) || return undef; | ||
| 209 | _format_date($time); | ||
| 210 | } | ||
| 211 | |||
| 212 | =item clean_subject ( STRING ) | ||
| 213 | |||
| 214 | Reformats a Subject: string to have a standardized Re: format. It should | ||
| 215 | probably get rid of REPOSTs (from Dave the Resurrector) too, but it | ||
| 216 | doesn't yet. | ||
| 217 | |||
| 218 | This currently makes "Re: Rejection threshold" into "Re: jection | ||
| 219 | threshold" This oughta be fixed. D'oh. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub clean_subject { | ||
| 224 | my ($self, $line) = @_; | ||
| 225 | $line =~ s/^(\s*(Re:?)?\s*REPOST\s*)//g; # Get rid of REPOST headers | ||
| 226 | $line =~ s/^\s*(Re(:\s*|\s+))+/Re: /i; # Standardize the Re: format | ||
| 227 | $line; | ||
| 228 | } | ||
| 229 | |||
| 230 | =item clean_from ( ADDRESS ) | ||
| 231 | |||
| 232 | Takes a From: string and reformats it. If the email address is | ||
| 233 | unqualified, it either adds $MY_DOMAIN (if it's a user of the system) or | ||
| 234 | "unknown.invalid" to the address; if it can't find an address at all, it | ||
| 235 | sets the address to "unknown@unknown.invalid". This obviously doesn't | ||
| 236 | demunge addresses, but it's a start. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub clean_from { | ||
| 241 | my ($self, $address) = @_; | ||
| 242 | my $comment = ""; | ||
| 243 | |||
| 244 | # RFC 1036 standard From: formats | ||
| 245 | if ($address =~ /^\s*(?:\"?($PLAIN_PHRASE)?\"?\s*<($ADDRESS)>| | ||
| 246 | ($ADDRESS)\s*(?:\(($PAREN_PHRASE)\))?)\s*$/x) { | ||
| 247 | $address = $2 || $3; | ||
| 248 | $comment = $1 || $4; | ||
| 249 | |||
| 250 | # No sitename was attached to the address - either append the local one if | ||
| 251 | # appropriate or set something saying that there wasn't one at all. | ||
| 252 | } elsif ($address =~ /^\s*(?:\"?($PLAIN_PHRASE)?\"?\s*<($LOCAL_PART)>| | ||
| 253 | ($LOCAL_PART)\s*(?:\(($PAREN_PHRASE)\))?)\s*$/x) { | ||
| 254 | $address = $2 || $3; | ||
| 255 | $comment = $1 || $4; | ||
| 256 | |||
| 257 | my $host = $MY_DOMAIN if (getpwnam ($address)); | ||
| 258 | $host ||= "unknown.invalid"; | ||
| 259 | $address .= "\@$host"; | ||
| 260 | |||
| 261 | # The phrases had a bad part to them - scrap those parts. | ||
| 262 | } elsif ($address =~ /^\s*(?:(.*)\s*<($LOCAL_PART\@?$DOMAIN?)>| | ||
| 263 | ($LOCAL_PART\@?$DOMAIN?)\s*(.*))\s*$/x) { | ||
| 264 | |||
| 265 | $address = $2 || $3; | ||
| 266 | $comment = $1 || $4; | ||
| 267 | |||
| 268 | unless ($address =~ /\@\S+$/) { | ||
| 269 | $address =~ s/\@$//g; | ||
| 270 | my $host = $MY_DOMAIN if (getpwnam ($address)); | ||
| 271 | $host ||= "unknown.invalid"; | ||
| 272 | $address .= "\@$host"; | ||
| 273 | } | ||
| 274 | |||
| 275 | # There's no way we're getting a valid address out of this; just give up. | ||
| 276 | } else { | ||
| 277 | $comment = $address; | ||
| 278 | $address = "unknown\@unknown.invalid"; | ||
| 279 | } | ||
| 280 | |||
| 281 | $address ||= ""; $comment ||= ""; | ||
| 282 | map { s%(^\s*|\s*$)%%g } ($address, $comment); | ||
| 283 | return $comment ? "$comment <$address>" : "$address"; | ||
| 284 | } | ||
| 285 | |||
| 286 | =item control ( STRING ) | ||
| 287 | |||
| 288 | Checks over the given C<STRING> to see if it's a valid Control: string. | ||
| 289 | Returns undef if not. | ||
| 290 | |||
| 291 | Not currently very well done. | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | sub clean_control { | ||
| 296 | my ($self, $line) = @_; | ||
| 297 | |||
| 298 | if ($line =~ /^\s*([a-zA-Z0-9]+)((?:\s+\S+)*)\s*$/) { | ||
| 299 | my $verb = lc $1; | ||
| 300 | my $args = $2; | ||
| 301 | if ($verb eq "cancel") { | ||
| 302 | return "$verb $args" if $args =~ /^\s*<$LOCAL_PART\@$DOMAIN>\s*$/; | ||
| 303 | } elsif ($verb =~ /^(ihave|sendme)$/) { | ||
| 304 | return "$verb $args" if $verb eq "ihave" && $args =~ | ||
| 305 | /^\s*(<$LOCAL_PART\@$DOMAIN>\s+)+\s*[a-zA-Z0-9\.-_]+\s*$/; | ||
| 306 | return "$verb $args" if $verb eq "sendme" && $args =~ | ||
| 307 | /^\s*(<$LOCAL_PART\@$DOMAIN>\s+)+\s*$/s; | ||
| 308 | } elsif ($verb eq "newgroup") { | ||
| 309 | return "$verb $args" if $args =~ /^\s*$GROUP_CHARS+(\.$GROUP_CHARS+)* | ||
| 310 | (\s+(moderated|unmoderated))?\s*$/x; | ||
| 311 | } elsif ($verb eq "rmgroup") { | ||
| 312 | return "$verb $args" if $args =~ /^\s*$GROUP_CHARS+(\.$GROUP_CHARS+)*$/x; | ||
| 313 | } elsif ($verb eq "sendsys") { | ||
| 314 | return "$verb $args" if $args =~ /^\s*([a-zA-Z0-9\.-_]+)?\s*$/; | ||
| 315 | } elsif ($verb eq "version") { | ||
| 316 | return "$verb $args" if $args =~ /^\s*$/; | ||
| 317 | } elsif ($verb eq "whogets") { | ||
| 318 | return "$verb $args" if $args =~ /^\s*$GROUP_CHARS+(\.$GROUP_CHARS+)* | ||
| 319 | (\s+[a-zA-Z0-9\.-_]+)?\s*$/x; | ||
| 320 | } elsif ($verb eq "checkgroups") { | ||
| 321 | return "$verb $args" if $args =~ /^\s*$/; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | return undef; | ||
| 325 | } | ||
| 326 | |||
| 327 | =item clean_distibution ( ARRAY ) | ||
| 328 | |||
| 329 | =item clean_keywords ( ARRAY ) | ||
| 330 | |||
| 331 | Takes an array of strings and returns a properly formatted array of their | ||
| 332 | contents. And yes, these are the same function. | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub clean_distribution { | ||
| 337 | my ($self, @array) = @_; | ||
| 338 | my %distribs; | ||
| 339 | foreach (@array) { | ||
| 340 | foreach (split('\s*,\s*', $_)) { | ||
| 341 | $distribs{$_}++ unless /^\s*$/; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | return keys %distribs; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub clean_keywords { clean_distribution(@_) } | ||
| 348 | |||
| 349 | =item clean_header ( HEADER, ARGHASHREF, VALUE ) | ||
| 350 | |||
| 351 | Basically a giant switch statement between all of the above. Passes | ||
| 352 | C<VALUE> into the functions if we get it; otherwise, we get it out of | ||
| 353 | C<header()>. Arguments come in C<ARGHASHREF>. | ||
| 354 | |||
| 355 | Additional headers that can be cleaned with this: | ||
| 356 | |||
| 357 | See-Also Parsed with clean_references() | ||
| 358 | Reply-To Parsed with clean_from() | ||
| 359 | Also-Control Parsed with clean_control() | ||
| 360 | Supersedes Clears unless clean_messageid() doesn't change anything. | ||
| 361 | |||
| 362 | Headers that are known to be clear text (X-*, NNTP-*, Organization, | ||
| 363 | Summary, Lines) have their leading and trailing whitespace trimmed. Other | ||
| 364 | headers have nothing change at all. | ||
| 365 | |||
| 366 | Returns the updated information. | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub clean_header { | ||
| 371 | my ($self, $header, $args, $value) = @_; | ||
| 372 | |||
| 373 | $args ||= []; | ||
| 374 | $value ||= $self->header($header); | ||
| 375 | |||
| 376 | if (lc $header eq 'references' || lc $header =~ /^see[_-]also$/ ) { | ||
| 377 | return $self->clean_references( @{$args}[0] || $MAX_REFERENCES, $value ); | ||
| 378 | |||
| 379 | } elsif ($header =~ /^(from|reply[-_]to)$/) { | ||
| 380 | return $self->clean_from( $value ); | ||
| 381 | |||
| 382 | } elsif (lc $header eq 'newsgroups') { | ||
| 383 | return scalar $self->clean_newsgroups( $value ); | ||
| 384 | |||
| 385 | } elsif ($header =~ /^followup[_-]to$/) { | ||
| 386 | return scalar $self->clean_followupto( $value ); | ||
| 387 | |||
| 388 | } elsif ($header =~ /^message[_-]id$/) { | ||
| 389 | return $self->clean_messageid( $value ); | ||
| 390 | |||
| 391 | } elsif (lc $header eq 'date' || lc $header eq 'expires') { | ||
| 392 | return $self->clean_date( $value ); | ||
| 393 | |||
| 394 | } elsif (lc $header eq 'subject') { | ||
| 395 | return $self->clean_subject( $value ); | ||
| 396 | |||
| 397 | } elsif (lc $header eq 'distribution' || lc $header eq 'keywords') { | ||
| 398 | return scalar $self->clean_distribution( $value ); | ||
| 399 | |||
| 400 | } elsif ($header =~ /^(also[-_])?control$/) { | ||
| 401 | return $self->clean_control( $value ); | ||
| 402 | |||
| 403 | } elsif (lc $header eq 'supersedes') { | ||
| 404 | my $return = $self->clean_messageid( $value ); | ||
| 405 | return undef unless ( $value =~ /^<?$return?>$/); | ||
| 406 | return $return; | ||
| 407 | |||
| 408 | ## Not yet implemented | ||
| 409 | # } elsif (lc $header eq 'approved') { | ||
| 410 | # return join(',', @args); | ||
| 411 | |||
| 412 | # } elsif (lc $header eq 'path') { | ||
| 413 | # return join('!', @args); | ||
| 414 | |||
| 415 | # Trim the leading/trailing whitespace, but that's all. | ||
| 416 | } elsif ($header =~ /^(x.*|nntp.*|organization|summary|lines)$/x) { | ||
| 417 | $value =~ s/(^\s+|\s+$)//; | ||
| 418 | return $value; | ||
| 419 | |||
| 420 | } else { return $value } | ||
| 421 | } | ||
| 422 | |||
| 423 | =item clean_head ( HEADER, ARGS ) | ||
| 424 | |||
| 425 | Sets the value of C<HEADER> to the response of C<clean_header( HEADER, | ||
| 426 | ARGS)>. Basically a one-step helper function. | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | sub clean_head { | ||
| 431 | my ($self, $header, $args) = @_; | ||
| 432 | $self->set_headers($header, scalar $self->clean_header( $header, $args ) ); | ||
| 433 | } | ||
| 434 | |||
| 435 | =item clean_head_all () | ||
| 436 | |||
| 437 | Runs C<clean_head()> on all headers in the message. | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub clean_head_all { | ||
| 442 | my ($self) = @_; | ||
| 443 | foreach ( $self->header_names() ) { $self->clean_head($_) } | ||
| 444 | $self; | ||
| 445 | } | ||
| 446 | |||
| 447 | =item clean_body () | ||
| 448 | |||
| 449 | Not yet done. Or really all that close. It does currently do its | ||
| 450 | modifications in place... | ||
| 451 | |||
| 452 | =cut | ||
| 453 | |||
| 454 | sub clean_body { | ||
| 455 | my ($self) = @_; | ||
| 456 | |||
| 457 | my @body = $self->body(); | ||
| 458 | |||
| 459 | # Convert Microsoft smart quotes to their real counterparts. | ||
| 460 | map { tr/\x91\x92\x93\x94/\`\'\"\"/ } @body; | ||
| 461 | map { s/\x85/--/g } @body; | ||
| 462 | |||
| 463 | # Remove CRs (DOS line endings, most likely) and delete | ||
| 464 | # characters. | ||
| 465 | map { tr/\r\x7f//d } @body; | ||
| 466 | |||
| 467 | # Wipe out any non-ISO 8859-1 characters | ||
| 468 | map { s/[^\s!-~\xa0-\xff]//g } @body; | ||
| 469 | |||
| 470 | # Finish off later | ||
| 471 | |||
| 472 | $self->set_body(@body); | ||
| 473 | return wantarray ? @body : join("\n", @body); | ||
| 474 | } | ||
| 475 | |||
| 476 | =item clean_article () | ||
| 477 | |||
| 478 | Runs C<clean_head_all()> and C<clean_body()> on the article. | ||
| 479 | |||
| 480 | =cut | ||
| 481 | |||
| 482 | sub clean_article { | ||
| 483 | my ($self) = @_; | ||
| 484 | $self->clean_head_all && $self->clean_body; | ||
| 485 | } | ||
| 486 | |||
| 487 | =back | ||
| 488 | |||
| 489 | =cut | ||
| 490 | |||
| 491 | ############################################################################### | ||
| 492 | ### Internal Subroutines ###################################################### | ||
| 493 | ############################################################################### | ||
| 494 | |||
| 495 | ## _format_refs ( COLS, REFARRAY ) | ||
| 496 | # Formats a References: line for COLS columns. Used by format_references(). | ||
| 497 | sub _format_refs { | ||
| 498 | my ($cols, @refs) = @_; | ||
| 499 | my $less = $cols - 8; | ||
| 500 | my $refs = shift(@refs) || ""; | ||
| 501 | my $length = 4 + length ($refs); | ||
| 502 | foreach (@refs) { | ||
| 503 | $length += 1 + length $_; | ||
| 504 | $refs .= ($length < $less ? ' ' : "\n\t") . $_; | ||
| 505 | $length = length $_ if ($length >= $less); | ||
| 506 | } | ||
| 507 | $refs; | ||
| 508 | } | ||
| 509 | |||
| 510 | ## _format_date ( TIME ) | ||
| 511 | # Takes TIME in seconds-since-epoch; returns a string suitable for formatting | ||
| 512 | # into a Date: format. | ||
| 513 | sub _format_date { | ||
| 514 | my $time = shift || time; | ||
| 515 | my ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); | ||
| 516 | my ($gsec,$gmin,$ghr,$gmday) = gmtime($time); | ||
| 517 | |||
| 518 | use integer; | ||
| 519 | $gmday = $mday + ($mday <=> $gmday) if (abs($mday-$gmday) > 1); | ||
| 520 | my $tzdiff = 24*60*($mday-$gmday) + 60*($hr-$ghr) + ($min-$gmin); | ||
| 521 | my $tz = sprintf("%+04.4d", $tzdiff + ($tzdiff/60*40)); | ||
| 522 | |||
| 523 | $mon = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon]; | ||
| 524 | $wday = (qw(Sun Mon Tue Wed Thu Fri Sat Sun))[$wday]; | ||
| 525 | $year += 1900; | ||
| 526 | |||
| 527 | my $return = sprintf("%s, %02d %s %d %02d:%02d:%02d $tz", | ||
| 528 | $wday,$mday,$mon,$year,$hr,$min,$sec,$tz); | ||
| 529 | $return; | ||
| 530 | } | ||
| 531 | |||
| 532 | 1; | ||
| 533 | |||
| 534 | =head2 Variables | ||
| 535 | |||
| 536 | The following global variables are added to News::Article when this | ||
| 537 | package is loaded. | ||
| 538 | |||
| 539 | =over 4 | ||
| 540 | |||
| 541 | =item $News::Article::MAX_REFERENCES | ||
| 542 | |||
| 543 | Used by clean_references() to determine what the maximum number of entries | ||
| 544 | in the References: header should be. Defaults to 10, can be set within | ||
| 545 | clean_references(). | ||
| 546 | |||
| 547 | =item $News::Article::MY_DOMAIN | ||
| 548 | |||
| 549 | Used by clean_messageid(), clean_references(), clean_from(), and | ||
| 550 | clean_control() as the default domain for IDs (see B<News::Article>) and | ||
| 551 | From: lines. Defaults to $ENV{'HOSTNAME'}, hostfqdn(), or | ||
| 552 | 'broken-configuration'; this is something that you may want to set on your | ||
| 553 | own. | ||
| 554 | |||
| 555 | =item $News::Article::MY_PREFIX | ||
| 556 | |||
| 557 | Used by clean_messageid() | ||
| 558 | as the default prefix new message-IDs (see B<News::Article>)IDs and From: lines. Defaults | ||
| 559 | to $ENV{'HOSTNAME'}, hostfqdn(), or 'broken-configuration'; this is | ||
| 560 | something that you may want to set on your own. | ||
| 561 | |||
| 562 | |||
| 563 | =item $News::Article::GROUP_CHARS, $News::Article::TAG_CHAR, [...] | ||
| 564 | |||
| 565 | Defined in RFC1036bis and used here to decide what header text is valid | ||
| 566 | and what is not. The full list of variables: | ||
| 567 | |||
| 568 | =over 2 | ||
| 569 | |||
| 570 | GROUP_CHARS TAG_CHAR CODE_CHAR CHARSET ENCODING CODES | ||
| 571 | ENCODED_WORD UNQUOTED_CHAR QUOTED_CHAR PAREN_CHAR UNQUOTED_WORD | ||
| 572 | QUOTED_WORD PLAIN_WORD PAREN_PHRASE PLAIN_PHRASE LOCAL_PART DOMAIN | ||
| 573 | ADDRESS FROM_CONTENT | ||
| 574 | |||
| 575 | =back | ||
| 576 | |||
| 577 | =head1 NOTES | ||
| 578 | |||
| 579 | The RFC1036bis character formatting bits are fairly old, but seem to be | ||
| 580 | fairly well in use across Usenet at this date. They may well be replaced | ||
| 581 | sometime in the near future, though. | ||
| 582 | |||
| 583 | =head1 REQUIREMENTS | ||
| 584 | |||
| 585 | Date::Parse, News::Article | ||
| 586 | |||
| 587 | =head1 SEE ALSO | ||
| 588 | |||
| 589 | B<News::Article> | ||
| 590 | |||
| 591 | =head1 TODO | ||
| 592 | |||
| 593 | Finish off clean_body(). Put into NewsLib. Set version to 'v1.0'. Use | ||
| 594 | the newer RFC when it's put out. | ||
| 595 | |||
| 596 | =head1 AUTHOR | ||
| 597 | |||
| 598 | Tim Skirvin <tskirvin@killfile.org> | ||
| 599 | |||
| 600 | =head1 LICENSE | ||
| 601 | |||
| 602 | This code may be redistributed under the same terms as Perl itself. | ||
| 603 | |||
| 604 | =head1 COPYRIGHT | ||
| 605 | |||
| 606 | Copyright 1996-2004, Tim Skirvin. | ||
| 607 | |||
| 608 | =cut | ||
| 609 | |||
| 610 | ############################################################################### | ||
| 611 | ##### Version History ######################################################### | ||
| 612 | ############################################################################### | ||
| 613 | |||
| 614 | # v1.00 Thu Mar 18 14:23:29 CST 2004 | ||
| 615 | ### Actually trying to get this into a useful, releasable format now. | ||
| 616 | # v1.01 Mon Mar 22 11:34:35 CST 2004 | ||
| 617 | ### Documentation fixes. | ||
| 618 | # v1.02 Thu Apr 22 11:40:18 CDT 2004 | ||
| 619 | ### Just fixing things to make them better for Perl. |