3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-13 23:49:31 +01:00
pbot/modules/cfaq.pl
2019-05-28 09:19:42 -07:00

82 lines
2.0 KiB
Perl
Executable File

#!/usr/bin/perl -w
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
my $match = 1;
my $matches = 0;
my $found = 0;
print "Usage: faq [match #] <search regex>\n" and exit 0 if not defined $ARGV[0];
my $query = join(".*?", @ARGV);
$query =~ s/\s+/.*?/g;
$query =~ s/\+/\\+/g;
$query =~ s/[^\.]\*/\\*/g;
$query =~ s/^\*/\\*/g;
$query =~ s/\[/\\[/g;
$query =~ s/\]/\\]/g;
if ($query =~ /^(\d+)\.\*\?/) {
$match = $1;
$query =~ s/^\d+\.\*\?//;
}
open(FILE, "< cfaq-questions.html") or print "Can't open cfaq-questions.html: $!" and exit 1;
my @contents = <FILE>;
close(FILE);
my ($heading, $question_full, $question_link, $question_number, $question_text, $result);
foreach my $line (@contents) {
if ($line =~ m/^<H4>(.*?)<\/H4>/) {
$heading = $1;
next;
}
if ($line =~ m/<p><a href="(.*?)" rel=subdocument>(.*?)<\/a>/) {
($question_link, $question_number) = ($1, $2);
if (defined $question_full) {
if ($question_full =~ m/$query/i) {
$matches++;
$found = 1;
if ($match == $matches) {
$question_text =~ s/\s+/ /g;
$result = $question_text;
}
}
}
$question_full = "$question_number $question_link ";
$question_text = "http://c-faq.com/$question_link - $heading, $question_number: ";
next;
}
if (defined $question_full) {
$line =~ s/[\n\r]/ /g;
$line =~ s/(<pre>|<\/pre>|<TT>|<\/TT>|<\/a>|<br>)//g;
$line =~ s/<a href=".*?">//g;
$line =~ s/&nbsp;/ /g;
$line =~ s/&amp;/&/g;
$line =~ s/&lt;/</g;
$line =~ s/&gt;/>/g;
$question_full .= $line;
$question_text .= $line;
}
}
if ($found == 1) {
print "But there are $matches results...\n" and exit if ($match > $matches);
print "$matches results, displaying #$match: " if ($matches > 1);
print "$result\n";
} else {
$query =~ s/\.\*\?/ /g;
print "No FAQs match $query\n";
}