| 1 | $VERSION = 0.6; | ||
| 2 | package Net::NNTP::Client; | ||
| 3 | our $VERSION = '0.6'; | ||
| 4 | |||
| 5 | # -*- Perl -*- | ||
| 6 | ############################################################################### | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org> | ||
| 8 | # Relies extensively on code from Net::NNTP, which was written and maintained | ||
| 9 | # by Graham Barr <gbarr@pobox.com>. Thanks. | ||
| 10 | # | ||
| 11 | # Copyright 2000, Tim Skirvin. Redistribution terms are below. | ||
| 12 | ############################################################################### | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Net::NNTP::Client - a module to simulate an entire NNTP client | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Net::NNTP::Client; | ||
| 21 | my $client = new Net::NNTP::Client('news.cso.uiuc.edu', | ||
| 22 | 'server' => 'news.cso.uiuc.edu', 'port' => 119, | ||
| 23 | 'user' => 'guest', 'pass' => 'guest' ); | ||
| 24 | |||
| 25 | See below for the list of functions. | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Net::NNTP is a module designed to provide a common interface to NNTP | ||
| 30 | servers. This module is an extension of this; it contains a Net::NNTP | ||
| 31 | reference, in addition to various cached information about the server and | ||
| 32 | enough information to reconnect again in the case of a hang-up. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | |||
| 37 | use strict; # Good programming is our friend | ||
| 38 | use Net::NNTP; | ||
| 39 | |||
| 40 | use vars qw( $DEBUG $TIMEOUT ); | ||
| 41 | $DEBUG = 1; | ||
| 42 | $TIMEOUT = 120; | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item new ( NAME [, OPTIONS] ) | ||
| 49 | |||
| 50 | Creates a new object and returns it. C<NAME> is mandatory, and is the | ||
| 51 | name of the connection, as well as (by default) the server name. C<OPTIONS> | ||
| 52 | is a list of key/value pairs; the useful keys are: | ||
| 53 | |||
| 54 | name, server, port, user, pass, debug, timeout | ||
| 55 | |||
| 56 | Each of these can be modified with its corresponding function. | ||
| 57 | |||
| 58 | =item name ( [NAME] ) | ||
| 59 | |||
| 60 | Returns the name of the object. If C<NAME> is passed, then it sets the | ||
| 61 | name to its value. | ||
| 62 | |||
| 63 | =item server ( [SERVER] ) | ||
| 64 | |||
| 65 | Returns the NNTP server name that the object will connect to. If | ||
| 66 | C<SERVER> is passed, then it sets the server name to its value. | ||
| 67 | |||
| 68 | =item port ( [PORT] ) | ||
| 69 | |||
| 70 | Returns the TCP port number that the object will communicate with. If | ||
| 71 | C<PORT> is passed, then the port number is set to its value. | ||
| 72 | |||
| 73 | =item user ( [USER] ) | ||
| 74 | |||
| 75 | Returns the user name that the object will authenticate itself to the NNTP | ||
| 76 | server with. If C<USER> is passed, then the user name is set to its value. | ||
| 77 | |||
| 78 | =item pass ( [PASS] ) | ||
| 79 | |||
| 80 | Returns the password that the object will authenticate itself to the NNTP | ||
| 81 | server with. If C<PASS> is passed, then the password is set to its value. | ||
| 82 | |||
| 83 | =item debug ( [DEBUG] ) | ||
| 84 | |||
| 85 | Returns true if we should print debugging information from the NNTP | ||
| 86 | connection. Can be set with C<DEBUG>. | ||
| 87 | |||
| 88 | =item timeout ( [TIMEOUT] ) | ||
| 89 | |||
| 90 | Returns the timeout value of the NNTP connection. Defaults to 120. | ||
| 91 | Can be set with C<TIMEOUT>. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub new { | ||
| 96 | my ($class, $name, %hash) = @_; | ||
| 97 | return undef unless $name; | ||
| 98 | my $object = { | ||
| 99 | 'NAME' => $name, | ||
| 100 | 'SERVER' => $hash{'server'} || $name, | ||
| 101 | 'PORT' => $hash{'port'} || 119, | ||
| 102 | 'USER' => $hash{'user'} || "", | ||
| 103 | 'PASS' => $hash{'pass'} || "", | ||
| 104 | 'DEBUG' => $hash{'debug'} || $DEBUG, | ||
| 105 | 'TIMEOUT' => $hash{'timeout'} || $TIMEOUT, | ||
| 106 | 'NNTP' => undef, | ||
| 107 | }; | ||
| 108 | bless $object, $class; | ||
| 109 | $object; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub name { my ($self, $arg) = @_; $arg ? $$self{NAME} = $arg : $$self{NAME}; } | ||
| 113 | sub server { | ||
| 114 | my ($self, $arg) = @_; | ||
| 115 | $arg ? $$self{SERVER} = $arg | ||
| 116 | : $$self{SERVER} ; | ||
| 117 | } | ||
| 118 | sub port { my ($self, $arg) = @_; $arg ? $$self{PORT} = $arg : $$self{PORT} } | ||
| 119 | sub user { my ($self, $arg) = @_; $arg ? $$self{USER} = $arg : $$self{USER} } | ||
| 120 | sub pass { my ($self, $arg) = @_; $arg ? $$self{PASS} = $arg : $$self{PASS} } | ||
| 121 | sub timeout { | ||
| 122 | my ($self, $arg) = @_; $arg ? $$self{TIMEOUT} = $arg : $$self{TIMEOUT}; | ||
| 123 | } | ||
| 124 | sub debug { | ||
| 125 | my ($self, $arg) = @_; defined $arg ? $$self{DEBUG} = $arg | ||
| 126 | : $$self{DEBUG} ; | ||
| 127 | } | ||
| 128 | |||
| 129 | =item connect () | ||
| 130 | |||
| 131 | =item reconnect () | ||
| 132 | |||
| 133 | =item nntp () | ||
| 134 | |||
| 135 | These three functions create the socket connection to an NNTP server. | ||
| 136 | The connection is made to the server found by C<server()> and the port | ||
| 137 | at C<port()>. If the connection is already open, then it returns that | ||
| 138 | connection; otherwise, it reconnects and continues. Either way, if | ||
| 139 | C<user()> and C<pass()> are set, then it authenticates as well. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub nntp { shift->connect(@_) } | ||
| 144 | sub reconnect { shift->connect(@_) } | ||
| 145 | sub connect { | ||
| 146 | my $self = shift; | ||
| 147 | my $connection = $$self{'NNTP'}; | ||
| 148 | # If we're already connected, return the connection. If we *look* | ||
| 149 | # like we're connected but aren't really, then close whatever it is | ||
| 150 | # that looks like it was connected and continue. | ||
| 151 | if ($connection) { | ||
| 152 | return $connection if $self->connected; | ||
| 153 | warn "Reconnecting to $$self{SERVER}\n" if $self->debug; | ||
| 154 | $connection->quit; | ||
| 155 | } | ||
| 156 | |||
| 157 | # (Re)connect to the server. | ||
| 158 | my $server = $self->server || return undef; | ||
| 159 | warn "Connecting to $server\n" if $self->debug; | ||
| 160 | my $NNTP = Net::NNTP2->new( $server, 'debug' => $self->debug, | ||
| 161 | 'Port' => $self->port, | ||
| 162 | 'timeout' => $self->timeout ) | ||
| 163 | || warn "Couldn't connect to $server: $!\n"; | ||
| 164 | |||
| 165 | # Authorize ourselves if possible | ||
| 166 | my $user = $self->user || ""; my $pass = $self->pass || ""; | ||
| 167 | $NNTP->authinfo($user, $pass) if ($user && $pass); | ||
| 168 | |||
| 169 | # Add to reconnect gracefully - NOT YET IMPLEMENTED | ||
| 170 | # $NNTP->group($$self{GROUP}) if ($$self{GROUP}); | ||
| 171 | # $NNTP->nntpstat($$self{POINTER}) if ($$self{POINTER}); | ||
| 172 | |||
| 173 | # Set the local NNTP variable, and return the server connection | ||
| 174 | $$self{'NNTP'} = $NNTP; | ||
| 175 | $NNTP; | ||
| 176 | } | ||
| 177 | |||
| 178 | =item disconnect () | ||
| 179 | |||
| 180 | Disconnect from the NNTP server. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub disconnect { $_[0]->{NNTP}->quit } | ||
| 185 | |||
| 186 | =item connected () | ||
| 187 | |||
| 188 | If the object is currently connected to the NNTP server, returns 1; | ||
| 189 | otherwise, returns 0. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub connected () { | ||
| 194 | my $NNTP = shift->{'NNTP'} || return 0; | ||
| 195 | $NNTP->date; # Make sure we can still talk to it | ||
| 196 | defined(fileno($NNTP)) ? 1 : 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | =item load ( ITEM [, ARGS] ) | ||
| 200 | |||
| 201 | Loads C<ITEM> from the NNTP server, which is one of C<active>, | ||
| 202 | C<active.times>, C<newsgroups>, or C<overview.fmt>. C<ARGS> is the list | ||
| 203 | of arguments to be passed into the appropriate NNTP call (see the | ||
| 204 | Net::NNTP manual pages for C<active()>, C<active_times()>, C<newsgroups()>, | ||
| 205 | and C<overview_fmt()> for details). These items are cached; if no | ||
| 206 | arguments are passed in C<ARGS>, then this value will be returned instead | ||
| 207 | of fetching new information from the server. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub load { | ||
| 212 | my ($self, $item, $arg1, @args) = @_; | ||
| 213 | warn "Loading $item from $$self{SERVER}\n" if $self->debug; | ||
| 214 | my $NNTP = $self->nntp || return undef; | ||
| 215 | |||
| 216 | if (lc $item eq '' || lc $item eq 'active') { | ||
| 217 | return $$self{'ACTIVE'} if (!$arg1 && $$self{'ACTIVE'}); | ||
| 218 | my $active = $NNTP->active($arg1) || return undef; | ||
| 219 | $$self{ACTIVE} = $active; | ||
| 220 | return $active; | ||
| 221 | } elsif (lc $item eq 'active.times') { | ||
| 222 | return $$self{'ACTIVE_TIMES'} if (!$arg1 && $$self{'ACTIVE_TIMES'}); | ||
| 223 | my $active_times = $NNTP->active_times() || return undef; | ||
| 224 | $$self{ACTIVE_TIMES} = $active_times; | ||
| 225 | return $active_times; | ||
| 226 | } elsif (lc $item eq 'newsgroups') { | ||
| 227 | return $$self{'NEWSGROUPS'} if (!$arg1 && $$self{'NEWSGROUPS'}); | ||
| 228 | my $newsgroups = $NNTP->newsgroups($arg1) || return undef; | ||
| 229 | $$self{NEWSGROUPS} = $newsgroups; | ||
| 230 | return $newsgroups; | ||
| 231 | } elsif (lc $item eq 'overview.fmt') { | ||
| 232 | return $$self{'OVERVIEW_FMT'} if (!$arg1 && $$self{'OVERVIEW_FMT'}); | ||
| 233 | my $overviewfmt = $NNTP->overview_fmt() || return undef; | ||
| 234 | $$self{'OVERVIEW_FMT'} = $overviewfmt; | ||
| 235 | return $overviewfmt; | ||
| 236 | } else { | ||
| 237 | return undef; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | =item Net::NNTP Methods | ||
| 242 | |||
| 243 | The following methods from Net::NNTP are directly implemented by this object. | ||
| 244 | The real difference is that they will all try to reconnect to the server | ||
| 245 | before running themselves; if this fail, then they return undef. Refer to | ||
| 246 | L<Net::NNTP> for details on how to use them. | ||
| 247 | |||
| 248 | article, head, body, nntpstat, group, ihave, last, date, postok, authinfo, | ||
| 249 | newgroups, newnews, next, post, slave, quit, distributions, subscriptions, | ||
| 250 | xgtitle, xhdr, xover, xpath, xpat, xrover, listgroup | ||
| 251 | |||
| 252 | Also, these functions have been re-implemented with C<load()>. | ||
| 253 | |||
| 254 | active, active_times, newsgroups, overview_fmt, list | ||
| 255 | |||
| 256 | And C<quit()> has been re-implemented with C<disconnect()>. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | # * = should keep track of $POINTER and $GROUP with these | ||
| 261 | |||
| 262 | sub article { my $nntp = shift->nntp || return undef; $nntp->article(@_) } #* | ||
| 263 | sub head { my $nntp = shift->nntp || return undef; $nntp->head(@_) } | ||
| 264 | sub body { my $nntp = shift->nntp || return undef; $nntp->body(@_) } | ||
| 265 | sub last { my $nntp = shift->nntp || return undef; $nntp->last(@_) } #* | ||
| 266 | sub nntpstat { my $nntp = shift->nntp || return undef; $nntp->nntpstat(@_) }#* | ||
| 267 | sub group { my $nntp = shift->nntp || return undef; $nntp->group(@_) } #* | ||
| 268 | sub ihave { my $nntp = shift->nntp || return undef; $nntp->ihave(@_) } | ||
| 269 | sub date { my $nntp = shift->nntp || return undef; $nntp->date(@_) } | ||
| 270 | sub postok { my $nntp = shift->nntp || return undef; $nntp->postok(@_) } | ||
| 271 | sub authinfo { my $nntp = shift->nntp || return undef; $nntp->authinfo(@_) } | ||
| 272 | sub newgroups { my $nntp = shift->nntp || return undef; $nntp->newgroups(@_) } | ||
| 273 | sub newnews { my $nntp = shift->nntp || return undef; $nntp->newnews(@_) } | ||
| 274 | sub next { my $nntp = shift->nntp || return undef; $nntp->next(@_) } #* | ||
| 275 | sub post { | ||
| 276 | warn "Posting: @_\n"; | ||
| 277 | my $nntp = shift->nntp || return undef; | ||
| 278 | $nntp->post(@_) | ||
| 279 | } | ||
| 280 | sub slave { my $nntp = shift->nntp || return undef; $nntp->slave(@_) } | ||
| 281 | sub distributions { # Didn't quite fit in 80 columns. | ||
| 282 | my $nntp = shift->nntp || return undef; | ||
| 283 | $nntp->distributions(@_) | ||
| 284 | } | ||
| 285 | sub subscriptions { # Again, didn't quite fit in 80 columns. | ||
| 286 | my $nntp = shift->nntp || return undef; $ | ||
| 287 | nntp->subscriptions(@_) | ||
| 288 | } | ||
| 289 | sub xgtitle { my $nntp = shift->nntp || return undef; $nntp->xgtitle(@_) } | ||
| 290 | sub xhdr { my $nntp = shift->nntp || return undef; $nntp->xhdr(@_) } | ||
| 291 | sub xover { my $nntp = shift->nntp || return undef; $nntp->xover(@_) } | ||
| 292 | sub xpath { my $nntp = shift->nntp || return undef; $nntp->xpath(@_) } | ||
| 293 | sub xpat { my $nntp = shift->nntp || return undef; $nntp->xpat(@_) } | ||
| 294 | sub xrover { my $nntp = shift->nntp || return undef; $nntp->xrover(@_) } | ||
| 295 | sub listgroup { my $nntp = shift->nntp || return undef; $nntp->listgroup(@_) } | ||
| 296 | |||
| 297 | sub active { shift->load('active', @_) } | ||
| 298 | sub active_times { shift->load('active.times', @_) } | ||
| 299 | sub list { shift->load(shift || 'active', @_) } | ||
| 300 | sub newsgroups { shift->load('newsgroups', @_) } | ||
| 301 | sub overview_fmt { shift->load('overview_fmt', @_) } | ||
| 302 | sub quit { shift->disconnect(@_) } | ||
| 303 | |||
| 304 | # Added to give better error messages | ||
| 305 | sub code { my $nntp = shift->nntp || return undef; $nntp->code(@_) } | ||
| 306 | sub message { my $nntp = shift->nntp || return undef; $nntp->message(@_) } | ||
| 307 | |||
| 308 | =head1 REQUIREMENTS | ||
| 309 | |||
| 310 | Requires C<Net::NNTP>. | ||
| 311 | |||
| 312 | =head1 NOTES | ||
| 313 | |||
| 314 | If you hadn't noticed, the real point of this is that this can be used | ||
| 315 | in the place of Net::NNTP in a lot of situations. It doesn't offer too | ||
| 316 | many advantages above it, though. Oh well. | ||
| 317 | |||
| 318 | =head1 SEE ALSO | ||
| 319 | |||
| 320 | L<Net::NNTP::Proxy>, L<newsproxy.pl>, L<Net::NNTP>, L<News::NNTPAuth> | ||
| 321 | |||
| 322 | =head1 TODO | ||
| 323 | |||
| 324 | The caching done by C<load()> isn't all that hot. I'm not sure of its | ||
| 325 | robustness for reconnections yet. | ||
| 326 | |||
| 327 | =head1 AUTHOR | ||
| 328 | |||
| 329 | Written by Tim Skirvin <tskirvin@killfile.org>. | ||
| 330 | |||
| 331 | =head1 COPYRIGHT | ||
| 332 | |||
| 333 | Copyright 2000 by Tim Skirvin <tskirvin@killfile.org>. This code may be | ||
| 334 | redistributed under the same terms as Perl itself. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | ### Net::NNTP2 | ||
| 339 | # This is pretty much just Net::NNTP, with a few changes made to make it | ||
| 340 | # compatible with Net::NNTP::Client. Nobody should know of this thing | ||
| 341 | # unless you're reading over the code directly. | ||
| 342 | |||
| 343 | package Net::NNTP2; | ||
| 344 | |||
| 345 | use strict; | ||
| 346 | use Net::NNTP; | ||
| 347 | |||
| 348 | use vars qw(@ISA); | ||
| 349 | @ISA = qw( Net::NNTP ); | ||
| 350 | |||
| 351 | # Get rid of the DESTROY function, because the default one is broken if | ||
| 352 | # multiple processes have the filehandle open (which we want to have | ||
| 353 | # happen.) This declaration causes a warning if we aren't making a new | ||
| 354 | # class, which is why we *are* making one. | ||
| 355 | sub DESTROY {} | ||
| 356 | |||
| 357 | 1; | ||
| 358 | |||
| 359 | ############################################################################### | ||
| 360 | ### Version History ########################################################### | ||
| 361 | ############################################################################### | ||
| 362 | # v0.5a Thu Nov 9 17:25:24 CST 2000 | ||
| 363 | ### First version ready for release. Generally does its job. The caching | ||
| 364 | ### work isn't the best, and I'm not sure about the robustness of the | ||
| 365 | ### reconnect()/isconnected() code. | ||
| 366 | # v0.6 Thu Apr 22 11:43:07 CDT 2004 | ||
| 367 | ### No real changes, just internal layout changes. |