#!/usr/bin/env perl
# This is a simple script that connects stdin/stdout with a client
# connection to an irc server. the command line arguments are:
#
# $ ./debug_console <nick> <server> <port>
#
use common::sense;
use IO::Handle;
use AnyEvent;
use AnyEvent::IRC::Client;
use AnyEvent::IRC::Util qw/mk_msg parse_irc_msg encode_ctcp/;
use AnyEvent::Redis;
use Data::Dump qw(dump);
use Carp qw(confess);

use lib 'lib';
use MQR::Redis;

my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
my $server = $ENV{IRC_SERVER} || die 'IRC_SERVER';
my $subscribe  = $ENV{IRC_SUBSCRIBE} || die 'IRC_SUBSCRIBE';

my $c = AnyEvent->condvar;
my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
my $con = new AnyEvent::IRC::Client;

warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";

MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
	my ( $message, $from ) = @_;
	warn "<<<< ",dump( $from, $message );
	return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
	my ( undef, $gw, $type, $user ) = split(/ /,$from);
	MQR::Redis->publish( "SEND $nick\@$server $room" => "<$user> $message" );
});

MQR::Redis->redis->psubscribe( "SEND $nick\@$server $room", sub {
	my ( $message, $from ) = @_;
	warn "<<<< ",dump( $from, $message );
	$con->send_msg( 'PRIVMSG', $room => $message );
});

$con->reg_cb (
   connect => sub {
      my ($con, $err) = @_;

      if (defined $err) {
         warn "Couldn't connect: $err\n";
         $c->broadcast;
      } else {
         $stdout->push_write ("Connected!\n");
      }

      $con->register ($nick, $nick, $nick);
   },
   debug_recv => sub {
      my ($con, $msg) = @_;
      $stdout->push_write (
         "< "
         . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
         . "\n"
      );

		if ( $msg->{command} eq 'PRIVMSG' ) {
			my ( $to, $body ) = @{ $msg->{params} };
			my $uid = $msg->{prefix};
			my $user = $uid;
			$user =~ s{!.+$}{};
			MQR::Redis->publish( "MSG $nick\@$server $to $user $uid" => $body );
		} elsif ( $msg->{command} eq 'MODE' ) {
			$con->send_srv( JOIN => $room );
		}
   },
   debug_send => sub {
      my ($con, @msg) = @_;
      $stdout->push_write (
         "> " . mk_msg (undef, @msg) . "\n"
      );
warn dump(@msg);
   },
   registered => sub {
      my ($con) = @_;

      my $stdin;
      $stdin = AnyEvent::Handle->new (
         fh => \*STDIN,
         on_eof => sub {
            warn "EOF on STDIN, disconnecting...\n";
            $con->disconnect ("Console EOF");
         },
         on_error => sub {
            warn "Error on STDIN: $!\n";
         },
         on_read => sub {
            $stdin->push_read (line => sub {
               my ($stdin, $line) = @_;

               if ($line =~ /^!/) {
                  my $r = eval $line;
                  if ($@) {
                     warn "eval error: $@\n";
                  } else {
                     $Data::Dumper::Terse = 1;
                     $stdout->push_write ("result: " . dump($r));
                  }
               } else {
                  my $msg = parse_irc_msg ($line);
                  $con->send_msg ($msg->{command}, @{$msg->{params}});
               }
            });
         }
      );
   },
   disconnect => sub {
      warn "disconnect: $_[1]!\n";
      $c->broadcast
   },
);

$con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);

$con->connect ($server, $ENV{IRC_PORT} || 6667);

$c->wait;
