-
Notifications
You must be signed in to change notification settings - Fork 0
/
gretell.pl
executable file
·1046 lines (866 loc) · 26.6 KB
/
gretell.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
#
# ===========================================================================
# Copyright (C) 2007 Marc H. Thoben
# Copyright (C) 2008 Darshan Shaligram
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
#
use strict;
use warnings;
use POSIX qw(setsid); # For daemonization.
use File::Find;
use File::Glob qw/:globally :nocase/;
my $nickname = 'Gretell';
my $ircname = 'Gretell the Crawl Bot';
my $ircserver = 'irc.libera.chat';
# my $ircserver = 'irc.freenode.net';
# my $ircserver = 'barjavel.freenode.net';
#my $ircserver = 'kornbluth.freenode.net';
# my $ircserver = 'bartol.freenode.net';
# my $ircserver = 'pratchett.freenode.net';
#my $ircserver = 'orwell.freenode.net';
my $port = 8001;
my @CHANNELS = ('#crawl', '#crawl-dev', '#crawl-sequell');
my $ANNOUNCE_CHAN = '#crawl';
my $DEV_CHAN = '#crawl-dev';
my @stonefiles = ('/var/lib/dgamelaunch/crawl-svn/saves/milestones',
'/var/lib/dgamelaunch/crawl-svn/saves/milestones-sprint',
'/srv/dgamelaunch/crawl-0.26/saves/milestones',
'/srv/dgamelaunch/crawl-0.25/saves/milestones',
'/srv/dgamelaunch/crawl-0.24/saves/milestones');
my @logfiles = ('/var/lib/dgamelaunch/crawl-svn/saves/logfile',
'/var/lib/dgamelaunch/crawl-svn/saves/logfile-sprint',
'/srv/dgamelaunch/crawl-0.26/saves/logfile',
'/srv/dgamelaunch/crawl-0.25/saves/logfile',
'/srv/dgamelaunch/crawl-0.24/saves/logfile');
my @announcefiles = ('/home/services/crawl/source/announcements.log');
my $DGL_INPROGRESS_DIR = '/var/lib/dgamelaunch/dgldir/inprogress';
my $DGL_TTYREC_DIR = '/var/lib/dgamelaunch/dgldir/ttyrec';
my $INACTIVE_IDLE_CEILING_SECONDS = 300;
my $MAX_LENGTH = 420;
# The largest message to paginate in PM.
my $MAX_PAGINATE_LENGTH = 2000;
my $SERVER_BASE_URL = 'http://crawl.develz.org';
my $MORGUE_BASE_URL = "$SERVER_BASE_URL/morgues";
my %GAME_TYPE_NAMES = (zot => 'ZotDef',
spr => 'Sprint');
my %COMMANDS = (
'@whereis' => \&cmd_whereis,
'@dump' => \&cmd_dump,
'!cdo' => \&cmd_players,
'@players' => \&cmd_players,
'@??' => \&cmd_trunk_monsterinfo,
'@?' => \&cmd_monsterinfo,
'@0.32?' => \&cmd_monsterinfo_0_32,
'@0.31?' => \&cmd_monsterinfo_0_31,
'@0.30?' => \&cmd_monsterinfo_0_30,
'@0.29?' => \&cmd_monsterinfo_0_29,
'@0.28?' => \&cmd_monsterinfo_0_28,
'@0.27?' => \&cmd_monsterinfo_0_27,
'@0.26?' => \&cmd_monsterinfo_0_26,
'@0.25?' => \&cmd_monsterinfo_0_25,
'@0.24?' => \&cmd_monsterinfo_0_24,
'@0.23?' => \&cmd_monsterinfo_0_23,
'@0.22?' => \&cmd_monsterinfo_0_22,
'@0.21?' => \&cmd_monsterinfo_0_21,
'@0.20?' => \&cmd_monsterinfo_0_20,
'@0.19?' => \&cmd_monsterinfo_0_19,
'@0.10?' => \&cmd_monsterinfo_0_10,
);
## Daemonify. http://www.webreference.com/perl/tutorial/9/3.html
#umask 0;
#defined(my $pid = fork) or die "Unable to fork: $!";
#exit if $pid;
#setsid or die "Unable to start a new session: $!";
## Done daemonifying.
my @stonehandles = open_handles(@stonefiles);
my @loghandles = open_handles(@logfiles);
my @announcehandles = open_handles(@announcefiles);
my $BOT = Gretell->new(nick => $nickname,
server => $ircserver,
port => $port,
ircname => $ircname,
channels => [ @CHANNELS ])
or die "Unable to instantiate Gretell\n";
$BOT->run();
exit 0;
sub open_handles
{
my (@files) = @_;
my @handles;
for my $file (@files) {
open my $handle, '<', $file or do {
warn "Unable to open $file for reading: $!";
next;
};
seek($handle, 0, 2); # EOF
push @handles, [ $file, $handle, tell($handle) ];
}
return @handles;
}
sub game_place_branch($)
{
my $g = shift;
my $place = $$g{place};
if ($place =~ /:/) {
($place) = $place =~ /(.*):/;
}
$place
}
sub milestone_is_uniq($) {
my $g = shift;
my $type = $$g{type} || '';
return grep($type eq $_, qw/uniq uniq.ban uniq.pac uniq.ens uniq.slime/);
}
sub milestone_is_ghost($) {
my $g = shift;
my $type = $$g{type} || '';
return grep($type eq $_, qw/ghost ghost.ban ghost.pac/);
}
sub game_type($) {
my $g = shift;
my ($type) = ($$g{lv} || '') =~ /-(.*)/;
$type = lc(substr($type, 0, 3)) if $type;
$type
}
sub game_type_name($) {
my $type = game_type(shift);
$type && $GAME_TYPE_NAMES{$type}
}
sub game_is_sprint($) {
(game_type(shift) || '') eq 'spr'
}
sub game_is_zotdef($) {
(game_type(shift) || '') eq 'zot'
}
sub newsworthy
{
my $g = shift;
return 0;
# Milestone type, empty if this is not a milestone.
my $type = $$g{type} || '';
my $place_branch = game_place_branch($g);
# Only announce certain milestone types
return 0 if !grep($_ eq $type, 'uniq', 'uniq.ban', 'uniq.pac', 'uniq.ens',
'uniq.slime', 'br.enter', 'br.end',
'rune', 'zig', 'zig.enter', 'zig.exit',
'abyss.enter', 'abyss.exit', 'orb',
'ghost', 'ghost.ban', 'ghost.pac', '');
return 0
if ($type eq 'br.enter')
&& !grep($place_branch eq $_, qw/Zot Tomb Hell Coc Geh Dis Tar Pan
WizLab/);
return 0
if ($type eq 'br.end')
&& !grep($place_branch eq $_, qw/Zot Tomb Coc Geh Dis Tar Vaults Shoals
Snake Spider Swamp Slime Abyss Elf/);
if ($type eq 'zig') {
my ($depth) = ($$g{milestone} || '') =~ /reached level (\d+)/;
return 0 if $depth < 20;
}
return 0
if milestone_is_uniq($g)
&& !grep($place_branch eq $_, qw/Abyss Zot Coc Geh Dis Tar Tomb Hell
Slime Pan/);
return 0
if milestone_is_ghost($g)
&& !grep($place_branch eq $_, qw/Abyss Zot Coc Geh Dis Tar Tomb Hell
Slime Pan Depths Vaults Crypt/);
# Suppress all Sprint/ZotDef events other than wins.
return 0 if (game_type($g) && ($$g{ktyp} || '') ne 'winning');
# Suppress deaths with less than 5000 score.
return 0 if (!$$g{milestone} && $g->{sc} < 5000);
return 1;
}
sub devworthy
{
my $g = shift;
my $type = $$g{type} || '';
return $type eq 'crash';
}
# Given an xlogfile hash, returns the place where the event occurred.
sub xlog_place
{
my $g = shift;
my $game_type_place_qualifier = game_type_name($g);
my $place = $$g{oplace} || $$g{place};
if ($game_type_place_qualifier) {
$place = "$place ($game_type_place_qualifier)";
}
return $place;
}
sub raw_message_post {
my ($m, $output) = @_;
# Handle emotes (/me does foo)
if ($output =~ m{^/me }) {
$output =~ s{^/me }{};
$BOT->emote(channel => $$m{channel},
who => $$m{who},
body => $output);
return;
}
$BOT->say(channel => $$m{channel},
who => $$m{who},
body => $output);
}
sub report_milestone
{
my $game_ref = shift;
my $channel = shift;
my $place = xlog_place($game_ref);
my $placestring = " ($place)";
my $milestone = $$game_ref{milestone} || '';
if ($milestone eq "escaped from the Abyss!"
|| $milestone eq "reached level 27 of the Dungeon.")
{
$placestring = "";
}
post_message({ channel => $channel },
sprintf("%s (L%s %s) %s%s",
$game_ref->{name},
$game_ref->{xl},
$game_ref->{char},
$milestone,
$placestring));
}
sub parse_milestone_file
{
my $href = shift;
my $stonehandle = $href->[1];
$href->[2] = tell($stonehandle);
my $line = <$stonehandle>;
# If the line isn't complete, seek back to where we were and wait for it
# to be done.
if (!defined($line) || $line !~ /\n$/) {
seek($stonehandle, $href->[2], 0);
return;
}
$href->[2] = tell($stonehandle);
return unless defined($line) && $line =~ /\S/;
my $game_ref = demunge_xlogline($line);
if ($game_ref) {
report_milestone($game_ref, $ANNOUNCE_CHAN) if newsworthy($game_ref);
report_milestone($game_ref, $DEV_CHAN) if devworthy($game_ref);
}
seek($stonehandle, $href->[2], 0);
}
sub parse_log_file
{
my $href = shift;
my $loghandle = $href->[1];
$href->[2] = tell($loghandle);
my $line = <$loghandle>;
if (!defined($line) || $line !~ /\n$/) {
seek($loghandle, $href->[2], 0);
return;
}
$href->[2] = tell($loghandle);
return unless defined($line) && $line =~ /\S/;
my $game_ref = demunge_xlogline($line);
if ($game_ref && newsworthy($game_ref)) {
my $output = pretty_print($game_ref);
$output =~ s/ on \d{4}-\d{2}-\d{2}//;
post_message({ channel => $ANNOUNCE_CHAN }, $output);
}
seek($loghandle, $href->[2], 0);
}
sub parse_announce_file
{
my $href = shift;
my $announcehandle = $href->[1];
$href->[2] = tell($announcehandle);
my $line = <$announcehandle>;
if (!defined($line) || $line !~ /\n$/) {
seek($announcehandle, $href->[2], 0);
return;
}
$href->[2] = tell($announcehandle);
return unless defined($line) && $line =~ /\S/;
post_message({ channel => $ANNOUNCE_CHAN }, $line);
post_message({ channel => $DEV_CHAN }, $line);
seek($announcehandle, $href->[2], 0);
}
sub check_stonefiles
{
for my $stoneh (@stonehandles) {
parse_milestone_file($stoneh);
}
}
sub check_logfiles
{
for my $logh (@loghandles) {
parse_log_file($logh);
}
}
sub check_announcefiles
{
for my $announceh (@announcehandles) {
parse_announce_file($announceh);
}
}
sub check_files
{
check_stonefiles();
check_logfiles();
check_announcefiles();
}
sub process_message {
my $m = shift;
my ($who, $channel, $verbatim) = @$m{qw/who channel body/};
return unless $who && $channel && $verbatim;
my $nick = get_nick($who) or return;
my $command = get_command($verbatim) or return;
process_command($m, $command, $nick, $verbatim);
undef;
}
sub sanitise_nick {
my $nick = shift;
return unless $nick;
$nick =~ tr/a-zA-Z_0-9-//cd;
return $nick;
}
sub get_nick {
my $nick = shift;
return $nick? sanitise_nick($nick) : undef;
}
sub get_command {
my $verbatim_input = shift;
my ($command) = $verbatim_input =~ /^(\S+)/;
return $command;
}
sub post_message($$) {
my ($m, $output) = @_;
my $private = $$m{channel} eq 'msg';
# Hard limit on output size, regardless of whether this is PM or
# public:
$output = substr($output, 0, $MAX_PAGINATE_LENGTH) . "..."
if length($output) > $MAX_PAGINATE_LENGTH;
# On PM, send multiple lines of output instead of truncating.
if ($private) {
my $length = length($output);
my $PAGE = $MAX_LENGTH;
for (my $start = 0; $start < $length; $start += $PAGE) {
if ($length - $start > $PAGE) {
my $spcpos = rindex($output, ' ', $start + $PAGE - 1);
if ($spcpos != -1 && $spcpos > $start) {
raw_message_post($m, substr($output, $start, $spcpos - $start));
$start = $spcpos + 1 - $PAGE;
next;
}
}
raw_message_post($m, substr($output, $start, $PAGE));
}
}
else {
$output = substr($output, 0, $MAX_LENGTH) . "..."
if length($output) > $MAX_LENGTH;
raw_message_post($m, $output);
}
}
#######################################################################
# Commands
sub process_command($$$$) {
my ($m, $command, $nick, $verbatim) = @_;
if (substr($command, 0, 3) eq '@??')
{
$command = "@??";
}
elsif (substr($command, 0, 2) eq '@?')
{
$command = "@?";
}
# Commands for previous versions.
elsif ($command =~ /^(\@0\.\d+\?)/)
{
$command = $1;
}
my $proc = $COMMANDS{$command} or return;
&$proc($m, $nick, $verbatim);
}
sub find_named_nick {
my ($default, $command) = @_;
$default = sanitise_nick($default);
my $named = (split ' ', $command)[1] or return $default;
return sanitise_nick($named) || $default;
}
sub make_shellsafe($) {
my $thing = shift;
# Toss out everything that might confuse the shell. Spaces are ok,
# quotes are not.
$thing =~ tr/a-zA-Z0-9_+ -//cd;
return $thing;
}
sub cmd_trunk_monsterinfo {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 3);
my $monster_info = qx/monster-trunk \Q$monster_name\E/;
post_message($m, $monster_info);
}
sub cmd_monsterinfo {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 2);
my $monster_info = `monster \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_32 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.32 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_31 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.31 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_30 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.30 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_29 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.29 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_28 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.28 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_27 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.27 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_26 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.26 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_25 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.25 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_24 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.24 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_23 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.23 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_22 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.22 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_21 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.21 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_20 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.20 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_19 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.19 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub cmd_monsterinfo_0_10 {
my ($m, $nick, $verbatim) = @_;
my $monster_name = substr($verbatim, 6);
my $monster_info = `monster-0.10 \Q$monster_name\E`;
post_message($m, $monster_info);
}
sub ttyrec_idle_time_seconds($) {
my $filename = shift;
my ($player, $ttyrec) = $filename =~ m{.*/([^:]+):(.*)$};
$filename = "$DGL_TTYREC_DIR/$player/$ttyrec" if $player && $ttyrec;
my $modtime = (stat $filename)[9];
return time() - $modtime;
}
sub active_player_hash($$) {
my ($player_name, $ttyrec_filename) = @_;
return { player_name => $player_name,
idle_seconds => ttyrec_idle_time_seconds($ttyrec_filename)
};
}
sub find_active_players {
my @player_where_list;
find(sub {
my $filename = $File::Find::name;
if (-f $filename && $filename =~ /\.ttyrec$/) {
my ($game_version, $player_name) =
$filename =~ m{.*/([^/]+)/(.*?):};
if ($game_version && $player_name) {
push @player_where_list,
active_player_hash($player_name, $filename);
}
}
},
$DGL_INPROGRESS_DIR);
return @player_where_list;
}
sub compare_player_where_infos($$) {
my ($wa, $wb) = @_;
my $axl = $$wa{where}{xl} || 0;
my $bxl = $$wb{where}{xl} || 0;
return $axl != $bxl? $bxl - $axl :
($$wa{player_name} cmp $$wb{player_name});
}
sub sort_active_player_where_infos(@) {
return sort { compare_player_where_infos($a, $b) } @_;
}
sub player_where_stats($) {
my $wr = shift;
return '' unless $wr;
my $place = xlog_place($wr) || '';
my $xl = $$wr{xl} || '';
my $turn = $$wr{turn} || '';
return "L$xl @ $place, T:$turn";
}
sub player_where_brief($) {
my $wref = shift;
my $extended = player_where_stats($$wref{where}) || '';
$extended = " ($extended)" if $extended;
return "$$wref{player_name}$extended";
}
sub get_active_players_line($) {
my $check_not_idle = shift;
my @active_players = find_active_players();
# If the command wanted active players, toss the idle layabouts.
if ($check_not_idle) {
@active_players =
grep($$_{idle_seconds} < $INACTIVE_IDLE_CEILING_SECONDS,
@active_players);
}
for my $r_player_info_hash (@active_players) {
player_whereis_add_info($r_player_info_hash);
}
my @sorted_players = sort_active_player_where_infos(@active_players);
my $message = join(", ", map(player_where_brief($_), @sorted_players));
unless ($message) {
my $qualifier = $check_not_idle? "active " : "";
$message = "No ${qualifier}players.";
}
return $message;
}
sub cmd_players {
my ($m, $nick, $verbatim) = @_;
my $check_not_idle = $verbatim =~ /-a/;
my $message = get_active_players_line($check_not_idle);
post_message($m, $message);
}
sub player_whereis_file($) {
my $realnick = shift;
my @crawldirs = glob('/var/lib/dgamelaunch/crawl-*');
my @whereis_path = map { "$_/morgue" } @crawldirs;
my $where_file;
my $final_where;
for my $where_path (@whereis_path) {
my @where_dir = glob("$where_path/$realnick*");
my @where_files;
if (@where_dir) {
@where_files = glob("$where_dir[0]/$realnick.where*");
}
if (@where_files) {
$where_file = $where_files[0];
if (defined($final_where) && length($final_where) > 0) {
if ((stat($final_where))[9] < (stat($where_file))[9]) {
$final_where = $where_file;
}
}
else {
$final_where = $where_file;
}
}
}
undef $final_where unless defined($final_where) && length($final_where) > 0;
return $final_where;
}
sub player_whereis_line($) {
my $realnick = shift;
my $where_file = player_whereis_file($realnick);
return undef unless $where_file;
open my $in, '<', $where_file or return undef;
chomp( my $where = <$in> );
close $in;
return $where;
}
sub player_whereis_hash($) {
my $nick = shift;
my $line = player_whereis_line($nick);
return $line ? demunge_xlogline($line) : undef;
}
sub player_whereis_add_info($) {
my $phash = shift;
$$phash{where} = player_whereis_hash($$phash{player_name});
}
sub cmd_whereis {
my ($m, $nick, $verbatim) = @_;
# Get the nick to act on.
my $realnick = find_named_nick($nick, $verbatim);
my $where = player_whereis_hash($realnick);
unless ($where) {
post_message($m, "No where information for $realnick.");
return;
}
show_where_information($m, $where);
}
sub show_dump_file($$) {
my ($m, $whereis_file) = @_;
my ($gamedir, $player) =
$whereis_file =~ m{/(crawl-[\w.]+)[^/]*/morgue/(\w+)/+\w+[.]where};
my %GAME_WEB_MAPPINGS =
( 'crawl-0.24' => '0.24',
'crawl-0.25' => '0.25',
'crawl-0.26' => '0.26',
'crawl-anc' => 'ancient',
'crawl-svn' => 'trunk' );
my $dump_file = "/var/lib/dgamelaunch/$gamedir/morgue/$player/$player.txt";
unless (-f $dump_file) {
post_message($m, "Can't find character dump for $player.");
return;
}
my $web_morgue_dir = $GAME_WEB_MAPPINGS{$gamedir};
unless ($web_morgue_dir) {
post_message($m, "Can't find URL base for character dump.");
return;
}
post_message($m, "$MORGUE_BASE_URL/$web_morgue_dir/$player/$player.txt");
}
sub cmd_dump {
my ($m, $nick, $verbatim) = @_;
my $realnick = find_named_nick($nick, $verbatim);
my $whereis_file = player_whereis_file($realnick);
unless ($whereis_file) {
post_message($m, "No where information for $realnick.");
return;
}
show_dump_file($m, $whereis_file);
}
sub format_crawl_date {
my $date = shift;
return '' unless $date;
my ($year, $mon, $day) = $date =~ /(.{4})(.{2})(.{2})/;
return '' unless $year && $mon && $day;
$mon++;
return sprintf("%04d-%02d-%02d", $year, $mon, $day);
}
sub show_where_information($$) {
my ($m, $wref) = @_;
return unless $wref;
my %wref = %$wref;
my $place = xlog_place($wref);
my $preposition = index($place, ':') != -1? " on" : " in";
$place = "the $place" if $place =~ 'Abyss' || $place eq 'Temple';
$place = " $place";
my $punctuation = '.';
my $date = ' on ' . format_crawl_date($wref{time});
my $turn = " after $wref{turn} turns";
chop $turn if $wref{turn} == 1;
my $what = $wref{status};
my $msg;
if ($what eq 'active') {
$what = 'is currently';
$date = '';
}
elsif ($what eq 'won') {
$punctuation = '!';
$preposition = $place = '';
}
elsif ($what eq 'bailed out') {
$what = 'got out of the dungeon alive';
$preposition = $place = '';
}
$what = " $what";
my $god = $wref{god}? ", a worshipper of $wref{god}," : "";
unless ($msg) {
$msg = "$wref{name} the $wref{title} (L$wref{xl} $wref{char})" .
"$god$what$preposition$place$date$turn$punctuation";
}
post_message($m, $msg);
}
#######################################################################
# Imports
sub pretty_print
{
my $game_ref = shift;
my $loc_string = "";
my $place = xlog_place($game_ref);
if ((defined($game_ref->{ltyp}) && $game_ref->{ltyp} ne 'D') || $place !~ ':')
{
$loc_string = " in $place";
}
else
{
if ($game_ref->{br} eq 'blade' or $game_ref->{br} eq 'temple' or $game_ref->{br} eq 'hell')
{
$loc_string = " in $place";
}
else
{
$loc_string = " on $place";
}
}
$loc_string = "" # For escapes of the dungeon, so it doesn't print the loc
if $game_ref->{ktyp} eq 'winning' or $game_ref->{ktyp} eq 'leaving';
$game_ref->{end} =~ /^(\d{4})(\d{2})(\d{2})/;
my $death_date = " on " . $1 . "-" . sprintf("%02d", $2 + 1) . "-" . $3;
my $deathmsg = $game_ref->{vmsg} || $game_ref->{tmsg};
$deathmsg =~ s/!$//;
sprintf '%s the %s (L%d %s)%s, %s%s%s, with %d point%s after %d turn%s and %s.',
$game_ref->{name},
$game_ref->{title},
$game_ref->{xl},
$game_ref->{char},
exists $game_ref->{god} ? ", worshipper of $game_ref->{god}" : '',
$deathmsg,
$loc_string,
$death_date,
$game_ref->{sc},
$game_ref->{sc} == 1 ? '' : 's',
$game_ref->{turn},
$game_ref->{turn} == 1 ? '' : 's',
serialize_time($game_ref->{dur})
}
sub demunge_xlogline
{
my $line = shift;
return {} if $line eq '';
my %game;
chomp $line;
die "Unable to handle internal newlines." if $line =~ y/\n//;
$line =~ s/::/\n\n/g;
while ($line =~ /\G(\w+)=([^:]*)(?::(?=[^:])|$)/cg)
{
my ($key, $value) = ($1, $2);
$value =~ s/\n\n/:/g;
$game{$key} = $value;
}
if (!defined(pos($line)) || pos($line) != length($line))
{
my $pos = defined(pos($line)) ? "Problem started at position " . pos($line) . "." : "Regex doesn't match.";
return undef;
}
return \%game;
}
sub serialize_time
{
my $seconds = int shift;
my $long = shift;
if (not $long)
{
my $hours = int($seconds/3600);
$seconds %= 3600;
my $minutes = int($seconds/60);
$seconds %= 60;
return sprintf "%d:%02d:%02d", $hours, $minutes, $seconds;
}
my $minutes = int($seconds / 60);
$seconds %= 60;
my $hours = int($minutes / 60);
$minutes %= 60;
my $days = int($hours / 24);
$hours %= 24;
my $weeks = int($days / 7);
$days %= 7;
my $years = int($weeks / 52);
$weeks %= 52;
my @fields;
push @fields, "about ${years}y" if $years;
push @fields, "${weeks}w" if $weeks;
push @fields, "${days}d" if $days;
push @fields, "${hours}h" if $hours;
push @fields, "${minutes}m" if $minutes;
push @fields, "${seconds}s" if $seconds;
return join ' ', @fields if @fields;
return '0s';
}
package Gretell;
use base 'Bot::BasicBot';
sub connected {
my $self = shift;
open(my $handle, '<', '.password') or warn "Unable to read .password: $!";
my $password = <$handle>;
chomp $password;
$self->say(channel => 'msg',
who => 'nickserv',
body => "identify $password");
return undef;
}
sub said {
my ($self, $m) = @_;
main::process_message($m);
return undef;
}