| 1 | # purldoc.pl - Part of the kinder, gentler #Perl. | ||
| 2 | |||
| 3 | # Though he hates to admit it, this was written by the gent | ||
| 4 | # on EFNet #Perl known most often as Masque. Comments to | ||
| 5 | # masque@pound.perl.org. This code is covered under the same | ||
| 6 | # license as the rest of infobot. | ||
| 7 | |||
| 8 | # Eternal thanks to oznoid for writing the other bits, and | ||
| 9 | # for being a good friend to all Perldom. We're fortunate | ||
| 10 | # to have him. | ||
| 11 | |||
| 12 | # Please note that in this version, purldoc only searches the | ||
| 13 | # question _titles_. This is MUCH faster, and reduces the | ||
| 14 | # amount of work that the host machine has to do. This is | ||
| 15 | # the same way that perldoc -q does it, so don't complain | ||
| 16 | # _too_ loudly. | ||
| 17 | |||
| 18 | # KNOWN BUGS: Still sucks in many ways. | ||
| 19 | |||
| 20 | # removed all throttling code and replaced with returning | ||
| 21 | # \n-delimited clumps rather than direct msg or say. | ||
| 22 | |||
| 23 | # 1999-Dec-12 <lenzo@cs.cmu.edu> coerced to new module format | ||
| 24 | |||
| 25 | package Amethyst::Brain::Infobot::Module::Purldoc; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use vars qw(@ISA); | ||
| 29 | use Amethyst::Message; | ||
| 30 | use Amethyst::Brain::Infobot; | ||
| 31 | |||
| 32 | @ISA = qw(Amethyst::Brain::Infobot::Module); | ||
| 33 | |||
| 34 | my $any_bad; | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift ; | ||
| 38 | return undef if $any_bad; | ||
| 39 | |||
| 40 | my $self = $class->SUPER::new( | ||
| 41 | Name => 'Purldoc', | ||
| 42 | Regex => qr/^p[ue]rldoc (.*)$/i, | ||
| 43 | Usage => 'purldoc (.*)', | ||
| 44 | Description => "Get related FAQ questions", | ||
| 45 | @_ | ||
| 46 | ); | ||
| 47 | |||
| 48 | return bless $self, $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub action { | ||
| 52 | my ($self, $message, $what) = @_; | ||
| 53 | |||
| 54 | my @results; | ||
| 55 | |||
| 56 | my $output = $self->purldoc_lookup($what, \@results); | ||
| 57 | |||
| 58 | if (@results) { | ||
| 59 | my $reply = $self->reply_to($message, join(", ", @results)); | ||
| 60 | $reply->send; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | my $reply = $self->reply_to($message, $output); | ||
| 64 | $reply->send; | ||
| 65 | } | ||
| 66 | |||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | # I probably don't need to pass the array to the subroutine, but | ||
| 73 | # it looks more impressive when the subroutine is all pr0totyped, | ||
| 74 | # etc., and perhaps I can distract you, the noble reader, from | ||
| 75 | # noticing the other less impressive bits of this code by putting | ||
| 76 | # in overly complicated code. We pass the array because we're only | ||
| 77 | # using return values if the sub blows up. Lame? Yes. Stupid? | ||
| 78 | # Perhaps. Intentional? Sure! This is perl, it's supposed to | ||
| 79 | # be fun. ;) | ||
| 80 | |||
| 81 | sub purldoc_lookup ($\$\@) { | ||
| 82 | my $self = shift ; | ||
| 83 | |||
| 84 | my $regex = shift; | ||
| 85 | my $original_regex = $regex; | ||
| 86 | my $target_filename = 'pod/perlfaq.pod'; | ||
| 87 | my $results = shift; | ||
| 88 | |||
| 89 | # There is most likely a much more elegant way to do this search, however | ||
| 90 | # this works, and it's 2am, so you're welcome to comment all you like either | ||
| 91 | # to /dev/null or to masque@pound.perl.com. Patches welcome. :] | ||
| 92 | |||
| 93 | foreach (@INC) { | ||
| 94 | if (-e "$_/$target_filename") { | ||
| 95 | $target_filename = "$_/$target_filename"; | ||
| 96 | last; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | # We don't do -f. -f would be crazy-long to return. It'd be easy | ||
| 101 | # enough to do, but it should only reply via /msg if implemented. | ||
| 102 | # Hmm...perhaps it should also be usable as | ||
| 103 | # 'tell $who about purldoc -f $function', though that has the | ||
| 104 | # potential for abuse. Perhaps purl should respond '$who wants | ||
| 105 | # you to ask me about purldoc -f $function,' but that is really | ||
| 106 | # pretty lame (and likely to be ignored.) Ah well. Reserved for | ||
| 107 | # future use. | ||
| 108 | |||
| 109 | return "No -f for you! NEXT!" if $regex =~ /^\s*-t?f/i; | ||
| 110 | |||
| 111 | # Sanity check on $regex. We don't want people searching for 'I', etc. | ||
| 112 | # It was most tempting to add 'HTML' and 'CGI' to the first regex, but | ||
| 113 | # I overcame the temptation...for now. ;) | ||
| 114 | |||
| 115 | $regex =~ s/(?:^|\b|\s)(?:\-t?qt?|I|do|how|my|what|which|who|can)\b/ /gi; | ||
| 116 | |||
| 117 | # I'm not proud of using the fearsome '.*?' here, but that leading and | ||
| 118 | # trailing whitespace MUST GO! IT ALL MUST GO! WE'LL MAKE ANY DEAL! | ||
| 119 | # IT'S CRAAAAAAAAAAAAAAAAAAZY MASQUE'S USED REGEX EMPORIUM! COME ON | ||
| 120 | # DOWN! WE'LL CLUB A SEAL TO MAKE A BETTER DEAL! (Weird Al, UHF)++ | ||
| 121 | |||
| 122 | $regex =~ s/^\s*(.*?)\s*$/$1/; | ||
| 123 | |||
| 124 | # We're pretty picky about the regex. Currently there are no helpful | ||
| 125 | # two-letter strings in perlfaq (with the possible exception of 'do', | ||
| 126 | # which is being filtered for other reasons) so we require the length | ||
| 127 | # to be above that, and also we only want letters of the alphabet, | ||
| 128 | # thanks. | ||
| 129 | |||
| 130 | return "\'$original_regex\' isn't a good purldoc search string." | ||
| 131 | unless $regex =~ /^[A-Za-z ]+$/ and length $regex > 2; | ||
| 132 | |||
| 133 | open PURLDOC, "<$target_filename" | ||
| 134 | or return "Sorry, guys. I can't open perlfaq right now: $!"; | ||
| 135 | |||
| 136 | # ACHTUNG! THE FOLLOWING CODE IS WILDLY INEFFICIENT! HAVE A CAPS LOCKY DAY. | ||
| 137 | |||
| 138 | my $chapter; | ||
| 139 | my $versecount; | ||
| 140 | |||
| 141 | while (<PURLDOC>) { | ||
| 142 | if (/^=head1 Credits/) { | ||
| 143 | last; | ||
| 144 | } | ||
| 145 | if (/^=item L<(\w+\d)/) { | ||
| 146 | $chapter = $1 and $versecount = 0 | ||
| 147 | } | ||
| 148 | elsif (s/=item \* //) { | ||
| 149 | chomp; | ||
| 150 | $versecount++; | ||
| 151 | push(@$results, "$chapter, question $versecount: $_") | ||
| 152 | if /$regex/i; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | return "No matches for keyphrase '$regex' found." unless scalar @$results; | ||
| 157 | } | ||
| 158 | 1; | ||
| 159 | |||
| 160 | __END__ |