-
Notifications
You must be signed in to change notification settings - Fork 20
/
ActionTrigger.pm
528 lines (420 loc) · 17.3 KB
/
ActionTrigger.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File: ActionTrigger.pm
#
# Purpose: provides interface to set/remove/modify regular expression triggers
# which invoke bot commands when matched against IRC messages.
#
# Usage: actiontrigger add <#channel or global> <capability> <rate-limit (in seconds)> <regex trigger> <command>
#
# Note that ActionTrigger does not match on raw IRC lines. It matches on a
# simplified message format:
#
# "<hostmask> <action> <arguments>"
#
# where <action> can be PRIVMSG, ACTION, KICK, JOIN, PART or QUIT.
#
# Examples:
#
# Greet a nick when they join the channel:
# actiontrigger add #channel none 0 ^(?i)([^!]+)![^\s]+.JOIN echo Hi $1, welcome to $channel!
#
# Same, but via private message (set capability to "admin" to use `msg` admin command):
# actiontrigger add #channel admin 0 ^(?i)([^!]+)![^\s]+.JOIN msg $1 Hi $1, welcome to $channel!
#
# Kick a nick if they say a naughty thing. Set capability to "can-kick" to use `kick` admin command.
# actiontrigger add global can-kick 0 "^(?i)([^!]+)![^\s]+.PRIVMSG.*bad phrase" kick $1 Do you talk to your mother with that mouth?
#
# Say something when a keyword is seen, but only once every 5 minutes:
# actiontrigger add global none 300 "some phrase" echo Something!
#
# Capture a part of somebody's message.
# actiontrigger add #channel none 0 "(?i)how is the weather (?:in|for) (.*) today" weather $1
#
# These are basic examples; more complex examples can be crafted.
# SPDX-FileCopyrightText: 2017-2023 Pragmatic Software <[email protected]>
# SPDX-License-Identifier: MIT
package PBot::Plugin::ActionTrigger;
use parent 'PBot::Plugin::Base';
use PBot::Imports;
use DBI;
use Time::Duration qw/duration/;
use Time::HiRes qw/gettimeofday/;
sub initialize($self, %conf) {
# register bot command
$self->{pbot}->{commands}->add(
name => 'actiontrigger',
help => 'Manages regular expression triggers to invoke bot commands',
requires_cap => 1,
subref => sub { $self->cmd_actiontrigger(@_) },
);
# add capability to admin group
$self->{pbot}->{capabilities}->add('admin', 'can-actiontrigger', 1);
# register IRC handlers
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_action(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.join', sub { $self->on_join(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) });
# database file
$self->{filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/triggers.sqlite3';
# open and initialize database
$self->dbi_begin;
$self->create_database;
}
sub unload($self) {
# close database
$self->dbi_end;
# unregister bot command
$self->{pbot}->{commands}->remove('actiontrigger');
# remove capability
$self->{pbot}->{capabilities}->remove('can-actiontrigger');
# remove IRC handlers
$self->{pbot}->{event_dispatcher}->remove_handler('irc.public');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.caction');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.join');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.part');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.quit');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.kick');
}
sub cmd_actiontrigger($self, $context) {
# database not available
return "Internal error." if not $self->{dbh};
my $command = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
given ($command) {
when ('list') {
my $channel = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
if (not defined $channel) {
if ($context->{from} !~ /^#/) {
# used from /msg
$channel = 'global';
} else {
# used in channel
$channel = $context->{from};
}
}
elsif ($channel !~ m/^#/ and $channel ne 'global') {
return "Invalid channel $channel. Usage: actiontrigger list [#channel or global]";
}
my @triggers = $self->list_triggers($channel);
if (not @triggers) {
return "No action triggers set for $channel.";
}
else {
my $result = "Triggers for $channel:\n";
my @items;
foreach my $trigger (@triggers) {
$trigger->{cap_override} //= 'none';
$trigger->{ratelimit} //= 0;
my $item = "$trigger->{trigger} -> $trigger->{action}";
if ($trigger->{cap_override} and $trigger->{cap_override} ne 'none') {
$item .= " (capability=$trigger->{cap_override})";
}
if ($trigger->{ratelimit} != 0) {
$item .= " (ratelimit=$trigger->{ratelimit})";
}
push @items, $item;
}
$result .= join ",\n", @items;
return $result;
}
}
when ('add') {
# TODO: use GetOpt flags instead of positional arguments
my $channel;
if ($context->{from} =~ m/^#/) {
$channel = $context->{from};
}
else {
$channel = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
if (not defined $channel) {
return
"To use this command from private message the <channel> argument is required. Usage: actiontrigger add <#channel or global> <capability> <rate-limit (in seconds)> <regex trigger> <command>";
}
elsif ($channel !~ m/^#/ and $channel ne 'global') {
return "Invalid channel $channel. Usage: actiontrigger add <#channel or global> <capability> <rate-limit (in seconds)> <regex trigger> <command>";
}
}
# split into 4 arguments, offset 0, preserving quotes
my ($cap_override, $ratelimit, $trigger, $action) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 4, 0, 1);
if (not defined $trigger or not defined $action) {
if ($context->{from} !~ m/^#/) {
return
"To use this command from private message the <channel> argument is required. Usage: actiontrigger add <#channel or global> <capability> <rate-limit (in seconds)> <regex trigger> <command>";
} else {
return "Usage: actiontrigger add <capability> <rate-limit (in seconds)> <regex trigger> <command>";
}
}
if (defined $self->get_trigger($channel, $trigger)) {
return "Trigger already exists.";
}
if ($ratelimit !~ m/^\d+$/) {
return "$context->{nick}: Missing rate-limit argument?\n";
}
if ($cap_override ne 'none') {
if (not $self->{pbot}->{capabilities}->exists($cap_override)) {
return "$context->{nick}: Capability '$cap_override' does not exist. Use 'none' to omit.\n";
}
my $u = $self->{pbot}->{users}->find_user($channel, $context->{hostmask});
if (not $self->{pbot}->{capabilities}->userhas($u, $cap_override)) {
return "You may not set a capability that you do not have.";
}
}
if ($self->add_trigger($channel, $trigger, $action, $context->{hostmask}, $cap_override, $ratelimit)) {
return "Trigger added.";
} else {
return "Failed to add trigger.";
}
}
when ('delete') {
my $channel;
if ($context->{from} =~ m/^#/) {
$channel = $context->{from};
}
else {
$channel = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
if ($channel !~ m/^#/ and $channel ne 'global') {
return "To use this command from private message the <channel> argument is required. Usage: actiontrigger delete <#channel or global> <regex trigger>";
}
}
my ($trigger) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 1);
if (not defined $trigger) {
if ($context->{from} !~ m/^#/) {
return "To use this command from private message the <channel> argument is required. Usage: from private message: actiontrigger delete <channel> <regex trigger>";
} else {
return "Usage: actiontrigger delete <regex trigger>";
}
}
if (not defined $self->get_trigger($channel, $trigger)) {
return "No such trigger.";
} else {
$self->delete_trigger($channel, $trigger);
return "Trigger deleted.";
}
}
default {
if ($context->{from} !~ m/^#/) {
return
"Usage from private message: actiontrigger list [#channel or global] | actiontrigger add <#channel or global> <capability> <rate-limit (in seconds)> <regex trigger> <command> | actiontrigger delete <#channel or global> <regex trigger>";
} else {
return
"Usage: actiontrigger list [#channel or global] | actiontrigger add <capability> <rate-limit (in seconds)> <regex trigger> <command> | actiontrigger delete <regex>";
}
}
}
}
sub create_database($self) {
return if not $self->{dbh};
eval {
$self->{dbh}->do(<<SQL);
CREATE TABLE IF NOT EXISTS Triggers (
channel TEXT,
trigger TEXT,
action TEXT,
owner TEXT,
cap_override TEXT,
ratelimit INTEGER,
lastused NUMERIC
)
SQL
};
$self->{pbot}->{logger}->log("ActionTrigger create database failed: $@") if $@;
}
sub dbi_begin($self) {
eval {
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1})
or die $DBI::errstr;
};
if ($@) {
$self->{pbot}->{logger}->log("Error opening ActionTrigger database: $@");
return 0;
} else {
return 1;
}
}
sub dbi_end($self) {
return if not $self->{dbh};
$self->{dbh}->disconnect;
delete $self->{dbh};
}
sub add_trigger($self, $channel, $trigger, $action, $owner, $cap_override, $ratelimit) {
return 0 if $self->get_trigger($channel, $trigger);
eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Triggers (channel, trigger, action, owner, cap_override, ratelimit, lastused) VALUES (?, ?, ?, ?, ?, ?, 0)');
$sth->execute(lc $channel, $trigger, $action, $owner, $cap_override, $ratelimit);
};
if ($@) {
$self->{pbot}->{logger}->log("Add trigger failed: $@");
return 0;
}
return 1;
}
sub delete_trigger($self, $channel, $trigger) {
return 0 if not $self->get_trigger($channel, $trigger);
my $sth = $self->{dbh}->prepare('DELETE FROM Triggers WHERE channel = ? AND trigger = ?');
$sth->execute(lc $channel, $trigger);
return 1;
}
sub list_triggers($self, $channel) {
my $triggers = eval {
my $sth;
$sth = $self->{dbh}->prepare('SELECT * FROM Triggers WHERE channel = ?');
$sth->execute(lc $channel);
return $sth->fetchall_arrayref({});
};
if ($@) { $self->{pbot}->{logger}->log("List triggers failed: $@"); }
$triggers = [] if not defined $triggers;
return @$triggers;
}
sub update_trigger($self, $channel, $trigger, $data) {
eval {
my $sql = 'UPDATE Triggers SET ';
my @triggers;
foreach my $key (keys %$data) {
push @triggers, "$key = ?";
}
$sql .= join ', ', @triggers;
$sql .= "WHERE trigger = ? AND channel = ?";
my $sth = $self->{dbh}->prepare($sql);
my $param = 1;
foreach my $key (keys %$data) { $sth->bind_param($param++, $data->{$key}); }
$sth->bind_param($param++, $trigger);
$sth->bind_param($param, $channel);
$sth->execute;
};
$self->{pbot}->{logger}->log("Update trigger $channel/$trigger failed: $@\n") if $@;
}
sub get_trigger($self, $channel, $trigger) {
my $row = eval {
my $sth = $self->{dbh}->prepare('SELECT * FROM Triggers WHERE channel = ? AND trigger = ?');
$sth->execute(lc $channel, $trigger);
my $row = $sth->fetchrow_hashref;
return $row;
};
if ($@) {
$self->{pbot}->{logger}->log("Get trigger failed: $@");
return undef;
}
return $row;
}
sub on_kick($self, $event_type, $event) {
# don't handle this event if it was processed by a bot command
return 0 if $event->{interpreted};
my ($nick, $user, $host) = (
$event->nick,
$event->user,
$event->host
);
my ($victim, $reason) = (
$event->to,
$event->{args}[1]
);
my $channel = $event->{args}[0];
$self->check_trigger($nick, $user, $host, $channel, "KICK $victim $reason");
return 0;
}
sub on_action($self, $event_type, $event) {
# don't handle this event if it was processed by a bot command
return 0 if $event->{interpreted};
my ($nick, $user, $host, $msg) = (
$event->nick,
$event->user,
$event->host,
$event->args
);
my $channel = $event->{to}[0];
$msg =~ s/^\/me\s+//;
$self->check_trigger($nick, $user, $host, $channel, "ACTION $msg");
return 0;
}
sub on_public($self, $event_type, $event) {
# don't handle this event if it was processed by a bot command
return 0 if $event->{interpreted};
my ($nick, $user, $host, $msg) = (
$event->nick,
$event->user,
$event->host,
$event->args
);
my $channel = $event->{to}[0];
$self->check_trigger($nick, $user, $host, $channel, "PRIVMSG $msg");
return 0;
}
sub on_join($self, $event_type, $event) {
my ($nick, $user, $host, $channel, $args) = (
$event->nick,
$event->user,
$event->host,
$event->to,
$event->args
);
$self->check_trigger($nick, $user, $host, $channel, "JOIN");
return 0;
}
sub on_departure($self, $event_type, $event) {
my ($nick, $user, $host, $channel, $args) = (
$event->nick,
$event->user,
$event->host,
$event->to,
$event->args
);
$self->check_trigger($nick, $user, $host, $channel, (uc $event->type) . " $args");
return 0;
}
sub check_trigger($self, $nick, $user, $host, $channel, $text) {
# database not available
return 0 if not $self->{dbh};
$channel = lc $channel;
# TODO: cache these instead of loading them again every message
my @triggers;
if ($channel =~ /^#/) {
@triggers = $self->list_triggers($channel);
} else {
my $channels = $self->{pbot}->{nicklist}->get_channels($nick);
foreach my $c (@$channels) {
next if not $self->{pbot}->{channels}->is_active($c);
push @triggers, $self->list_triggers($c);
}
}
my @globals = $self->list_triggers('global');
push @triggers, @globals;
$text = "$nick!$user\@$host $text";
my $now = gettimeofday;
foreach my $trigger (@triggers) {
eval {
$trigger->{lastused} //= 0;
$trigger->{ratelimit} //= 0;
if ($now - $trigger->{lastused} >= $trigger->{ratelimit} and $text =~ m/$trigger->{trigger}/) {
my @stuff = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
$trigger->{lastused} = $now;
$self->update_trigger($trigger->{channel}, $trigger->{trigger}, { lastused => $now });
my $action = $trigger->{action};
my $i;
map { ++$i; $action =~ s/\$$i/$_/g; } @stuff;
my $command = {
nick => $nick,
user => $user,
host => $host,
hostmask => "$nick!$user\@$host",
command => $action,
};
if ($trigger->{cap_override} and $trigger->{cap_override} ne 'none') {
$command->{'cap-override'} = $trigger->{cap_override};
}
my $target_channel;
if ($trigger->{channel} eq 'global') {
$target_channel = $channel;
} else {
$target_channel = $trigger->{channel};
}
my $cap = '';
$cap = " (capability=$command->{'cap-override'})" if exists $command->{'cap-override'};
$self->{pbot}->{logger}->log("ActionTrigger: ($target_channel) $trigger->{trigger} -> $action$cap\n");
$self->{pbot}->{interpreter}->add_to_command_queue($target_channel, $command);
}
};
if ($@) { $self->{pbot}->{logger}->log("Skipping bad trigger $trigger->{trigger}: $@"); }
}
return 0;
}
1;