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