| 1 | $VERSION = 1.0; | ||
| 2 | package Net::NNTP::Auth; | ||
| 3 | our $VERSION = "1.0"; | ||
| 4 | |||
| 5 | # -*- Perl -*- Thu Apr 22 11:01:27 CDT 2004 | ||
| 6 | ############################################################################### | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org> | ||
| 8 | # Copyright 2000-2004, Tim Skirvin. Redistribution terms are below. | ||
| 9 | ############################################################################### | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Net::NNTP::Auth - a standard NNTP authentication method | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Net::NNTP::Auth; | ||
| 18 | my ($nntpuser, $nntppass) = Net::NNTP::Auth->nntpauth($server->server); | ||
| 19 | |||
| 20 | use Net::NNTP; | ||
| 21 | my $nntp = new Net::NNTP; | ||
| 22 | $nntp->authinfo($nntpuser, $nntppass); | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | NNTP authentication is a standard feature of most news servers nowadays, | ||
| 27 | though many news readers have yet to catch up. When writing your own news | ||
| 28 | reader, though, you have to keep track of this data separately, unless you | ||
| 29 | have a standardized place to store the information - like this module. | ||
| 30 | |||
| 31 | This module offers a few functions which parse the local ~/.nntpauth file | ||
| 32 | and return the appropriate data. This file contains lines that look like | ||
| 33 | this: | ||
| 34 | |||
| 35 | news.server.invalid username password | ||
| 36 | news2.server2.invalid username2 password4 | ||
| 37 | |||
| 38 | That's it. The functions are called by offering forth a C<SERVER>, and | ||
| 39 | the first user/password pair in the file is returned. These values can | ||
| 40 | then be passed into Net::NNTP's B<nntpauth()> function. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | |||
| 45 | use strict; | ||
| 46 | use Exporter; | ||
| 47 | use vars qw( $NNTPAUTH $DEBUG $VERSION ); | ||
| 48 | |||
| 49 | $NNTPAUTH = "$ENV{'HOME'}/.nntpauth"; | ||
| 50 | $DEBUG = 0; | ||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item nntpauth ( SERVER ) | ||
| 57 | |||
| 58 | Parses ~/.nntpauth for the given C<SERVER>. Returns two items - the user | ||
| 59 | name and the password. If one or both of these is not found, then the | ||
| 60 | appropriate value is set to 'undef'. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub nntpauth { | ||
| 65 | my ($class, $server) = @_; return (undef, undef) unless $server; | ||
| 66 | warn "Trying to get auth information for $server\n" if $DEBUG; | ||
| 67 | return (undef, undef) unless (-r $NNTPAUTH); | ||
| 68 | my ($nntpuser, $nntppass); | ||
| 69 | open (CONFIG, "<$NNTPAUTH") | ||
| 70 | or warn "Couldn't open $NNTPAUTH: $@" && return undef; | ||
| 71 | while (<CONFIG>) { | ||
| 72 | chomp; | ||
| 73 | if (/^\s*(\S+)\s+(\S+)\s+(\S+)\s*$/) { | ||
| 74 | my $serv = $1; my $user = $2; my $pass = $3; | ||
| 75 | $nntpuser = $user if ($serv =~ /^$server$/i); | ||
| 76 | $nntppass = $pass if ($serv =~ /^$server$/i); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | close (CONFIG); | ||
| 80 | ($nntpuser || undef, $nntppass || undef); | ||
| 81 | } | ||
| 82 | |||
| 83 | =item nntpuser ( SERVER ) | ||
| 84 | |||
| 85 | =item nntppass ( SERVER ) | ||
| 86 | |||
| 87 | Invokes B<nntpauth()>, and returns the appropriate item from the list - | ||
| 88 | the user name for B<nntpuser()>, or the password for B<nntppass()>. | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub nntpuser { @{nntpauth(@_)}[0] } | ||
| 95 | sub nntppass { @{nntpauth(@_)}[1] } | ||
| 96 | |||
| 97 | =head1 NOTES | ||
| 98 | |||
| 99 | Please note that this is an extremely small module. The only real purpose | ||
| 100 | is to standardize on the ~/.nntpauth format that I've been using for a | ||
| 101 | long time. It's convenient. Why not? | ||
| 102 | |||
| 103 | =head1 AUTHOR | ||
| 104 | |||
| 105 | Written by Tim Skirvin <tskirvin@killfile.org> | ||
| 106 | |||
| 107 | =head1 LICENSE | ||
| 108 | |||
| 109 | This code may be redistributed under the same terms as Perl itself. | ||
| 110 | |||
| 111 | =head1 COPYRIGHT | ||
| 112 | |||
| 113 | Copyright 2000-2004 by Tim Skirvin <tskirvin@killfile.org>. This code may | ||
| 114 | be redistributed under the same terms as Perl itself. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | ############################################################################### | ||
| 121 | ### Version History ########################################################### | ||
| 122 | ############################################################################### | ||
| 123 | # v1.0 Thu Apr 22 11:01:44 CDT 2004 | ||
| 124 | ### Nothing's changed in four years; it's working; let's call this v1.0. |