| 1 | $VERSION = "0.21"; | ||
| 2 | package Net::NNTP::Functions; | ||
| 3 | our $VERSION = "0.21"; | ||
| 4 | # -*- Perl -*- # Fri Oct 10 10:17:44 CDT 2003 | ||
| 5 | ############################################################################# | ||
| 6 | # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2003, Tim | ||
| 7 | # Skirvin. Redistribution terms are below. | ||
| 8 | ############################################################################# | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::NNTP::Functions - code to implement NNTP-standard functions | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Net::NNTP::Functions; | ||
| 17 | |||
| 18 | my ($first1, $last1) = messagespec( "4-12" ); # Returns (4, 12) | ||
| 19 | my ($first2, $last2) = messagespec( 4 ); # Returns (4, undef) | ||
| 20 | my ($first3, $last3) = messagespec( [4, 12] ); # Returns (4, 12) | ||
| 21 | my ($first4, $last4) = messagespec( "4-3" ); # Returns (4, -1) | ||
| 22 | |||
| 23 | my $match = wildmat( 'rec.*', $string ); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | The NNTP specification, as described by Net::NNTP, defines two | ||
| 28 | speficiations: C<MESSAGE-SPEC>, for defining a range of messages, and | ||
| 29 | C<PATTERN>, for pattern-matching over NNTP. These functions attempt to | ||
| 30 | implement this behaviour. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | ############################################################################### | ||
| 35 | ### main() #################################################################### | ||
| 36 | ############################################################################### | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use Exporter; | ||
| 40 | |||
| 41 | use vars qw( @EXPORT @EXPORT_OK @ISA ); | ||
| 42 | @ISA = "Exporter"; | ||
| 43 | @EXPORT = qw( messagespec wildmat ); | ||
| 44 | |||
| 45 | ############################################################################### | ||
| 46 | ### Methods ################################################################### | ||
| 47 | ############################################################################### | ||
| 48 | |||
| 49 | =head2 Functions | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item messagespec ( ARRAYREF | MESSAGEID | MESSAGENUMBER ) | ||
| 54 | |||
| 55 | Returns an array (or array reference) of two items (FIRST and LAST for | ||
| 56 | reference), based on the passed item. If we get an C<ARRAYREF>, it's | ||
| 57 | assumed to be list of two items FIRST and LAST; if it's a C<MESSAGEID> or | ||
| 58 | C<MESSAGENUMBER> then FIRST is the value, and LAST is 'undef'; | ||
| 59 | |||
| 60 | If the LAST item is less than FIRST, then we set LAST to '-1'. This | ||
| 61 | allows later functions to (correctly) interpret this for a list of all | ||
| 62 | items after FIRST. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub messagespec { | ||
| 67 | my ($spec) = @_; | ||
| 68 | my ($first, $last); | ||
| 69 | |||
| 70 | if (ref $spec) { # List of two numbers | ||
| 71 | ($first, $last) = @{$spec}; | ||
| 72 | $last = -1 if ($last < $first); | ||
| 73 | } elsif ($spec =~ m/^\s*(\d+)\s*-?\s*(\d+)\s*$/) { # Two numbers | ||
| 74 | $first = $1; $last = $2; | ||
| 75 | $last = -1 if ($last < $first); | ||
| 76 | } else { # Message-ID or a single number | ||
| 77 | $first = $spec; $last = undef; | ||
| 78 | } | ||
| 79 | wantarray ? ($first, $last) : [ $first, $last ]; | ||
| 80 | } | ||
| 81 | |||
| 82 | =item wildmat ( EXPRESSION, STRING ) | ||
| 83 | |||
| 84 | Implements the WILDMAT format, as described in the Net::NNTP page (which I | ||
| 85 | won't repeat here). | ||
| 86 | |||
| 87 | Doesn't currently implement the entire functionality; all that currently | ||
| 88 | works is the anchoring, '*', and '?'. Ranges of characters ('[]') and | ||
| 89 | invertings ('^') don't work. (Correction: they might work, I haven't | ||
| 90 | tried). | ||
| 91 | |||
| 92 | Taken from | ||
| 93 | http://cvs.trainedmonkey.com/viewcvs.cgi/colobus/colobus?rev=1.41 . | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub wildmat ($$) { | ||
| 98 | my ($expr, $string) = @_; | ||
| 99 | $expr =~ s/(?<!\\)\./\\./g; # Escape '.' | ||
| 100 | $expr =~ s/(?<!\\)\$/\\\$/g; # Escape '$' | ||
| 101 | $expr =~ s/(?<!\\)\?/./g; # '?' functionality | ||
| 102 | $expr =~ s/(?<!\\)\*/.*/g; # '*' functionality | ||
| 103 | return $string =~ /^$expr$/; | ||
| 104 | } | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | =head1 SEE ALSO | ||
| 109 | |||
| 110 | B<Net::NNTP>, specifically its manual page sections on WILDMAT and | ||
| 111 | MESSAGESPEC. | ||
| 112 | |||
| 113 | =head1 TODO | ||
| 114 | |||
| 115 | Write the rest of the functionality for wildmat(). | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | Tim Skirvin <tskirvin@killfile.org>. wildmat() in its current form was | ||
| 120 | written by Jim Winstead Jr. | ||
| 121 | |||
| 122 | =head1 COPYRIGHT | ||
| 123 | |||
| 124 | Copyright 2003 by Tim Skirvin <tskirvin@killfile.org>. This code may be | ||
| 125 | distributed under the same terms as Perl itself. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | ############################################################################### | ||
| 132 | ### Version History ########################################################### | ||
| 133 | ############################################################################### | ||
| 134 | # v0.2a Fri Oct 10 10:17:44 CDT 2003 | ||
| 135 | ### First commented version. | ||
| 136 | # v0.21 Thu Apr 22 11:42:13 CDT 2004 | ||
| 137 | ### No real changes; just version number change. |