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

535 lines
18 KiB
Perl
Raw Normal View History

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/.
2016-02-14 03:39:12 +01:00
package Plugins::Counter;
2020-02-09 04:48:05 +01:00
use parent 'Plugins::Plugin';
2016-02-14 03:39:12 +01:00
2020-02-09 04:48:05 +01:00
use warnings; use strict;
use feature 'unicode_strings';
2016-02-14 03:39:12 +01:00
use feature 'switch';
no if $] >= 5.018, warnings => "experimental::smartmatch";
2016-02-14 03:39:12 +01:00
use DBI;
use Time::Duration qw/duration/;
use Time::HiRes qw/gettimeofday/;
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->cmd_counteradd(@_) }, 'counteradd', 0);
$self->{pbot}->{commands}->register(sub { $self->cmd_counterdel(@_) }, 'counterdel', 0);
$self->{pbot}->{commands}->register(sub { $self->cmd_counterreset(@_) }, 'counterreset', 0);
$self->{pbot}->{commands}->register(sub { $self->cmd_countershow(@_) }, 'countershow', 0);
$self->{pbot}->{commands}->register(sub { $self->cmd_counterlist(@_) }, 'counterlist', 0);
$self->{pbot}->{commands}->register(sub { $self->cmd_countertrigger(@_) }, 'countertrigger', 1);
2020-02-15 23:38:32 +01:00
$self->{pbot}->{capabilities}->add('admin', 'can-countertrigger', 1);
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public(@_) });
$self->{filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/counters.sqlite3';
$self->create_database;
2016-02-14 03:39:12 +01:00
}
sub unload {
2020-02-15 23:38:32 +01:00
my $self = shift;
$self->{pbot}->{commands}->unregister('counteradd');
$self->{pbot}->{commands}->unregister('counterdel');
$self->{pbot}->{commands}->unregister('counterreset');
$self->{pbot}->{commands}->unregister('countershow');
$self->{pbot}->{commands}->unregister('counterlist');
$self->{pbot}->{commands}->unregister('countertrigger');
$self->{pbot}->{capabilities}->remove('can-countertrigger');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.public');
2016-02-14 03:39:12 +01:00
}
sub create_database {
2020-02-15 23:38:32 +01:00
my $self = shift;
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
eval {
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1})
or die $DBI::errstr;
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
$self->{dbh}->do(<<SQL);
2016-02-14 03:39:12 +01:00
CREATE TABLE IF NOT EXISTS Counters (
channel TEXT,
name TEXT,
description TEXT,
2016-02-20 05:44:57 +01:00
timestamp NUMERIC,
created_on NUMERIC,
created_by TEXT,
counter NUMERIC
2016-02-14 03:39:12 +01:00
)
SQL
2020-02-15 23:38:32 +01:00
$self->{dbh}->do(<<SQL);
CREATE TABLE IF NOT EXISTS Triggers (
channel TEXT,
trigger TEXT,
2016-04-11 03:35:11 +02:00
target TEXT
)
2016-02-14 03:39:12 +01:00
SQL
2020-02-15 23:38:32 +01:00
$self->{dbh}->disconnect;
};
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
$self->{pbot}->{logger}->log("Counter create database failed: $@") if $@;
2016-02-14 03:39:12 +01:00
}
sub dbi_begin {
2020-02-15 23:38:32 +01:00
my ($self) = @_;
eval { $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1}) or die $DBI::errstr; };
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Error opening Counters database: $@");
return 0;
} else {
return 1;
}
2016-02-14 03:39:12 +01:00
}
sub dbi_end {
2020-02-15 23:38:32 +01:00
my ($self) = @_;
$self->{dbh}->disconnect;
2016-02-14 03:39:12 +01:00
}
sub add_counter {
2020-02-15 23:38:32 +01:00
my ($self, $owner, $channel, $name, $description) = @_;
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
my ($desc, $timestamp) = $self->get_counter($channel, $name);
if (defined $desc) { return 0; }
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Counters (channel, name, description, timestamp, created_on, created_by, counter) VALUES (?, ?, ?, ?, ?, ?, ?)');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $name);
$sth->bind_param(3, $description);
$sth->bind_param(4, scalar gettimeofday);
$sth->bind_param(5, scalar gettimeofday);
$sth->bind_param(6, $owner);
$sth->bind_param(7, 0);
$sth->execute();
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Add counter failed: $@");
return 0;
}
return 1;
2016-02-14 03:39:12 +01:00
}
sub reset_counter {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $name) = @_;
my ($description, $timestamp, $counter) = $self->get_counter($channel, $name);
if (not defined $description) { return (undef, undef); }
2020-02-15 23:38:32 +01:00
eval {
my $sth = $self->{dbh}->prepare('UPDATE Counters SET timestamp = ?, counter = ? WHERE channel = ? AND name = ?');
$sth->bind_param(1, scalar gettimeofday);
$sth->bind_param(2, ++$counter);
$sth->bind_param(3, lc $channel);
$sth->bind_param(4, lc $name);
$sth->execute();
};
if ($@) {
$self->{pbot}->{logger}->log("Reset counter failed: $@");
return (undef, undef);
}
return ($description, $timestamp);
2016-02-14 03:39:12 +01:00
}
sub delete_counter {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $name) = @_;
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
my ($description, $timestamp) = $self->get_counter($channel, $name);
if (not defined $description) { return 0; }
2020-02-15 23:38:32 +01:00
eval {
my $sth = $self->{dbh}->prepare('DELETE FROM Counters WHERE channel = ? AND name = ?');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $name);
$sth->execute();
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Delete counter failed: $@");
return 0;
}
return 1;
2016-02-14 03:39:12 +01:00
}
sub list_counters {
2020-02-15 23:38:32 +01:00
my ($self, $channel) = @_;
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
my $counters = eval {
my $sth = $self->{dbh}->prepare('SELECT name FROM Counters WHERE channel = ?');
$sth->bind_param(1, lc $channel);
$sth->execute();
return $sth->fetchall_arrayref();
};
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
if ($@) { $self->{pbot}->{logger}->log("List counters failed: $@"); }
return map { $_->[0] } @$counters;
2016-02-14 03:39:12 +01:00
}
sub get_counter {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $name) = @_;
my ($description, $time, $counter, $created_on, $created_by) = eval {
my $sth = $self->{dbh}->prepare('SELECT description, timestamp, counter, created_on, created_by FROM Counters WHERE channel = ? AND name = ?');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $name);
$sth->execute();
my $row = $sth->fetchrow_hashref();
return ($row->{description}, $row->{timestamp}, $row->{counter}, $row->{created_on}, $row->{created_by});
};
2016-02-14 03:39:12 +01:00
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Get counter failed: $@");
return undef;
}
return ($description, $time, $counter, $created_on, $created_by);
2016-02-14 03:39:12 +01:00
}
sub add_trigger {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $trigger, $target) = @_;
2020-02-15 23:38:32 +01:00
my $exists = $self->get_trigger($channel, $trigger);
if (defined $exists) { return 0; }
2020-02-15 23:38:32 +01:00
eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Triggers (channel, trigger, target) VALUES (?, ?, ?)');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $trigger);
$sth->bind_param(3, lc $target);
$sth->execute();
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Add trigger failed: $@");
return 0;
}
return 1;
}
sub delete_trigger {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $trigger) = @_;
2020-02-15 23:38:32 +01:00
my $target = $self->get_trigger($channel, $trigger);
if (not defined $target) { return 0; }
2020-02-15 23:38:32 +01:00
my $sth = $self->{dbh}->prepare('DELETE FROM Triggers WHERE channel = ? AND trigger = ?');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $trigger);
$sth->execute();
return 1;
}
sub list_triggers {
2020-02-15 23:38:32 +01:00
my ($self, $channel) = @_;
2020-02-15 23:38:32 +01:00
my $triggers = eval {
my $sth = $self->{dbh}->prepare('SELECT trigger, target FROM Triggers WHERE channel = ?');
$sth->bind_param(1, lc $channel);
$sth->execute();
return $sth->fetchall_arrayref({});
};
2020-02-15 23:38:32 +01:00
if ($@) { $self->{pbot}->{logger}->log("List triggers failed: $@"); }
return @$triggers;
}
sub get_trigger {
2020-02-15 23:38:32 +01:00
my ($self, $channel, $trigger) = @_;
my $target = eval {
my $sth = $self->{dbh}->prepare('SELECT target FROM Triggers WHERE channel = ? AND trigger = ?');
$sth->bind_param(1, lc $channel);
$sth->bind_param(2, lc $trigger);
$sth->execute();
my $row = $sth->fetchrow_hashref();
return $row->{target};
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Get trigger failed: $@");
return undef;
}
return $target;
}
sub cmd_counteradd {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my ($channel, $name, $description);
if ($context->{from} !~ m/^#/) {
($channel, $name, $description) = split /\s+/, $context->{arguments}, 3;
2020-02-15 23:38:32 +01:00
if (not defined $channel or not defined $name or not defined $description or $channel !~ m/^#/) {
return "Usage from private message: counteradd <channel> <name> <description>";
}
} else {
$channel = $context->{from};
($name, $description) = split /\s+/, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if (not defined $name or not defined $description) { return "Usage: counteradd <name> <description>"; }
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
my $result;
if ($self->add_counter($context->{hostmask}, $channel, $name, $description)) { $result = "Counter added."; }
else { $result = "Counter '$name' already exists."; }
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return $result;
2016-02-14 03:39:12 +01:00
}
sub cmd_counterdel {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my ($channel, $name);
if ($context->{from} !~ m/^#/) {
($channel, $name) = split /\s+/, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if (not defined $channel or not defined $name or $channel !~ m/^#/) { return "Usage from private message: counterdel <channel> <name>"; }
} else {
$channel = $context->{from};
($name) = split /\s+/, $context->{arguments}, 1;
2020-02-15 23:38:32 +01:00
if (not defined $name) { return "Usage: counterdel <name>"; }
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
my $result;
if ($self->delete_counter($channel, $name)) { $result = "Counter removed."; }
else { $result = "No such counter."; }
$self->dbi_end;
return $result;
2016-02-14 03:39:12 +01:00
}
sub cmd_counterreset {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my ($channel, $name);
if ($context->{from} !~ m/^#/) {
($channel, $name) = split /\s+/, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if (not defined $channel or not defined $name or $channel !~ m/^#/) { return "Usage from private message: counterreset <channel> <name>"; }
} else {
$channel = $context->{from};
($name) = split /\s+/, $context->{arguments}, 1;
2020-02-15 23:38:32 +01:00
if (not defined $name) { return "Usage: counterreset <name>"; }
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
my $result;
my ($description, $timestamp) = $self->reset_counter($channel, $name);
if (defined $description) {
my $ago = duration gettimeofday - $timestamp;
$result = "It had been $ago since $description.";
} else {
$result = "No such counter.";
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return $result;
2016-02-14 03:39:12 +01:00
}
sub cmd_countershow {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my ($channel, $name);
if ($context->{from} !~ m/^#/) {
($channel, $name) = split /\s+/, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if (not defined $channel or not defined $name or $channel !~ m/^#/) { return "Usage from private message: countershow <channel> <name>"; }
} else {
$channel = $context->{from};
($name) = split /\s+/, $context->{arguments}, 1;
2020-02-15 23:38:32 +01:00
if (not defined $name) { return "Usage: countershow <name>"; }
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
my $result;
my ($description, $timestamp, $counter, $created_on) = $self->get_counter($channel, $name);
if (defined $description) {
my $ago = duration gettimeofday - $timestamp;
$created_on = duration gettimeofday - $created_on;
$result = "It has been $ago since $description. It has been reset $counter time" . ($counter == 1 ? '' : 's') . " since its creation $created_on ago.";
} else {
$result = "No such counter.";
2016-02-14 03:39:12 +01:00
}
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return $result;
2016-02-14 03:39:12 +01:00
}
sub cmd_counterlist {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my $channel;
if ($context->{from} !~ m/^#/) {
if (not length $context->{arguments} or $context->{arguments} !~ m/^#/) { return "Usage from private message: counterlist <channel>"; }
$channel = $context->{arguments};
2020-02-15 23:38:32 +01:00
} else {
$channel = $context->{from};
2020-02-15 23:38:32 +01:00
}
2020-02-15 23:38:32 +01:00
my @counters = $self->list_counters($channel);
2020-02-15 23:38:32 +01:00
my $result;
if (not @counters) { $result = "No counters available for $channel."; }
else {
my $comma = '';
2020-02-15 23:38:32 +01:00
$result = "Counters for $channel: ";
foreach my $counter (sort @counters) {
$result .= "$comma$counter";
$comma = ', ';
}
}
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return $result;
}
sub cmd_countertrigger {
my ($self, $context) = @_;
2020-02-15 23:38:32 +01:00
return "Internal error." if not $self->dbi_begin;
my $command;
($command, $context->{arguments}) = split / /, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
my ($channel, $result);
given ($command) {
when ('list') {
if ($context->{from} =~ m/^#/) { $channel = $context->{from}; }
2020-02-15 23:38:32 +01:00
else {
($channel) = split / /, $context->{arguments}, 1;
2020-02-15 23:38:32 +01:00
if ($channel !~ m/^#/) {
$self->dbi_end;
return "Usage from private message: countertrigger list <channel>";
}
}
my @triggers = $self->list_triggers($channel);
if (not @triggers) { $result = "No counter triggers set for $channel."; }
else {
$result = "Triggers for $channel: ";
my $comma = '';
foreach my $trigger (@triggers) {
$result .= "$comma$trigger->{trigger} -> $trigger->{target}";
$comma = ', ';
}
}
}
2020-02-15 23:38:32 +01:00
when ('add') {
if ($context->{from} =~ m/^#/) { $channel = $context->{from}; }
2020-02-15 23:38:32 +01:00
else {
($channel, $context->{arguments}) = split / /, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if ($channel !~ m/^#/) {
$self->dbi_end;
return "Usage from private message: countertrigger add <channel> <regex> <target>";
}
}
my ($trigger, $target) = split / /, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if (not defined $trigger or not defined $target) {
if ($context->{from} !~ m/^#/) { $result = "Usage from private message: countertrigger add <channel> <regex> <target>"; }
2020-02-15 23:38:32 +01:00
else { $result = "Usage: countertrigger add <regex> <target>"; }
$self->dbi_end;
return $result;
}
my $exists = $self->get_trigger($channel, $trigger);
if (defined $exists) {
$self->dbi_end;
return "Trigger already exists.";
}
if ($self->add_trigger($channel, $trigger, $target)) { $result = "Trigger added."; }
else { $result = "Failed to add trigger."; }
}
2020-02-15 23:38:32 +01:00
when ('delete') {
if ($context->{from} =~ m/^#/) { $channel = $context->{from}; }
2020-02-15 23:38:32 +01:00
else {
($channel, $context->{arguments}) = split / /, $context->{arguments}, 2;
2020-02-15 23:38:32 +01:00
if ($channel !~ m/^#/) {
$self->dbi_end;
return "Usage from private message: countertrigger delete <channel> <regex>";
}
}
my ($trigger) = split / /, $context->{arguments}, 1;
2020-02-15 23:38:32 +01:00
if (not defined $trigger) {
if ($context->{from} !~ m/^#/) { $result = "Usage from private message: countertrigger delete <channel> <regex>"; }
2020-02-15 23:38:32 +01:00
else { $result = "Usage: countertrigger delete <regex>"; }
$self->dbi_end;
return $result;
}
my $target = $self->get_trigger($channel, $trigger);
if (not defined $target) { $result = "No such trigger."; }
else {
$self->delete_trigger($channel, $trigger);
$result = "Trigger deleted.";
}
}
2020-02-15 23:38:32 +01:00
default { $result = "Usage: countertrigger <list/add/delete> [arguments]"; }
}
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return $result;
}
sub on_public {
2020-02-15 23:38:32 +01:00
my ($self, $event_type, $event) = @_;
my ($nick, $user, $host, $msg) = ($event->{event}->nick, $event->{event}->user, $event->{event}->host, $event->{event}->args);
my $channel = $event->{event}->{to}[0];
2020-02-15 23:38:32 +01:00
return 0 if $event->{interpreted};
2016-04-11 03:35:11 +02:00
if ($self->{pbot}->{ignorelist}->is_ignored($channel, "$nick!$user\@$host")) {
return 0;
}
2020-02-15 23:38:32 +01:00
if (not $self->dbi_begin) { return 0; }
2020-02-15 23:38:32 +01:00
my @triggers = $self->list_triggers($channel);
2020-02-15 23:38:32 +01:00
my $hostmask = "$nick!$user\@$host";
2020-02-15 23:38:32 +01:00
foreach my $trigger (@triggers) {
eval {
my $message;
2016-03-07 07:25:22 +01:00
2020-02-15 23:38:32 +01:00
if ($trigger->{trigger} =~ m/^\^/) { $message = "$hostmask $msg"; }
else { $message = $msg; }
2016-02-20 05:44:57 +01:00
2020-02-15 23:38:32 +01:00
my $silent = 0;
2016-03-07 07:25:22 +01:00
2020-02-15 23:38:32 +01:00
if ($trigger->{trigger} =~ s/:silent$//i) { $silent = 1; }
2016-03-07 07:25:22 +01:00
2020-02-15 23:38:32 +01:00
if ($message =~ m/$trigger->{trigger}/i) {
my ($desc, $timestamp) = $self->reset_counter($channel, $trigger->{target});
2020-02-15 23:38:32 +01:00
if (defined $desc) {
if (not $silent and gettimeofday - $timestamp >= 60 * 60) {
my $ago = duration gettimeofday - $timestamp;
$event->{conn}->privmsg($channel, "It had been $ago since $desc.");
}
}
}
};
2020-02-15 23:38:32 +01:00
if ($@) { $self->{pbot}->{logger}->log("Skipping bad trigger $trigger->{trigger}: $@"); }
}
2020-02-15 23:38:32 +01:00
$self->dbi_end;
return 0;
}
2016-02-14 03:39:12 +01:00
1;