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

ActionTrigger: Use command effective-level override

This commit is contained in:
Pragmatic Software 2017-12-11 12:47:21 -08:00
parent 8fb98d614f
commit b290bd30b8

View File

@ -59,7 +59,8 @@ CREATE TABLE IF NOT EXISTS Triggers (
channel TEXT, channel TEXT,
trigger TEXT, trigger TEXT,
action TEXT, action TEXT,
owner TEXT owner TEXT,
level INTEGER
) )
SQL SQL
@ -90,16 +91,17 @@ sub dbi_end {
} }
sub add_trigger { sub add_trigger {
my ($self, $channel, $trigger, $action, $owner) = @_; my ($self, $channel, $trigger, $action, $owner, $level) = @_;
return 0 if $self->get_trigger($channel, $trigger); return 0 if $self->get_trigger($channel, $trigger);
eval { eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Triggers (channel, trigger, action, owner) VALUES (?, ?, ?, ?)'); my $sth = $self->{dbh}->prepare('INSERT INTO Triggers (channel, trigger, action, owner, level) VALUES (?, ?, ?, ?, ?)');
$sth->bind_param(1, lc $channel); $sth->bind_param(1, lc $channel);
$sth->bind_param(2, $trigger); $sth->bind_param(2, $trigger);
$sth->bind_param(3, $action); $sth->bind_param(3, $action);
$sth->bind_param(4, $owner); $sth->bind_param(4, $owner);
$sth->bind_param(5, $level);
$sth->execute(); $sth->execute();
}; };
@ -174,13 +176,11 @@ sub actiontrigger {
given ($command) { given ($command) {
when ('list') { when ('list') {
if ($from =~ m/^#/) {
$channel = $from;
} else {
($channel) = split / /, $arguments, 1; ($channel) = split / /, $arguments, 1;
if ($channel !~ m/^#/) { if (not defined $channel) {
return "Usage from private message: actiontrigger list <channel>"; $channel = $from;
} } elsif ($channel !~ m/^#/) {
return "Usage: actiontrigger list [channel]";
} }
my @triggers = $self->list_triggers($channel); my @triggers = $self->list_triggers($channel);
@ -192,6 +192,7 @@ sub actiontrigger {
my $comma = ''; my $comma = '';
foreach my $trigger (@triggers) { foreach my $trigger (@triggers) {
$result .= "$comma$trigger->{trigger} -> $trigger->{action}"; $result .= "$comma$trigger->{trigger} -> $trigger->{action}";
$result .= " ($trigger->{level})" if $trigger->{level} != 0;
$comma = ",\n"; $comma = ",\n";
} }
} }
@ -207,14 +208,13 @@ sub actiontrigger {
} }
} }
my ($level, $trigger, $action) = split / /, $arguments, 3;
my ($trigger, $action) = split / /, $arguments, 2;
if (not defined $trigger or not defined $action) { if (not defined $trigger or not defined $action) {
if ($from !~ m/^#/) { if ($from !~ m/^#/) {
$result = "Usage from private message: actiontrigger add <channel> <regex> <action>"; $result = "Usage from private message: actiontrigger add <channel> <level> <regex> <action>";
} else { } else {
$result = "Usage: actiontrigger add <regex> <action>"; $result = "Usage: actiontrigger add <level> <regex> <action>";
} }
return $result; return $result;
} }
@ -225,7 +225,18 @@ sub actiontrigger {
return "Trigger already exists."; return "Trigger already exists.";
} }
if ($self->add_trigger($channel, $trigger, $action, "$nick!$user\@$host")) { if ($level > 0) {
my $admin = $self->{pbot}->{admins}->find_admin($channel, "$nick!$user\@$host");
if (not defined $admin or $level > $admin->{level}) {
return "You may not set a level higher than your own.";
}
}
if ($level < 0) {
return "Please be serious.";
}
if ($self->add_trigger($channel, $trigger, $action, "$nick!$user\@$host", $level)) {
$result = "Trigger added."; $result = "Trigger added.";
} else { } else {
$result = "Failed to add trigger."; $result = "Failed to add trigger.";
@ -264,7 +275,7 @@ sub actiontrigger {
} }
default { default {
$result = "Usage: actiontrigger list | actiontrigger add <regex> <trigger> | actiontrigger delete <regex>"; $result = "Usage: actiontrigger list | actiontrigger add <level> <regex> <trigger> | actiontrigger delete <regex>";
} }
} }
@ -325,6 +336,7 @@ sub check_trigger {
my @triggers = $self->list_triggers($channel); my @triggers = $self->list_triggers($channel);
$text = "$nick!$user\@$host $text"; $text = "$nick!$user\@$host $text";
# $self->{pbot}->{logger}->log("Checking action trigger: [$text]\n");
foreach my $trigger (@triggers) { foreach my $trigger (@triggers) {
eval { eval {
@ -338,12 +350,13 @@ sub check_trigger {
my ($n, $u, $h) = $trigger->{owner} =~ /^([^!]+)!([^@]+)\@(.*)$/; my ($n, $u, $h) = $trigger->{owner} =~ /^([^!]+)!([^@]+)\@(.*)$/;
my $command = { my $command = {
nick => $n, nick => $nick,
user => $u, user => $user,
host => $h, host => $host,
command => $action command => $action,
level => $trigger->{level} // 0
}; };
$self->{pbot}->{logger}->log("ActionTrigger: ($channel) $trigger->{trigger} -> $action\n"); $self->{pbot}->{logger}->log("ActionTrigger: ($channel) $trigger->{trigger} -> $action [$command->{level}]\n");
$self->{pbot}->{interpreter}->add_to_command_queue($channel, $command, $delay); $self->{pbot}->{interpreter}->add_to_command_queue($channel, $command, $delay);
} }
}; };
@ -353,7 +366,6 @@ sub check_trigger {
} }
} }
return 0; return 0;
} }