-
Notifications
You must be signed in to change notification settings - Fork 31
/
bucket.pl
executable file
·3800 lines (3369 loc) · 123 KB
/
bucket.pl
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# Copyright (C) 2011 Dan Boger - [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# $Id: bucket.pl 685 2009-08-04 19:15:15Z dan $
use strict;
use POE;
use POE::Component::IRC;
use POE::Component::IRC::State;
use POE::Component::IRC::Plugin::NickServID;
use POE::Component::IRC::Plugin::Connector;
use POE::Component::SimpleDBI;
use Lingua::EN::Conjugate qw/past gerund/;
use Lingua::EN::Inflect qw/A PL_N/;
use Lingua::EN::Syllable qw//; # don't import anything
use IO::Handle;
use YAML qw/LoadFile DumpFile/;
use Data::Dumper;
use Fcntl qw/:seek/;
use HTML::Entities;
use URI::Escape;
use DBI;
$Data::Dumper::Indent = 1;
# try to load Math::BigFloat if possible
my $math = "";
eval { require Math::BigFloat; };
unless ($@) {
$math = "Math::BigFloat";
&Log("$math loaded");
}
sub DEBUG {
return &config('debug');
}
# work around a bug: https://rt.cpan.org/Ticket/Display.html?id=50991
sub s_form { return Lingua::EN::Conjugate::s_form(@_); }
$SIG{CHLD} = 'IGNORE';
$|++;
### IRC portion
my $configfile = shift || "bucket.yml";
my $config = LoadFile($configfile);
my $nick = &config("nick") || "Bucket";
my $pass = &config("password") || "somethingsecret";
$config->{nick} = $nick =
&DEBUG ? ( &config("debug_nick") || "bucketgoat" ) : $nick;
my $channel =
&DEBUG
? ( &config("debug_channel") || "#bucket" )
: ( &config("control_channel") || "#billygoat" );
our ($irc) = POE::Component::IRC::State->spawn();
my %channels = ( $channel => 1 );
my $mainchannel = &config("main_channel") || "#xkcd";
my %_talking;
my %fcache;
my %stats;
my %undo;
my %last_activity;
my @inventory;
my @random_items;
my %replacables;
my %handles;
my %plugin_signals;
my @registered_commands;
my %config_keys = (
autoload_plugins => [ s => '' ],
band_name => [ p => 5 ],
band_var => [ s => 'band' ],
ex_to_sex => [ p => 1 ],
file_input => [ f => "" ],
idle_source => [ s => 'factoid' ],
increase_mute => [ i => 60 ],
inventory_preload => [ i => 0 ],
inventory_size => [ i => 20 ],
item_drop_rate => [ i => 3 ],
literal_page_max => [ i => 10],
lookup_tla => [ i => 10 ],
max_sub_length => [ i => 80 ],
minimum_length => [ i => 6 ],
random_exclude_verbs => [ s => '<reply>,<action>' ],
random_item_cache_size => [ i => 20 ],
random_wait => [ i => 3 ],
repeated_queries => [ i => 5 ],
timeout => [ i => 60 ],
the_fucking => [ p => 100 ],
tumblr_name => [ p => 50 ],
uses_reply => [ i => 5 ],
user_activity_timeout => [ i => 360 ],
value_cache_limit => [ i => 1000 ],
var_limit => [ i => 3 ],
your_mom_is => [ p => 5 ],
);
$stats{startup_time} = time;
&open_log;
if ( &config("autoload_plugins") ) {
foreach my $plugin ( split ' ', &config("autoload_plugins") ) {
&load_plugin($plugin);
}
}
my %gender_vars = (
subjective => {
male => "he",
female => "she",
androgynous => "they",
inanimate => "it",
"full name" => "%N",
aliases => [qw/he she they it heshe shehe/]
},
objective => {
male => "him",
female => "her",
androgynous => "them",
inanimate => "it",
"full name" => "%N",
aliases => [qw/him her them himher herhim/]
},
reflexive => {
male => "himself",
female => "herself",
androgynous => "themself",
inanimate => "itself",
"full name" => "%N",
aliases =>
[qw/himself herself themself itself himselfherself herselfhimself/]
},
possessive => {
male => "his",
female => "hers",
androgynous => "theirs",
inanimate => "its",
"full name" => "%N's",
aliases => [qw/hers theirs hishers hershis/]
},
determiner => {
male => "his",
female => "her",
androgynous => "their",
inanimate => "its",
"full name" => "%N's",
aliases => [qw/their hisher herhis/]
},
);
# make sure the file_input file is empty, if it is defined
# (so that we don't delete anything important by mistake)
if ( &config("file_input") and -f &config("file_input") ) {
&Log( "Ignoring the file_input directive since that file already exists "
. "at startup" );
delete $config->{file_input};
}
# set up gender aliases
foreach my $type ( keys %gender_vars ) {
foreach my $alias ( @{$gender_vars{$type}{aliases}} ) {
$gender_vars{$alias} = $gender_vars{$type};
&Log("Setting gender alias: $alias => $type");
}
}
$irc->plugin_add( 'NickServID',
POE::Component::IRC::Plugin::NickServID->new( Password => $pass ) );
POE::Component::SimpleDBI->new('db') or die "Can't create DBI session";
POE::Session->create(
inline_states => {
_start => \&irc_start,
irc_001 => \&irc_on_connect,
irc_kick => \&irc_on_kick,
irc_quit => \&irc_on_quit,
irc_public => \&irc_on_public,
irc_ctcp_action => \&irc_on_public,
irc_msg => \&irc_on_public,
irc_notice => \&irc_on_notice,
irc_disconnected => \&irc_on_disconnect,
irc_topic => \&irc_on_topic,
irc_join => \&irc_on_join,
irc_part => \&irc_on_part,
irc_332 => \&irc_on_jointopic,
irc_331 => \&irc_on_jointopic,
irc_nick => \&irc_on_nick,
irc_chan_sync => \&irc_on_chan_sync,
db_success => \&db_success,
delayed_post => \&delayed_post,
heartbeat => \&heartbeat,
},
);
POE::Kernel->run;
print "POE::Kernel has left the building.\n";
sub Log {
print scalar localtime, " - @_\n";
if ( &config("logfile") ) {
print LOG scalar localtime, " - @_\n";
}
}
sub Report {
my $delay = shift if $_[0] =~ /^\d+$/;
my $logchannel = &DEBUG ? $channel : &config("logchannel");
unshift @_, "REPORT:" if &DEBUG;
if ( $logchannel and $irc ) {
if ($delay) {
Log "Delayed msg ($delay): @_";
POE::Kernel->delay_add(
delayed_post => 2 * $delay => $logchannel => "@_" );
} else {
&say( $logchannel, "@_" );
}
}
}
sub delayed_post {
&say( $_[ARG0], $_[ARG1] );
}
sub irc_on_topic {
my $chl = $_[ARG1];
my $topic = $_[ARG2];
return if &signal_plugin( "on_topic", {chl => $chl, topic => $topic} );
}
sub irc_on_kick {
my ($kicker) = split /!/, $_[ARG0];
my $chl = $_[ARG1];
my $kickee = $_[ARG2];
my $desc = $_[ARG3];
Log "$kicker kicked $kickee from $chl";
return
if &signal_plugin(
"on_kick",
{
kicker => $kicker,
chl => $chl,
kickee => $kickee,
desc => $desc
}
);
&lookup(
msgs => [ "$kicker kicked $kickee", "$kicker kicked someone" ],
chl => $chl,
who => $kickee,
op => 1,
type => 'irc_kick',
);
delete $stats{users}{$chl}{$kickee};
unless ( $irc->nick_channels( $kickee ) ) {
delete $stats{users}{genders}{lc $kickee};
}
}
sub irc_on_quit {
my ($quitter) = split /!/, $_[ARG0];
return if &signal_plugin( "on_quit", {who => $quitter} );
foreach my $chl (keys %{$stats{users}}) {
next if $chl !~ /^#/;
delete $stats{users}{$chl}{$quitter};
}
delete $stats{users}{genders}{lc $quitter};
}
sub irc_on_public {
my ($who) = split /!/, $_[ARG0];
my $type = $_[STATE];
my $chl = $_[ARG1];
$chl = $chl->[0] if ref $chl eq 'ARRAY';
my $msg = $_[ARG2];
$msg =~ s/\s\s+/ /g;
my %bag;
$bag{who} = $who;
$bag{msg} = $msg;
$bag{chl} = $chl;
$bag{type} = $type;
if ( not $stats{tail_time} or time - $stats{tail_time} > 60 ) {
&tail( $_[KERNEL] );
$stats{tail_time} = time;
}
$last_activity{$chl} = time;
if ( exists $config->{ignore}{lc $bag{who}} ) {
Log("ignoring $bag{who} in $bag{chl}");
return;
}
$bag{addressed} = 0;
if ( $type eq 'irc_msg' or $bag{msg} =~ s/^$nick[:,]\s*|,\s+$nick\W+$//i ) {
$bag{addressed} = 1;
$bag{to} = $nick;
} else {
if( $bag{msg} =~ m/^(\S+)[:,]\s*/ and $irc->is_channel_member( $bag{chl}, $1 ) ) {
$bag{msg} =~ s/^(\S+)[:,]\s*//;
$bag{to} = $1;
}
}
$bag{op} = 0;
if ( $irc->is_channel_member( $channel, $bag{who} )
or $irc->is_channel_operator( $mainchannel, $bag{who} )
or $irc->is_channel_owner( $mainchannel, $bag{who} )
or $irc->is_channel_admin( $mainchannel, $bag{who} ) )
{
$bag{op} = 1;
}
# allow editing only in public channels (other than #bots), or by ops.
$bag{editable} = 1 if ( $chl =~ /^#/ and $chl ne '#bots' ) or $bag{op};
if ( $type eq 'irc_msg' ) {
return if &signal_plugin( "on_msg", \%bag );
} else {
return if &signal_plugin( "on_public", \%bag );
}
my $editable = $bag{editable};
my $addressed = $bag{addressed};
my $operator = $bag{op};
$msg = $bag{msg};
# keep track of who's active in each channel
if ( $chl =~ /^#/ ) {
$stats{users}{$chl}{$bag{who}}{last_active} = time;
}
unless ( exists $stats{users}{genders}{lc $bag{who}} ) {
&load_gender( $bag{who} );
}
# flood protection
if ( not $operator and $addressed ) {
$stats{last_talk}{$chl}{$bag{who}}{when} = time;
if ( $stats{last_talk}{$chl}{$bag{who}}{count}++ > 20
and time - $stats{last_talk}{$chl}{$bag{who}}{when} <
&config("user_activity_timeout") )
{
if ( $stats{last_talk}{$chl}{$bag{who}}{count} == 21 ) {
Report "Ignoring $bag{who} who is flooding in $chl.";
&say( $chl =>
"$bag{who}, I'm a bit busy now, try again in 5 minutes?"
);
}
return;
}
}
$bag{msg} =~ s/^\s+|\s+$//g;
unless ( &talking($chl) == -1 or ( $operator and $addressed ) ) {
my $timeout = &talking($chl);
if ( $addressed and &config("increase_mute") and $timeout > 0 ) {
&talking( $chl, $timeout + &config("increase_mute") );
Report "Shutting up longer in $chl - "
. ( &talking($chl) - time )
. " seconds remaining";
}
return;
}
if ( time - $stats{last_updated} > 600 ) {
&get_stats( $_[KERNEL] );
&clear_cache();
&random_item_cache( $_[KERNEL], 1 );
}
if ( $type eq 'irc_msg' ) {
$bag{chl} = $chl = $bag{who};
}
Log(
"$type($chl): $bag{who}(o=$operator, a=$addressed, e=$editable): $bag{msg}"
);
# check all registered commands
foreach my $cmd (@registered_commands) {
if ( $addressed >= $cmd->{addressed}
and $operator >= $cmd->{operator}
and $editable >= $cmd->{editable}
and $bag{msg} =~ $cmd->{re} )
{
Log("Matched cmd '$cmd->{label}' from $cmd->{plugin}.");
$cmd->{callback}->( \%bag );
return;
}
}
if (
$addressed
and $editable
and $bag{msg} =~ m{ (.*?) # $1 key to edit
\s+(?:=~|~=)\s+ # match operator
s(\W) # start match ($2 delimiter)
( # $3 - string to replace
[^\2]+ # anything but a delimiter
) # end of $3
\2 # separator
(.*) # $4 - text to replace with
\2
([gi]*) # $5 - i/g flags
\s* $ # trailing spaces
}x
)
{
my ( $fact, $old, $new, $flag ) = ( $1, $3, $4, $5 );
Report
"$bag{who} is editing $fact in $chl: replacing '$old' with '$new'";
Log "Editing $fact: replacing '$old' with '$new'";
if ( $fact =~ /^#(\d+)$/ ) {
&sql(
'select * from bucket_facts where id = ?',
[$1],
{
%bag,
cmd => "edit",
old => $old,
'new' => $new,
flag => $flag,
db_type => 'MULTIPLE',
}
);
} else {
&sql(
'select * from bucket_facts where fact = ? order by id',
[$fact],
{
%bag,
cmd => "edit",
fact => $fact,
old => $old,
'new' => $new,
flag => $flag,
db_type => 'MULTIPLE',
}
);
}
} elsif (
$bag{msg} =~ m{ (.*?) # $1 key to look up
\s+(?:=~|~=)\s+ # match operator
(\W) # start match (any delimiter, $2)
( # $3 - string to search
[^\2]+ # anything but a delimiter
) # end of $3
\2 # same delimiter that opened the match
}x
)
{
my ( $fact, $search ) = ( $1, $3 );
$fact = &trim($fact);
$bag{msg} = $fact;
Log "Looking up a particular factoid - '$search' in '$fact'";
&lookup( %bag, search => $search, );
} elsif ( $addressed and $operator and $bag{msg} =~ /^list plugins\W*$/i ) {
&say(
$chl => "$bag{who}: Currently loaded plugins: "
. &make_list(
map { "$_($stats{loaded_plugins}{$_})" }
sort keys %{$stats{loaded_plugins}}
)
);
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^load plugin (\w+)\W*$/i )
{
if ( &load_plugin( lc $1 ) ) {
&say( $chl => "Okay, $bag{who}. Plugin $1 loaded." );
} else {
&say( $chl => "Sorry, $bag{who}. Plugin $1 failed to load." );
}
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^unload plugin (\w+)\W*$/i )
{
&unload_plugin( lc $1 );
&say( $chl => "Okay, $bag{who}. Plugin $1 unloaded." );
} elsif ( $addressed and $bag{msg} =~ /^literal(?:\[(\*|\d+)\])?\s+(.*)/i )
{
my ( $page, $fact ) = ( $1 || 1, $2 );
$stats{literal}++;
$fact = &trim($fact);
$fact = &decommify($fact);
Log "Literal[$page] $fact";
&sql(
'select id, verb, tidbit, mood, chance, protected from
bucket_facts where fact = ? order by id',
[$fact],
{
%bag,
cmd => "literal",
page => $page,
fact => $fact,
addressed => $addressed,
db_type => 'MULTIPLE',
}
);
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^delete item #?(\d+)\W*$/i )
{
unless ( $stats{detailed_inventory}{$bag{who}} ) {
&say( $chl => "$bag{who}: ask me for a detailed inventory first." );
return;
}
my $num = $1 - 1;
my $item = $stats{detailed_inventory}{$bag{who}}[$num];
unless ( defined $item ) {
&say( $chl => "Sorry, $bag{who}, I can't find that!" );
return;
}
&say( $chl => "Okay, $bag{who}, deleting item '$item'" );
@inventory = grep { $_ ne $item } @inventory;
&sql( "delete from bucket_items where `what` = ?", [$item] );
delete $stats{detailed_inventory}{$bag{who}}[$num];
} elsif ( $addressed and $operator and $bag{msg} =~ /^delete ((#)?.+)/i ) {
my $id = $2;
my $fact = $1;
$stats{deleted}++;
if ($id) {
while ( $fact =~ s/#(\d+)\s*// ) {
&sql(
'select fact, tidbit, verb, RE, protected, mood, chance
from bucket_facts where id = ?',
[$1],
{
%bag,
cmd => "delete_id",
fact => $1,
db_type => "SINGLE",
}
);
}
} else {
&sql(
'select fact, tidbit, verb, RE, protected, mood, chance from
bucket_facts where fact = ?',
[$fact],
{
%bag,
cmd => "delete",
fact => $fact,
db_type => 'MULTIPLE',
}
);
}
} elsif (
$addressed
and $bag{msg} =~ /^(?:shut \s up | go \s away)
(?: \s for \s (\d+)([smh])?|
\s for \s a \s (bit|moment|while|min(?:ute)?))?[.!]?$/xi
)
{
$stats{shutup}++;
my ( $num, $unit, $word ) = ( $1, lc $2, lc $3 );
if ($operator) {
my $target = 0;
unless ( $num or $word ) {
$num = 60 * 60; # by default, shut up for one hour
}
if ($num) {
$target += $num if not $unit or $unit eq 's';
$target += $num * 60 if $unit eq 'm';
$target += $num * 60 * 60 if $unit eq 'h';
$target += $num * 60 * 60 * 24 if $unit eq 'd';
Report
"Shutting up in $chl at ${who}'s request for $target seconds";
&say( $chl => "Okay $bag{who}. I'll be back later" );
&talking( $chl, time + $target );
} elsif ($word) {
$target += 60 if $word eq 'min' or $word eq 'minute';
$target += 30 + int( rand(60) ) if $word eq 'moment';
$target += 4 * 60 + int( rand( 4 * 60 ) ) if $word eq 'bit';
$target += 30 * 60 + int( rand( 30 * 60 ) ) if $word eq 'while';
Report
"Shutting up in $chl at ${who}'s request for $target seconds";
&say( $chl => "Okay $bag{who}. I'll be back later" );
&talking( $chl, time + $target );
}
} else {
&say( $chl => "Okay, $bag{who} - be back in a bit!" );
&talking( $chl, time + &config("timeout") );
}
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^unshut up\W*$|^come back\W*$/i )
{
&say( $chl => "\\o/" );
&talking( $chl, -1 );
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^(join|part) (#\S+)(?: (.*))?/i )
{
my ( $cmd, $dst, $msg ) = ( $1, $2, $3 );
unless ($dst) {
&say( $chl => "$bag{who}: $cmd what channel?" );
return;
}
$irc->yield( $cmd => $msg ? ( $dst, $msg ) : $dst );
&say( $chl => "$bag{who}: ${cmd}ing $dst" );
Report "${cmd}ing $dst at ${who}'s request";
} elsif ( $addressed and $operator and lc $bag{msg} eq 'list ignored' ) {
&say_long(
$chl => "Currently ignored:",
&make_list( sort keys %{$config->{ignore}} )
);
} elsif ( $addressed
and $operator
and $bag{msg} =~ /^([\w']+) has (\d+) syllables?\W*$/i )
{
$config->{sylcheat}{lc $1} = $2;
&save;
&say( $chl => "Okay, $bag{who}. Cheat sheet updated." );
} elsif ( $addressed and $operator and $bag{msg} =~ /^(un)?ignore (\S+)/i )
{
Report "$bag{who} is $1ignoring $2";
if ($1) {
delete $config->{ignore}{lc $2};
} else {
$config->{ignore}{lc $2} = 1;
}
&save;
&say( $chl => "Okay, $bag{who}. Ignore list updated." );
} elsif ( $addressed and $operator and $bag{msg} =~ /^(un)?exclude (\S+)/i )
{
Report "$bag{who} is $1excluding $2";
if ($1) {
delete $config->{exclude}{lc $2};
} else {
$config->{exclude}{lc $2} = 1;
}
&save;
&say( $chl => "Okay, $bag{who}. Exclude list updated." );
} elsif ( $addressed and $operator and $bag{msg} =~ /^(un)?protect (.+)/i )
{
my ( $protect, $fact ) = ( ( $1 ? 0 : 1 ), $2 );
my $perm = ( $protect ? "read-only" : "editable" );
Report "$bag{who} is $1protecting $fact";
Log "$1protecting $fact";
if ( $fact =~ s/^\$// ) { # it's a variable!
unless ( exists $replacables{lc $fact} ) {
&say( $chl =>
"Sorry, $bag{who}, \$$fact isn't a valid variable." );
return;
}
$replacables{lc $fact}{perms} = $perm;
&sql(
'update bucket_vars set perms=? where name=?',
[ $perm, $fact ] );
} else {
&sql( 'update bucket_facts set protected=? where fact=?',
[ $protect, $fact ] );
}
&say( $chl => "Okay, $bag{who}, updated the protection bit." );
} elsif ( $addressed and $bag{msg} =~ /^undo last(?: (#\S+))?/ ) {
Log "$bag{who} called undo:";
my $uchannel = $1 || $chl;
my $undo = $undo{$uchannel};
unless ( $operator or $undo->[1] eq $bag{who} ) {
&say( $chl => "Sorry, $bag{who}, you can't undo that." );
return;
}
Log Dumper $undo;
if ( $undo->[0] eq 'delete' ) {
&sql(
'delete from bucket_facts where id=? limit 1',
[ $undo->[2] ],
);
Report "$bag{who} called undo: deleted $undo->[3].";
&say( $chl => "Okay, $bag{who}, deleted $undo->[3]." );
delete $undo{$uchannel};
} elsif ( $undo->[0] eq 'insert' ) {
if ( $undo->[2] and ref $undo->[2] eq 'ARRAY' ) {
foreach my $entry ( @{$undo->[2]} ) {
my %old = %$entry;
$old{RE} = 0 unless $old{RE};
$old{protected} = 0 unless $old{protected};
&sql(
'insert bucket_facts
(fact, verb, tidbit, protected, RE, mood, chance)
values(?, ?, ?, ?, ?, ?, ?)',
[ @old{qw/fact verb tidbit protected RE mood chance/} ],
);
}
Report "$bag{who} called undo: undeleted $undo->[3].";
&say( $chl => "Okay, $bag{who}, undeleted $undo->[3]." );
} elsif ( $undo->[2] and ref $undo->[2] eq 'HASH' ) {
my %old = %{$undo->[2]};
$old{RE} = 0 unless $old{RE};
$old{protected} = 0 unless $old{protected};
&sql(
'insert bucket_facts
(id, fact, verb, tidbit, protected, RE, mood, chance)
values(?, ?, ?, ?, ?, ?, ?, ?)',
[ @old{qw/id fact verb tidbit protected RE mood chance/} ],
);
Report "$bag{who} called undo:",
"unforgot $old{fact} $old{verb} $old{tidbit}.";
&say( $chl =>
"Okay, $bag{who}, unforgot $old{fact} $old{verb} $old{tidbit}."
);
} else {
&say( $chl =>
"Sorry, $bag{who}, that's an invalid undo structure. "
. "Please tell Zigdon, or report the command used at "
. "https://github.com/zigdon/xkcd-Bucket/issues/new" );
}
} elsif ( $undo->[0] eq 'edit' ) {
if ( $undo->[2] and ref $undo->[2] eq 'ARRAY' ) {
foreach my $entry ( @{$undo->[2]} ) {
if ( $entry->[0] eq 'update' ) {
&sql(
'update bucket_facts set verb=?, tidbit=?
where id=? limit 1',
[ $entry->[2], $entry->[3], $entry->[1] ],
);
} elsif ( $entry->[0] eq 'insert' ) {
my %old = %{$entry->[1]};
$old{RE} = 0 unless $old{RE};
$old{protected} = 0 unless $old{protected};
&sql(
'insert bucket_facts
(fact, verb, tidbit, protected, RE, mood, chance)
values(?, ?, ?, ?, ?, ?, ?)',
[
@old{
qw/fact verb tidbit protected RE mood chance/
}
],
);
}
}
Report "$bag{who} called undo: undone $undo->[3].";
&say( $chl => "Okay, $bag{who}, undone $undo->[3]." );
} else {
&say( $chl =>
"Sorry, $bag{who}, that's an invalid undo structure. "
. "Please tell Zigdon, or report the command used at "
. "https://github.com/zigdon/xkcd-Bucket/issues/new" );
}
delete $undo{$uchannel};
} else {
&say( $chl => "Sorry, $bag{who}, can't undo $undo->[0] yet" );
}
} elsif ( $addressed and $operator and $bag{msg} =~ /^merge (.*) => (.*)/ )
{
my ( $src, $dst ) = ( $1, $2 );
$stats{merge}++;
&sql(
'select id, verb, tidbit from bucket_facts where fact = ? limit 1',
[$src],
{
%bag,
cmd => "merge",
src => $src,
dst => $dst,
db_type => "SINGLE",
}
);
} elsif ( $addressed and $operator and $bag{msg} =~ /^alias (.*) => (.*)/ )
{
my ( $src, $dst ) = ( $1, $2 );
$stats{alias}++;
&sql(
'select id, verb, tidbit from bucket_facts where fact = ? limit 1',
[$src],
{
%bag,
cmd => "alias1",
src => $src,
dst => $dst,
db_type => "SINGLE",
}
);
} elsif ( $operator and $addressed and $bag{msg} =~ /^lookup #?(\d+)\W*$/ )
{
&sql(
'select id, fact, verb, tidbit from bucket_facts where id = ? ',
[$1],
{
%bag,
msg => undef,
cmd => "lookup",
id => $1,
addressed => 0,
editable => 0,
op => 0,
db_type => "SINGLE",
}
);
} elsif ( $operator
and $addressed
and $bag{msg} =~ /^forget (?:that|#(\d+))\W*$/ )
{
my $id = $1 || $stats{last_fact}{$chl};
unless ($id) {
&say( $chl => "Sorry, $bag{who}, forget what?" );
return;
}
&sql( 'select * from bucket_facts where id = ?',
[$id], {%bag, cmd => "forget", id => $id, db_type => "SINGLE",} );
} elsif ( $addressed and $bag{msg} =~ /^what was that\??$/i ) {
my $id = $stats{last_fact}{$chl};
unless ($id) {
&say( $chl => "Sorry, $bag{who}, I have no idea." );
return;
}
if ( $id =~ /^(\d+)$/ ) {
&sql( 'select * from bucket_facts where id = ?',
[$id],
{%bag, cmd => "report", id => $id, db_type => "SINGLE",} );
} else {
&say( $chl => "$bag{who}: that was $id" );
}
} elsif ( $addressed and $bag{msg} eq 'something random' ) {
&lookup(%bag);
} elsif ( $addressed and $bag{msg} eq 'stats' ) {
unless ( $stats{stats_cached} ) {
&say( $chl => "$bag{who}: Hold on, I'm still counting" );
return;
}
# get the last modified time for any bit of the code
my $mtime = ( stat($0) )[9];
my $dir = &config("plugin_dir");
if ( $dir and opendir( PLUGINS, $dir ) ) {
foreach my $file ( readdir(PLUGINS) ) {
next unless $file =~ /^plugin\.\w+\.pl$/;
if ( $mtime < ( stat("$dir/$file") )[9] ) {
$mtime = ( stat(_) )[9];
}
}
closedir PLUGINS;
}
my ( $mod, $modu ) = &round_time( time - $mtime );
my ( $awake, $units ) = &round_time( time - $stats{startup_time} );
my $reply;
$reply = sprintf "I've been awake since %s (about %d %s), ",
scalar localtime( $stats{startup_time} ),
$awake, $units;
if ( $awake != $mod or $units ne $modu ) {
if ( ( stat($0) )[9] < $stats{startup_time} ) {
$reply .= sprintf "and was last changed about %d %s ago. ",
$mod, $modu;
} else {
$reply .=
sprintf "and a newer version has been available for %d %s. ",
$mod, $modu;
}
} else {
$reply .= "and that was when I was last changed. ";
}
if ( $stats{learn} + $stats{edited} + $stats{deleted} ) {
$reply .= "Since waking up, I've ";
my @fact_stats;
push @fact_stats,
sprintf "learned %d new factoid%s",
$stats{learn}, &s( $stats{learn} )
if ( $stats{learn} );
push @fact_stats,
sprintf "updated %d factoid%s", $stats{edited},
&s( $stats{edited} )
if ( $stats{edited} );
push @fact_stats,
sprintf "forgot %d factoid%s",
$stats{deleted}, &s( $stats{deleted} )
if ( $stats{deleted} );
push @fact_stats, sprintf "found %d haiku", $stats{haiku}
if ( $stats{haiku} );
# strip out the string 'factoids' from all but the first entry
if ( @fact_stats > 1 ) {
s/ factoids?// foreach @fact_stats[ 1 .. $#fact_stats ];
}
if (@fact_stats) {
$reply .= &make_list(@fact_stats) . ". ";
} else {
$reply .= "haven't had a chance to do much!";
}
}
$reply .= sprintf "I know now a total of %s thing%s "
. "about %s subject%s. ",
&commify( $stats{rows} ), &s( $stats{rows} ),
&commify( $stats{triggers} ), &s( $stats{triggers} );
$reply .=
sprintf "I know of %s object%s" . " and am carrying %d of them. ",
&commify( $stats{items} ), &s( $stats{items} ), scalar @inventory;
if ( &talking($chl) == 0 ) {
$reply .= "I'm being quiet right now. ";
} elsif ( &talking($chl) > 0 ) {
$reply .=
sprintf "I'm being quiet right now, "
. "but I'll be back in about %s %s. ",
&round_time( &talking($chl) - time );
}
&say( $chl => $reply );
} elsif ( $operator and $addressed and $bag{msg} =~ /^stat (\w+)\??/ ) {
my $key = $1;
if ( $key eq 'keys' ) {
&say_long( $chl => "$bag{who}: valid keys are: "
. &make_list( sort keys %stats )
. "." );
} elsif ( exists $stats{$key} ) {
if ( ref $stats{$key} ) {
my $dump = Dumper( $stats{$key} );
$dump =~ s/[\s\n]+/ /g;
&say( $chl => "$bag{who}: $key: $dump." );
Log $dump;
} else {
&say( $chl => "$bag{who}: $key: $stats{$key}." );
}
} else {
&say( $chl =>
"Sorry, $bag{who}, I don't have statistics for '$key'." );
}
} elsif ( $operator and $addressed and $bag{msg} eq 'restart' ) {
Report "Restarting at ${who}'s request";
Log "Restarting at ${who}'s request";
&say( $chl => "Okay, $bag{who}, I'll be right back." );
$irc->yield( quit => "OHSHI--" );
} elsif ( $operator
and $addressed
and $bag{msg} =~ /^set(?: (\w+) (.*)|$)/ )
{
my ( $key, $val ) = ( $1, $2 );
unless ( $key and exists $config_keys{$key} ) {
&say_long( $chl => "$bag{who}: Valid keys are: "
. &make_list( sort keys %config_keys ) );
return;
}
if ( $config_keys{$key}[0] eq 'p' and $val =~ /^(\d+)%?$/ ) {
$config->{$key} = $1;
} elsif ( $config_keys{$key}[0] eq 'i' and $val =~ /^(\d+)$/ ) {
$config->{$key} = $1;
} elsif ( $config_keys{$key}[0] eq 's' ) {
$val =~ s/^\s+|\s+$//g;
$config->{$key} = $val;
} elsif ( $config_keys{$key}[0] eq 'b' and $val =~ /^(true|false)$/ ) {
$config->{$key} = $val eq 'true';
} elsif ( $config_keys{$key}[0] eq 'f' and length $val ) {
if ( -f $val ) {
&say( $chl => "Sorry, $bag{who}, $val already exists." );
return;
} else {
$config->{$key} = $val;
}
} else {
&say(
$chl => "Sorry, $bag{who}, that's an invalid value for $key." );
return;
}