2 # This is a simple script that connects stdin/stdout with a client
3 # connection to an irc server. the command line arguments are:
5 # $ ./debug_console <nick> <server> <port>
10 use AnyEvent::IRC::Client;
11 use AnyEvent::IRC::Util qw/mk_msg parse_irc_msg encode_ctcp/;
13 use Data::Dump qw(dump);
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';
20 my $c = AnyEvent->condvar;
21 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
22 my $con = new AnyEvent::IRC::Client;
24 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
26 warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";
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);
34 $con->send_msg( 'PRIVMSG', $room => "<$user> $message" );
42 warn "Couldn't connect: $err\n";
45 $stdout->push_write ("Connected!\n");
48 $con->register ($nick, $nick, $nick);
54 . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
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 );
71 "> " . mk_msg (undef, @msg) . "\n"
79 $stdin = AnyEvent::Handle->new (
82 warn "EOF on STDIN, disconnecting...\n";
83 $con->disconnect ("Console EOF");
86 warn "Error on STDIN: $!\n";
89 $stdin->push_read (line => sub {
90 my ($stdin, $line) = @_;
95 warn "eval error: $@\n";
97 $Data::Dumper::Terse = 1;
98 $stdout->push_write ("result: " . dump($r));
101 my $msg = parse_irc_msg ($line);
102 $con->send_msg ($msg->{command}, @{$msg->{params}});
109 warn "disconnect: $_[1]!\n";
114 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
116 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);