join IRC_ROOM and subscribe
[MQR.git] / scripts / mqr-irc-client.pl
1 #!/usr/bin/env perl
2 # This is a simple script that connects stdin/stdout with a client
3 # connection to an irc server. the command line arguments are:
4 #
5 # $ ./debug_console <nick> <server> <port>
6 #
7 use common::sense;
8 use IO::Handle;
9 use AnyEvent;
10 use AnyEvent::IRC::Client;
11 use AnyEvent::IRC::Util qw/mk_msg parse_irc_msg encode_ctcp/;
12 use AnyEvent::Redis;
13 use Data::Dump qw(dump);
14
15 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
16 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
17
18 my $c = AnyEvent->condvar;
19 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
20 my $con = new AnyEvent::IRC::Client;
21
22 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
23 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
24
25 $sub->psubscribe( 'channel *', sub {
26         my ( $message, $from ) = @_;
27         return unless $from !~ m/\Q$nick\E/; # skip our messages
28         warn "#Q<< $from | $message\n";
29         my ( undef, $channel, $user ) = split(/ /,$from,3);
30         $con->send_msg( $room => join(' ',$channel, $user, $message) );
31 });
32
33 $con->reg_cb (
34    connect => sub {
35       my ($con, $err) = @_;
36
37       if (defined $err) {
38          warn "Couldn't connect: $err\n";
39          $c->broadcast;
40       } else {
41          $stdout->push_write ("Connected!\n");
42       }
43
44       $con->register ($nick, $nick, $nick);
45    },
46    debug_recv => sub {
47       my ($con, $msg) = @_;
48       $stdout->push_write (
49          "< "
50          . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
51          . "\n"
52       );
53
54                 if ( $msg->{command} eq 'PRIVMSG' ) {
55                         my $channel = join(' ', 'channel', $msg->{params}->[0], $msg->{prefix});
56                         $pub->publish( $channel, $msg->{params}->[1] );
57                 } elsif ( $msg->{command} eq 'MODE' ) {
58                         $con->send_srv( JOIN => $room );
59                 }
60    },
61    debug_send => sub {
62       my ($con, @msg) = @_;
63       $stdout->push_write (
64          "> " . mk_msg (undef, @msg) . "\n"
65       );
66 warn dump(@msg);
67    },
68    registered => sub {
69       my ($con) = @_;
70
71       my $stdin;
72       $stdin = AnyEvent::Handle->new (
73          fh => \*STDIN,
74          on_eof => sub {
75             warn "EOF on STDIN, disconnecting...\n";
76             $con->disconnect ("Console EOF");
77          },
78          on_error => sub {
79             warn "Error on STDIN: $!\n";
80          },
81          on_read => sub {
82             $stdin->push_read (line => sub {
83                my ($stdin, $line) = @_;
84
85                if ($line =~ /^!/) {
86                   my $r = eval $line;
87                   if ($@) {
88                      warn "eval error: $@\n";
89                   } else {
90                      $Data::Dumper::Terse = 1;
91                      $stdout->push_write ("result: " . dump($r));
92                   }
93                } else {
94                   my $msg = parse_irc_msg ($line);
95                   $con->send_msg ($msg->{command}, @{$msg->{params}});
96                }
97             });
98          }
99       );
100    },
101    disconnect => sub {
102       warn "disconnect: $_[1]!\n";
103       $c->broadcast
104    },
105 );
106
107 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
108
109 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
110
111 $c->wait;