###########################################
# Babelfish Gaim Translator.
# Copyright Donour Sizemore 2002
#
############################################
use Lingua::Translate;

GAIM::register("Gaim Translator", "0.58", "goodbye", "");

my %trans_send_stat;
my %trans_send_obj;
my %trans_send_src;
my %trans_send_dest;

my %trans_recv_obj;
my %trans_recv_src;
my %trans_recv_dest;

my $translation = 1;
my $default_src = "en";
my $default_dest = "de";
my $inds = GAIM::get_info(1);

GAIM::add_event_handler("event_im_send", "trans_im_send");
GAIM::add_event_handler("event_im_recv", "trans_im_recv");
GAIM::add_event_handler("event_new_conversation", "trans_new_conv");

sub goodbye {
  GAIM::print(" ", "Translate.pl Unloaded");
}

sub trans_new_conv {  
    $trans_send_stat{$_[0]} = 0;
    $trans_send_obj{$_[0]} = Lingua::Translate->new(src => "$default_src",dest => "$default_dest")
	or die "No translation server available";
    $trans_send_src{$_[0]} = "$default_src";
    $trans_send_dest{$_[0]} = "$default_dest";

    $trans_recv_stat{$_[0]} = 0;
    $trans_recv_obj{$_[0]} = Lingua::Translate->new(src => "$default_dest",dest => "$default_src")
	or die "No translation server available";
    $trans_recv_src{$_[0]} = "$default_dest";
    $trans_recv_dest{$_[0]} = "$default_srct";
}

sub trans_im_send {
    
    my $cmd = $_[2];
    $cmd =~ /\/language_send_dest\s*(.*)/;
    if(length($1) >= 2 ){
	# Check dest language
	if($trans_send_src{$_[1]} eq "en"){ # From english TO
	    if($1 ne "fr" &&               # French
	       $1 ne "de" &&               # German
	       $1 ne "es" &&               # Spanish
	       $1 ne "it" &&               # Italian
	       $1 ne "pt" &&               # Portuguese
	       $1 ne "ko" &&               # Korean
	       $1 ne "ja" &&               # Japanese
	       $1 ne "zh"                  # Chinese
	       ){
	      GAIM::write_to_conv($_[1], 2, "$trans_send_src{$_[1]} - > $1 not supported", $_[0]);
		return 1;
	    }
	    
	}
	$trans_send_dest{$_[1]} = $1;
	$trans_send_obj{$_[1]} = Lingua::Translate->new(src => "$trans_send_src{$_[1]}",dest => "$1")
	    or die "No translation server available for en -> de";
        GAIM::write_to_conv($_[1], 2, "Outgoing translation $trans_send_src{$_[1]} -> $1", $_[0]); 
	return 1;
    }
    $cmd = $_[2];
    $cmd =~ /\/language_send_src\s*(.*)/;
    if(length($1) >= 2){
       
	$trans_send_src{$_[1]} = $1;
	$trans_send_obj{$_[1]} = Lingua::Translate->new(src => "$1",dest => "$trans_send_dest{$_[1]}")
	    or die "No translation server available for en -> de";
        GAIM::write_to_conv($_[1], 2, 
	    "Outgoing translation $trans_send_src{$_[1]} -> $trans_send_dest{$_[1]}", $_[0]); 
	
	return 1;
    }

    if($_[2] eq "/outfish"){
	if($trans_send_stat{$_[1]} == 1 ){
	    $trans_send_stat{$_[1]} = 0;
	    GAIM::write_to_conv($_[1], 2, "Sending translation off", $_[0]); 
	}else{

	    $trans_send_stat{$_[1]} = 1;
	    GAIM::write_to_conv($_[1], 2, "Sending translation on", $_[0]); 
	}
	return 1;
    }
    if($_[2] eq "/infish"){
	if($trans_recv_stat{$_[1]} == 1 ){
	    $trans_recv_stat{$_[1]} = 0;
	    GAIM::write_to_conv($_[1], 2, "Recv translation off", $_[0]); 
	}else{

	    $trans_recv_stat{$_[1]} = 1;
	    GAIM::write_to_conv($_[1], 2, "Recv translation on", $_[0]); 
	}
	return 1;
    }
    if($trans_send_stat{$_[1]} == 1){
	
	#kludge, stuff that doesn't get translated
	if($_[2] eq '?'){
	      return 0;
	}
	
	$result = $trans_send_obj{$_[1]}->translate($_[2]);
        GAIM::print_to_conv($inds, $_[1], "$result", 0);  
	return 1;
    }

}

sub trans_im_recv {
    if($trans_recv_stat{$_[1]} == 1){
	
	#kludge, stuff that doesn't get translated
	if($_[2] eq '?'){
	    return 0;
	}
	
	
	$result = $trans_recv_obj{$_[1]}->translate($_[2]);
      GAIM::write_to_conv($_[1], 1, "$result", $_[1]);  
	return 1;
    }
    # FIXME
}

