#!/usr/bin/perl
#
# Quick and dirty session interaction tool
#
# $Id: dcc-chat,v 1.3 2005/01/17 13:54:39 nemesis Exp $
#
# Copyright (C) 2002-2005 Kees Cook
# kees@outflux.net, http://outflux.net/
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
#
use IO::Handle;
use IO::Select;

$fd_from=$ARGV[0];
$fd_to=$ARGV[1];

$from=IO::Handle->new_from_fd($fd_from,"r") || warn "from failed";
$to=IO::Handle->new_from_fd($fd_to,"w") || warn "to failed";
select($from); $|=1;
select($to); $|=1;
select(STDOUT);

if (($pid=fork())==0)
{
        # child
        $to->close();
        while ( ($got = sysread($from,$line,16)) > 0)
        {
                # strip mIRC color for now...
                $line=~s/\d\d//g;
                print STDOUT $line;
        }
        exit(0);
}
else
{
        # parent
        $from->close();
        while ( $line = <STDIN> )
        {
                print $to $line || warn "print to";
        }
}
print STDOUT "Hit enter to close...";
<STDIN>;
undef $from;
undef $to;
wait();
