| 1 | $VERSION = "0.5"; | ||
| 2 | package News::Article; | ||
| 3 | our $VERSION = "0.5"; | ||
| 4 | |||
| 5 | # -*- Perl -*- # Fri Oct 10 09:55:42 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::Article::Response - create responses to News::Articles | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use News::Article::Response; | ||
| 18 | |||
| 19 | my $newart = News::Article->response($oldart, | ||
| 20 | { 'From' => 'Random User <random@user.org.invalid>' }, | ||
| 21 | 'prefix' => 'random.', 'colwrap' => 80, 'nodate' => 1 ); | ||
| 22 | |||
| 23 | use News::Article::Response qw( quotewrap ); | ||
| 24 | |||
| 25 | my @wrappedtext = quotewrap( 80, '> ', 'overflow', @text ); | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | News::Article::Response is a set of additional functions for News::Article | ||
| 30 | to create responses to previous articles. | ||
| 31 | |||
| 32 | =head1 USAGE | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | ############################################################################### | ||
| 37 | ### main() #################################################################### | ||
| 38 | ############################################################################### | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | use Exporter; | ||
| 42 | |||
| 43 | use vars qw( @ISA @EXPORT @EXPORT_OK ); | ||
| 44 | @ISA = qw( Exporter ); | ||
| 45 | @EXPORT = qw( ); | ||
| 46 | @EXPORT_OK = qw( quotewrap ); | ||
| 47 | |||
| 48 | use News::Article; | ||
| 49 | # use Text::Wrap; # Is this strictly necessary? Let's try it without... | ||
| 50 | |||
| 51 | ############################################################################### | ||
| 52 | ### Variables ################################################################# | ||
| 53 | ############################################################################### | ||
| 54 | |||
| 55 | use vars qw( $QUOTECHAR $COLWRAP $WRAPTYPE $QUOTECHARS $VERSION $RESTRING | ||
| 56 | $RESTRINGPATTERN ); | ||
| 57 | |||
| 58 | $QUOTECHAR = '> '; | ||
| 59 | $COLWRAP = 80; | ||
| 60 | $WRAPTYPE = "overflow"; | ||
| 61 | $QUOTECHARS = '(\s{0,5}[><=|#:}])+\s*'; | ||
| 62 | $RESTRING = "Re: "; | ||
| 63 | $RESTRINGPATTERN = '[rR][eE]:?\s*'; | ||
| 64 | |||
| 65 | ############################################################################### | ||
| 66 | ### Methods ################################################################### | ||
| 67 | ############################################################################### | ||
| 68 | |||
| 69 | =head2 Functions | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =item response ( ARTICLE, HEADERS, ARGUMENTS ) | ||
| 74 | |||
| 75 | Creates a new article that's in response to C<ARTICLE> - that is, with | ||
| 76 | headers based on the original C<ARTICLE>. Base headers that are created | ||
| 77 | should be Message-ID, Date, Newsgroups, References, and Subject; more | ||
| 78 | headers will be added as given in the hash reference C<HEADERS>. The body | ||
| 79 | of the message is a quoted version of the original message with the | ||
| 80 | signature trimmed off. | ||
| 81 | |||
| 82 | C<ARGUMENTS>, a hash, allows for user-modifed behavior. Arguments that we | ||
| 83 | listen to: | ||
| 84 | |||
| 85 | prefix Message-ID prefix (see News::Article) | ||
| 86 | domain Message-ID domain (see News::Article) | ||
| 87 | nomessageid Don't actually add a Message-ID: header | ||
| 88 | |||
| 89 | time Time to base Date: off (defaults to local time) | ||
| 90 | nodate Don't actually add a Date: header | ||
| 91 | |||
| 92 | ignore_followups Ignore the Followup-To: header when deciding | ||
| 93 | what groups to respond to. Especially important | ||
| 94 | if the original line was 'poster', as we return | ||
| 95 | with 'undef' in that case (we should be sending | ||
| 96 | an email) | ||
| 97 | newsgroups Newsgroups to respond to, ignoring both the old | ||
| 98 | Newsgroups: and Followup-To: headers. | ||
| 99 | |||
| 100 | keepsig Keep the signature for quoting. | ||
| 101 | quotechar The quote method; defaults to '> ', for use | ||
| 102 | with wrap(). | ||
| 103 | colwrap Columns to wrap at, for use with wrap(). | ||
| 104 | wraptype Wrapping type, for use with wrap(). | ||
| 105 | respstring A code reference that takes as input the old | ||
| 106 | article, and generates a string of the sort | ||
| 107 | "Tim Skirvin writes:" for the start of the | ||
| 108 | body. The default gets the information out of | ||
| 109 | From: or Reply-To:, or failing that uses uses | ||
| 110 | '(unknown)'. Doesn't get added if there's no | ||
| 111 | quoted material. | ||
| 112 | |||
| 113 | Returns 'undef' on failure (not enough arguments, followups were set to | ||
| 114 | poster so there shouldn't be an article anyway), otherwise returns a | ||
| 115 | News::Article object. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub response { | ||
| 120 | my ($self, $original, $headers, %args) = @_; | ||
| 121 | my $art = ref $self ? $self : new News::Article; | ||
| 122 | return undef unless ($original && ref $original); | ||
| 123 | $headers ||= {}; | ||
| 124 | |||
| 125 | $art->add_message_id( $args{'prefix'} || "", $args{'domain'} || "" ) | ||
| 126 | unless $args{nomessageid}; | ||
| 127 | $art->add_date($args{'time'} || "") unless $args{'nodate'}; | ||
| 128 | |||
| 129 | # Newsgroups: - we shouldn't respond at all if we're supposed to | ||
| 130 | # followup to poster | ||
| 131 | if ($args{'newsgroups'}) { | ||
| 132 | $art->set_headers('newsgroups'), $args{'newsgroups'}; | ||
| 133 | } elsif ($args{'ignore_followups'}) { | ||
| 134 | $art->set_headers('newsgroups', $original->header('newsgroups') || ""); | ||
| 135 | } else { | ||
| 136 | return undef if ($original->header('followup-to') eq 'poster' ); | ||
| 137 | $art->set_headers('newsgroups', $original->header('followup-to') || | ||
| 138 | $original->header('newsgroups') || ""); | ||
| 139 | } | ||
| 140 | |||
| 141 | # References - the original References: header, plus the Message ID | ||
| 142 | $art->set_headers('references', | ||
| 143 | join(" ", $original->header('references'), | ||
| 144 | $original->header('message-id') )); | ||
| 145 | |||
| 146 | # Add a Subject: based on the last Subject, with $RESTRING; this is a | ||
| 147 | # bit hard-coded for my tastes, but we need the regexp too... | ||
| 148 | my $subject = $$headers{'subject'} || ""; | ||
| 149 | $subject ||= join('', $RESTRING, $original->header('subject')) | ||
| 150 | if $original->header('subject') ; | ||
| 151 | $subject =~ s/^($RESTRINGPATTERN)+/$RESTRING/i if $subject; | ||
| 152 | $art->set_headers('subject', $subject || ""); | ||
| 153 | |||
| 154 | # Parse the $headers hash for more headers to add | ||
| 155 | foreach (keys %{$headers}) { | ||
| 156 | $art->set_headers($_, $$headers{$_}) if $$headers{$_} | ||
| 157 | } | ||
| 158 | |||
| 159 | # Trim off the old signature | ||
| 160 | my (@body, $postsig); | ||
| 161 | foreach ($original->body) { | ||
| 162 | if (/^-- /) { $postsig++ unless $args{'keepsig'}; } | ||
| 163 | next if $postsig; | ||
| 164 | push @body, $_; | ||
| 165 | } | ||
| 166 | |||
| 167 | # Wrap the message body and add the quote character | ||
| 168 | my $quotechar = $args{'quotechar'} || $QUOTECHAR || '>'; | ||
| 169 | my $columns = $args{'colwrap'} || $COLWRAP || 80; | ||
| 170 | my $wraptype = $args{'wraptype'} || $WRAPTYPE || "wrap"; | ||
| 171 | @body = quotewrap($columns, $quotechar, $wraptype, @body); | ||
| 172 | |||
| 173 | # Added in the "response string" thing if we have a body | ||
| 174 | if (scalar @body) { | ||
| 175 | my $code = $args{'respstring'} || \&_respstring; | ||
| 176 | unshift @body, $code->($original), ''; | ||
| 177 | } | ||
| 178 | |||
| 179 | $art->set_body(@body); | ||
| 180 | |||
| 181 | $art; | ||
| 182 | } | ||
| 183 | |||
| 184 | =item quotewrap ( COLUMNS, QUOTECHAR, WRAPTYPE, TEXT ) | ||
| 185 | |||
| 186 | Adds C<QUOTECHAR> to the start of each line of text from C<TEXT>, and then | ||
| 187 | wraps the text at C<COLUMNS> columns where necessary. This differs from | ||
| 188 | Text::Wrap in that lines that don't need to be wrapped won't be; that is, | ||
| 189 | we keep as much of the original formatting as possible. | ||
| 190 | |||
| 191 | If C<COLUMNS> is less than 0, then don't actually do the wrapping. | ||
| 192 | |||
| 193 | C<WRAPTYPE> is currently ignored. It probably won't be forever. Offer it | ||
| 194 | the same values as in Text::Wrap. | ||
| 195 | |||
| 196 | This function can be imported by other programs, but is not by default | ||
| 197 | (@EXPORT_OK). | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | sub quotewrap { | ||
| 202 | if (ref $_[0]) { shift @_ } # Is this being run as an object? | ||
| 203 | my ($columns, $quotechar, $wraptype, @body) = @_; | ||
| 204 | |||
| 205 | my @return; | ||
| 206 | map { s/^/$quotechar/g } @body; | ||
| 207 | foreach my $line (@body) { | ||
| 208 | if ($columns < 0) { push @return, $line; next } | ||
| 209 | while ($line =~ /^.{$columns,}$/) { # If the line has >$max chars | ||
| 210 | my ($First, $Second) = $line =~ /^(.{$columns})(.*)$/; | ||
| 211 | if ($First =~ /^(.*)\s+(\S+)$/) { | ||
| 212 | $First = $1; $Second = join('', $2, $Second); | ||
| 213 | } | ||
| 214 | if ($First =~ /^($QUOTECHARS)/) { # Add quote chars to the second | ||
| 215 | $Second = "$1$Second"; # half if this would be useful. | ||
| 216 | } | ||
| 217 | push (@return, $First); # Process the lines | ||
| 218 | $line = $Second; | ||
| 219 | } | ||
| 220 | push (@return, $line); | ||
| 221 | } | ||
| 222 | @return; | ||
| 223 | } | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | ############################################################################### | ||
| 230 | ### Internal Functions ######################################################## | ||
| 231 | ############################################################################### | ||
| 232 | |||
| 233 | ### _respstring ( ARTICLE ) | ||
| 234 | # Generates a basic "%s writes:" string. Not much to write home about. | ||
| 235 | sub _respstring { | ||
| 236 | my ($article) = @_; | ||
| 237 | sprintf("%s writes:", $article->header('from') | ||
| 238 | || $article->header('reply-to') || "(unknown)"); | ||
| 239 | } | ||
| 240 | |||
| 241 | ### _wrap ($columns, $quotechar, $wraptype, @body) | ||
| 242 | # Wraps body at columns using quotechar as a prefix of each line. Uses | ||
| 243 | # Text::Wrap in a non-destructive way. See the Text::Wrap manpage for | ||
| 244 | # more information ('overflow' and 'wrap' are the two interesting ones) | ||
| 245 | |||
| 246 | # sub _wrap { | ||
| 247 | # my ($columns, $quotechar, $wraptype, @body) = @_; | ||
| 248 | # my $origcols = $Text::Wrap::columns; $Text::Wrap::columns = $columns; | ||
| 249 | # my $orighuge = $Text::Wrap::huge; $Text::Wrap::huge = $wraptype; | ||
| 250 | # @body = Text::Wrap::wrap($quotechar, $quotechar, @body); | ||
| 251 | # $Text::Wrap::columns = $origcols; $Text::Wrap::huge = $orighuge; | ||
| 252 | # @body; | ||
| 253 | # } | ||
| 254 | |||
| 255 | =head1 REQUIREMENTS | ||
| 256 | |||
| 257 | B<News::Article>, B<Exporter>, and of course Perl (probably a fairly | ||
| 258 | modern version at that). | ||
| 259 | |||
| 260 | =head1 SEE ALSO | ||
| 261 | |||
| 262 | B<News::Article>, B<News::Web> | ||
| 263 | |||
| 264 | =head1 NOTES | ||
| 265 | |||
| 266 | This was originally written to work with my News::Web project (which I | ||
| 267 | plan to release), and much of the code was from old News::Verimod (which I | ||
| 268 | may not release). It's now part of my newslib project (which *is* | ||
| 269 | released: http://www.killfile.org/~tskirvin/software/newslib/). It should | ||
| 270 | be fairly general-purpose, but we'll see. | ||
| 271 | |||
| 272 | =head1 TODO | ||
| 273 | |||
| 274 | It would be nice if quotewrap() were to reformat paragraphs into something | ||
| 275 | reasonable when they're quoted; we'll see if it's something worth doing. | ||
| 276 | |||
| 277 | Get rid of the dependency on Text::Wrap, since we're not actually using it | ||
| 278 | except in an internal function that doesn't get called. | ||
| 279 | |||
| 280 | =head1 AUTHOR | ||
| 281 | |||
| 282 | Tim Skirvin <tskirvin@killfile.org> | ||
| 283 | |||
| 284 | =head1 COPYRIGHT | ||
| 285 | |||
| 286 | Copyright 2003 by Tim Skirvin <tskirvin@killfile.org>. This code may be | ||
| 287 | distributed under the same terms as Perl itself. | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | 1; | ||
| 292 | |||
| 293 | ############################################################################### | ||
| 294 | ### Version History ########################################################### | ||
| 295 | ############################################################################### | ||
| 296 | |||
| 297 | # v0.5a Fri Oct 10 09:55:42 CDT 2003 | ||
| 298 | ### First commented version. | ||
| 299 | # v0.5 Thu Apr 22 12:57:16 CDT 2004 | ||
| 300 | ### Just internal layout/documentation changes. |