This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlogbot-irc
executable file
·691 lines (591 loc) · 23.4 KB
/
logbot-irc
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
#!/usr/bin/perl
# bare-bones irc logging bot
# saves events as json files for processing
use local::lib;
use v5.10;
use strict;
use warnings;
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
BEGIN { $ENV{TZ} = 'UTC' }
use Data::Dumper qw( Dumper );
use Encode qw( decode );
use Encode::Guess;
use Errno qw( ETIMEDOUT EWOULDBLOCK );
use IO::Socket::INET ();
use IO::Socket::SSL qw( $SSL_ERROR );
use IO::Socket::Timeout ();
use List::MoreUtils qw( natatime );
use List::Util qw( any min );
use LogBot::Config qw( find_config load_config reload_config save_config );
use LogBot::JobQueue;
use LogBot::MemCache ();
use LogBot::Util qw( file_for logbot_init normalise_channel slurp source_to_nick squash_error timestamp touch );
use Mojo::Log ();
use Readonly;
use Time::HiRes ();
use Try::Tiny qw( catch try );
# globals
my ($config, $state, $connection, $job_queue, $log, $memcache);
#
# read message from irc server, or undef if no message ready or disconnected
sub irc_read {
return undef unless $connection;
my $message = $connection->getline();
# timeout
if (!$message && (0 + $! == ETIMEDOUT || 0 + $! == EWOULDBLOCK)) {
return undef;
}
# disconnect
if (!defined $message) {
undef $connection;
return undef;
}
$message =~ s/^\@\S+ +//; # strip tags
$message =~ s/[\r\n]+$//; # strip eol
if (substr($message, 0, 1) ne ':') { # add source
$message = ':- ' . $message;
}
# print all server messages (except ping/pong to reduce noise)
say timestamp(), ' << ', $message
if ($message !~ /^:\S+ P[IO]NG /) || $ENV{DEBUG};
return $message;
}
# send raw irc command to server
sub irc_send {
my ($message) = @_;
# print all client messages (except ping/pong to reduce noise)
say timestamp(), ' >> ', $message
if ($message !~ /^P[IO]NG /) || $ENV{DEBUG};
$connection->print($message . "\r\n");
}
# send private message
sub irc_send_message {
my ($target, $message) = @_;
irc_send('PRIVMSG ' . $target . ' :' . $message);
}
# connect to irc server
sub irc_connect {
my $irc_host = $config->{irc}->{host};
my $ssl = $irc_host =~ s/^ssl:// || $irc_host =~ /:6697$/;
say timestamp(), ' -- connecting to irc on ', $irc_host, ($ssl ? ' (ssl)' : '');
if ($ssl) {
$connection = IO::Socket::SSL->new($irc_host)
or die "connection to $irc_host failed: $! $SSL_ERROR\n";
} else {
$connection = IO::Socket::INET->new($irc_host)
or die "connection to $irc_host failed: $@\n";
}
IO::Socket::Timeout->enable_timeouts_on($connection);
$connection->read_timeout(0.5);
# login
irc_send('USER ' . $config->{irc}->{nick} . ' 0 * :' . $config->{irc}->{real_name});
irc_send('NICK ' . $config->{irc}->{nick});
# wait for connect (end of motd)
while (1) {
my $message = irc_read();
die 'Disconnected' . ($ssl ? '' : ' (possible SSL mismatch)') . "\n" unless $connection;
exit if $state->{quit};
next unless $message;
if ($message =~ /^:\S+ PING (.+)/) {
irc_send('PONG ' . $1);
next;
}
next unless $message =~ /^:(\S+) (\d+) /;
my ($server, $code) = ($1, $2);
die 'Nick ' . $config->{irc}->{nick} . " in use\n" if $code eq '433'; # ERR_NICKNAMEINUSE
if ($code eq '376') { # RPL_ENDOFMOTD
$state->{server} = $server;
last;
}
}
# identify
if ($config->{irc}->{password}) {
irc_send_message('NickServ', 'identify ' . $config->{irc}->{password});
}
}
sub block_invite {
my ($source, $channel) = @_;
my $who = source_to_nick($source);
foreach my $blocked (@{ $config->{blocked} }) {
if (substr($blocked, 0, 1) eq '#') {
return 1 if $blocked eq $channel;
} else {
my $umask = quotemeta(lc($blocked));
$umask =~ s/\\\*/[\x01-\xFF]{0,}/g;
$umask =~ s/\\\?/[\x01-\xFF]{1,1}/g;
return 1 if lc($source) =~ /^$umask$/;
}
}
return 0;
}
sub init_logging {
$log = Mojo::Log->new(
path => ($config->{_derived}->{is_dev} ? 'log' : '/var/log/logbot') . '/irc_' . $config->{name} . '.log',
level => 'info',
);
}
#
# publish event to processing queue
sub publish {
my ($type, $nick, $channel, $text) = @_;
return unless exists $config->{channels}->{$channel};
return if $config->{channels}->{$channel}->{no_logs};
try {
# decode
my $utf8 = guess_encoding($text, 'utf8');
$text = $utf8 ? decode('utf8', $text) : decode('cp1252', $text);
# strip colours, formatting
$text =~ s/\x03(?:,\d{1,2}|\d{1,2}(?:,\d{1,2})?)?//g; # mirc
$text =~ s/\x04[0-9a-fA-F]{0,6}//g; # rgb
$text =~ s/\x1B\[.*?[\x00-\x1F\x40-\x7E]//g; # ecma-84
$text =~ s/[\x02\x1f\x16\x1d\x11\x06]//g; # formatting
$text =~ s/\x0f//g; # cancellation
$job_queue->publish_job(
{
time => Time::HiRes::time(),
channel => $channel,
type => $type,
nick => $nick,
text => $text,
}
);
}
catch {
say timestamp(), ' !! ', squash_error($_);
$log->error(squash_error($_));
};
}
#
# init config
$config = load_config(find_config(shift))
// die "syntax: logbot-irc <config file> [--reload][--cyclelogs][--debug][--quit]\n";
# pid
my $pid_file = file_for($config, 'pid', 'logbot-irc');
my $pid = 0;
if (-e $pid_file) {
chomp($pid = slurp($pid_file));
$pid = 0 unless kill(0, $pid);
}
# commands / signals
if (@ARGV) {
if ($ARGV[0] eq '--reload') {
$pid || die 'logbot-irc (' . $config->{name} . ") is not running\n";
kill('HUP', $pid);
say "reload request sent to pid $pid";
} elsif ($ARGV[0] eq '--debug') {
$pid || die 'logbot-irc (' . $config->{name} . ") is not running\n";
kill('USR1', $pid);
say "debug-dump request sent to pid $pid";
} elsif ($ARGV[0] eq '--cyclelogs') {
$pid || die 'logbot-irc (' . $config->{name} . ") is not running\n";
kill('USR2', $pid);
say "cyclelogs request sent to pid $pid";
} elsif ($ARGV[0] eq '--quit') {
$pid || die 'logbot-irc (' . $config->{name} . ") is not running\n";
kill('INT', $pid);
say "quit request sent to pid $pid";
} else {
die "unrecognised parameter\n";
}
exit;
}
# init
STDOUT->autoflush(1);
$pid && die 'logbot-irc (' . $config->{name} . ") is already running\n";
logbot_init($config);
init_logging();
$job_queue = LogBot::JobQueue->new($config);
$memcache = LogBot::MemCache->new(binary => !$config->{_derived}->{is_dev});
# init signals and state
$SIG{HUP} = sub {
$state->{next_channel_reload} = time();
};
$SIG{USR1} = sub { $state->{dump} = 1 };
$SIG{USR2} = sub { $state->{relog} = 1 };
$SIG{INT} = sub { $state->{quit} = 1 };
$state->{pending_invites} = {};
# event loop
while (1) {
# connect/reconnect
while (!$connection) {
exit if $state->{quit};
try {
# try to connect
irc_connect();
# connected, setup initial state
$log->info('connected to ' . $config->{irc}->{host});
delete $state->{backoff};
$state->{next_channel_reload} = time();
$state->{next_topic_reload} = time() + 5 * 60;
$state->{next_ping} = time() + $config->{timing}->{initial_ping_delay};
$state->{pong_timeouts} = 0;
$state->{touch_file} = file_for($config, 'connected');
touch($state->{touch_file});
}
catch {
# connection failed, retry with backoff
say timestamp(), ' !! ', squash_error($_);
$log->error(squash_error($_));
undef($connection);
$state->{backoff} = min(($state->{backoff} // 1) * 2, $config->{timing}->{max_reconnect_interval});
say timestamp(), ' ** sleeping for ', $state->{backoff} if $ENV{DEBUG};
sleep($state->{backoff});
};
}
my $message = irc_read();
my $time = time();
# ping timer
if (exists $state->{pong_timeout} && $state->{pong_timeout} <= $time) {
$state->{pong_timeouts}++;
$state->{next_ping} = $time;
say timestamp(), ' !! PING timeout (', $state->{pong_timeouts}, ')';
$log->error('PING timeout (' . $state->{pong_timeouts} . ')');
# server not responding to our pings, reconnect
if ($state->{pong_timeouts} == $config->{timing}->{ping_timeout_attempts}) {
undef($connection);
next;
}
}
if (exists $state->{next_ping} && $state->{next_ping} <= $time) {
delete $state->{next_ping};
$state->{pong_timeout} = $time + $config->{timing}->{ping_timeout};
irc_send('PING :' . $state->{server});
}
# update stored channel topics
if (exists $state->{next_topic_reload} && $state->{next_topic_reload} <= $time) {
if (keys %{ $state->{channels_in} }) {
say timestamp(), ' -- refresh topics';
$state->{pending_topics} = [sort keys %{ $state->{channels_in} }];
$state->{next_pending_topics} = $time;
}
$state->{next_topic_reload} = time() + $config->{timing}->{topic_reload_interval};
}
if (exists $state->{next_pending_topics} && $state->{next_pending_topics} <= $time) {
irc_send('TOPIC ' . shift(@{ $state->{pending_topics} }));
if (@{ $state->{pending_topics} }) {
$state->{next_pending_topics} = time() + 1;
} else {
delete $state->{next_pending_topics};
delete $state->{pending_topics};
}
}
# trigger reconciliation of joined channels
if (exists $state->{next_channel_reload} && $state->{next_channel_reload} <= $time) {
say timestamp(), ' -- reload channel config';
delete $state->{next_channel_reload};
$state->{channels_reconcile} = {};
irc_send('WHOIS ' . $config->{irc}->{nick});
}
# dump config and state on SIGUSR1
if (delete $state->{dump}) {
print Dumper($config);
print Dumper($state);
}
# drop log handle on SIGUSR2
if (delete $state->{relog}) {
say timestamp(), ' -- cycling logs';
init_logging();
}
# quit cleanly when requested
if (delete $state->{quit}) {
say timestamp(), ' -- quit requested';
$log->info('quitting');
irc_send('QUIT');
last;
}
next unless $message;
# server initiated ping
if ($message =~ /^:\S+ PING (.+)/) {
irc_send('PONG ' . $1);
next;
}
# response to our ping
if ($message =~ /^:\S+ PONG /) {
delete $state->{pong_timeout};
$state->{pong_timeouts} = 0;
$state->{next_ping} = $time + $config->{timing}->{ping_interval};
touch($state->{touch_file});
next;
}
# invited
if ($message =~ /^:(\S+) INVITE \S+ :(#.+)/) {
my ($source, $channel) = ($1, normalise_channel($2));
my $who = source_to_nick($source);
$config = reload_config($config);
# honour invite blocklist
if (block_invite($source, $channel)) {
say timestamp(), ' -- ignoring invite to blocked ', $channel, ' from ', $source;
$log->info('ignoring invite to blocked ' . $channel . ' from ' . $source);
next;
}
# ignore no-op invites
if ($state->{channels_in}->{$channel}) {
say timestamp(), ' -- ignoring invite to already-in ', $channel, ' from ', $who;
$log->info('ignoring invite to already-in ' . $channel . ' from ' . $who);
next;
}
# can't log channels starting with underscores - used for internal web calls
if (substr($channel, 1, 1) eq '_') {
irc_send_message($who, 'unable to join ' . $channel . ': unable to log channels starting with underscore');
$log->info('rejecting invite to underscored ' . $channel . ' from ' . $who);
next;
}
# can't log channels starting with # - confuses channel normalisation
if (substr($channel, 1, 1) eq '#') {
irc_send_message($who, 'unable to join ' . $channel . ': unable to log channels starting with ##');
$log->info('rejecting invite to double-hashes ' . $channel . ' from ' . $who);
next;
}
# cooldown
my $cooldown_key = $config->{name} . $channel . ',' . $source;
if (my $last_request = $memcache->get($cooldown_key)) {
if ($time - $last_request < $config->{timing}->{invite_cooldown}) {
irc_send_message($who, "unable to join $channel: please wait longer before asking again");
$log->info("rejecting invite to $channel from $who: cooldown expires in "
. ($config->{timing}->{invite_cooldown} - ($time - $last_request))
. 's');
next;
}
}
$state->{pending_invites}->{$channel} = {
who => lc($who),
source => $source,
ops => [],
cooldown_key => $cooldown_key,
};
irc_send('JOIN ' . $channel);
next;
}
# joined
if ($message =~ /^:(\S+) JOIN :(#.+)/) {
my ($who, $channel) = (source_to_nick($1), normalise_channel($2));
next unless $who eq $config->{irc}->{nick};
# if we're joining due to an invite, check the requester is an op
if (exists $state->{pending_invites}->{$channel}) {
say timestamp(), ' ** found pending-invite for ', $channel if $ENV{DEBUG};
irc_send('NAMES ' . $channel);
next;
}
$state->{channels_in}->{$channel} = 1;
irc_send('MODE ' . $channel); # trigger mode to grab channel password
next;
}
# parted
if ($message =~ /^:(\S+) PART :(#.+)/) {
my ($who, $channel) = (source_to_nick($1), normalise_channel($2));
next unless $who eq $config->{irc}->{nick};
delete $state->{channels_in}->{$channel};
next;
}
# RPL_NAMREPLY (pending-invites)
if ($message =~ /^:\S+ 353 (\S+) . (#\S+) :(.+)$/) {
my ($who, $channel, $nicks) = ($1, normalise_channel($2), $3);
next unless $who eq $config->{irc}->{nick};
next unless exists $state->{pending_invites}->{$channel};
# ~owners, &admins, and @ops (note this excludes %half-ops)
foreach my $nick (split(' ', $nicks)) {
next unless $nick =~ s/^[~&@]//;
push @{ $state->{pending_invites}->{$channel}->{ops} }, lc($nick);
}
next;
}
# RPL_ENDOFNAMES (pending-invites)
if ($message =~ /^:\S+ 366 (\S+) (#\S+) :/) {
my ($who, $channel) = ($1, normalise_channel($2));
next unless $who eq $config->{irc}->{nick};
next unless exists $state->{pending_invites}->{$channel};
my $invite = delete $state->{pending_invites}->{$channel};
if ($ENV{DEBUG}) {
say timestamp(), ' ** ', $channel, ' invited by: ', $invite->{who};
say timestamp(), ' ** ', $channel, ' ops: ', join(' ', @{ $invite->{ops} });
}
if (any { $_ eq $invite->{who} } @{ $invite->{ops} }) {
say timestamp(), ' -- joined ', $channel, ' via invite from ', $invite->{source};
$log->info('joined ' . $channel . ' via invite from ' . $invite->{source});
$config->{channels}->{$channel}->{invite} = timestamp() . ' <' . $invite->{source} . '>';
delete $config->{channels}->{$channel}->{disabled};
delete $config->{channels}->{$channel}->{archived};
save_config($config);
$state->{channels_in}->{$channel} = 1;
irc_send('MODE ' . $channel); # trigger mode to grab channel password
my $url = $config->{url} . substr($channel, 1);
my $announcement = 'channel logging requested by ' . $invite->{who} . ': ' . $url;
irc_send_message($channel, $announcement);
publish(0, $config->{irc}->{nick}, $channel, $announcement);
} else {
say timestamp(), ' -- join ', $channel, ' rejected - non-op invite from ', $invite->{source};
$log->info('join ' . $channel . ' rejected - non-op invite from ' . $invite->{source});
irc_send('PART ' . $channel);
irc_send_message($invite->{who}, 'unable to join ' . $channel . ': you are not a channel op');
$memcache->set($invite->{cooldown_key} => $time);
}
next;
}
# join failed
# NOSUCHCHANNEL, TOOMANYCHANNELS NEEDMOREPARAMS, INVITEONLYCHAN, BANNEDFROMCHAN, BADCHANNELKEY
if ($message =~ /^:\S+ (403|405|461|471|473|474|475) (\S+) (#\S+) :(.+)/) {
my ($code, $who, $channel, $text) = ($1, normalise_channel($2), $3);
next unless $who eq $config->{irc}->{nick};
say timestamp(), ' -- join ', $channel, ' failed: ', $text;
$log->error('join ' . $channel . ' failed: ' . $text);
delete $state->{channels_in}->{$channel};
$config->{channels}->{$channel}->{error} = timestamp() . ' ' . $text;
$config->{channels}->{$channel}->{archived} = ($code == 405 ? 1 : 0);
save_config($config);
if (my $invite = delete $state->{pending_invites}->{$channel}) {
irc_send_message($invite->{who}, 'unable to join ' . $channel . ': ' . $text);
}
next;
}
# unable to send to channel - likely means we've been banned and then invited back
# treat the same as banned and leave the channel
if ($message =~ /^:\S+ 404 (\S+) (#\S+) :(.+)/) {
my ($who, $channel, $text) = ($1, normalise_channel($2), $3);
next unless $who eq $config->{irc}->{nick};
say timestamp(), ' -- join ', $channel, ' failed: ', $text;
$log->error('join ' . $channel . ' failed: ' . $text);
delete $state->{channels_in}->{$channel};
$config->{channels}->{$channel}->{archived} = 0;
$config->{channels}->{$channel}->{error} = timestamp() . ' ' . $text;
save_config($config);
irc_send('PART ' . $channel . ' I\'m banned from this channel');
next;
}
# mode --> extract channel password
if ($message =~ /^:\S+ 324 \S+ (#\S+) (\S+) (\S+)/) {
my ($channel, $mode, $password) = (normalise_channel($1), $2, $3);
next unless $mode =~ /k/;
next if ($config->{channels}->{$channel}->{password} // '') eq $password;
$config->{channels}->{$channel}->{password} = $password;
save_config($config);
next;
}
# when kicked track why, and don't try to rejoin
if ($message =~ /^:(\S+) KICK (#\S+) (\S+) :(.*)/) {
my ($who, $channel, $kicked, $text) = (source_to_nick($1), normalise_channel($2), $3, $4);
next unless $kicked eq $config->{irc}->{nick};
$text = 'kicked' if ($text // '') eq '';
say timestamp(), ' -- kicked from ', $channel, ' by ', $who, ': ', $text;
$log->info('kicked from ' . $channel . ' by ' . $who . ': ' . $text);
delete $state->{channels_in}->{$channel};
$config->{channels}->{$channel}->{disabled} = 1;
$config->{channels}->{$channel}->{kick} = timestamp() . ' <' . $who . '> ' . $text;
save_config($config);
next;
}
# channel /me ctcp action
if ($message =~ /^:(\S+) PRIVMSG (#\S+) :\x01ACTION (.+)\x01/) {
publish(1, source_to_nick($1), normalise_channel($2), $3);
next;
}
# channel message
if ($message =~ /^:(\S+) PRIVMSG (#\S+) :(.+)/) {
publish(0, source_to_nick($1), normalise_channel($2), $3);
next;
}
# channel notice
if ($message =~ /^:(\S+) NOTICE (#\S+) :(.+)/) {
publish(2, source_to_nick($1), normalise_channel($2), $3);
next;
}
# topic updated
if ($message =~ /^:\S+ TOPIC (#\S+) :(.+)/) {
publish(3, '-', normalise_channel($1), $2);
next;
}
# topic (RPL_NOTOPIC, RPL_TOPIC)
if ($message =~ /^:\S+ (331|332) \S+ (#\S+) :(.+)/) {
publish(3, '-', normalise_channel($2), $1 eq '331' ? '' : $3);
next;
}
# private message
if ($message =~ /^:(\S+) PRIVMSG \S+ :.+/) {
my $who = source_to_nick($1);
irc_send_message($who, $config->{help}) if $config->{help};
}
# channel reconciliation RPL_WHOISCHANNELS
if ($message =~ /^:\S+ 319 \S+ \S+ :(.+)/) {
my $channels = $1;
foreach my $channel (split(' ', $channels)) {
$channel =~ s/^[^#]+//; # strip usermode prefix
$state->{channels_reconcile}->{ normalise_channel($channel) } = 1;
}
next;
}
# channel reconciliation RPL_ENDOFWHOIS
if ($message =~ /^:\S+ 318 /) {
$config = reload_config($config);
# join channels
my @join;
foreach my $channel (sort keys %{ $config->{channels} }) {
next if $config->{channels}->{$channel}->{disabled};
next if $config->{channels}->{$channel}->{archived};
# update channels_in (in)
if ($state->{channels_reconcile}->{$channel}) {
$state->{channels_in}->{$channel} = 1;
next;
}
# join channels with passwords immediately
if (exists $config->{channels}->{$channel}->{password}) {
irc_send('JOIN ' . $channel . ' ' . $config->{channels}->{$channel}->{password});
$log->info('joining ' . $channel);
} else {
push @join, $channel;
}
}
# 10 at a time
my $iter = natatime(10, @join);
while (my @join_channels = $iter->()) {
irc_send('JOIN ' . join(',', @join_channels));
$log->info('joining ' . join(' ', @join_channels));
}
# part channels
my @part;
foreach my $channel (sort keys %{ $state->{channels_reconcile} }) {
next if $config->{channels}->{$channel};
push @part, $channel;
}
# 10 at a time
$iter = natatime(10, @part);
while (my @part_channels = $iter->()) {
irc_send('PART ' . join(',', @part_channels));
$log->info('parting ' . join(' ', @part_channels));
}
# update channels_in (out)
foreach my $channel (sort keys %{ $state->{channels_in} }) {
next if $state->{channels_reconcile}->{$channel};
$log->info('unexpectedly no longer in ' . $channel);
delete $state->{channels_in}->{$channel};
}
delete $state->{channels_reconcile};
$state->{next_channel_reload} = time() + $config->{timing}->{channel_reload_interval};
next;
}
}
__END__
connect
-> channel reload
-> +5m topic reload
channel reload
-> WHOIS logbot
<= RPL_WHOISCHANNELS
- collect channels in
<= RPL_ENDOFWHOIS
- diff channels-in with config
- join/part as required
topic reload
-> TOPIC (foreach channel)
<= RPL_TOPIC / RPL_NOTOPIC
- update stored topic
invite to channel
<= INVITE
- store invite request
-> JOIN channel
<= JOINED pending invite channel
-> NAMES channel
<= RPL_NAMREPLY pending invite channel
- collect owners, admins, and ops
<= RPL_ENDOFNAMES pending invite channel
- if invite from owner/admin/op update config
- otherwise PART