mark redis messages as utf8
[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 Encode;
15
16 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
17 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
18
19 warn "# $ENV{IRC_SERVER}:$ENV{IRC_PORT} $room $nick\n";
20
21 my $c = AnyEvent->condvar;
22 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
23 my $con = new AnyEvent::IRC::Client;
24
25 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } );
26 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } );
27
28 $sub->psubscribe( 'channel *', sub {
29         my ( $message, $from ) = @_;
30         return unless $from !~ m/\Q$nick\E/; # skip our messages
31         Encode::_utf8_on($message);
32         warn "#Q<< ",dump( $from, $message );
33         my ( undef, $channel, $user ) = split(/ /,$from,3);
34         $con->send_msg( 'PRIVMSG', $room => join(' ',$channel, $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 $channel = join(' ', 'channel', $msg->{params}->[0], $msg->{prefix});
60                         $pub->publish( $channel, $msg->{params}->[1] );
61                 } elsif ( $msg->{command} eq 'MODE' ) {
62                         $con->send_srv( JOIN => $room );
63                 }
64    },
65    debug_send => sub {
66       my ($con, @msg) = @_;
67       $stdout->push_write (
68          "> " . mk_msg (undef, @msg) . "\n"
69       );
70 warn dump(@msg);
71    },
72    registered => sub {
73       my ($con) = @_;
74
75       my $stdin;
76       $stdin = AnyEvent::Handle->new (
77          fh => \*STDIN,
78          on_eof => sub {
79             warn "EOF on STDIN, disconnecting...\n";
80             $con->disconnect ("Console EOF");
81          },
82          on_error => sub {
83             warn "Error on STDIN: $!\n";
84          },
85          on_read => sub {
86             $stdin->push_read (line => sub {
87                my ($stdin, $line) = @_;
88
89                if ($line =~ /^!/) {
90                   my $r = eval $line;
91                   if ($@) {
92                      warn "eval error: $@\n";
93                   } else {
94                      $Data::Dumper::Terse = 1;
95                      $stdout->push_write ("result: " . dump($r));
96                   }
97                } else {
98                   my $msg = parse_irc_msg ($line);
99                   $con->send_msg ($msg->{command}, @{$msg->{params}});
100                }
101             });
102          }
103       );
104    },
105    disconnect => sub {
106       warn "disconnect: $_[1]!\n";
107       $c->broadcast
108    },
109 );
110
111 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
112
113 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
114
115 $c->wait;