3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 04:19:27 +01:00
pbot/Plugins/Quotegrabs.pm

356 lines
14 KiB
Perl
Raw Normal View History

# File: Quotegrabs.pm
# Author: pragma_
#
# Purpose: Allows users to "grab" quotes from message history and store them for later retrieval.
License project under MPL2 This patch adds the file LICENSE which is the verbatim copy of the Mozilla Public License Version 2.0 as retreived from https://www.mozilla.org/media/MPL/2.0/index.815ca599c9df.txt on 2017-03-05. This patch also places license headers for the MPL2 type A variant of the license header in the following files: PBot/AntiFlood.pm PBot/BanTracker.pm PBot/BlackList.pm PBot/BotAdminCommands.pm PBot/BotAdmins.pm PBot/ChanOpCommands.pm PBot/ChanOps.pm PBot/Channels.pm PBot/Commands.pm PBot/DualIndexHashObject.pm PBot/EventDispatcher.pm PBot/FactoidCommands.pm PBot/FactoidModuleLauncher.pm PBot/Factoids.pm PBot/HashObject.pm PBot/IRCHandlers.pm PBot/IgnoreList.pm PBot/IgnoreListCommands.pm PBot/Interpreter.pm PBot/LagChecker.pm PBot/Logger.pm PBot/MessageHistory.pm PBot/MessageHistory_SQLite.pm PBot/NickList.pm PBot/PBot.pm PBot/Plugins.pm PBot/Plugins/AntiAway.pm PBot/Plugins/AntiKickAutoRejoin.pm PBot/Plugins/AntiRepeat.pm PBot/Plugins/AntiTwitter.pm PBot/Plugins/AutoRejoin.pm PBot/Plugins/Counter.pm PBot/Plugins/Quotegrabs.pm PBot/Plugins/Quotegrabs/Quotegrabs_Hashtable.pm PBot/Plugins/Quotegrabs/Quotegrabs_SQLite.pm PBot/Plugins/UrlTitles.pm PBot/Plugins/_Example.pm PBot/Refresher.pm PBot/Registerable.pm PBot/Registry.pm PBot/RegistryCommands.pm PBot/SQLiteLogger.pm PBot/SQLiteLoggerLayer.pm PBot/SelectHandler.pm PBot/StdinReader.pm PBot/Timer.pm PBot/Utils/ParseDate.pm PBot/VERSION.pm build/update-version.pl modules/acronym.pl modules/ago.pl modules/c11std.pl modules/c2english.pl modules/c2english/CGrammar.pm modules/c2english/c2eng.pl modules/c99std.pl modules/cdecl.pl modules/cfaq.pl modules/cjeopardy/IRCColors.pm modules/cjeopardy/QStatskeeper.pm modules/cjeopardy/Scorekeeper.pm modules/cjeopardy/cjeopardy.pl modules/cjeopardy/cjeopardy_answer.pl modules/cjeopardy/cjeopardy_filter.pl modules/cjeopardy/cjeopardy_hint.pl modules/cjeopardy/cjeopardy_qstats.pl modules/cjeopardy/cjeopardy_scores.pl modules/cjeopardy/cjeopardy_show.pl modules/codepad.pl modules/compiler_block.pl modules/compiler_client.pl modules/compiler_vm/Diff.pm modules/compiler_vm/cc modules/compiler_vm/compiler_client.pl modules/compiler_vm/compiler_server.pl modules/compiler_vm/compiler_server_vbox_win32.pl modules/compiler_vm/compiler_server_watchdog.pl modules/compiler_vm/compiler_vm_client.pl modules/compiler_vm/compiler_vm_server.pl modules/compiler_vm/compiler_watchdog.pl modules/compiler_vm/languages/_c_base.pm modules/compiler_vm/languages/_default.pm modules/compiler_vm/languages/bash.pm modules/compiler_vm/languages/bc.pm modules/compiler_vm/languages/bf.pm modules/compiler_vm/languages/c11.pm modules/compiler_vm/languages/c89.pm modules/compiler_vm/languages/c99.pm modules/compiler_vm/languages/clang.pm modules/compiler_vm/languages/clang11.pm modules/compiler_vm/languages/clang89.pm modules/compiler_vm/languages/clang99.pm modules/compiler_vm/languages/clangpp.pm modules/compiler_vm/languages/clisp.pm modules/compiler_vm/languages/cpp.pm modules/compiler_vm/languages/freebasic.pm modules/compiler_vm/languages/go.pm modules/compiler_vm/languages/haskell.pm modules/compiler_vm/languages/java.pm modules/compiler_vm/languages/javascript.pm modules/compiler_vm/languages/ksh.pm modules/compiler_vm/languages/lua.pm modules/compiler_vm/languages/perl.pm modules/compiler_vm/languages/python.pm modules/compiler_vm/languages/python3.pm modules/compiler_vm/languages/qbasic.pm modules/compiler_vm/languages/scheme.pm modules/compiler_vm/languages/server/_c_base.pm modules/compiler_vm/languages/server/_default.pm modules/compiler_vm/languages/server/c11.pm modules/compiler_vm/languages/server/c89.pm modules/compiler_vm/languages/server/c99.pm modules/compiler_vm/languages/server/clang.pm modules/compiler_vm/languages/server/clang11.pm modules/compiler_vm/languages/server/clang89.pm modules/compiler_vm/languages/server/clang99.pm modules/compiler_vm/languages/server/cpp.pm modules/compiler_vm/languages/server/freebasic.pm modules/compiler_vm/languages/server/haskell.pm modules/compiler_vm/languages/server/java.pm modules/compiler_vm/languages/server/qbasic.pm modules/compiler_vm/languages/server/tendra.pm modules/compiler_vm/languages/sh.pm modules/compiler_vm/languages/tendra.pm modules/compliment modules/cstd.pl modules/define.pl modules/dice_roll.pl modules/excuse.sh modules/expand_macros.pl modules/fnord.pl modules/funnyish_quote.pl modules/g.pl modules/gdefine.pl modules/gen_cfacts.pl modules/gencstd.pl modules/get_title.pl modules/getcfact.pl modules/google.pl modules/gspy.pl modules/gtop10.pl modules/gtop15.pl modules/headlines.pl modules/horoscope modules/horrorscope modules/ideone.pl modules/insult.pl modules/love_quote.pl modules/man.pl modules/map.pl modules/math.pl modules/prototype.pl modules/qalc.pl modules/random_quote.pl modules/seen.pl modules/urban modules/weather.pl modules/wikipedia.pl pbot.pl pbot.sh It is highly recommended that this list of files is reviewed to ensure that all files are the copyright of the sole maintainer of the repository. If any files with license headers contain the intellectual property of anyone else, it is recommended that a request is made to revise this patch or that the explicit permission of the co-author is gained to allow for the license of the work to be changed. I (Tomasz Kramkowski), the contributor, take no responsibility for any legal action taken against the maintainer of this repository for incorrectly claiming copyright to any work not owned by the maintainer of this repository.
2017-03-05 22:33:31 +01:00
# 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/.
package Plugins::Quotegrabs;
2020-02-15 23:38:32 +01:00
2020-02-09 04:48:05 +01:00
use parent 'Plugins::Plugin';
2020-02-09 04:48:05 +01:00
use warnings; use strict;
2019-07-11 03:40:53 +02:00
use feature 'unicode_strings';
use HTML::Entities;
use Time::Duration;
use Time::HiRes qw(gettimeofday);
use Getopt::Long qw(GetOptionsFromArray);
2020-02-15 23:38:32 +01:00
use Plugins::Quotegrabs::Quotegrabs_SQLite; # use SQLite backend for quotegrabs database
#use Plugins::Quotegrabs::Quotegrabs_Hashtable; # use Perl hashtable backend for quotegrabs database
use PBot::Utils::ValidateString;
use POSIX qw(strftime);
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
$self->{filename} = $conf{quotegrabs_file} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/quotegrabs.sqlite3';
$self->{database} = Plugins::Quotegrabs::Quotegrabs_SQLite->new(pbot => $self->{pbot}, filename => $self->{filename});
2020-02-15 23:38:32 +01:00
#$self->{database} = Plugins::Quotegrabs::Quotegrabs_Hashtable->new(pbot => $self->{pbot}, filename => $self->{filename});
$self->{database}->begin();
2020-02-15 23:38:32 +01:00
$self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; });
2020-02-15 23:38:32 +01:00
$self->{pbot}->{commands}->register(sub { $self->grab_quotegrab(@_) }, 'grab', 0);
$self->{pbot}->{commands}->register(sub { $self->show_quotegrab(@_) }, 'getq', 0);
$self->{pbot}->{commands}->register(sub { $self->delete_quotegrab(@_) }, 'delq', 0);
$self->{pbot}->{commands}->register(sub { $self->show_random_quotegrab(@_) }, 'rq', 0);
}
sub unload {
2020-02-15 23:38:32 +01:00
my ($self) = @_;
$self->{pbot}->{commands}->unregister('grab');
$self->{pbot}->{commands}->unregister('getq');
$self->{pbot}->{commands}->unregister('delq');
$self->{pbot}->{commands}->unregister('rq');
}
sub uniq { my %seen; grep !$seen{$_}++, @_ }
2019-06-26 18:34:19 +02:00
sub export_quotegrabs {
2020-02-15 23:38:32 +01:00
my $self = shift;
$self->{export_path} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/quotegrabs.html';
my $quotegrabs = $self->{database}->get_all_quotegrabs();
my $text;
my $table_id = 1;
my $had_table = 0;
open FILE, "> $self->{export_path}" or return "Could not open export path.";
my $time = localtime;
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
print FILE "<html>\n<head><link href=\"css/blue.css\" rel=\"stylesheet\" type=\"text/css\">\n";
print FILE '<script type="text/javascript" src="js/jquery-latest.js"></script>' . "\n";
print FILE '<script type="text/javascript" src="js/jquery.tablesorter.js"></script>' . "\n";
print FILE '<script type="text/javascript" src="js/picnet.table.filter.min.js"></script>' . "\n";
print FILE "</head>\n<body><i>Generated at $time</i><hr><h2>$botnick\'s Quotegrabs</h2>\n";
my $i = 0;
my $last_channel = "";
foreach my $quotegrab (sort { $$a{channel} cmp $$b{channel} or $$a{nick} cmp $$b{nick} } @$quotegrabs) {
if (not $quotegrab->{channel} =~ /^$last_channel$/i) {
print FILE "<a href='#" . encode_entities($quotegrab->{channel}) . "'>" . encode_entities($quotegrab->{channel}) . "</a><br>\n";
$last_channel = $quotegrab->{channel};
}
}
2020-02-15 23:38:32 +01:00
$last_channel = "";
foreach my $quotegrab (sort { $$a{channel} cmp $$b{channel} or lc $$a{nick} cmp lc $$b{nick} } @$quotegrabs) {
if (not $quotegrab->{channel} =~ /^$last_channel$/i) {
print FILE "</tbody>\n</table>\n" if $had_table;
print FILE "<a name='" . encode_entities($quotegrab->{channel}) . "'></a>\n";
print FILE "<hr><h3>" . encode_entities($quotegrab->{channel}) . "</h3><hr>\n";
print FILE "<table border=\"0\" id=\"table$table_id\" class=\"tablesorter\">\n";
print FILE "<thead>\n<tr>\n";
print FILE "<th>id&nbsp;&nbsp;&nbsp;&nbsp;</th>\n";
print FILE "<th>author(s)</th>\n";
print FILE "<th>quote</th>\n";
print FILE "<th>date</th>\n";
print FILE "<th>grabbed by</th>\n";
print FILE "</tr>\n</thead>\n<tbody>\n";
$had_table = 1;
$table_id++;
}
$last_channel = $quotegrab->{channel};
$i++;
if ($i % 2) { print FILE "<tr bgcolor=\"#dddddd\">\n"; }
else { print FILE "<tr>\n"; }
print FILE "<td>" . ($quotegrab->{id}) . "</td>";
my @nicks = split /\+/, $quotegrab->{nick};
$text = join ', ', uniq(@nicks);
print FILE "<td>" . encode_entities($text) . "</td>";
my $nick;
$text = $quotegrab->{text};
if ($text =~ s/^\/me\s+//) { $nick = "* $nicks[0]"; }
else { $nick = "<$nicks[0]>"; }
$text = "<td><b>" . encode_entities($nick) . "</b> " . encode_entities($text) . "</td>\n";
print FILE $text;
print FILE "<td>" . encode_entities(strftime "%Y/%m/%d %a %H:%M:%S", localtime $quotegrab->{timestamp}) . "</td>\n";
print FILE "<td>" . encode_entities($quotegrab->{grabbed_by}) . "</td>\n";
print FILE "</tr>\n";
}
2020-02-15 23:38:32 +01:00
print FILE "</tbody>\n</table>\n" if $had_table;
print FILE "<script type='text/javascript'>\n";
$table_id--;
print FILE '$(document).ready(function() {' . "\n";
while ($table_id > 0) {
print FILE '$("#table' . $table_id . '").tablesorter();' . "\n";
print FILE '$("#table' . $table_id . '").tableFilter();' . "\n";
$table_id--;
}
print FILE "});\n";
print FILE "</script>\n";
print FILE "</body>\n</html>\n";
close(FILE);
return "$i quotegrabs exported.";
}
2020-02-15 23:38:32 +01:00
sub grab_quotegrab {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
2020-02-15 23:38:32 +01:00
if (not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
2020-02-15 23:38:32 +01:00
if (not defined $arguments or not length $arguments) {
return
"Usage: grab <nick> [history [channel]] [+ <nick> [history [channel]] ...] -- where [history] is an optional regex argument; e.g., to grab a message containing 'pizza', use `grab nick pizza`; you can chain grabs with + to grab multiple messages";
}
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
$arguments = lc $arguments;
2020-02-15 23:38:32 +01:00
my @grabs = split /\s\+\s/, $arguments;
2020-02-15 23:38:32 +01:00
my ($grab_nick, $grab_history, $channel, $grab_nicks, $grab_text);
2020-02-15 23:38:32 +01:00
foreach my $grab (@grabs) {
($grab_nick, $grab_history, $channel) = $self->{pbot}->{interpreter}->split_line($grab, strip_quotes => 1);
2020-02-15 23:38:32 +01:00
$grab_history = $nick eq $grab_nick ? 2 : 1 if not defined $grab_history; # skip grab command if grabbing self without arguments
$channel = $from if not defined $channel;
2020-02-15 23:38:32 +01:00
if (not $channel =~ m/^#/) {
return "'$channel' is not a valid channel; usage: grab <nick> [[history] channel] (you must specify a history parameter before the channel parameter)";
}
2020-02-15 23:38:32 +01:00
my ($account, $found_nick) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($grab_nick);
2020-02-15 23:38:32 +01:00
if (not defined $account) { return "I don't know anybody named $grab_nick"; }
2020-02-15 23:38:32 +01:00
$found_nick =~ s/!.*$//;
2020-02-15 23:38:32 +01:00
$grab_nick = $found_nick; # convert nick to proper casing
2020-02-15 23:38:32 +01:00
my $message;
2020-02-15 23:38:32 +01:00
if ($grab_history =~ /^\d+$/) {
# integral history
my $max_messages = $self->{pbot}->{messagehistory}->{database}->get_max_messages($account, $channel);
if ($grab_history < 1 || $grab_history > $max_messages) { return "Please choose a history between 1 and $max_messages"; }
2020-02-15 23:38:32 +01:00
$grab_history--;
2020-02-15 23:38:32 +01:00
$message = $self->{pbot}->{messagehistory}->{database}->recall_message_by_count($account, $channel, $grab_history, 'grab');
} else {
# regex history
$message = $self->{pbot}->{messagehistory}->{database}->recall_message_by_text($account, $channel, $grab_history, 'grab');
2020-02-15 23:38:32 +01:00
if (not defined $message) { return "No such message for nick $grab_nick in channel $channel containing text '$grab_history'"; }
}
2020-02-15 23:38:32 +01:00
$self->{pbot}->{logger}->log("$nick ($from) grabbed <$grab_nick/$channel> $message->{msg}\n");
2020-02-15 23:38:32 +01:00
if (not defined $grab_nicks) { $grab_nicks = $grab_nick; }
else { $grab_nicks .= "+$grab_nick"; }
2020-02-15 23:38:32 +01:00
my $text = $message->{msg};
2020-02-15 23:38:32 +01:00
if (not defined $grab_text) { $grab_text = $text; }
else {
if ($text =~ s/^\/me\s+//) { $grab_text .= " * $grab_nick $text"; }
else { $grab_text .= " <$grab_nick> $text"; }
}
}
2020-02-15 23:38:32 +01:00
my $quotegrab = {};
$quotegrab->{nick} = $grab_nicks;
$quotegrab->{channel} = $channel;
$quotegrab->{timestamp} = gettimeofday;
$quotegrab->{grabbed_by} = "$nick!$user\@$host";
$quotegrab->{text} = validate_string($grab_text);
$quotegrab->{id} = undef;
2020-02-15 23:38:32 +01:00
$quotegrab->{id} = $self->{database}->add_quotegrab($quotegrab);
if (not defined $quotegrab->{id}) { return "Failed to grab quote."; }
2020-02-15 23:38:32 +01:00
$self->export_quotegrabs();
2020-02-15 23:38:32 +01:00
my $text = $quotegrab->{text};
($grab_nick) = split /\+/, $grab_nicks, 2;
if ($text =~ s/^(NICKCHANGE)\b/changed nick to/ or $text =~ s/^(KICKED|QUIT)\b/lc "$1"/e or $text =~ s/^(JOIN|PART)\b/lc "$1ed"/e) {
# fix ugly "[nick] quit Quit: Leaving." messages
$text =~ s/^(quit) (.*)/$1 ($2)/;
return "Quote grabbed: $quotegrab->{id}: $grab_nick $text";
} elsif ($text =~ s/^\/me\s+//) {
return "Quote grabbed: $quotegrab->{id}: * $grab_nick $text";
} else {
2020-02-15 23:38:32 +01:00
return "Quote grabbed: $quotegrab->{id}: <$grab_nick> $text";
}
}
sub delete_quotegrab {
2020-02-15 23:38:32 +01:00
my ($self, $from, $nick, $user, $host, $arguments) = @_;
2020-02-15 23:38:32 +01:00
my $quotegrab = $self->{database}->get_quotegrab($arguments);
2020-02-15 23:38:32 +01:00
if (not defined $quotegrab) { return "/msg $nick No quotegrab matching id $arguments found."; }
2020-02-15 23:38:32 +01:00
if (not $self->{pbot}->{users}->loggedin_admin($from, "$nick!$user\@$host") and $quotegrab->{grabbed_by} ne "$nick!$user\@$host") {
return "You are not the grabber of this quote.";
}
2020-02-15 23:38:32 +01:00
$self->{database}->delete_quotegrab($arguments);
$self->export_quotegrabs();
2020-02-15 23:38:32 +01:00
my $text = $quotegrab->{text};
2020-02-15 23:38:32 +01:00
my ($first_nick) = split /\+/, $quotegrab->{nick}, 2;
2020-02-15 23:38:32 +01:00
if ($text =~ s/^\/me\s+//) { return "Deleted $arguments: * $first_nick $text"; }
else { return "Deleted $arguments: <$first_nick> $text"; }
}
sub show_quotegrab {
2020-02-15 23:38:32 +01:00
my ($self, $from, $nick, $user, $host, $arguments) = @_;
2020-02-15 23:38:32 +01:00
my $quotegrab = $self->{database}->get_quotegrab($arguments);
2020-02-15 23:38:32 +01:00
if (not defined $quotegrab) { return "/msg $nick No quotegrab matching id $arguments found."; }
2020-02-15 23:38:32 +01:00
my $timestamp = $quotegrab->{timestamp};
my $ago = ago(gettimeofday - $timestamp);
my $text = $quotegrab->{text};
my ($first_nick) = split /\+/, $quotegrab->{nick}, 2;
2020-02-15 23:38:32 +01:00
if ($text =~ s/^\/me\s+//) {
return "$arguments: grabbed by $quotegrab->{grabbed_by} in $quotegrab->{channel} on " . localtime($timestamp) . " [$ago] * $first_nick $text";
} else {
return "$arguments: grabbed by $quotegrab->{grabbed_by} in $quotegrab->{channel} on " . localtime($timestamp) . " [$ago] <$first_nick> $text";
}
}
sub show_random_quotegrab {
2020-02-15 23:38:32 +01:00
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my @quotes = ();
my ($nick_search, $channel_search, $text_search);
2020-02-15 23:38:32 +01:00
if (not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
2020-02-15 23:38:32 +01:00
my $usage = 'Usage: rq [nick [channel [text]]] [-c <channel>] [-t <text>]';
2020-02-15 23:38:32 +01:00
if (defined $arguments) {
my $getopt_error;
local $SIG{__WARN__} = sub {
$getopt_error = shift;
chomp $getopt_error;
};
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, preserve_escapes => 1, strip_quotes => 1);
GetOptionsFromArray(
\@opt_args,
'channel|c=s' => \$channel_search,
'text|t=s' => \$text_search
);
2020-02-15 23:38:32 +01:00
return "$getopt_error -- $usage" if defined $getopt_error;
$nick_search = shift @opt_args;
$channel_search = shift @opt_args if not defined $channel_search;
$text_search = shift @opt_args if not defined $text_search;
2020-02-15 23:38:32 +01:00
if ($nick_search =~ m/^#/) {
my $tmp = $channel_search;
$channel_search = $nick_search;
$nick_search = $tmp;
}
if (not defined $channel_search) { $channel_search = $from; }
}
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
if (defined $channel_search and $channel_search !~ /^#/) {
if ($channel_search eq $nick) { $channel_search = undef; }
elsif ($channel_search =~ m/^\./) {
# do nothing
} else {
return "$channel_search is not a valid channel.";
}
}
2020-02-15 23:38:32 +01:00
my $quotegrab = $self->{database}->get_random_quotegrab($nick_search, $channel_search, $text_search);
if (not defined $quotegrab) {
my $result = "No quotes grabbed ";
2020-02-15 23:38:32 +01:00
if (defined $nick_search) { $result .= "for nick $nick_search "; }
2020-02-15 23:38:32 +01:00
if (defined $channel_search) { $result .= "in channel $channel_search "; }
if (defined $text_search) { $result .= "matching text '$text_search' "; }
return $result . "yet ($usage).";
}
my $text = $quotegrab->{text};
my ($first_nick) = split /\+/, $quotegrab->{nick}, 2;
if ($text =~ s/^\/me\s+//) {
return "$quotegrab->{id}: " . (($channel_search eq '.*' or $quotegrab->{channel} ne $from) ? "[$quotegrab->{channel}] " : "") . "* $first_nick $text";
} else {
return "$quotegrab->{id}: " . (($channel_search eq '.*' or $quotegrab->{channel} ne $from) ? "[$quotegrab->{channel}] " : "") . "<$first_nick> $text";
}
}
1;