| 1 | $VERSION = "0.51"; | ||
| 2 | package News::Web; | ||
| 3 | our $VERSION = "0.51"; | ||
| 4 | |||
| 5 | # -*- Perl -*- # Sun Oct 12 16:05:05 CDT 2003 | ||
| 6 | ############################################################################# | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2003, Tim | ||
| 8 | # Skirvin. Redistribution terms are below. | ||
| 9 | ############################################################################# | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | News::Web - a News<->Web gateway, for a web-based newsreader | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use News::Web; | ||
| 18 | [...] | ||
| 19 | |||
| 20 | See 'news.cgi', included with this distribution, to see how this actually | ||
| 21 | works. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | News::Web is the basis for web-based newsreaders. It's essentially a | ||
| 26 | collection of functions called by CGI scripts appropriately. | ||
| 27 | |||
| 28 | =head1 USAGE | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | ############################################################################### | ||
| 33 | ### main() #################################################################### | ||
| 34 | ############################################################################### | ||
| 35 | |||
| 36 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 1 | ▶ | use Net::NNTP; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 1 | ▶ | use News::Article; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 1 | ▶ | use News::Article::Response; # Newslib |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 1 | ▶ | use News::Article::Ref; # Newslib |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 0 | ▶ | use CGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 44 | use Date::Parse; | ||
| 45 | use News::Overview; | ||
| 46 | |||
| 47 | use vars qw( $DEBUG $TIMEOUT @ISA $NAME ); | ||
| 48 | $DEBUG = 0; | ||
| 49 | $TIMEOUT = 120; | ||
| 50 | $NAME = "News::Web v$VERSION"; | ||
| 51 | |||
| 52 | use vars qw( @DEFAULTHEAD $MAXHEADLENGTH $COLUMNS $ROWS $SIGROWS ); | ||
| 53 | |||
| 54 | @DEFAULTHEAD = qw( message-id newsgroups followup-to from subject date | ||
| 55 | references ); | ||
| 56 | # References? | ||
| 57 | $MAXHEADLENGTH = 1024; | ||
| 58 | $COLUMNS = 80; | ||
| 59 | $ROWS = 30; | ||
| 60 | $SIGROWS = 4; | ||
| 61 | |||
| 62 | =head2 Basic Functions | ||
| 63 | |||
| 64 | These functions create and deal with the object itself. | ||
| 65 | |||
| 66 | =over 4 | ||
| 67 | |||
| 68 | =item new ( ITEM ) | ||
| 69 | |||
| 70 | C<ITEM> is a reference to an object similar to Net::NNTP - that is, this | ||
| 71 | was designed to work directly with Net::NNTP, but will also work with | ||
| 72 | other similar classes such as News::Archive. Returns a reference to the | ||
| 73 | new object. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my ($proto, $item) = @_; | ||
| 79 | my $class = ref($proto) || $proto; | ||
| 80 | my $self = { 'connection' => $item }; | ||
| 81 | bless $self, $class; | ||
| 82 | $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | =item connect () | ||
| 86 | |||
| 87 | =item nntp () | ||
| 88 | |||
| 89 | These functions return the actual NNTP connection (or whatever was passed | ||
| 90 | into new()), to be manipulated directly by other functions. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub connection { shift->{connection} } | ||
| 95 | sub nntp { shift->{connection} } | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =head2 HTML functions | ||
| 100 | |||
| 101 | These functions create the HTML tables used by the various CGI scripts. | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item html_article ( ARGHASH ) | ||
| 106 | |||
| 107 | Returns an HTML-formatted version of a news article, either by passing in | ||
| 108 | a full article, a message ID of an article to retrieve, or a newsgroup and | ||
| 109 | message number to pass. Also includes several linkback()'d sets of actions | ||
| 110 | we can perform on the article. | ||
| 111 | |||
| 112 | Arguments we take from C<ARGHASH>: | ||
| 113 | |||
| 114 | article A full news article, read directly into the | ||
| 115 | News::Article object. | ||
| 116 | mid The Message-ID of an article to read. | ||
| 117 | group \ Together, the group and article number to read in | ||
| 118 | number / from the NNTP connection to get the article. | ||
| 119 | fullhead Should we use full headers, or a limited set (as | ||
| 120 | specified in @News::Web::DEFAULTHEAD)? Defaults to 0. | ||
| 121 | clean Just print the article, and not the linkbacks. | ||
| 122 | Defaults to 0. | ||
| 123 | plaintext Should we print this as plaintext or as HTML? | ||
| 124 | Defaults to 0 (HTML). | ||
| 125 | default A hashref of defaults; this isn't actually used here, | ||
| 126 | but is passed on where necessary to other functions. | ||
| 127 | |||
| 128 | Current linkbacks (we'll work out a way to better format and select these | ||
| 129 | later). | ||
| 130 | |||
| 131 | Follow Up Respond to this article (with html_makearticle()). | ||
| 132 | Full Headers Show all of the headers, not just the limited set. | ||
| 133 | Original Format The message in its original format (only if we're | ||
| 134 | not already doing so). | ||
| 135 | First in Thread The first article in the thread (only if there | ||
| 136 | is one). | ||
| 137 | Immediate Parent The article that this message responded to (if | ||
| 138 | there is one). | ||
| 139 | |||
| 140 | Next Article \ Links to the next/previous article in each | ||
| 141 | Previous Article / group (not based on thread, sadly) | ||
| 142 | |||
| 143 | I'm not entirely happy with the format of this yet, but it works for now. | ||
| 144 | I would also to add "next/previous in thread", rot13, lists of children, | ||
| 145 | and so forth. And the linkback list may at some point be more consistent | ||
| 146 | and available programmatically, which would let it be tied into other | ||
| 147 | functions, such as moderation 'bots. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub html_article { | ||
| 152 | my ($self, %args) = @_; | ||
| 153 | |||
| 154 | # Get the article, one way or the other | ||
| 155 | my ($article, @return, %linkback); | ||
| 156 | if ($args{'article'}) { $article = $args{'article'}; } | ||
| 157 | elsif (my $mid = $self->clean('Message-ID', $args{'mid'})) { | ||
| 158 | (carp "Bad Message-ID" && return '') unless $mid; | ||
| 159 | $article = $self->nntp->article($mid); | ||
| 160 | unless ($article) { | ||
| 161 | my $clean = $args{'plaintext'} ? $mid : $self->html_clean($mid); | ||
| 162 | return "No such article: $clean"; | ||
| 163 | } | ||
| 164 | $mid =~ s/^<//g; $mid =~ s/>$//g; | ||
| 165 | $linkback{'mid'} = $mid; | ||
| 166 | } elsif ($args{'group'} && $args{'number'}) { | ||
| 167 | $self->nntp->group($args{'group'}) || return "No such group '$args{group}'"; | ||
| 168 | $article = $self->nntp->article($args{'number'}) | ||
| 169 | || return "No such message '$args{number}' in '$args{group}'"; | ||
| 170 | $linkback{'group'} = $args{'group'}; $linkback{'number'} = $args{'number'}; | ||
| 171 | } else { return "No message specified" } | ||
| 172 | |||
| 173 | # Read in the article into a News::Article object | ||
| 174 | my $art = News::Article->new($article); | ||
| 175 | return "No article" unless $art; | ||
| 176 | |||
| 177 | # Actually print the article | ||
| 178 | if ($args{'plaintext'}) { | ||
| 179 | push @return, join('', join("\n", $art->headers, '', $art->body, '')); | ||
| 180 | } else { push @return, $self->_html_article($art, $args{'fullhead'} || 0) } | ||
| 181 | |||
| 182 | # If we're just getting the article with 'clean' or 'plaintext', don't | ||
| 183 | # offer the linkbacks. | ||
| 184 | return @return if $args{'clean'} || $args{'plaintext'}; | ||
| 185 | |||
| 186 | # Additional linkbacks for the article | ||
| 187 | my @linkback; | ||
| 188 | my $mid = $art->header('message-id'); $mid =~ s/(^<|>$)//g; | ||
| 189 | push @linkback, $self->linkback( "Follow Up", { 'post' => 1, 'mid' => $mid }) | ||
| 190 | if $self->nntp->postok; # Should look at something else | ||
| 191 | push @linkback, $self->linkback( "Full Headers", | ||
| 192 | { 'fullhead' => 1, %linkback } ); | ||
| 193 | push @linkback, $self->linkback( "Normal Format", { %linkback } ) | ||
| 194 | if ($args{'fullhead'} || $args{'plaintext'}); | ||
| 195 | |||
| 196 | my @ids = split(/\s+/, $art->header('references')); | ||
| 197 | my %linkarts = ( "First in Thread" => $ids[0], | ||
| 198 | "Immediate Parent" => $ids[scalar @ids - 1] ); | ||
| 199 | foreach my $desc (sort keys %linkarts) { | ||
| 200 | my $id = $linkarts{$desc}; $id =~ s/^<|>$//g; | ||
| 201 | push @linkback, $self->linkback( $desc, { 'mid' => $id } ) if $id; | ||
| 202 | } | ||
| 203 | |||
| 204 | push @return, join(" ", "<p>Article actions: [ ", | ||
| 205 | join(" | ", @linkback), " ]</p>") if scalar @linkback; | ||
| 206 | |||
| 207 | # Determine which groups we will use the 'next' and 'previous' article | ||
| 208 | # options on. We use Xref for the list; this isn't really ideal, IMO, | ||
| 209 | # but it's a good start. | ||
| 210 | my %grouplist; | ||
| 211 | if (my $group = $args{'group'} and my $number = $args{'number'}) { | ||
| 212 | $grouplist{$group} = $number; | ||
| 213 | } else { | ||
| 214 | my ($server, @grouplist) = split(/\s+/, $art->header('xref')); | ||
| 215 | foreach (@grouplist) { | ||
| 216 | my ($group, $number) = split(':', $_); | ||
| 217 | $grouplist{$group} = $number; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | foreach my $group (sort keys %grouplist) { | ||
| 222 | my $number = $grouplist{$group}; | ||
| 223 | |||
| 224 | # We really want a 'next in thread' option, sadly. Or whatever sorting | ||
| 225 | # method we used. This will be *TRICKY* to accomplish. | ||
| 226 | my ($count, $first, $last, $name) = $self->nntp->group($group); | ||
| 227 | my @linkback2; | ||
| 228 | |||
| 229 | push @linkback2, $number <= $first ? "<i>Previous</i>" | ||
| 230 | : $self->linkback( "Previous", | ||
| 231 | { 'group' => $group, 'number' => $number - 1 } ); | ||
| 232 | push @linkback2, $number >= $last ? "<i>Next</i>" | ||
| 233 | : $self->linkback( "Next", | ||
| 234 | { 'group' => $group, 'number' => $number + 1 } ); | ||
| 235 | |||
| 236 | push @return, join("", "<p> Articles in $group: [ ", | ||
| 237 | join(" | ", @linkback2), " ]</p>") if scalar @linkback2; | ||
| 238 | } | ||
| 239 | |||
| 240 | join("\n", @return); | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | =item html_makearticle ( ARGHASH ) | ||
| 245 | |||
| 246 | Creates an HTML form to write new articles, based on a previous article if | ||
| 247 | the proper information is passed in with C<ARGHASH>. This is put into a | ||
| 248 | table that has three major sections - the header section, the body, and | ||
| 249 | the signature. | ||
| 250 | |||
| 251 | If a previous article is indicated (with 'mid'), then we base the new | ||
| 252 | message off of that article with News::Article::Response. | ||
| 253 | |||
| 254 | Arguments we take from C<ARGHASH>: | ||
| 255 | mid The Message-ID of a message we're responding to; | ||
| 256 | group The newsgroup we're posting to | ||
| 257 | prefix A prefix to the new message-ID (see News::Article) | ||
| 258 | domain The domain of the new message-ID (see News::Article). | ||
| 259 | columns The number of columns to format the body and | ||
| 260 | signature. Defaults to $News::Web::COLUMNS or 80. | ||
| 261 | rows The number of rows for the textarea box of the body; | ||
| 262 | defaults to $News::Web::ROWS or 30. | ||
| 263 | sigrows The number of rows for the textarea box of the | ||
| 264 | signature box; defaults to $News::Web::SIGROWS or 30. | ||
| 265 | nosignature Don't offer use the signature box. | ||
| 266 | wraptype How should we wrap the quoted material? See | ||
| 267 | News::Article::Reference. Defaults to 'overflow'. | ||
| 268 | params A hashref of extra parameters to pass into html_post() | ||
| 269 | default A hashref of defaults; this isn't actually used here, | ||
| 270 | but is passed on where necessary to other functions. | ||
| 271 | |||
| 272 | Current linkbacks: | ||
| 273 | |||
| 274 | Post Meant to invoke html_post() | ||
| 275 | Preview Meant to invoke html_post() with the preview flag | ||
| 276 | |||
| 277 | We're not really using CSS at all yet, which is a mistake. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub html_makearticle { | ||
| 282 | my ($self, %args) = @_; | ||
| 283 | my $cgi = new CGI; | ||
| 284 | |||
| 285 | my $default = $args{'default'} || {}; | ||
| 286 | my $params = $args{'params'} || {}; | ||
| 287 | |||
| 288 | # Get defaults out of %args or from the module | ||
| 289 | my $cols = $args{'columns'} || $COLUMNS || 80; | ||
| 290 | my $rows = $args{'rows'} || $ROWS || 30; | ||
| 291 | my $sigrows = $args{'sigrows'} || $SIGROWS || 4; | ||
| 292 | |||
| 293 | # Get the article that we're starting from | ||
| 294 | my $oldart; | ||
| 295 | if (my $mid = $args{'mid'}) { | ||
| 296 | my $clean = $self->clean('Message-ID', $mid); | ||
| 297 | my $message = $self->nntp->article($clean) || []; | ||
| 298 | $oldart = News::Article->new( $message ); | ||
| 299 | } else { $oldart = new News::Article } | ||
| 300 | my $art = $args{'article'} ? $args{'article'} | ||
| 301 | : News::Article->response($oldart, | ||
| 302 | { 'From' => $args{'author'} || "", | ||
| 303 | 'Newsgroups' => $args{'group'} || undef }, | ||
| 304 | |||
| 305 | 'prefix' => $args{'prefix'} || "", | ||
| 306 | 'domain' => $args{'domain'} || "", | ||
| 307 | 'colwrap' => $cols, | ||
| 308 | 'wraptype' => $args{'wraptype'} || 'overflow', | ||
| 309 | 'nodate' => 1, | ||
| 310 | ); | ||
| 311 | |||
| 312 | my @return = "<h2> Composing Article </h2>"; | ||
| 313 | push @return, $cgi->start_form('post'); | ||
| 314 | |||
| 315 | push @return, $cgi->start_table({ -cellpadding => 0, | ||
| 316 | -cellspacing => 0 }); | ||
| 317 | |||
| 318 | # User-modifiable headers | ||
| 319 | foreach ( qw( Newsgroups From Subject ) ) { | ||
| 320 | next unless $art->header($_); | ||
| 321 | push @return, $cgi->Tr({-align=>'left', -valign=>'CENTER'}, | ||
| 322 | [ $cgi->td( [$_, $cgi->textfield(-name => "header_$_", | ||
| 323 | -default => $art->header($_) || "", | ||
| 324 | -size => $cols - length $_, -maxlength => $MAXHEADLENGTH) ]) ]); | ||
| 325 | } | ||
| 326 | # Headers that are already set | ||
| 327 | foreach ( qw( Message-ID References ) ) { | ||
| 328 | next unless $art->header($_); | ||
| 329 | push @return, $cgi->Tr({-align=>'left', -valign=>'CENTER'}, | ||
| 330 | [ $cgi->td( [$_, join("\n", | ||
| 331 | $self->html_clean($art->header($_) || ""), | ||
| 332 | $cgi->hidden(-name => "header_$_", | ||
| 333 | -default => $art->header($_) || "" )) ]) ]); | ||
| 334 | } | ||
| 335 | |||
| 336 | # Body of the message - this will be in its own textarea box. | ||
| 337 | my @body = $art->body; | ||
| 338 | |||
| 339 | # Put headers beyond the ones we've already processed into the body of | ||
| 340 | # the message, so they can be looked at and all. | ||
| 341 | my %extrahead; | ||
| 342 | BODYHEAD: | ||
| 343 | foreach my $head ($art->header_names) { | ||
| 344 | foreach ( qw( Message-ID References Newsgroups From Subject ) ) { | ||
| 345 | next BODYHEAD if lc $_ eq lc $head; | ||
| 346 | } | ||
| 347 | $extrahead{News::Article::canonical($head)} = $art->header($head); | ||
| 348 | } | ||
| 349 | unshift @body, '' if scalar keys %extrahead; | ||
| 350 | foreach (keys %extrahead) { unshift @body, "$_: $extrahead{$_}" } | ||
| 351 | my $body = join("\n", @body); | ||
| 352 | |||
| 353 | # Blank line between the headers and body | ||
| 354 | push @return, $cgi->Tr( [ $cgi->td( " " ) ] ); | ||
| 355 | push @return, $cgi->Tr({-align=>'left', -valign=>'CENTER'}, | ||
| 356 | [ $cgi->td( { -colspan => 2, -nowrap => 1 }, | ||
| 357 | [ $cgi->textarea( -name => 'body', -default => $body, | ||
| 358 | -rows => $rows, -cols => $cols, -wrap => 'hard') ] | ||
| 359 | ) ]); | ||
| 360 | |||
| 361 | # Signature field, if so desired (with '-- ') | ||
| 362 | unless ($args{'nosignature'}) { | ||
| 363 | push @return, $cgi->Tr({-align=>'left', -valign=>'CENTER'}, | ||
| 364 | [ $cgi->td( { -align=>'left', -colspan=>2}, "-- ") ] ); | ||
| 365 | |||
| 366 | push @return, $cgi->Tr({-align=>'left', -valign=>'CENTER'}, | ||
| 367 | [ $cgi->td( { -colspan=>2, -nowrap=>1 }, [ | ||
| 368 | $cgi->textarea(-name => 'signature', -wrap => 'hard', | ||
| 369 | -default => $args{signature} || "", | ||
| 370 | -rows => $sigrows, -cols => $cols) ] ) ]); | ||
| 371 | } | ||
| 372 | |||
| 373 | # Submit/preview the article | ||
| 374 | push @return, $cgi->Tr({}, | ||
| 375 | [ $cgi->td({-colspan=>2, -align=>'right'}, | ||
| 376 | [ join(" ", $cgi->submit(-name=>'preview', -value=>'Preview'), | ||
| 377 | $cgi->submit(-name=>'post', -value=>'Post') ) ]) ]); | ||
| 378 | |||
| 379 | foreach (keys %{$params}) { | ||
| 380 | next if lc $_ eq 'preview'; | ||
| 381 | next if lc $_ eq 'post'; | ||
| 382 | push @return, $cgi->hidden($_, $$params{$_}); | ||
| 383 | } | ||
| 384 | |||
| 385 | push @return, $cgi->end_table; | ||
| 386 | push @return, $cgi->end_form; | ||
| 387 | |||
| 388 | join("\n", @return); | ||
| 389 | } | ||
| 390 | |||
| 391 | =item html_post ( ARGHASH ) | ||
| 392 | |||
| 393 | Actually posts the message. Gets the article from passed in arguments | ||
| 394 | through C<ARGHASH>, adds some headers, and does the work. | ||
| 395 | |||
| 396 | Arguments we take from C<ARGHASH>: | ||
| 397 | |||
| 398 | params CGI parameters that were passed in | ||
| 399 | header_* The headers of the message | ||
| 400 | body The body of the message, separated by newlines | ||
| 401 | signature The signature of the message | ||
| 402 | |||
| 403 | trace The content to set 'X-Local-Trace' to, which is | ||
| 404 | currently set by the CGI (but should probably be | ||
| 405 | done locally). | ||
| 406 | default A hashref of defaults; this isn't actually used here, | ||
| 407 | but is passed on where necessary to other functions. | ||
| 408 | |||
| 409 | Extra headers are pulled out of the first lines of the body of the | ||
| 410 | message. Adds 'X-Newsreader' and 'X-Local-Trace', drops 'Approved' and | ||
| 411 | 'Date'. Runs html_makearticle() if necessary because the article didn't, | ||
| 412 | or wouldn't, post. | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | sub html_post { | ||
| 417 | my ($self, %args) = @_; | ||
| 418 | return "Can't post to this server" unless $self->nntp->postok; | ||
| 419 | |||
| 420 | my $article = new News::Article; | ||
| 421 | my $params = $args{'params'} || {}; | ||
| 422 | |||
| 423 | # Get headers out of the $params hashref; all headers are preceded with | ||
| 424 | # header_. | ||
| 425 | foreach (sort %{$params}) { | ||
| 426 | next unless /^header_(.*)$/; | ||
| 427 | my $header = $1; | ||
| 428 | $article->set_headers($header, $$params{$_}); | ||
| 429 | } | ||
| 430 | |||
| 431 | my $body = $$params{'body'}; | ||
| 432 | my $signature = $$params{'signature'}; | ||
| 433 | $signature =~ s/\s*$//; # Trim out trailing whitespace | ||
| 434 | |||
| 435 | $article->set_body($body); | ||
| 436 | $article->trim_blank_lines; | ||
| 437 | if ($signature) { $article->add_body("-- ", $signature); } | ||
| 438 | |||
| 439 | # Get headers out of the body | ||
| 440 | my (@body, $headers); | ||
| 441 | foreach my $line ($article->body) { | ||
| 442 | if (!$headers && $line =~ /^([\w-]+):\s+(.*)$/) { | ||
| 443 | $article->set_headers($1, $2); | ||
| 444 | } elsif (!$headers && /^\s+/) { $headers++ } | ||
| 445 | else { push @body, $line } | ||
| 446 | } | ||
| 447 | $article->set_body(@body); | ||
| 448 | |||
| 449 | $article->drop_headers('approved', 'date', 'x-newsreader'); | ||
| 450 | $article->add_date; | ||
| 451 | $article->set_headers('x-newsreader', $NAME ); | ||
| 452 | $article->set_headers('x-local-trace', $args{'trace'}); | ||
| 453 | |||
| 454 | # Abort if we don't have all of the necessary headers | ||
| 455 | my @problems; | ||
| 456 | foreach ( qw( Newsgroups Date Message-ID From Subject ) ) { | ||
| 457 | push @problems, "Missing the '$_' header" unless $article->header($_); | ||
| 458 | } | ||
| 459 | |||
| 460 | if (scalar @problems) { | ||
| 461 | my @return; | ||
| 462 | push @return, "<h2> Problems Posting </h2>", "<ul>"; | ||
| 463 | foreach (@problems) { push @return, "<li> $_"; } | ||
| 464 | push @return, "</ul>"; | ||
| 465 | push @return, $self->html_makearticle( 'article' => $article, %args); | ||
| 466 | return @return; | ||
| 467 | } | ||
| 468 | |||
| 469 | # Here's the actual posting | ||
| 470 | my $messageid = $article->header('message-id'); | ||
| 471 | my $nntp = $self->nntp; | ||
| 472 | my @return; | ||
| 473 | my $preview = $$params{'preview'}; | ||
| 474 | if ($preview) { | ||
| 475 | push @return, "<h2> Preview </h2>"; | ||
| 476 | push @return, $self->_html_article($article, 1); | ||
| 477 | push @return, $self->html_makearticle( 'article' => $article, %args); | ||
| 478 | } else { | ||
| 479 | my $ret = eval { $article->post( $nntp ) }; | ||
| 480 | if ($@) { | ||
| 481 | my $error = $@; | ||
| 482 | chomp $error; # Remove trailing whitespace | ||
| 483 | warn "Error in posting $messageid: $error\n"; | ||
| 484 | push @return, "<h2> Problems Posting </h2>", "<ul>"; | ||
| 485 | push @return, "<li> $error"; | ||
| 486 | push @return, "</ul>"; | ||
| 487 | push @return, $self->html_makearticle( 'article' => $article, %args); | ||
| 488 | } else { | ||
| 489 | my $id = $article->header('message-id'); | ||
| 490 | push @return, "Message $id posted successfully <br /> <br />\n"; | ||
| 491 | |||
| 492 | push @return, $self->_html_article($article, 1); | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | @return; | ||
| 497 | } | ||
| 498 | |||
| 499 | =item html_overview ( ARGHASH ) | ||
| 500 | |||
| 501 | Generate an HTML-formatted table of the overview entries of a given | ||
| 502 | newsgroup (see News::Overview). This table consists of nexttable(), | ||
| 503 | tableheaders(), lines for each entry, then nexttable() again. | ||
| 504 | |||
| 505 | COUNT is the number of articles we should get; it should be the number of | ||
| 506 | articles that we actually return, but this isn't done yet. The subject is | ||
| 507 | linkback()'d to the actual message. | ||
| 508 | |||
| 509 | Arguments we take in C<ARGHASH>: | ||
| 510 | |||
| 511 | count The number of articles that we should return. | ||
| 512 | Currently, this is actually the number that we ask | ||
| 513 | for. | ||
| 514 | last The last article we should get. With 'count', FIRST = | ||
| 515 | COUNT - LAST + 1. | ||
| 516 | first The first article we should get. With 'count' and no | ||
| 517 | 'last', LAST = first + count - 1 | ||
| 518 | sort The sorting method for the articles, as set in | ||
| 519 | News::Overview. | ||
| 520 | fields The fields from the overview DB to add columns for; | ||
| 521 | defaults to News::Overview's defaults. These | ||
| 522 | default A hashref of defaults; this isn't actually used here, | ||
| 523 | but is passed on where necessary to other functions. | ||
| 524 | |||
| 525 | =cut | ||
| 526 | |||
| 527 | sub html_overview { | ||
| 528 | my ($self, %args) = @_; | ||
| 529 | my $group = $args{'group'} || ""; | ||
| 530 | my ($count, $first, $last, $name) = $self->nntp->group($group) | ||
| 531 | or ( carp "Couldn't connect to $group: $!\n" && return ''); | ||
| 532 | |||
| 533 | # If we actually set count in args, then we should base things off of it | ||
| 534 | if (defined $args{count}) { | ||
| 535 | if ($args{first}) { | ||
| 536 | $first = $args{first}; | ||
| 537 | $last = $first + $args{count} - 1; | ||
| 538 | } elsif ($args{last} && $args{last} >= $first ) { | ||
| 539 | $first = $args{last} - $args{count} + 1; | ||
| 540 | $last = $args{last}; | ||
| 541 | } else { # We always take the last batch of articles; good idea? | ||
| 542 | $first = $last - $args{count} + 1; | ||
| 543 | } | ||
| 544 | $count = $args{count}; | ||
| 545 | } else { | ||
| 546 | # Figure out where to start and stop with this overview information | ||
| 547 | $first = $args{first} if (defined $args{first} && $args{first} >= $first); | ||
| 548 | $last = $args{last} if (defined $args{last} && $args{last} <= $last ); | ||
| 549 | } | ||
| 550 | |||
| 551 | $first = 1 if ($first <= 1); | ||
| 552 | |||
| 553 | my $default = $args{'default'} || \%args; | ||
| 554 | my $sort = $args{'sort'} || 'thread'; | ||
| 555 | |||
| 556 | # Get the overview format information | ||
| 557 | my $fmt = $self->nntp->overview_fmt || undef; | ||
| 558 | |||
| 559 | # Start the overview object | ||
| 560 | my $overview = News::Overview->new( ref $fmt ? $fmt : "" ); | ||
| 561 | |||
| 562 | # Request and parse the overview information | ||
| 563 | my $xover= $self->nntp->xover("$first-$last") | ||
| 564 | or ( carp "Couldn't get xover info: $!\n" && return '' ); | ||
| 565 | foreach my $msg (sort keys %{$xover}) { | ||
| 566 | $overview->add_from_nntp($msg, $$xover{$msg}); | ||
| 567 | } | ||
| 568 | |||
| 569 | # return '' unless scalar keys %{$xover}; | ||
| 570 | unless (scalar keys %{$xover}) { | ||
| 571 | return $self->nntp->postok ? | ||
| 572 | join("", "<p align=center>", $self->linkback( "Post a New Message", | ||
| 573 | { 'post' => 1, 'group' => $group }), "</p>") | ||
| 574 | : ""; | ||
| 575 | } | ||
| 576 | |||
| 577 | # If we didn't get a fields list, default to the default overview ones | ||
| 578 | my $fields = $args{'fields'} || $overview->fields(); | ||
| 579 | |||
| 580 | my @return; | ||
| 581 | |||
| 582 | # Not ideal, but it'll do briefly; we should have it only pass those | ||
| 583 | # values that we absolutely need. | ||
| 584 | my %passargs; | ||
| 585 | $passargs{'group'} = $group if $group; | ||
| 586 | $passargs{'first'} = $first; | ||
| 587 | $passargs{'last'} = $last; | ||
| 588 | $passargs{'count'} = $count; | ||
| 589 | $passargs{'sort'} = $args{'sort'}; | ||
| 590 | |||
| 591 | push @return, $self->nexttable ( \%passargs, [ $self->nntp->group($group) ], | ||
| 592 | $default ); | ||
| 593 | |||
| 594 | push @return, "<table cellpadding=0 width=100%>", "<tr>"; | ||
| 595 | push @return, $self->tableheaders( $fields, \%passargs, $default); | ||
| 596 | push @return, "</tr>"; | ||
| 597 | |||
| 598 | my $even = '0'; | ||
| 599 | |||
| 600 | foreach my $entry ( $overview->sort( $sort, $overview->entries ) ) { | ||
| 601 | my $mid = $entry->values->{'Message-ID'}; | ||
| 602 | $mid =~ s/^<//g; $mid =~ s/>$//g; | ||
| 603 | my $number = $entry->values->{'Number'}; | ||
| 604 | |||
| 605 | push @return, "<tr>"; | ||
| 606 | foreach (@{$fields}) { | ||
| 607 | my $nowrap = "nowrap"; | ||
| 608 | my $value = $self->html_clean( | ||
| 609 | $self->clean($_, $entry->values->{$_}, $entry)); | ||
| 610 | $value = $self->linkback($value, | ||
| 611 | { 'group' => $group, 'number' => $number }) | ||
| 612 | # { 'mid' => $mid }) | ||
| 613 | if lc $_ eq 'subject'; | ||
| 614 | if (lc $_ eq 'newsgroups') { | ||
| 615 | $nowrap = ""; | ||
| 616 | my @groups = split(',', $value); | ||
| 617 | @groups = grep { $_ !~ /archive\..*/ } @groups; # Drop archive.* groups | ||
| 618 | map { $_ = $self->linkback($_, { 'group' => $_ }) } @groups; | ||
| 619 | $value = join(', ', @groups); | ||
| 620 | } | ||
| 621 | |||
| 622 | push @return, defined $value | ||
| 623 | ? "<td class='overview_$even' $nowrap>$value</td>" | ||
| 624 | : "<td class='overview_$even' $nowrap>   </td>"; | ||
| 625 | } | ||
| 626 | push @return, "</tr>"; | ||
| 627 | if ($even == 0) { $even++ } else { $even = 0 } | ||
| 628 | } | ||
| 629 | |||
| 630 | push @return, "</tr>", "</table>"; | ||
| 631 | my ($self, $params, $groupinfo, $defaults) = @_; | ||
| 632 | push @return, $self->nexttable ( \%passargs, [ $self->nntp->group($group) ], | ||
| 633 | $default ); | ||
| 634 | |||
| 635 | push @return, join("", "<p align=center>", | ||
| 636 | $self->linkback( "Post a New Message", { 'post' => 1, 'group' => $group }), | ||
| 637 | "</p>") if $self->nntp->postok; | ||
| 638 | join("\n", @return); | ||
| 639 | } | ||
| 640 | |||
| 641 | =item nexttable ( PARAMHASH, GROUPINFO, DEFAULTHASH ) | ||
| 642 | |||
| 643 | Creates the 'next table' bits for html_overview(). C<GROUPINFO> is an | ||
| 644 | arrayref that is the response of Net::NNTP->group(), and is used to | ||
| 645 | determine what articles exist so we know what to link to. | ||
| 646 | |||
| 647 | C<PARAMHASH> and C<DEFAULTHASH> are passed to linkback() (with different | ||
| 648 | 'sort' options). | ||
| 649 | |||
| 650 | We don't have any CSS hooks right now, again a mistake. | ||
| 651 | |||
| 652 | Returns as an array of HTML lines. | ||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub nexttable { | ||
| 657 | my ($self, $params, $groupinfo, $defaults) = @_; | ||
| 658 | return "" unless ($params && ref $params); | ||
| 659 | my $count = $$params{count}; return "" unless $count; | ||
| 660 | my $first = $$params{first}; my $last = $$params{last}; | ||
| 661 | |||
| 662 | $defaults ||= {}; | ||
| 663 | |||
| 664 | $last = @{$groupinfo}[2] if $last >= @{$groupinfo}[2]; | ||
| 665 | |||
| 666 | $first ||= 1; | ||
| 667 | my @return = "<table width=100%>"; | ||
| 668 | push @return, "<tr>"; | ||
| 669 | |||
| 670 | # Initialize hashes | ||
| 671 | my %prev; foreach (keys %{$params}) { $prev{$_} = $$params{$_} }; | ||
| 672 | my %next; foreach (keys %{$params}) { $next{$_} = $$params{$_} }; | ||
| 673 | |||
| 674 | $prev{last} = $first - 1; delete $prev{first}; | ||
| 675 | $next{first} = $first + $count; delete $next{last}; | ||
| 676 | |||
| 677 | my %sort1; foreach (keys %{$params}) { $sort1{$_} = $$params{$_} }; | ||
| 678 | $sort1{'sort'} ||= "thread"; | ||
| 679 | my %sort2; foreach (keys %{$params}) { $sort2{$_} = $$params{$_} }; | ||
| 680 | $sort2{'sort'} ||= "-thread"; | ||
| 681 | |||
| 682 | my $prev = $first < @{$groupinfo}[1] | ||
| 683 | ? "<i>Previous</i" | ||
| 684 | : $self->linkback("Previous", \%prev, $defaults) ; | ||
| 685 | push @return, "<th align=left>$prev</th>\n"; | ||
| 686 | push @return, "<th width=100%>", "Articles $first - $last", | ||
| 687 | ( $$params{sort} eq 'thread' ? "" : | ||
| 688 | "<br />" . $self->linkback( "Sort by Thread", \%sort1, $defaults ) ), | ||
| 689 | "</th>"; | ||
| 690 | my $next = $last >= @{$groupinfo}[2] | ||
| 691 | ? "<i>Next</i>" | ||
| 692 | : $self->linkback("Next", \%next, $defaults) ; | ||
| 693 | push @return, "<th align=right>$next</th>\n"; | ||
| 694 | |||
| 695 | push @return, "</tr>", "</table>"; | ||
| 696 | @return; | ||
| 697 | } | ||
| 698 | |||
| 699 | =item tableheaders ( FIELDARRAYREF, PARAMHASH, DEFAULTHASH ) | ||
| 700 | |||
| 701 | Creates the table headers for html_overview(). C<FIELDARRAYREF> is the | ||
| 702 | list of fields that will be printed in the table body. Each of these is | ||
| 703 | printed as two linkback()s, one to sort based on this field and the other | ||
| 704 | to sort the same but backwards. These links are parsed by | ||
| 705 | html_overview(). C<PARAMHASH> and C<DEFAULTHASH> are passed to linkback() | ||
| 706 | (with different 'sort' options). | ||
| 707 | |||
| 708 | Stylesheet hooks: | ||
| 709 | |||
| 710 | groupinfo TH style to describe the headers | ||
| 711 | |||
| 712 | Returns as an array of <th> lines. | ||
| 713 | |||
| 714 | =cut | ||
| 715 | |||
| 716 | sub tableheaders { | ||
| 717 | my ($self, $fields, $params, $default) = @_; | ||
| 718 | my @return; | ||
| 719 | foreach my $field (@{$fields}) { | ||
| 720 | my %local; foreach (keys %{$params}) { $local{$_} = $$params{$_} } | ||
| 721 | $local{'sort'} = ucfirst lc $field; | ||
| 722 | my $field1 = $self->linkback($field, \%local, $default); | ||
| 723 | $local{'sort'} = join('-', '', ucfirst lc $field); | ||
| 724 | my $field2 = $self->linkback('-', \%local, $default); | ||
| 725 | push @return, "<th class='groupinfo'>$field1 <br /> ($field2)</th>" | ||
| 726 | }; | ||
| 727 | @return; | ||
| 728 | } | ||
| 729 | |||
| 730 | =item html_grouplist ( [PATTERN] ) | ||
| 731 | |||
| 732 | Lists all of the active newsgroups, based on C<PATTERN> (defaults to '*'), | ||
| 733 | with descriptions. Returns the text to be printed, joined by newlines. | ||
| 734 | |||
| 735 | If C<PATTERN> is not passed in, then we will instead get the default list | ||
| 736 | of groups out of 'subscriptions'. | ||
| 737 | |||
| 738 | Possible refinements: should we list the number of messages (estimated or | ||
| 739 | real)? The posting status of the group (moderated, no-posting, etc)? | ||
| 740 | |||
| 741 | Stylesheet hooks: | ||
| 742 | |||
| 743 | grouplist_head TR and TD style, for the headers of the table. | ||
| 744 | grouplist TR style, for the actual table content lines. | ||
| 745 | grouplist_1 TD style, alternating between the two styles, | ||
| 746 | grouplist_2 to allow the lines to look different (and | ||
| 747 | and therefore be easier to follow). | ||
| 748 | |||
| 749 | =cut | ||
| 750 | |||
| 751 | sub html_grouplist { | ||
| 752 | my ($self, $pattern, $newsrc, $print) = @_; # Not using the last two | ||
| 753 | my ($groups, $descs); | ||
| 754 | if ($pattern) { | ||
| 755 | $groups = $self->nntp->active($pattern) | ||
| 756 | or ( carp "Couldn't get newsgroups: $!\n" && return ''); | ||
| 757 | } else { | ||
| 758 | $groups = {}; | ||
| 759 | foreach ( @{$self->nntp->subscriptions()} ) { | ||
| 760 | my $value = [ $self->nntp->active($_) ]; | ||
| 761 | next unless $value; | ||
| 762 | $$groups{$_} = [ $value ]; | ||
| 763 | } | ||
| 764 | $pattern = "*"; | ||
| 765 | } | ||
| 766 | $descs = $self->nntp->newsgroups('*') | ||
| 767 | or ( carp "Couldn't get group descriptions: $!\n" && return ''); | ||
| 768 | |||
| 769 | return "" unless scalar keys %{$groups}; | ||
| 770 | |||
| 771 | my (@return); | ||
| 772 | my $even = 0; | ||
| 773 | |||
| 774 | push @return, "<table cellpadding=0 width=100%>"; | ||
| 775 | push @return, "<tr class='grouplist_head'>", | ||
| 776 | "<th class='grouplist_head'>Group</th>", | ||
| 777 | "<th class='grouplist_head'>Description</th>", "</tr>"; | ||
| 778 | foreach my $group (sort keys %{$groups}) { | ||
| 779 | my ($last, $first, $flags) = @{$$groups{$group}}; | ||
| 780 | push @return, "<tr class='grouplist'>"; | ||
| 781 | my $link = $self->linkback($group, { 'group' => $group }); | ||
| 782 | push @return, | ||
| 783 | "<td class='grouplist_$even'> $link </td>"; | ||
| 784 | push @return, $$descs{$group} | ||
| 785 | ? "<td class='grouplist_$even'>$$descs{$group}</td>" | ||
| 786 | : "<td class='grouplist_$even'> </td>"; | ||
| 787 | push @return, "</tr>"; | ||
| 788 | if ($even == 0) { $even++ } else { $even = 0 } | ||
| 789 | } | ||
| 790 | |||
| 791 | push @return, "</table>"; | ||
| 792 | join("\n", @return, ''); | ||
| 793 | } | ||
| 794 | |||
| 795 | =item html_hierarchies ( LEVELS, PATTERN ) | ||
| 796 | |||
| 797 | Gives a set of linkback()'d group listings, based on the newsgroups | ||
| 798 | available on the server. C<PATTERN> is the WILDMAT pattern to decide | ||
| 799 | which groups to match; C<LEVELS> defines how many levels down to go down | ||
| 800 | when matching the pattern ('news' would be one level, 'news.admin' would | ||
| 801 | be two, etc). Doesn't match actual newsgroups, just hierarchies. | ||
| 802 | |||
| 803 | Returns the list of linkbacks in an array context, or a line of them | ||
| 804 | combined with ' | ' as a scalar. | ||
| 805 | |||
| 806 | =cut | ||
| 807 | |||
| 808 | sub html_hierarchies { | ||
| 809 | my ($self, $levels, $pattern) = @_; | ||
| 810 | $levels ||= 1; $pattern ||= "*"; | ||
| 811 | |||
| 812 | my $groups = $self->nntp->active($pattern) | ||
| 813 | or ( carp "Couldn't get newsgroups: $!\n" && return ''); | ||
| 814 | |||
| 815 | my %hiers; | ||
| 816 | foreach (sort keys %{$groups}) { | ||
| 817 | my @array = split('\.', $_); | ||
| 818 | $hiers{$array[0]}++; | ||
| 819 | for (my $i = 1; ( $i < scalar @array ) && ( $i < $levels ); $i++) { | ||
| 820 | my $string = $i eq 0 ? $array[0] : join('.', @array[0..$i]); | ||
| 821 | $hiers{$string}++; | ||
| 822 | } | ||
| 823 | } | ||
| 824 | my @return; | ||
| 825 | foreach (sort keys %hiers) { | ||
| 826 | push @return, $self->linkback($_, { 'pattern' => "$_.*" }); | ||
| 827 | } | ||
| 828 | wantarray ? @return : join(" | ", @return); | ||
| 829 | } | ||
| 830 | |||
| 831 | =item linkback( TEXT, HASHREF, DEFAULT ) | ||
| 832 | |||
| 833 | Returns an HTML link back to the same program, based on the hash | ||
| 834 | references C<HASHREF> and C<DEFAULT>. C<TEXT> is the string that appears | ||
| 835 | in the link. The key/value pairs in C<HASHREF> are the options passed in | ||
| 836 | the URL; however, if the C<DEFAULT> hash matching value matches | ||
| 837 | C<HASHREF>, then we assume that we don't need that argument (and we should | ||
| 838 | try to keep the URL short anyway). | ||
| 839 | |||
| 840 | This probably needs more refinement, but it more or less works. | ||
| 841 | |||
| 842 | =cut | ||
| 843 | |||
| 844 | sub linkback { | ||
| 845 | my ($self, $text, $hash, $default) = @_; | ||
| 846 | $hash ||= {}; $default ||= {}; | ||
| 847 | my $url = $0; $url =~ s%.*/%%g; # Should be something better | ||
| 848 | my @opts; | ||
| 849 | foreach (sort keys %{$hash}) { | ||
| 850 | push @opts, "$_=$$hash{$_}" unless $$default{$_} eq $$hash{$_}; | ||
| 851 | } | ||
| 852 | my $opts = join('&', @opts); | ||
| 853 | my $text = "<a href='$url?$opts'>$text</a>"; | ||
| 854 | $text; | ||
| 855 | } | ||
| 856 | |||
| 857 | =item clean ( HEADER, INFO, ENTRY, ARGS ) | ||
| 858 | |||
| 859 | Cleans up the news information for the best distribution. Mostly useful | ||
| 860 | for creating new articles and parsing article inforation properly; not so | ||
| 861 | useful for actually printing articles, where the original formatting may | ||
| 862 | have been generally useful. C<HEADER> choices that are currently | ||
| 863 | supported: | ||
| 864 | |||
| 865 | subject Formats the Subject: line to have quote characters | ||
| 866 | at the start (based on the number of entries in | ||
| 867 | References: within C<ENTRY>) and trim the total | ||
| 868 | length of the string. | ||
| 869 | from Formats the From: line consistently; by default it | ||
| 870 | gets the actual author and drops the email address. | ||
| 871 | Also trims the total length of the string (see | ||
| 872 | the arguments section). | ||
| 873 | date Format the date consistently with Format::Date's | ||
| 874 | str2time() command. | ||
| 875 | message-id Make sure that the passed ID is properly formatted | ||
| 876 | with '<' and '>' characters. | ||
| 877 | |||
| 878 | Arguments we take: | ||
| 879 | |||
| 880 | subjwidth Width to trim the Subject: line to; if less than 0, | ||
| 881 | then we don't trim the header at all. Defaults to 55. | ||
| 882 | fromwidth Width to trim the From: line to; if less than 0, then | ||
| 883 | we don't trim the header at all. Defaults to 25. | ||
| 884 | fromtype The formatting method for the From: line. Possible | ||
| 885 | options: 'name', 'nameemail', 'email', 'emailname'. | ||
| 886 | Defaults to 'name'. | ||
| 887 | |||
| 888 | This should be replaced with something from News::Article::Ref, or moved | ||
| 889 | into there. | ||
| 890 | |||
| 891 | =cut | ||
| 892 | |||
| 893 | sub clean { | ||
| 894 | my ($self, $header, $info, $entry, %args) = @_; | ||
| 895 | my @return; | ||
| 896 | if (lc $header eq 'subject') { | ||
| 897 | @return = $self->_subject($info, $entry ? $entry->depth : 0, | ||
| 898 | $args{'subjwidth'} || 55 ) | ||
| 899 | } | ||
| 900 | elsif (lc $header eq 'from') { | ||
| 901 | @return = $self->_author($info, $args{'fromwidth'} || 25, | ||
| 902 | $args{'fromtype'} || 'name' ) | ||
| 903 | } | ||
| 904 | elsif (lc $header eq 'date') { @return = $self->_date($info, %args) } | ||
| 905 | elsif (lc $header eq 'message-id') { @return = $self->_mid($info, %args) } | ||
| 906 | else { @return = $info } | ||
| 907 | wantarray ? @return : join("\n", @return); | ||
| 908 | } | ||
| 909 | |||
| 910 | |||
| 911 | =item html_clean ( LINE [, LINE [...]] ) | ||
| 912 | |||
| 913 | Cleans up C<LINE>(s) for the web - ie, fixes special HTML characters, and | ||
| 914 | sets up links for http:// and ftp:// links. Returns a string containing | ||
| 915 | the modified lines, joined with newlines. | ||
| 916 | |||
| 917 | There's probably a lot more that can be done here. | ||
| 918 | |||
| 919 | =cut | ||
| 920 | |||
| 921 | sub html_clean { | ||
| 922 | my ($self, @return) = @_; | ||
| 923 | map { s/\</\</g } @return; | ||
| 924 | map { s/\>/\>/g } @return; | ||
| 925 | map { s%((?:http|ftp):\/\/[^\s&]+)%<a href="$1">$1</a>%g } @return; | ||
| 926 | join("\n", @return); | ||
| 927 | } | ||
| 928 | |||
| 929 | =item html_markup ( HEADER, TEXT [, ARGS] ) | ||
| 930 | |||
| 931 | Marks up C<HEADER> and C<TEXT> to be printed in HTML. C<TEXT> is put | ||
| 932 | through html_clean() and an additional set of fixes: | ||
| 933 | |||
| 934 | newsgroups linkback() to the given group | ||
| 935 | |||
| 936 | C<HEADER> is bolded. The final layout - "HEADER: TEXT". | ||
| 937 | |||
| 938 | =cut | ||
| 939 | |||
| 940 | sub html_markup { | ||
| 941 | my ($self, $header, $text, %args) = @_; | ||
| 942 | $text = $self->html_clean($text); | ||
| 943 | if (lc $header eq 'newsgroups') { | ||
| 944 | $text = $self->_html_newsgroups($text, %args); | ||
| 945 | } | ||
| 946 | $header = News::Article::canonical($header); | ||
| 947 | $text =~ s/(\r?\n)/<br \/>$1 /g; | ||
| 948 | $text ? "<b>$header</b>: $text" : ""; | ||
| 949 | } | ||
| 950 | |||
| 951 | ############################################################################### | ||
| 952 | ### Internal Functions ######################################################## | ||
| 953 | ############################################################################### | ||
| 954 | |||
| 955 | ### _subject ( STRING, DEPTH, WIDTH ) | ||
| 956 | # Update the subject with an appropriate number of '>' marks (based on | ||
| 957 | # DEPTH), and to a certain text width (based on WIDTH). | ||
| 958 | sub _subject { | ||
| 959 | my ($self, $string, $depth, $width) = @_; | ||
| 960 | $width ||= 55; $depth ||= 0; $string ||= ""; | ||
| 961 | |||
| 962 | # This should be more general-purpose, but it'll do for now | ||
| 963 | $string =~ s/^Re:\s*//i if $depth; | ||
| 964 | |||
| 965 | my $quotestring = ""; | ||
| 966 | if ($depth < 10) { | ||
| 967 | for (my $i = 0; $i < $depth; $i++) { | ||
| 968 | $quotestring = join('', '>', $quotestring); | ||
| 969 | } | ||
| 970 | } | ||
| 971 | elsif ($depth < 100) { $quotestring = ">>> $depth >>"; } | ||
| 972 | else { $quotestring = ">> lots >"; } | ||
| 973 | $string = $depth ? "$quotestring $string" : $string; | ||
| 974 | |||
| 975 | my $real = $width - 5; | ||
| 976 | $string =~ s%^(.{0,$real})(.{5})(.*)$%join('', $1, $3 ? '[...]' : $2)%egx | ||
| 977 | unless ( $width <= 0 && length $string <= $real ); | ||
| 978 | |||
| 979 | $string; | ||
| 980 | } | ||
| 981 | |||
| 982 | ### _author ( STRING, WIDTH, TYPE ) | ||
| 983 | # Get the actual writer out of a From: line, dropping the email address | ||
| 984 | # and trimming it down to WIDTH characters, and canonicalizes the format | ||
| 985 | # based on TYPE ('name', 'email', 'namemail', or 'emailname'). | ||
| 986 | # | ||
| 987 | # Note that this is *far* too complicated, and based on old code that I | ||
| 988 | # wrote a long time ago and never bothered to make into a real module; I | ||
| 989 | # should really write that module. Should also do something better than | ||
| 990 | # 'unknown.site.invalid' and such. | ||
| 991 | sub _author { | ||
| 992 | my ($self, $string, $width, $type) = @_; return undef unless $string; | ||
| 993 | $width ||= 25; $type ||= 'nameemail'; $string ||= ""; | ||
| 994 | my $address = ""; my $comment = ""; | ||
| 995 | |||
| 996 | # Information from News::Article::Ref | ||
| 997 | my $PLAIN_PHRASE = $News::Article::Ref::PLAIN_PHRASE; | ||
| 998 | my $ADDRESS = $News::Article::Ref::ADDRESS; | ||
| 999 | my $PAREN_PHRASE = $News::Article::Ref::PAREN_PHRASE; | ||
| 1000 | my $LOCAL_PART = $News::Article::Ref::LOCAL_PART; | ||
| 1001 | my $DOMAIN = $News::Article::Ref::DOMAIN; | ||
| 1002 | |||
| 1003 | # RFC 1036 standard From: formats | ||
| 1004 | if ($string =~ /^\s*(?:\"?($PLAIN_PHRASE)?\"?\s*<($ADDRESS)>| | ||
| 1005 | ($ADDRESS)\s*(?:\(($PAREN_PHRASE)\))?)\s*$/x) { | ||
| 1006 | $address = $2 || $3; | ||
| 1007 | $comment = $1 || $4; | ||
| 1008 | |||
| 1009 | # No sitename was attached to the address - either append the local one if | ||
| 1010 | # appropriate or set something saying that there wasn't one at all. | ||
| 1011 | } elsif ($string =~ /^\s*(?:\"?($PLAIN_PHRASE)?\"?\s*<($LOCAL_PART)>| | ||
| 1012 | ($LOCAL_PART)\s*(?:\(($PAREN_PHRASE)\))?)\s*$/x) { | ||
| 1013 | $address = $2 || $3; | ||
| 1014 | $comment = $1 || $4; | ||
| 1015 | |||
| 1016 | my $host = "unknown.site.invalid"; | ||
| 1017 | $address = join('@', $address, $host); | ||
| 1018 | |||
| 1019 | # The phrases had a bad part to them - scrap those parts. | ||
| 1020 | } elsif ($string =~ /^\s*(?:(.*)\s*<($LOCAL_PART\@?$DOMAIN?)>| | ||
| 1021 | ($LOCAL_PART\@?$DOMAIN?)\s*(.*))\s*$/x) { | ||
| 1022 | $address = $2 || $3; | ||
| 1023 | $comment = $1 || $4; | ||
| 1024 | |||
| 1025 | unless ($address =~ /\@\S+$/) { | ||
| 1026 | $address =~ s/\@$//g; | ||
| 1027 | my $host = "unknown.site.invalid"; | ||
| 1028 | $address = join('@', $address, $host); | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | # There's no way we're getting a valid address out of this, so let's see | ||
| 1032 | # if we can find something *invalid* | ||
| 1033 | } else { | ||
| 1034 | if ($string =~ /^\s*(.*)\s*<(.*\@.*)>\s*$/) { $comment= $1; $address = $2 } | ||
| 1035 | elsif ($string =~ /^\s*(\S+\@\S+)\s*(.*)\s*$/) { | ||
| 1036 | if ($2) { $address = $1; $comment= $2 } | ||
| 1037 | } | ||
| 1038 | else { $string =~ s/^\s+|\s+$//g; } | ||
| 1039 | map { s/^\s*|\s*//g } $comment, $string, $address; | ||
| 1040 | $comment =~ s/[\"\(\)]//g; | ||
| 1041 | |||
| 1042 | $comment ||= $string; | ||
| 1043 | $address ||= 'unknown@unknown.site.invalid'; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | $address ||= ""; $comment ||= ""; | ||
| 1047 | map { s%(^\s*|\s*$)%%g } ($address, $comment); | ||
| 1048 | |||
| 1049 | $comment =~ s/[\"\(\)]//g; | ||
| 1050 | |||
| 1051 | my $retstring; | ||
| 1052 | if (lc $type eq 'email') { $retstring = $address; } | ||
| 1053 | elsif (lc $type eq 'nameemail') { $retstring = "$comment <$address>" } | ||
| 1054 | elsif (lc $type eq 'name') { $retstring = $comment } | ||
| 1055 | elsif (lc $type eq 'emailname') { $retstring = "$address ($comment)" } | ||
| 1056 | else { $retstring = "$comment <$address>" } | ||
| 1057 | |||
| 1058 | # We always want *something* | ||
| 1059 | $retstring ||= $address; | ||
| 1060 | |||
| 1061 | my $real = $width - 5; | ||
| 1062 | $retstring =~ s%^(.{0,$real})(.{5})(.*)$%join('', $1, $3 ? '[...]' : $2)%ex | ||
| 1063 | unless ( $width <= 0 && length $retstring <= $real ); | ||
| 1064 | $retstring; | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | ### _date ( [DATE] ) | ||
| 1068 | # Print the date in a consistent format; this really ought to be the | ||
| 1069 | # consistent news format, though. If DATE isn't passed, we just use the | ||
| 1070 | # current time. We should use the stuff from News::Article's add_date(). | ||
| 1071 | sub _date { | ||
| 1072 | my $self = shift; | ||
| 1073 | my $time = str2time(shift); | ||
| 1074 | my @MONTHS = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); | ||
| 1075 | sprintf("%03s %02d %04d %02d:%02d:%02d", | ||
| 1076 | @MONTHS[(localtime($time))[4]], (localtime($time))[3], | ||
| 1077 | (localtime($time))[5] + 1900, # (localtime($time))[4] + 1, | ||
| 1078 | (localtime($time))[2], (localtime($time))[1], (localtime($time))[0] ); | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | ### _mid ( ID ) | ||
| 1082 | # Make sure the '<' and '>' are on the ID, if the ID exists. Probably | ||
| 1083 | # ought to make sure the ID is good first. | ||
| 1084 | sub _mid { | ||
| 1085 | my ($self, $mid) = @_; | ||
| 1086 | return "" unless $mid; | ||
| 1087 | $mid = join('', '<', $mid, '>'); | ||
| 1088 | $mid =~ s/^<</</g; $mid =~ s/>>$/>/g; | ||
| 1089 | $mid; | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | ### _html_newsgroups ( LINE ) | ||
| 1093 | # html-ify the Newsgroups: header, with linkback(). | ||
| 1094 | sub _html_newsgroups { | ||
| 1095 | my ($self, $line) = @_; | ||
| 1096 | my @groups = split(',', $line); | ||
| 1097 | map { $_ = $self->linkback($_, { 'group' => $_ }) } @groups; | ||
| 1098 | join(',', @groups); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | ### _html_article ( ARTICLE, FULLHEAD ) | ||
| 1102 | # Actually does the work of returning the article in HTML form. If | ||
| 1103 | # FULLHEAD is set, returns all headers; otherwise, only uses DEFAULTHEAD. | ||
| 1104 | sub _html_article { | ||
| 1105 | my ($self, $article, $fullhead) = @_; | ||
| 1106 | my @return; | ||
| 1107 | my @headers = $fullhead ? sort $article->header_names : @DEFAULTHEAD; | ||
| 1108 | foreach my $header ( @headers ) { | ||
| 1109 | my $value = $self->html_markup($header, $article->header($header)); | ||
| 1110 | push @return, "$value<br />" if $value; | ||
| 1111 | } | ||
| 1112 | my @body; | ||
| 1113 | foreach ($article->body) { push @body, $self->html_clean($_) } | ||
| 1114 | push @return, join('', "<pre>", join("\n", @body), "</pre>"); | ||
| 1115 | @return; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | =head1 REQUIREMENTS | ||
| 1119 | |||
| 1120 | News::Overview, Net::NNTP, News::Article, News::Article::Response and | ||
| 1121 | News::Article::Ref (both part of NewsLib), IO::File, CGI.pm, Date::Parse | ||
| 1122 | |||
| 1123 | =head1 SEE ALSO | ||
| 1124 | |||
| 1125 | B<News::Overview>, B<News::Web::CookieAuth>, B<News::Article::Ref>, | ||
| 1126 | B<News::Article::Response> | ||
| 1127 | |||
| 1128 | =head1 NOTES | ||
| 1129 | |||
| 1130 | I'm not really done with this thing yet. This is just something that | ||
| 1131 | generally *works*, and has something resembling documentation. I've got a | ||
| 1132 | lot of work to do to make this what I really want to do, but I'm happy | ||
| 1133 | with the start. | ||
| 1134 | |||
| 1135 | =head1 TODO | ||
| 1136 | |||
| 1137 | Still have a ways to go with stylesheets. | ||
| 1138 | |||
| 1139 | Should really use the Tr() type functions for making tables. | ||
| 1140 | |||
| 1141 | Various user interface improvements in html_article(), as well as backend | ||
| 1142 | improvements. | ||
| 1143 | |||
| 1144 | $count should really offer the number of articles we asked for, no matter | ||
| 1145 | what, rather than estimating things. | ||
| 1146 | |||
| 1147 | mod_perl-ify this stuff. | ||
| 1148 | |||
| 1149 | It'd be nice if the documentation were a bit more transparent, enough so | ||
| 1150 | that someone could recreate the actual gateway .cgi files without having | ||
| 1151 | to refer to them. | ||
| 1152 | |||
| 1153 | =head1 AUTHOR | ||
| 1154 | |||
| 1155 | Tim Skirvin <tskirvin@killfile.org> | ||
| 1156 | |||
| 1157 | =head1 COPYRIGHT | ||
| 1158 | |||
| 1159 | Copyright 2003 by Tim Skirvin <tskirvin@killfile.org>. This code may be | ||
| 1160 | distributed under the same terms as Perl itself. | ||
| 1161 | |||
| 1162 | =cut | ||
| 1163 | |||
| 1164 | 1; | ||
| 1165 | |||
| 1166 | ############################################################################### | ||
| 1167 | ### Version History ########################################################### | ||
| 1168 | ############################################################################### | ||
| 1169 | # v0.1 Thu Sep 25 16:??:?? CDT 2003 | ||
| 1170 | ### First working version. | ||
| 1171 | # v0.2 | ||
| 1172 | ### Allows for arbitrary NNTP-like connections. | ||
| 1173 | ### Formatting in _subject() and _author() works more like it should | ||
| 1174 | # v0.3 | ||
| 1175 | ### We're still very far away from being releasable, but this version allows | ||
| 1176 | ### posting and is getting closer to set up in a reasoanable manner | ||
| 1177 | ### code-wide. | ||
| 1178 | # v0.40b Fri Oct 10 16:18:48 CDT 2003 | ||
| 1179 | ### Starting the proper commenting. | ||
| 1180 | # v0.50b Sun Oct 12 16:05:05 CDT 2003 | ||
| 1181 | ### Changed html_overview() to do linkbacks to group/number pairs, instead | ||
| 1182 | ### of the Message-ID directly | ||
| 1183 | # v0.50.01 Mon Oct 13 17:32:30 CDT 2003 | ||
| 1184 | ### 'Articles in xxx:' weren't getting the group from the right place. | ||
| 1185 | # v0.50.02 Mon Oct 13 19:29:56 CDT 2003 | ||
| 1186 | ### Wasn't offering 'post a new article' if the group was empty. Fixed. | ||
| 1187 | # v0.50.03 Fri Nov 07 15:44:46 CST 2003 | ||
| 1188 | ### Gets information out of 'subscriptions' if there's no PATTERN passed | ||
| 1189 | ### html_grouplist() | ||
| 1190 | # v0.50.04 Sun Mar 28 23:52:23 CST 2004 | ||
| 1191 | ### Small fixes in printing article info in html_makearticle(). | ||
| 1192 | # v0.51 Thu Apr 22 14:12:31 CDT 2004 | ||
| 1193 | ### Code cleanup. | ||