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