removed debug line
[rrq/slirc.git] / slirc.pl
1 #!/usr/bin/perl -w
2 # slirc.pl: local Slack IRC gateway
3 # Copyright (C) 2017-2019 Daniel Beer <dlbeer@gmail.com>
4 #
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #
17 # To run, create a configuration file with the following content:
18 #
19 #     slack_token=<legacy API token>
20 #     password=<password of your choice>
21 #     port=<port>
22 #
23 # Then run ./slirc.pl <config-file>. Connect to the chosen port on
24 # 127.0.0.1 with your chosen password.
25 #
26 # Updated 2018-05-15 to add support for listening on Unix domain sockets
27 # by Colin Watson <cjwatson@chiark.greenend.org.uk>. To use this feature
28 # with an IRC client supporting Unix domain connections, add the line
29 # "unix_socket=<path>" to the config file.
30 #
31 # Updated 2019-02-18:
32 # - HTML entities are now escaped/unescaped properly
33 # - Channel IDs are translated with the correct sigil
34 # - You can now close accumulated group chats. This is mapped to
35 #   JOIN/PART (the behaviour of JOIN/PART for public channels is
36 #   unaffected)
37 # - IRC-side PING checks are now more lenient, to work around bugs in
38 #   some IRC clients
39 # - Added X commands for debug dumps and dynamically switching protocol
40 #   debug on/off
41 #
42 # Updated 2019-05-08 based on changes from Neia Finch to improve
43 # support for bots.
44
45 use strict;
46 use warnings;
47 use utf8;
48
49 use AnyEvent;
50 use AnyEvent::HTTP;
51 use AnyEvent::Socket;
52 use AnyEvent::WebSocket::Client;
53 use URI::Encode qw(uri_encode);
54 use Data::Dumper;
55 use Time::localtime;
56 use Digest::SHA qw(sha256);
57 use JSON;
58
59 my $VERSION = "20190225";
60 my $start_time = time();
61 my %config;
62
63 $| = 1;
64
65 ########################################################################
66 # Global chat state
67 ########################################################################
68
69 my $connected;                  # Is the RTM connection ready?
70 my $self_id;                    # Slack ID of our own user, or undef
71
72 my %channels;                   # Slack ID -> channel hash ref
73 my %channels_by_name;           # irc_lcase -> channel hash ref
74 # Properties:
75 # - Id: Slack ID
76 # - Members: Slack ID set
77 # - Name: text (IRC name)
78 # - Topic: text
79
80 my %users;                      # Slack ID -> user hash ref
81 my %users_by_name;              # irc_lcase -> user hash ref
82 my %users_by_dmid;              # Slack DMID -> user hash ref
83 # Properties:
84 # - Id: Slack ID
85 # - Name: text (IRC name)
86 # - Channels: Slack ID set
87 # - Realname: text
88 # - DMId: DM session ID (may be undefined, or blank if open in progress)
89 # - TxQueue: messages awaiting transmission if no DM open
90
91 ########################################################################
92 # IRC names
93 ########################################################################
94
95 # Canonicalize an IRC name
96 sub irc_lcase {
97     my $name = lc(shift);
98
99     $name =~ s/\[/{/g;
100     $name =~ s/]/}/g;
101     $name =~ s/\|/\\/g;
102     $name =~ s/\^/~/g;
103
104     return $name;
105 }
106
107 # Compare two names
108 sub irc_eq {
109     my ($a, $b) = @_;
110
111     return irc_lcase($a) eq irc_lcase($b);
112 }
113
114 # Choose an unused valid name with reference to the hash
115 sub irc_pick_name {
116     my ($name, $hash) = @_;
117
118     $name =~ s/[#,<>!\0\r\n: ]/_/g;
119
120     return $name if length($name) && irc_lcase($name) ne "x" &&
121         !defined($hash->{irc_lcase($name)});
122
123     my $i = 1;
124
125     for (;;) {
126         my $prop = "$name$i";
127
128         return $prop unless defined($hash->{irc_lcase($prop)});
129         $i++;
130     }
131 }
132
133 ########################################################################
134 # IRC server
135 ########################################################################
136
137 # Forward decls to RTM subsystem
138 sub rtm_send;
139 sub rtm_send_to_user;
140 sub rtm_apicall;
141 sub rtm_download;
142 sub rtm_destroy;
143 sub rtm_update_join;
144 sub rtm_update_part;
145
146 my %irc_clients;
147
148 sub irc_send_args {
149     my ($c, $source, $short, $long) = @_;
150
151     $source =~ s/[\t\r\n\0 ]//g;
152     $source =~ s/^://g;
153     my @arg = (":$source");
154
155     for my $a (@$short) {
156         $a =~ s/[\t\r\n\0 ]//g;
157         $a =~ s/^://g;
158         utf8::encode($a);
159         $a = "*" unless length($a);
160         push @arg, $a;
161     }
162
163     if (defined($long)) {
164         $long =~ s/[\r\n\0]/ /g;
165         utf8::encode($long);
166         push @arg, ":$long";
167     }
168
169     my $line = join(' ', @arg);
170     print "IRC $c->{Handle} SEND: $line\n" if $config{debug_dump};
171     $c->{Handle}->push_write("$line\r\n");
172 }
173
174 sub irc_send_num {
175     my ($c, $num, $short, $long) = @_;
176     my $dst = $c->{Nick};
177
178     $dst = "*" unless defined($c->{Nick});
179     irc_send_args $c, "localhost",
180         [sprintf("%03d", $num), $dst, @$short], $long;
181 }
182
183 sub irc_send_from {
184     my ($c, $uid, $short, $long) = @_;
185     my $user = $users{$uid};
186     my $nick = $uid eq $self_id ? $c->{Nick} : $user->{Name};
187
188     irc_send_args $c, "$nick!$user->{Id}\@localhost", $short, $long;
189 }
190
191 sub irc_disconnect {
192     my ($c, $msg) = @_;
193
194     print "IRC $c->{Handle} DISCONNECT: $msg\n";
195     delete $irc_clients{$c->{Handle}};
196     $c->{Handle}->destroy;
197 }
198
199 sub irc_disconnect_all {
200     print "IRC: disconnect all\n";
201     foreach my $k (keys %irc_clients) {
202         $irc_clients{$k}->{Handle}->destroy;
203     }
204     %irc_clients = ();
205 }
206
207 sub irc_send_names {
208     my ($c, $chan) = @_;
209     my $i = 0;
210     my @ulist = keys %{$chan->{Members}};
211
212     while ($i < @ulist) {
213         my $n = @ulist - $i;
214         $n = 8 if $n > 8;
215         my $chunk = join(' ', map {
216             $_ eq $self_id ? $c->{Nick} : $users{$_}->{Name}
217         } @ulist[$i .. ($i + $n - 1)]);
218         irc_send_num $c, 353, ['@', "#$chan->{Name}"], $chunk;
219         $i += $n;
220     }
221
222     irc_send_num $c, 366, ["#$chan->{Name}"], "End of /NAMES list";
223 }
224
225 sub irc_server_notice {
226     my ($c, $msg) = @_;
227     my $nick = $c->{Nick};
228
229     $nick = "*" unless defined($nick);
230     irc_send_args $c, "localhost", ["NOTICE", $nick], $msg;
231 }
232
233 sub irc_gateway_notice {
234     my ($c, $msg) = @_;
235     my $nick = $c->{Nick};
236
237     $nick = "*" unless defined($nick);
238     irc_send_args $c, "X!X\@localhost", ["NOTICE", $nick], $msg;
239 }
240
241 sub irc_notify_away {
242     my $c = shift;
243     my $user = $users{$self_id};
244     my ($num, $msg);
245
246     if ($user->{Presence} eq 'away') {
247         $num = 306;
248         $msg = "You have been marked as being away";
249     } else {
250         $num = 305;
251         $msg = "You are no longer marked as being away";
252     }
253
254     irc_send_num $c, $num, [], $msg if $c->{Ready};
255 }
256
257 sub irc_broadcast_away {
258     for my $k (keys %irc_clients) {
259         my $c = $irc_clients{$k};
260         irc_notify_away $c if $c->{Ready};
261     }
262 }
263
264 sub irc_send_motd {
265     my $c = shift;
266     my @banner =
267       (
268        '     _ _                  _',
269        ' ___| (_)_ __ ___   _ __ | |',
270        '/ __| | | \'__/ __| | \'_ \| |',
271        '\__ \ | | | | (__ _| |_) | |',
272        '|___/_|_|_|  \___(_) .__/|_|',
273        '                   |_|',
274        'slirc.pl, Copyright (C) 2017-2019 Daniel Beer <dlbeer@gmail.com>'
275       );
276
277     for my $x (@banner) {
278         irc_send_num $c, 372, [], $x;
279     }
280     irc_send_num $c, 376, [], "End of /MOTD command";
281 }
282
283 sub irc_check_welcome {
284     my $c = shift;
285
286     if (defined $c->{Nick} && defined $c->{User} &&
287         (!$config{password} || defined $c->{Password}) &&
288         !$c->{Authed}) {
289         if (!$config{password} ||
290             sha256($c->{Password}) eq sha256($config{password})) {
291             $c->{Authed} = 1;
292         } else {
293             irc_server_notice $c, "Incorrect password";
294             irc_disconnect $c, "Incorrect password";
295             return;
296         }
297     }
298
299     return unless $c->{Authed} && !$c->{Ready} && $connected;
300
301     my $u = $users_by_name{irc_lcase($c->{Nick})};
302     if (defined($u) && ($u->{Id} ne $self_id)) {
303         irc_server_notice $c, "Nick already in use";
304         irc_disconnect $c, "Nick already in use";
305         return;
306     }
307
308     my $lt = localtime($start_time);
309
310     irc_send_num $c, 001, [], "slirc.pl IRC-to-Slack gateway";
311     irc_send_num $c, 002, [], "This is slirc.pl version $VERSION";
312     irc_send_num $c, 003, [], "Server started " . ctime($start_time);
313     irc_send_motd $c;
314     $c->{Ready} = 1;
315
316     my $user = $users{$self_id};
317
318     for my $k (keys %{$user->{Channels}}) {
319         my $chan = $channels{$k};
320         irc_send_from $c, $self_id, ["JOIN", "#$chan->{Name}"];
321         irc_send_num $c, 332, ["#$chan->{Name}"], $chan->{"Topic"};
322         irc_send_names $c, $chan;
323     }
324
325     irc_notify_away $c;
326 }
327
328 sub irc_check_welcome_all {
329     foreach my $k (keys %irc_clients) {
330         irc_check_welcome $irc_clients{$k};
331     }
332 }
333
334 sub irc_broadcast_nick {
335     my ($id, $newname) = @_;
336     return if $id eq $self_id;
337
338     for my $k (keys %irc_clients) {
339         my $c = $irc_clients{$k};
340         irc_send_from $c, $id, ["NICK", $newname] if $c->{Ready};
341     }
342 }
343
344 sub irc_broadcast_join {
345     my ($uid, $chid) = @_;
346     my $chan = $channels{$chid};
347
348     for my $k (keys %irc_clients) {
349         my $c = $irc_clients{$k};
350         next unless $c->{Ready};
351
352         if ($uid eq $self_id) {
353             irc_send_from $c, $self_id, ["JOIN", "#$chan->{Name}"];
354             irc_send_num $c, 332, ["#$chan->{Name}"], $chan->{Topic};
355             irc_send_names $c, $chan;
356         } else {
357             irc_send_from $c, $uid, ["JOIN", "#$chan->{Name}"];
358         }
359     }
360 }
361
362 sub irc_broadcast_part {
363     my ($uid, $chid) = @_;
364     my $chan = $channels{$chid};
365
366     for my $k (keys %irc_clients) {
367         my $c = $irc_clients{$k};
368         next unless $c->{Ready};
369         irc_send_from $c, $uid, ["PART", "#$chan->{Name}"];
370     }
371 }
372
373 sub irc_send_who {
374     my ($c, $chname, $u) = @_;
375
376     my $user = $users{$u};
377     my $nick = $u eq $self_id ? $c->{Nick} : $user->{Name};
378     my $here = $user->{Presence} eq 'away' ? 'G' : 'H';
379
380     irc_send_num $c, 352, [$chname, $user->{Id}, "localhost", "localhost",
381                            $nick, $here], "0 $user->{Realname}";
382 }
383
384 sub irc_send_whois {
385     my ($c, $uid) = @_;
386     my $u = $users{$uid};
387     my $nick = $uid eq $self_id ? $c->{Nick} : $u->{Name};
388
389     irc_send_num $c, 311,
390         [$nick, $u->{Id}, "localhost", "*"], $u->{Realname};
391     irc_send_num $c, 312, [$nick, "localhost"], "slirc.pl";
392     my $clist = join(' ', map { "#" . $channels{$_}->{Name} }
393                           keys %{$u->{Channels}});
394     irc_send_num $c, 319, [$nick], $clist;
395     irc_send_num $c, 301, [$nick], "away" if $u->{Presence} eq 'away';
396 }
397
398 sub irc_invite_or_kick {
399     my ($c, $action, $name, $chname) = @_;
400
401     $chname =~ s/^#//;
402     my $chan = $channels_by_name{irc_lcase($chname)};
403
404     unless (defined $chan) {
405         irc_send_num $c, 401, ["#$chname"], "No such nick/channel";
406         return;
407     }
408
409     my $what = $chan->{Type} eq "C" ? "channels" : "groups";
410
411     foreach (split(/,/, $name)) {
412         my $user = $users_by_name{irc_lcase($_)};
413
414         if (defined $user) {
415             rtm_apicall "$what.$action", { user => $user->{Id},
416                                            channel => $chan->{Id} };
417         } else {
418             irc_send_num $c, 401, [$user], "No such nick/channel";
419         }
420     }
421 }
422
423 my %gateway_command = (
424     "debug_dump_state" => sub {
425         my $c = shift;
426         irc_gateway_notice $c, "Dumping debug state on stdout";
427         print Dumper({ "connected" => $connected,
428                        "self_id", => $self_id,
429                        "%channels" => \%channels,
430                        "%channels_by_name" => \%channels_by_name,
431                        "users" => \%users,
432                        "users_by_name" => \%users_by_name,
433                        "users_by_dmid" => \%users_by_dmid
434                      });
435     },
436     "debug_dump" => sub {
437         my ($c, $arg) = @_;
438         $config{debug_dump} = $arg ? 1 : 0 if defined $arg;
439         irc_gateway_notice $c, "Protocol debug is " .
440           ($config{debug_dump} ? "on" : "off");
441     },
442     "newgroup" => sub {
443         my ($c, $name) = @_;
444         unless (defined($name)) {
445             irc_gateway_notice $c, "Syntax: newgroup <name>";
446             return;
447         }
448         $name =~ s/^#//;
449         irc_gateway_notice $c, "Creating group $name";
450         rtm_apicall "groups.create", { name => $name };
451     },
452     "newchan" => sub {
453         my ($c, $name) = @_;
454         unless (defined($name)) {
455             irc_gateway_notice $c, "Syntax: newchan <name>";
456             return;
457         }
458         $name =~ s/^#//;
459         irc_gateway_notice $c, "Creating channel $name";
460         rtm_apicall "channels.create", { name => $name };
461     },
462     "archive" => sub{
463         my ($c, $name) = @_;
464         unless (defined($name)) {
465             irc_gateway_notice $c, "Syntax: archive <name>";
466             return;
467         }
468         $name =~ s/^#//;
469         my $g = $channels_by_name{irc_lcase($name)};
470         my $what = $g->{Type} eq "C" ? "channels" : "groups";
471         if (defined $g) {
472             irc_gateway_notice $c, "Archiving $name";
473             rtm_apicall "$what.archive", { channel => $g->{Id} };
474         } else {
475             irc_gateway_notice $c, "No such channel: $name";
476         }
477     },
478     "close" => sub{
479         my ($c, $name) = @_;
480         unless (defined($name)) {
481             irc_gateway_notice $c, "Syntax: close <name>";
482             return;
483         }
484
485         $name =~ s/^#//;
486         my $g = $channels_by_name{irc_lcase($name)};
487         my $what = $g->{Type} eq "C" ? "channels" : "groups";
488         if (defined $g) {
489             irc_gateway_notice $c, "Closing $name";
490             rtm_apicall "$what.close", { channel => $g->{Id} };
491         } else {
492             irc_gateway_notice $c, "No such channel: $name";
493         }
494     },
495     "cat" => sub {
496         my ($c, $fileid) = @_;
497         unless (defined($fileid)) {
498             irc_gateway_notice $c, "Syntax: cat <fileid> <filename>";
499             return;
500         }
501
502         rtm_apicall "files.info", { file => $fileid }, sub {
503             my $data = shift;
504             return unless defined $data;
505
506             my $body = $data->{content};
507
508             if (length($body) > 65536) {
509                 irc_gateway_notice $c, "File too big";
510             } else {
511                 irc_gateway_notice $c, "---- BEGIN $fileid ----";
512                 foreach (split(/\n/, $body)) {
513                     irc_gateway_notice $c, "$_";
514                 }
515                 irc_gateway_notice $c, "---- END $fileid ----";
516             }
517         };
518     },
519     "disconnect" => sub {
520         my ($c) = @_;
521         irc_gateway_notice $c, "Disconnecting";
522         rtm_destroy "User disconnection request";
523     },
524     "delim" => sub {
525         my ($c, $nick) = @_;
526         unless (defined($nick)) {
527             irc_gateway_notice $c, "Syntax: delim <name>";
528             return;
529         }
530         my $user;
531
532         if ($nick eq $c->{Nick}) {
533             $user = $users{$self_id};
534         } else {
535             $user = $users_by_name{irc_lcase($nick)};
536             $user = undef if $user->{Id} eq $self_id;
537         }
538
539         unless (defined($user)) {
540             irc_gateway_notice $c, "No such nick: $nick";
541             return;
542         }
543
544         unless (defined $user->{DMId}) {
545             irc_gateway_notice $c, "DM already closed for $nick";
546             return;
547         }
548
549         irc_gateway_notice $c, "Closing DM for $nick";
550         rtm_apicall "im.close", { channel => $user->{DMId} };
551     },
552 );
553
554 my %irc_command = (
555     "NICK" => sub {
556         my ($c, $newnick) = @_;
557         return unless defined($newnick);
558
559         if (defined $c->{Nick}) {
560             my $u = $users_by_name{irc_lcase($newnick)};
561             if (defined($u) && ($u->{Id} ne $self_id)) {
562                 irc_send_num $c, 433, [$newnick], "Nickname is already in use";
563             } else {
564                 irc_send_from $c, $self_id, ["NICK"], $newnick;
565                 $c->{Nick} = $newnick;
566             }
567         } else {
568             $c->{Nick} = $newnick;
569             irc_check_welcome $c;
570         }
571     },
572     "PASS" => sub {
573         my ($c, $pass) = @_;
574
575         $c->{Password} = $pass;
576         irc_check_welcome $c;
577     },
578     "USER" => sub {
579         my ($c, @arg) = @_;
580         return unless scalar(@arg) >= 4;
581
582         $c->{User} = $arg[0];
583         $c->{Realname} = $arg[3];
584         irc_check_welcome $c;
585     },
586     "AWAY" => sub {
587         my ($c, $msg) = @_;
588         return unless $c->{Ready};
589         my $presence = defined $msg ? "away" : "auto";
590         rtm_apicall "users.setPresence", { presence => $presence };
591     },
592     "PING" => sub {
593         my ($c, $reply) = @_;
594         $reply = "" unless defined($reply);
595         irc_send_args $c, "localhost", ["PONG"], $reply;
596     },
597     "INVITE" => sub {
598         my ($c, $name, $chname) = @_;
599         return unless $c->{Ready};
600         irc_invite_or_kick $c, "invite", $name, $chname;
601     },
602     "KICK" => sub {
603         my ($c, $name, $chname) = @_;
604         return unless $c->{Ready};
605         irc_invite_or_kick $c, "kick", $name, $chname;
606     },
607     "JOIN" => sub {
608         my ($c, $name) = @_;
609         return unless $c->{Ready};
610         return unless defined($name);
611
612         foreach my $n (split(/,/, $name)) {
613             $n =~ s/^#//;
614             my $chan = $channels_by_name{irc_lcase($n)};
615             if (not defined $chan) {
616                 irc_send_num $c, 401, ["#$n"], "No such nick/channel";
617             } elsif ($chan->{Members}->{$self_id}) {
618                 # Already joined
619             } elsif ($chan->{Type} eq "G") {
620                 rtm_apicall "groups.open", { channel => $chan->{Id} };
621                 irc_broadcast_join $self_id, $chan->{Id}
622                   if rtm_update_join $self_id, $chan->{Id};
623             } else {
624                 #rtm_apicall "channels.join", { channel => $chan->{Id} };
625                 rtm_apicall "conversations.join", { channel => $chan->{Id} };
626             }
627         }
628     },
629     "MODE" => sub {
630         my ($c, $arg, $what) = @_;
631         return unless $c->{Ready};
632         return unless defined($arg);
633
634         $what = $what || "";
635
636         if ($arg eq $c->{Nick}) {
637             irc_send_num $c, 221, [], "+i";
638         } elsif ($arg =~ /^#(.*)$/) {
639             my $chan = $channels_by_name{$1};
640
641             if (defined $chan) {
642                 if ($what eq "b") {
643                     irc_send_num $c, 368, ["#$chan->{Name}"],
644                         "End of channel ban list";
645                 } else {
646                     irc_send_num $c, 324,
647                         ["#$chan->{Name}",
648                          ($chan->{Type} eq "G" ? "+ip" : "+p")];
649                     irc_send_num $c, 329,
650                         ["#$chan->{Name}", $start_time];
651                 }
652             } else {
653                 irc_send_num $c, 403, [$arg], "No such channel";
654             }
655         } else {
656             irc_send_num $c, 403, [$arg], "No such channel";
657         }
658     },
659     "TOPIC" => sub {
660         my ($c, $name, $topic) = @_;
661         return unless defined($name) && defined($topic);
662         return unless $c->{Ready};
663
664         $name =~ s/^#//;
665         my $chan = $channels_by_name{irc_lcase($name)};
666         unless (defined($chan)) {
667             irc_send_num $c, 401, ["#$name"], "No such nick/channel";
668             return;
669         }
670
671         my $what = $chan->{Type} eq "C" ? "channels" : "groups";
672
673         rtm_apicall "$what.setTopic", { channel => $chan->{Id},
674                                         topic => $topic };
675     },
676     "PART" => sub {
677         my ($c, $name) = @_;
678         return unless $c->{Ready};
679         return unless defined($name);
680
681         foreach my $n (split(/,/, $name)) {
682             $n =~ s/^#//;
683             my $chan = $channels_by_name{irc_lcase($n)};
684             if (not defined($chan)) {
685                 irc_send_num $c, 401, ["#$n"], "No such nick/channel";
686             } elsif ($chan->{Members}->{$self_id}) {
687                 if ($chan->{Type} eq "G") {
688                     rtm_apicall "groups.close", { channel => $chan->{Id} };
689                     irc_broadcast_part $self_id, $chan->{Id}
690                       if rtm_update_part $self_id, $chan->{Id};
691                 } else {
692                     #rtm_apicall "channels.leave", { channel => $chan->{Id} };
693                     rtm_apicall "conersations.leave", { channel => $chan->{Id} };
694                 }
695             }
696         }
697     },
698     "LIST" => sub {
699         my ($c, @arg) = @_;
700         return unless $c->{Ready};
701
702         irc_send_num $c, 321, ["Channel"], "Users Name";
703         foreach my $chid (keys %channels) {
704             my $chan = $channels{$chid};
705             my $n = keys %{$chan->{Members}};
706
707             irc_send_num $c, 322, ["#$chan->{Name}", $n], $chan->{Topic};
708         }
709         irc_send_num $c, 323, [], "End of /LIST";
710     },
711     "WHOIS" => sub {
712         my ($c, $nicklist) = @_;
713         return unless $c->{Ready};
714         return unless defined($nicklist);
715         my $some = 0;
716
717         for my $nick (split(/,/, $nicklist)) {
718             if (irc_eq($nick, "x")) {
719                 irc_send_num $c, 311,
720                     ["X", "X", "localhost", "*"], "Gateway service";
721                 irc_send_num $c, 312, ["X", "localhost"], "slirc.pl";
722             } elsif (irc_eq($nick, $c->{Nick})) {
723                 irc_send_whois $c, $self_id;
724             } else {
725                 my $user;
726
727                 if ($nick =~ /^.*!([^@]*)/) {
728                     $user = $users{$1};
729                 } else {
730                     $user = $users_by_name{irc_lcase($nick)};
731                     $user = undef if defined($user) && $user->{Id} eq $self_id;
732                 }
733
734                 if (defined($user)) {
735                     irc_send_whois $c, $user->{Id};
736                 } else {
737                     irc_send_num $c, 401, [$nick], "No such nick/channel";
738                 }
739             }
740         }
741
742         irc_send_num $c, 318, [$nicklist], "End of /WHOIS list";
743     },
744     "NAMES" => sub {
745         my ($c, $name) = @_;
746         return unless $c->{Ready};
747         return unless defined($name);
748
749         my $n = $name;
750         $n =~ s/^#//;
751         my $chan = $channels_by_name{irc_lcase($n)};
752         unless (defined($chan)) {
753             irc_send_num $c, 401, [$name], "No such nick/channel";
754             return;
755         }
756
757         irc_send_names $c, $chan;
758     },
759     "WHO" => sub {
760         my ($c, $name) = @_;
761         return unless $c->{Ready};
762
763         if (!defined($name)) {
764             foreach my $u (keys %users) {
765                 irc_send_who $c, "*", $u;
766             }
767             irc_send_num $c, 352, ["*", "X", "localhost", "localhost",
768                                    "X", "H"], "0 Gateway service";
769         } elsif (irc_eq($name, "X")) {
770             irc_send_num $c, 352, ["*", "X", "localhost", "localhost",
771                                    "X", "H"], "0 Gateway service";
772         } elsif ($name =~ /^.*!([^@]*)/) {
773             unless (exists $users{$1}) {
774                 irc_send_num $c, 401, [$name], "No such nick/channel";
775                 return;
776             }
777
778             irc_send_who $c, $name, $1;
779         } elsif ($name =~ /^#(.*)$/) {
780             my $chan = $channels_by_name{irc_lcase($1)};
781
782             unless (defined($chan)) {
783                 irc_send_num $c, 401, [$name], "No such nick/channel";
784                 return;
785             }
786
787             foreach my $u (keys %{$chan->{Members}}) {
788                 irc_send_who $c, "#$chan->{Name}", $u;
789             }
790         } else {
791             my $user = $users_by_name{irc_lcase($name)};
792
793             unless (defined($user)) {
794                 irc_send_num $c, 401, [$name], "No such nick/channel";
795                 return;
796             }
797
798             irc_send_who $c, $name, $self_id;
799         }
800
801         $name = "*" unless defined($name);
802         irc_send_num $c, 315, [$name], "End of /WHO list";
803     },
804     "MOTD" => sub {
805         my $c = shift;
806         irc_send_motd $c;
807     },
808     "PRIVMSG" => sub {
809         my ($c, $namelist, $msg) = @_;
810         return unless $c->{Ready};
811         return unless defined($namelist) && defined($msg);
812
813         $msg =~ s/&/&amp;/g;
814         $msg =~ s/</&lt;/g;
815         $msg =~ s/>/&gt;/g;
816         $msg =~ s/"/&quot;/g;
817         $msg =~ s/&lt;@([^>]+)&gt;/'<@' . irc_name_to_id($c, $1) . '>'/eg;
818         $msg =~ s/&lt;#([^>]+)&gt;/'<#' . irc_chan_to_id($c, $1) . '>'/eg;
819
820         foreach my $name (split(/,/, $namelist)) {
821             if (irc_eq($name, "X")) {
822                 my @args = split(/  */, $msg);
823                 my $cmd = shift @args;
824                 my $handler = $gateway_command{lc($cmd)};
825
826                 if (defined $handler) {
827                     $handler->($c, @args);
828                 } else {
829                     irc_gateway_notice $c, "Unknown command: $cmd";
830                 }
831             } elsif ($name =~ /^#(.*)$/) {
832                 my $chan = $channels_by_name{irc_lcase($1)};
833
834                 if (defined $chan) {
835                     rtm_send {
836                         type => "message", channel => $chan->{Id},
837                         text => $msg };
838                 } else {
839                     irc_send_num $c, 401, [$name], "No such nick/channel";
840                 }
841             } elsif (irc_eq($name, $c->{Nick})) {
842                 rtm_send_to_user $self_id, $msg;
843             } else {
844                 my $user = $users_by_name{irc_lcase($name)};
845
846                 if (defined $user) {
847                     rtm_send_to_user $user->{Id}, $msg;
848                 } else {
849                     irc_send_num $c, 401, [$name], "No such nick/channel";
850                 }
851             }
852         }
853     },
854     "PONG" => sub {
855         shift->{PingCount} = 0;
856     },
857     "QUIT" => sub {
858         my $c = shift;
859         irc_disconnect $c, "QUIT";
860     },
861 );
862
863 sub irc_broadcast_notice {
864     my ($msg) = @_;
865
866     print "NOTICE: $msg\n";
867     foreach my $k (keys %irc_clients) {
868         my $c = $irc_clients{$k};
869         irc_server_notice $c, $msg if $c->{Authed};
870     }
871 }
872
873 sub irc_id_to_name {
874     my ($c, $id) = @_;
875     return $c->{Nick} if $id eq $self_id;
876     my $u = $users{$id};
877     return $u->{Name} if defined($u);
878     return $id;
879 }
880
881 sub irc_name_to_id {
882     my ($c, $name) = @_;
883     return $self_id if $name eq $c->{Nick};
884     my $u = $users_by_name{$name};
885     return $u->{Id} if defined($u);
886     return $name;
887 }
888
889 sub irc_id_to_chan {
890     my ($c, $id) = @_;
891     my $ch = $channels{$id};
892     return $ch->{Name} if defined ($ch);
893     return $id;
894 }
895
896 sub irc_chan_to_id {
897     my ($c, $chan) = @_;
898     my $ch = $channels_by_name{$chan};
899     return $ch->{Id} if defined($ch);
900     return $chan;
901 }
902
903 sub irc_do_message {
904     my ($srcid, $dstname, $subtype, $text) = @_;
905
906     $text =~ s/\002//g;
907     my $prefix = "";
908     $prefix = "\002[$subtype]\002 " if defined($subtype);
909
910     for my $k (keys %irc_clients) {
911         my $c = $irc_clients{$k};
912         next unless $c->{Ready};
913
914         my $translate = $text;
915         $translate =~ s/<@([^>]+)>/'<@' . irc_id_to_name($c, $1) . '>'/eg;
916         $translate =~ s/<#([^>]+)>/'<#' . irc_id_to_chan($c, $1) . '>'/eg;
917         $translate =~ s/&lt;/</g;
918         $translate =~ s/&gt;/>/g;
919         $translate =~ s/&quot;/"/g;
920         $translate =~ s/&amp;/&/g;
921
922         for my $line (split(/\n/, $translate)) {
923             irc_send_from $c, $srcid, ["PRIVMSG", $dstname], "$prefix$line";
924         }
925     }
926 }
927
928 sub irc_privmsg {
929     my ($id, $subtype, $msg) = @_;
930     irc_do_message $id, $users{$id}->{Name}, $subtype, $msg;
931 }
932
933 sub irc_chanmsg {
934     my ($id, $subtype, $chid, $msg) = @_;
935     irc_do_message $id, "#$channels{$chid}->{Name}", $subtype, $msg;
936 }
937
938 sub irc_topic_change {
939     my ($id, $chid) = @_;
940     my $chan = $channels{$chid};
941
942     foreach my $k (keys %irc_clients) {
943         my $c = $irc_clients{$k};
944
945         irc_send_from $c, $id, ["TOPIC", "#$chan->{Name}"], $chan->{Topic}
946             if $c->{Ready};
947     }
948 }
949
950 sub irc_ping {
951     my $c = shift;
952
953     if (++$c->{PingCount} >= 3) {
954         irc_disconnect $c, "Ping timeout";
955         return;
956     }
957
958     irc_send_args $c, "localhost", ["PING"], time();
959     $c->{PingTimer} = AnyEvent->timer(after => 60, cb => sub { irc_ping($c); });
960 }
961
962 sub irc_line {
963     my ($c, $fh, $line, $eol) = @_;
964
965     print "IRC $fh RECV: $line\n" if $config{debug_dump};
966
967     utf8::decode($line);
968
969     my $smallargs = $line;
970     my $bigarg = undef;
971
972     if ($line =~ /^(.*?) :(.*)$/) {
973         $smallargs = $1;
974         $bigarg = $2;
975     }
976
977     my @words = split /  */, $smallargs;
978     push @words, $bigarg if defined($bigarg);
979
980     if (scalar(@words)) {
981         my $cmd = shift @words;
982         my $handler = $irc_command{uc($cmd)};
983
984         $handler->($c, @words) if (defined($handler));
985     }
986
987     $fh->push_read(line => sub { irc_line($c, @_) });
988 }
989
990 sub irc_listen {
991     print "Start IRC listener\n";
992     my $listen_host = $config{unix_socket} ? "unix/" : "127.0.0.1";
993     tcp_server $listen_host,
994                ($config{unix_socket} || $config{port} || 6667), sub {
995         my ($fd, $host, $port) = @_;
996
997         my $fh;
998         $fh = new AnyEvent::Handle
999           fh => $fd,
1000           on_error => sub {
1001               my ($fh, $fatal, $msg) = @_;
1002               irc_disconnect $irc_clients{$fh}, "error: $msg";
1003           },
1004           on_eof => sub {
1005               my $fh = shift;
1006               irc_disconnect $irc_clients{$fh}, "EOF";
1007           };
1008
1009         print "IRC $fh Got connection from $host:$port\n";
1010
1011         my $c = { Handle => $fh };
1012         $c->{PingTimer} = AnyEvent->timer(after => 30,
1013                 cb => sub { irc_ping $c; });
1014         $c->{PingCount} = 0;
1015         $irc_clients{$fh} = $c;
1016         $fh->push_read(line => sub { irc_line($c, @_) });
1017
1018         irc_server_notice $c, "Waiting for RTM connection" if not $connected;
1019     }, sub {
1020         chmod 0600, $config{unix_socket} if $config{unix_socket};
1021     }
1022 }
1023
1024 ########################################################################
1025 # RTM client
1026 ########################################################################
1027
1028 my $rtm_client;
1029 my $rtm_con;
1030 my $rtm_msg_id = 1;
1031 my %rtm_apicall_handles;
1032 my $rtm_cooldown_timer;
1033
1034 my %rtm_mark_queue;
1035 my $rtm_mark_timer;
1036
1037 my $rtm_ping_timer;
1038 my $rtm_ping_count;
1039
1040 sub rtm_apicall {
1041     my ($method, $args, $cb) = @_;
1042     my @encode;
1043
1044     print "RTM APICALL $method ", Dumper($args) if $config{debug_dump};
1045
1046     $args->{token} = $config{slack_token};
1047
1048     foreach my $k (keys %$args) {
1049         my $ek = uri_encode($k);
1050         my $ev = uri_encode($args->{$k});
1051
1052         push @encode, "$ek=$ev";
1053     }
1054
1055     my $x;
1056     $x = http_post "https://slack.com/api/$method", join('&', @encode),
1057         headers => {
1058             "Content-Type", "application/x-www-form-urlencoded"
1059         }, sub {
1060             my ($body, $hdr) = @_;
1061             delete $rtm_apicall_handles{$x};
1062
1063             unless ($hdr->{Status} =~ /^2/) {
1064                 irc_broadcast_notice
1065                   "API HTTP error: $method: $hdr->{Status} $hdr->{Reason}";
1066                 $cb->(undef) if defined($cb);
1067                 return;
1068             }
1069
1070             my $data = decode_json $body;
1071
1072             print "RTM REPLY $method ", Dumper($data) if $config{debug_dump};
1073
1074             unless ($data->{ok}) {
1075                 irc_broadcast_notice "API error: $data->{error}";
1076                 $cb->(undef) if defined($cb);
1077                 return;
1078             }
1079
1080             $cb->($data) if defined($cb);
1081         };
1082
1083     $rtm_apicall_handles{$x} = 1;
1084 }
1085
1086 sub rtm_send {
1087     my $frame = shift;
1088
1089     $frame->{id} = $rtm_msg_id++;
1090     print "RTM SEND: ", Dumper($frame) if $config{debug_dump};
1091     $rtm_con->send(encode_json $frame);
1092 }
1093
1094 sub rtm_update_join {
1095     my ($uid, $chid) = @_;
1096     my $chan = $channels{$chid};
1097     my $user = $users{$uid};
1098
1099     if (!$chan->{Members}->{$uid}) {
1100         $chan->{Members}->{$uid} = 1;
1101         $user->{Channels}->{$chid} = 1;
1102         return 1;
1103     }
1104
1105     return undef;
1106 }
1107
1108 sub rtm_update_part {
1109     my ($uid, $chid) = @_;
1110     my $chan = $channels{$chid};
1111     my $user = $users{$uid};
1112
1113     if ($chan->{Members}->{$uid}) {
1114         delete $channels{$chid}->{Members}->{$uid};
1115         delete $users{$uid}->{Channels}->{$chid};
1116         return 1;
1117     }
1118
1119     return undef;
1120 }
1121
1122 sub rtm_update_user {
1123     my $c = shift;
1124     my $user;
1125
1126     if (exists $users{$c->{id}}) {
1127         $user = $users{$c->{id}};
1128         my $oldname = $user->{Name};
1129         delete $users_by_name{irc_lcase($oldname)};
1130         my $newname = irc_pick_name($c->{name}, \%users_by_name);
1131
1132         $user->{Realname} = $c->{real_name} // $c->{name};
1133
1134         irc_broadcast_nick $c->{id}, $newname
1135             if $oldname ne $newname;
1136
1137         $user->{Name} = $newname;
1138         $user->{Presence} = $c->{presence} // 'active';
1139         $users_by_name{$newname} = $user;
1140     } else {
1141         my $name = irc_pick_name($c->{name}, \%users_by_name);
1142         $user = {
1143             Id => $c->{id},
1144             Name => $name,
1145             Channels => {},
1146             Realname => $c->{real_name} // $c->{name},
1147             TxQueue => [],
1148             Presence => $c->{presence} // 'active'
1149         };
1150
1151         $users{$c->{id}} = $user;
1152         $users_by_name{$name} = $user;
1153     }
1154
1155     $user->{Realname} = "" unless defined($user->{Realname});
1156 }
1157
1158 sub rtm_record_unknown_uid {
1159     my $uid = shift;
1160
1161     unless (exists $users{$uid}) {
1162         # Temporary name
1163         my $name = irc_pick_name($uid, \%users_by_name);
1164
1165         my $u = {
1166             Id => $uid,
1167             Name => $name,
1168             Channels => {},
1169             Realname => "",
1170             TxQueue => []
1171         };
1172
1173         $users{$uid} = $u;
1174         $users_by_name{irc_lcase($name)} = $u;
1175
1176         rtm_apicall "users.info", { user => $uid }, sub {
1177             my $data = shift;
1178
1179             rtm_update_user $data->{user} if defined $data;
1180         };
1181     }
1182 }
1183
1184 sub rtm_update_channel {
1185     my ($type, $c) = @_;
1186     my $id = $c->{id};
1187
1188     rtm_apicall "conversations.members", { channel => $id }, sub {
1189
1190         my $id = $c->{id};
1191         my $data = shift;
1192         my $mhash = {};
1193         my $name = $c->{name};
1194
1195         $c->{members} = $data->{'members'};
1196
1197         $name = "+$name" if $type eq "G";
1198
1199         # Cross-reference users/channels
1200         foreach my $u (@{$c->{members}}) {
1201             rtm_record_unknown_uid $u;
1202             # Filter ourselves out of the membership list if this is a
1203             # closed group chat.
1204             next if $type eq 'G' && $u eq $self_id && !$c->{is_open};
1205             $mhash->{$u} = 1;
1206             $users{$u}->{Channels}->{$id} = 1;
1207         }
1208
1209         if (exists $channels{$id}) {
1210             my $chan = $channels{$id};
1211             $chan->{Members} = $mhash;
1212             $chan->{Topic} = $c->{topic}->{value};
1213             $chan->{Type} = $type;
1214         } else {
1215             my $name = irc_pick_name($name, \%channels_by_name);
1216             my $chan = {
1217                 Id => $c->{id},
1218                 Members => $mhash,
1219                 Name => $name,
1220                 Type => $type,
1221                 Topic => $c->{topic}->{value}
1222             };
1223
1224             $channels{$c->{id}} = $chan;
1225             $channels_by_name{irc_lcase($name)} = $chan;
1226         }
1227     };
1228 }
1229
1230 sub rtm_delete_channel {
1231     my $chid = shift;
1232     my $chan = $channels{$chid};
1233     return unless defined $chan;
1234
1235     foreach ($chan->{Members}) {
1236         my $user = $users{$_};
1237
1238         delete $user->{Channels}->{$chid};
1239     }
1240
1241     delete $channels_by_name{irc_lcase($chan->{Name})};
1242     delete $channels{$chid};
1243 }
1244
1245 sub rtm_mark_channel {
1246     my ($chid, $ts) = @_;
1247
1248     $rtm_mark_queue{$chid} = $ts;
1249
1250     unless (defined $rtm_mark_timer) {
1251         $rtm_mark_timer = AnyEvent->timer(after => 5, cb => sub {
1252             for my $chid (keys %rtm_mark_queue ) {
1253                 rtm_apicall "conversations.mark", {
1254                     channel => $chid,
1255                     ts => $rtm_mark_queue{$chid}
1256                 };
1257             }
1258             %rtm_mark_queue = ();
1259             undef $rtm_mark_timer;
1260         });
1261     }
1262 }
1263
1264 my %rtm_command = (
1265     "presence_change" => sub {
1266         my $msg = shift;
1267         my $user = $users{$msg->{user}};
1268
1269         if (defined $user) {
1270             my $old = $user->{Presence};
1271             $user->{Presence} = $msg->{presence} if defined $user;
1272             irc_broadcast_away if
1273               $msg->{user} eq $self_id and $old ne $msg->{presence};
1274         }
1275     },
1276     "manual_presence_change" => sub {
1277         my $msg = shift;
1278         my $user = $users{$self_id};
1279         my $old = $user->{Presence};
1280
1281         $user->{Presence} = $msg->{presence};
1282         irc_broadcast_away if $old ne $msg->{presence};
1283     },
1284     "im_open" => sub {
1285         my $msg = shift;
1286         rtm_record_unknown_uid $msg->{user};
1287
1288         my $u = $users{$msg->{user}};
1289         $u->{DMId} = $msg->{channel};
1290         $users_by_dmid{$msg->{channel}} = $u;
1291
1292         foreach my $msg (@{$u->{TxQueue}}) {
1293             rtm_send { type => "message",
1294                 channel => $u->{DMId}, text => $msg };
1295         }
1296
1297         $u->{TxQueue} = [];
1298     },
1299     "im_close" => sub {
1300         my $msg = shift;
1301         my $u = $users_by_dmid{$msg->{channel}};
1302         return unless defined($u);
1303
1304         delete $u->{DMId};
1305         delete $users_by_dmid{$msg->{channel}};
1306     },
1307     "group_joined" => sub {
1308         my $msg = shift;
1309
1310         rtm_update_channel "G", $msg->{channel};
1311         irc_broadcast_join $self_id, $msg->{channel}->{id};
1312     },
1313     "group_left" => sub {
1314         my $msg = shift;
1315
1316         irc_broadcast_part $self_id, $msg->{channel}
1317           if rtm_update_part $self_id, $msg->{channel};
1318     },
1319     "group_archive" => sub {
1320         my $msg = shift;
1321
1322         irc_broadcast_part $self_id, $msg->{channel}
1323           if rtm_update_part $self_id, $msg->{channel};
1324         rtm_delete_channel $msg->{channel};
1325     },
1326     "channel_joined" => sub {
1327         my $msg = shift;
1328
1329         rtm_update_channel "C", $msg->{channel};
1330         irc_broadcast_join $self_id, $msg->{channel}->{id};
1331     },
1332     "channel_left" => sub {
1333         my $msg = shift;
1334
1335         irc_broadcast_part $self_id, $msg->{channel}
1336           if rtm_update_part $self_id, $msg->{channel};
1337     },
1338     "channel_archive" => sub {
1339         my $msg = shift;
1340
1341         irc_broadcast_part $self_id, $msg->{channel}
1342           if rtm_update_part $self_id, $msg->{channel};
1343         rtm_delete_channel $msg->{channel};
1344     },
1345     "member_joined_channel" => sub {
1346         my $msg = shift;
1347
1348         rtm_record_unknown_uid $msg->{user};
1349         irc_broadcast_join $msg->{user}, $msg->{channel}
1350           if rtm_update_join($msg->{user}, $msg->{channel});
1351     },
1352     "member_left_channel" => sub {
1353         my $msg = shift;
1354
1355         irc_broadcast_part $msg->{user}, $msg->{channel}
1356           if rtm_update_part($msg->{user}, $msg->{channel});
1357     },
1358     "pong" => sub {
1359         $rtm_ping_count = 0;
1360     },
1361     "message" => sub {
1362         my $msg = shift;
1363         my $chan = $channels{$msg->{channel}};
1364         my $subtype = $msg->{subtype} || "";
1365         my $uid = $msg->{user} || $msg->{comment}->{user} || $msg->{bot_id};
1366         my $text = $msg->{text} // '';
1367
1368         if (defined($msg->{attachments})) {
1369             my $attext = join '\n', map {
1370                 ($_->{title} or "")
1371                   . " " . ($_->{text} or "")
1372                   . " " . ($_->{title_link} or "") } @{$msg->{attachments}};
1373             $text .= "\n" unless length($text);
1374             $text .= $attext;
1375         }
1376
1377         if (defined($chan)) {
1378             if ($subtype eq "channel_topic" or $subtype eq "group_topic") {
1379                 $chan->{Topic} = $msg->{topic};
1380                 irc_topic_change $uid, $chan->{Id};
1381             } else {
1382                 irc_chanmsg $uid, $msg->{subtype}, $chan->{Id}, $text;
1383             }
1384             rtm_mark_channel $chan->{Id}, $msg->{ts};
1385         } else {
1386             irc_privmsg $uid, $msg->{subtype}, $text;
1387         }
1388
1389         if ($subtype eq "file_share") {
1390             my $fid = $msg->{file}->{id};
1391             rtm_apicall "files.info", { file => $fid }, sub {
1392                 my $data = shift;
1393                 return unless defined $data;
1394
1395                 my $body = $data->{content};
1396                 return unless length($body) <= 65536;
1397
1398                 if (defined $chan) {
1399                     irc_chanmsg $uid, ">$fid", $chan->{Id}, $body;
1400                 } else {
1401                     irc_privmsg $uid, ">$fid", $body;
1402                 }
1403             };
1404         }
1405     },
1406 );
1407
1408 sub rtm_send_to_user {
1409     my ($id, $msg) = @_;
1410     my $u = $users{$id};
1411
1412     if (defined($u->{DMId}) && length($u->{DMId})) {
1413         rtm_send { type => "message",
1414                 channel => $u->{DMId}, text => $msg };
1415         return;
1416     }
1417
1418     push @{$u->{TxQueue}}, $msg;
1419
1420     if (!defined($u->{DMId})) {
1421         rtm_apicall "im.open", { user => $u->{Id} }, sub {
1422             my $result = shift;
1423             unless (defined $result) {
1424                 delete $u->{DMId};
1425                 foreach my $m (@{$u->{TxQueue}}) {
1426                     irc_broadcast_notice "Failed to send to $u->{Name}: $m";
1427                 }
1428                 $u->{TxQueue} = [];
1429             }
1430         };
1431
1432         $u->{DMId} = "";
1433     }
1434 }
1435
1436 sub rtm_start;
1437
1438 sub rtm_cooldown {
1439     return if defined($rtm_cooldown_timer);
1440     print "Waiting before reinitiating RTM\n";
1441     $rtm_cooldown_timer = AnyEvent->timer(after => 5, cb => sub {
1442         undef $rtm_cooldown_timer;
1443         rtm_start;
1444     });
1445 };
1446
1447 sub rtm_destroy {
1448     my $msg = shift;
1449     return unless defined($rtm_con);
1450
1451     irc_broadcast_notice $msg;
1452
1453     $connected = 0;
1454     undef $self_id;
1455     %channels = ();
1456     %channels_by_name = ();
1457     %users = ();
1458     %users_by_name = ();
1459     %users_by_dmid = ();
1460
1461     %rtm_apicall_handles = (); # cancel outstanding requests
1462     %rtm_mark_queue = ();
1463     undef $rtm_mark_timer;
1464     undef $rtm_ping_timer;
1465     $rtm_con->close;
1466     undef $rtm_con;
1467     undef $rtm_client;
1468
1469     irc_disconnect_all;
1470     rtm_cooldown;
1471 }
1472
1473 sub rtm_ping {
1474     if (++$rtm_ping_count >= 2) {
1475         rtm_destroy "RTM ping timeout";
1476         return;
1477     }
1478
1479     rtm_send { type => "ping" };
1480     $rtm_ping_timer = AnyEvent->timer(after => 60, cb => \&rtm_ping);
1481 }
1482
1483 sub rtm_start_ws {
1484     my $url = shift;
1485
1486     return if defined($rtm_client);
1487     $rtm_client = AnyEvent::WebSocket::Client->new;
1488
1489     print "WSS URL: $url\n" if $config{debug_dump};
1490     $rtm_client->connect($url)->cb(sub {
1491         $rtm_con = eval { shift->recv; };
1492         if ($@) {
1493             irc_broadcast_notice "WSS connection failed: $@\n";
1494             undef $rtm_client;
1495             rtm_cooldown;
1496         }
1497
1498         print "WSS connected\n";
1499         $rtm_msg_id = 1;
1500         $rtm_ping_count = 0;
1501         $connected = 1;
1502         irc_check_welcome_all;
1503
1504         $rtm_ping_timer = AnyEvent->timer(after => 60, cb => \&rtm_ping);
1505
1506         $rtm_con->on(each_message => sub {
1507             eval {
1508                 shift;
1509                 my $msg = decode_json shift->{body};
1510
1511                 print "RTM RECV: ", Dumper($msg) if $config{debug_dump};
1512                 irc_broadcast_notice "RTM error: $msg->{error}->{msg}"
1513                     if $msg->{error};
1514
1515                 if (defined $msg->{type}) {
1516                     my $handler = $rtm_command{$msg->{type}};
1517                     $handler->($msg) if defined($handler);
1518                 }
1519             };
1520             print "Error in message handler: $@" if $@;
1521         });
1522
1523         $rtm_con->on(finish => sub {
1524             eval {
1525                 my ($con) = @_;
1526
1527                 if (defined $con->close_error) {
1528                     rtm_destroy "RTM connection error: $con->close_error";
1529                 } elsif (defined $con->close_reason) {
1530                     rtm_destroy "RTM connection closed: $con->close_reason";
1531                 } else {
1532                     rtm_destroy "RTM connection finished";
1533                 }
1534             };
1535             print "Error in finish handler: $@" if $@;
1536         });
1537     });
1538 };
1539
1540 sub rtm_start {
1541     print "Requesting RTM connection\n";
1542     rtm_apicall "rtm.start", {}, sub {
1543         my $data = shift;
1544
1545         unless (defined($data)) {
1546             rtm_cooldown;
1547             return;
1548         }
1549
1550         $self_id = $data->{self}->{id};
1551
1552         foreach my $c (@{$data->{users}}) {
1553             rtm_update_user $c;
1554         }
1555
1556         foreach my $c (@{$data->{ims}}) {
1557             my $u = $users{$c->{user}};
1558
1559             $u->{DMId} = $c->{id};
1560             $users_by_dmid{$c->{id}} = $u;
1561         }
1562
1563         foreach my $c (@{$data->{channels}}) {
1564             rtm_update_channel "C", $c unless $c->{is_archived};
1565         }
1566
1567         foreach my $c (@{$data->{bots}}) {
1568             rtm_update_user $c;
1569             my $n = $c->{id};
1570         }
1571
1572         foreach my $c (@{$data->{groups}}) {
1573             rtm_update_channel "G", $c unless $c->{is_archived};
1574         }
1575
1576         rtm_start_ws $data->{url};
1577     };
1578 };
1579
1580 ########################################################################
1581 # RTM kick-off
1582 ########################################################################
1583
1584 my $cfgfile = shift || die "You must specify a config file";
1585 open(my $cfg, $cfgfile) || die "Can't open $cfgfile";
1586 foreach (<$cfg>) {
1587     chomp;
1588     $config{$1} = $2 if /^([-_0-9a-zA-Z]+)=(.*)$/;
1589 }
1590 close($cfg);
1591
1592 rtm_start;
1593 irc_listen;
1594 AnyEvent->condvar->recv;