03f981ae4aea32b7cfbb23bd8c26fc9f3059fb5a
[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 use Carp qw(confess);
15
16 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
17 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
18 my $subscribe  = $ENV{IRC_SUBSCRIBE} || die 'IRC_SUBSCRIBE';
19
20 my $c = AnyEvent->condvar;
21 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
22 my $con = new AnyEvent::IRC::Client;
23
24 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
25
26 warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";
27
28 $sub->psubscribe( "MSG $subscribe", sub {
29         my ( $message, $from ) = @_;
30         return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
31         warn "#Q<< ",dump( $from, $message );
32         my ( undef, $gw, $user ) = split(/ /,$from,3);
33         $user =~ s{^.+/}{};
34         $con->send_msg( 'PRIVMSG', $room => "<$user> $message" );
35 });
36
37 $con->reg_cb (
38    connect => sub {
39       my ($con, $err) = @_;
40
41       if (defined $err) {
42          warn "Couldn't connect: $err\n";
43          $c->broadcast;
44       } else {
45          $stdout->push_write ("Connected!\n");
46       }
47
48       $con->register ($nick, $nick, $nick);
49    },
50    debug_recv => sub {
51       my ($con, $msg) = @_;
52       $stdout->push_write (
53          "< "
54          . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
55          . "\n"
56       );
57
58                 if ( $msg->{command} eq 'PRIVMSG' ) {
59                         my ( $to, $body ) = @{ $msg->{params} };
60                         my $channel = join(' ', 'MSG', "$nick/$to", $msg->{prefix});
61                         warn "#Q>> $channel | $body\n";
62                         my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
63                         $pub->publish( $channel, $body );
64                 } elsif ( $msg->{command} eq 'MODE' ) {
65                         $con->send_srv( JOIN => $room );
66                 }
67    },
68    debug_send => sub {
69       my ($con, @msg) = @_;
70       $stdout->push_write (
71          "> " . mk_msg (undef, @msg) . "\n"
72       );
73 warn dump(@msg);
74    },
75    registered => sub {
76       my ($con) = @_;
77
78       my $stdin;
79       $stdin = AnyEvent::Handle->new (
80          fh => \*STDIN,
81          on_eof => sub {
82             warn "EOF on STDIN, disconnecting...\n";
83             $con->disconnect ("Console EOF");
84          },
85          on_error => sub {
86             warn "Error on STDIN: $!\n";
87          },
88          on_read => sub {
89             $stdin->push_read (line => sub {
90                my ($stdin, $line) = @_;
91
92                if ($line =~ /^!/) {
93                   my $r = eval $line;
94                   if ($@) {
95                      warn "eval error: $@\n";
96                   } else {
97                      $Data::Dumper::Terse = 1;
98                      $stdout->push_write ("result: " . dump($r));
99                   }
100                } else {
101                   my $msg = parse_irc_msg ($line);
102                   $con->send_msg ($msg->{command}, @{$msg->{params}});
103                }
104             });
105          }
106       );
107    },
108    disconnect => sub {
109       warn "disconnect: $_[1]!\n";
110       $c->broadcast
111    },
112 );
113
114 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
115
116 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
117
118 $c->wait;