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);
33 $con->send_msg( 'PRIVMSG', $room => "<$user> $message" );
41 warn "Couldn't connect: $err\n";
44 $stdout->push_write ("Connected!\n");
47 $con->register ($nick, $nick, $nick);
53 . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
57 if ( $msg->{command} eq 'PRIVMSG' ) {
58 my ( $to, $body ) = @{ $msg->{params} };
59 my $channel = join(' ', 'MSG', "$nick/$to", $msg->{prefix});
60 warn "#Q>> $channel | $body\n";
61 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
62 $pub->publish( $channel, $body );
63 } elsif ( $msg->{command} eq 'MODE' ) {
64 $con->send_srv( JOIN => $room );
70 "> " . mk_msg (undef, @msg) . "\n"
78 $stdin = AnyEvent::Handle->new (
81 warn "EOF on STDIN, disconnecting...\n";
82 $con->disconnect ("Console EOF");
85 warn "Error on STDIN: $!\n";
88 $stdin->push_read (line => sub {
89 my ($stdin, $line) = @_;
94 warn "eval error: $@\n";
96 $Data::Dumper::Terse = 1;
97 $stdout->push_write ("result: " . dump($r));
100 my $msg = parse_irc_msg ($line);
101 $con->send_msg ($msg->{command}, @{$msg->{params}});
108 warn "disconnect: $_[1]!\n";
113 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
115 $con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);