2014-05-13 12:15:52 +02:00
|
|
|
# File: MessageHistory.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Keeps track of who has said what and when, as well as their
|
|
|
|
# nickserv accounts and alter-hostmasks.
|
|
|
|
#
|
|
|
|
# Used in conjunction with AntiFlood and Quotegrabs for kick/ban on
|
|
|
|
# flood/ban-evasion and grabbing quotes, respectively.
|
|
|
|
|
|
|
|
package PBot::MessageHistory;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
use Getopt::Long qw(GetOptionsFromString);
|
2014-05-13 12:15:52 +02:00
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
|
|
|
use Time::Duration;
|
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
use PBot::MessageHistory_SQLite;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{filename} = delete $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/message_history.sqlite3';
|
2014-05-13 12:15:52 +02:00
|
|
|
|
|
|
|
$self->{database} = PBot::MessageHistory_SQLite->new(pbot => $self->{pbot}, filename => $self->{filename});
|
|
|
|
$self->{database}->begin();
|
|
|
|
$self->{database}->devalidate_all_channels();
|
|
|
|
|
2014-05-16 02:48:46 +02:00
|
|
|
$self->{MSG_CHAT} = 0; # PRIVMSG, ACTION
|
|
|
|
$self->{MSG_JOIN} = 1; # JOIN
|
|
|
|
$self->{MSG_DEPARTURE} = 2; # PART, QUIT, KICK
|
|
|
|
$self->{MSG_NICKCHANGE} = 3; # CHANGED NICK
|
2014-05-13 12:15:52 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'messagehistory', 'max_messages', $conf{max_messages} // 32);
|
|
|
|
|
2014-07-11 14:57:18 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->recall_message(@_) }, "recall", 0);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->list_also_known_as(@_) }, "aka", 0);
|
2014-05-17 00:11:31 +02:00
|
|
|
|
|
|
|
$self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; });
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_message_account {
|
|
|
|
my ($self, $nick, $user, $host) = @_;
|
|
|
|
return $self->{database}->get_message_account($nick, $user, $host);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_message {
|
|
|
|
my ($self, $account, $mask, $channel, $text, $mode) = @_;
|
|
|
|
$self->{database}->add_message($account, $mask, $channel, { timestamp => scalar gettimeofday, msg => $text, mode => $mode });
|
|
|
|
}
|
|
|
|
|
2014-07-11 14:57:18 +02:00
|
|
|
sub list_also_known_as {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2014-07-13 09:06:04 +02:00
|
|
|
my $usage = "Usage: aka [-h] <nick>";
|
|
|
|
|
2014-07-11 14:57:18 +02:00
|
|
|
if(not length $arguments) {
|
2014-07-13 09:06:04 +02:00
|
|
|
return $usage;
|
2014-07-11 14:57:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 09:06:04 +02:00
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
my $show_hostmasks;
|
|
|
|
my ($ret, $args) = GetOptionsFromString($arguments,
|
|
|
|
'h' => \$show_hostmasks);
|
|
|
|
|
|
|
|
return "$getopt_error -- $usage" if defined $getopt_error;
|
|
|
|
return "Too many arguments -- $usage" if @$args > 1;
|
|
|
|
return "Missing argument -- $usage" if @$args != 1;
|
|
|
|
|
|
|
|
my @akas = $self->{database}->get_also_known_as(@$args[0]);
|
2014-07-11 14:57:18 +02:00
|
|
|
if(@akas) {
|
2014-08-11 09:33:05 +02:00
|
|
|
my $result = "@$args[0] also known as:\n";
|
2014-07-11 14:57:18 +02:00
|
|
|
|
|
|
|
my %uniq;
|
|
|
|
foreach my $aka (@akas) {
|
2014-07-13 09:06:04 +02:00
|
|
|
if (not $show_hostmasks) {
|
|
|
|
my ($nick) = $aka =~ /^([^!]+)!/;
|
|
|
|
$uniq{$nick} = $nick;
|
|
|
|
} else {
|
|
|
|
$uniq{$aka} = $aka;
|
|
|
|
}
|
2014-07-11 14:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $sep = "";
|
|
|
|
foreach my $aka (sort keys %uniq) {
|
2014-07-13 09:06:04 +02:00
|
|
|
next if $aka =~ /^Guest\d+(!.*)?$/;
|
2014-07-11 14:57:18 +02:00
|
|
|
$result .= "$sep$aka";
|
2014-07-13 09:06:04 +02:00
|
|
|
if ($show_hostmasks) {
|
|
|
|
$sep = ",\n";
|
|
|
|
} else {
|
|
|
|
$sep = ", ";
|
|
|
|
}
|
2014-07-11 14:57:18 +02:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
} else {
|
2014-07-13 09:06:04 +02:00
|
|
|
return "I don't know anybody named @$args[0].";
|
2014-07-11 14:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
sub recall_message {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
if(not defined $from) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
|
2014-05-13 12:15:52 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel <channel>] [-t,text,h,history <history>] [+ ...]';
|
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
if(not defined $arguments or not length $arguments) {
|
2014-05-14 23:23:59 +02:00
|
|
|
return $usage;
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = lc $arguments;
|
|
|
|
|
|
|
|
my @recalls = split /\s\+\s/, $arguments;
|
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
my $recall_text;
|
2014-05-13 12:15:52 +02:00
|
|
|
|
|
|
|
foreach my $recall (@recalls) {
|
2014-05-14 23:23:59 +02:00
|
|
|
my ($recall_nick, $recall_history, $recall_channel);
|
|
|
|
|
|
|
|
my ($ret, $args) = GetOptionsFromString($recall,
|
|
|
|
'channel|c=s' => \$recall_channel,
|
|
|
|
'text|t|history|h=s' => \$recall_history);
|
|
|
|
|
|
|
|
return "$getopt_error -- $usage" if defined $getopt_error;
|
|
|
|
|
|
|
|
my $channel_arg = 1 if defined $recall_channel;
|
|
|
|
my $history_arg = 1 if defined $recall_history;
|
2014-05-13 12:15:52 +02:00
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
$recall_nick = shift @$args;
|
|
|
|
$recall_history = shift @$args if not defined $recall_history;
|
|
|
|
$recall_channel = shift @$args if not defined $recall_channel;
|
2014-05-13 12:15:52 +02:00
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
# swap nick and channel if recall nick looks like channel and channel wasn't specified
|
|
|
|
if(not $channel_arg and $recall_nick =~ m/^#/) {
|
|
|
|
my $temp = $recall_nick;
|
|
|
|
$recall_nick = $recall_channel;
|
|
|
|
$recall_channel = $temp;
|
|
|
|
}
|
2014-05-13 12:15:52 +02:00
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
# swap history and channel if history looks like a channel and neither history or channel were specified
|
|
|
|
if(not $channel_arg and not $history_arg and $recall_history =~ m/^#/) {
|
|
|
|
my $temp = $recall_history;
|
|
|
|
$recall_history = $recall_channel;
|
|
|
|
$recall_channel = $temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
# skip recall command if recalling self without arguments
|
|
|
|
$recall_history = $nick eq $recall_nick ? 2 : 1 if defined $recall_nick and not defined $recall_history;
|
|
|
|
|
|
|
|
# set history to most recent message if not specified
|
|
|
|
$recall_history = '1' if not defined $recall_history;
|
|
|
|
|
|
|
|
# set channel to current channel if not specified
|
|
|
|
$recall_channel = $from if not defined $recall_channel;
|
|
|
|
|
|
|
|
my ($account, $found_nick);
|
|
|
|
|
|
|
|
if(defined $recall_nick) {
|
|
|
|
($account, $found_nick) = $self->{database}->find_message_account_by_nick($recall_nick);
|
|
|
|
|
|
|
|
if(not defined $account) {
|
|
|
|
return "I don't know anybody named $recall_nick.";
|
|
|
|
}
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $message;
|
|
|
|
|
|
|
|
if($recall_history =~ /^\d+$/) {
|
|
|
|
# integral history
|
2014-05-14 23:23:59 +02:00
|
|
|
if(defined $account) {
|
|
|
|
my $max_messages = $self->{database}->get_max_messages($account, $recall_channel);
|
|
|
|
if($recall_history < 1 || $recall_history > $max_messages) {
|
|
|
|
return "Please choose a history between 1 and $max_messages";
|
|
|
|
}
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$recall_history--;
|
2014-05-14 23:23:59 +02:00
|
|
|
$message = $self->{database}->recall_message_by_count($account, $recall_channel, $recall_history, 'recall');
|
2014-05-13 12:15:52 +02:00
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
if(not defined $message) {
|
|
|
|
return "No message found at index $recall_history in channel $recall_channel.";
|
|
|
|
}
|
2014-05-13 12:15:52 +02:00
|
|
|
} else {
|
|
|
|
# regex history
|
2014-05-14 23:23:59 +02:00
|
|
|
$message = $self->{database}->recall_message_by_text($account, $recall_channel, $recall_history, 'recall');
|
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
if(not defined $message) {
|
2014-05-14 23:23:59 +02:00
|
|
|
if(defined $account) {
|
|
|
|
return "No such message for nick $found_nick in channel $recall_channel containing text '$recall_history'";
|
|
|
|
} else {
|
|
|
|
return "No such message in channel $recall_channel containing text '$recall_history'";
|
|
|
|
}
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:23:59 +02:00
|
|
|
if(defined $message->{id}) {
|
|
|
|
my $hostmask = $self->{database}->find_most_recent_hostmask($message->{id});
|
|
|
|
($found_nick) = $hostmask =~ m/^([^!]+)/;
|
|
|
|
$recall_nick = $found_nick;
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("$nick ($from) recalled <$recall_nick/$recall_channel> $message->{msg}\n");
|
2014-05-13 12:15:52 +02:00
|
|
|
|
|
|
|
my $text = $message->{msg};
|
|
|
|
my $ago = ago(gettimeofday - $message->{timestamp});
|
|
|
|
|
|
|
|
if(not defined $recall_text) {
|
2014-10-14 04:32:22 +02:00
|
|
|
if($text =~ s/^\/me\s+// or $text =~ m/^KICKED /) {
|
2014-05-13 12:15:52 +02:00
|
|
|
$recall_text = "[$ago] * $found_nick $text";
|
|
|
|
} else {
|
|
|
|
$recall_text = "[$ago] <$found_nick> $text";
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-14 04:32:22 +02:00
|
|
|
if($text =~ s/^\/me\s+// or $text =~ m/^KICKED /) {
|
2014-05-13 12:15:52 +02:00
|
|
|
$recall_text .= " [$ago] * $found_nick $text";
|
|
|
|
} else {
|
|
|
|
$recall_text .= " [$ago] <$found_nick> $text";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $recall_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|