#!/usr/bin/perl -w
# Bot traductor (se basa en google)
# By Sha0 4 BadCheckSum.tk

use IO::Socket;
use LWP;
require HTTP::Headers;
require LWP::UserAgent;


my $ircd='irc.blessed.net';
my $port=6667;
my $timeout=10;
my $nick='translt';
my $channel='linuxlab';
my $dentro=0;
my $motordetraduccion='http://translate.google.com/translate_t'; 
my $tranmode=0;

my @langpairs = ("en|de","en|es","en|fr","en|it","en|pt","en|ja","en|ko","en|zh-CN","de|en","de|fr","es|en","fr|en","fr|de",
		 "it|en","pt|en","ja|en","ko|en","zh-CN|en");

my $lang="en|pt"; #$langpairs[4];


my $sock= new IO::Socket::INET (
	PeerAddr => $ircd,
	PeerPort => $port,
	Timeout  => $timeout
);

$sock->send("USER $nick a b c :yeap\r\nNICK $nick \r\n");
sleep 1;
$sock->send("JOIN #$channel \r\n");


while (<$sock>) {

	s/\r//g;
	s/\n//g;


	if ($_ =~ /PING/i && !($_ =~ /PRIVMSG/i)) {
		s/PING/PONG/g;
		$sock->send($_);
		if (!$dentro) {
			$dentro=1;
			$sock->send("JOIN #$channel \r\n");
		}
	} else {

		if ($_ =~ /\$/ && $_ =~ /PRIVMSG/i) {
			s/.*\$//;
			$sock->send("PRIVMSG #$channel :".&traduce($_)."\r\n");
		}

		if ($_ =~ /\#langpairs/ && $_ =~ /PRIVMSG/i) {
			$sock->send("PRIVMSG #$channel :@langpairs\r\n");
			$sock->send("PRIVMSG #$channel :lenguage actual $lang\r\n");
		}

		if ($_ =~ /\#lang / && $_ =~ /PRIVMSG/i) {
		  	s/.*\#lang//g;
			s/ //g;
			$lang=$_;
			$sock->send("PRIVMSG #$channel :lenguage cambiado a >$lang<\r\n");
		}

		if ($_ =~ /\#trans/ && $_ =~ /PRIVMSG/i) {
                        s/.*\#trans//g;
                        s/ //g;
			$transmode=$_;
			$sock->send("JOIN #$transmode \r\n");
			$sock->send("JOIN #".$transmode."tr \r\n");
			
		}

		if ($transmode && $_ =~ /PRIVMSG #$transmode/) {
			$phrase = &traduce($_);
			$sock->send("PRIVMSG #".$transmode."tr :$phrase \r\n");
		}

	}
}

close ($sock);



sub traduce {
	($str) = @_;

	#pedir por post a translate_t   text="texto a traducir" langpair="es|de"  hl="en" ie="UTF8"
	#he cogido este codigo de simkin ;) bot en 5 min r00lz ;)
	#local $usr_agent = LWP::UserAgent->new('agent'=>'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041219 Firefox/1.0 (Debian package 1.0+dfsg.1-1)');
	#local $usr_agent->timeout(10);
	#local $usr_agent->max_size(60000);

	#$hdr = HTTP::Headers->new(
	#'Host' => 'www.google.com',
	#'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
	#'Accept-Language' => 'es,en-us;q=0.7,en;q=0.3',
	#'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
	#'Keep-Alive' => '300',
	#'Connection' => 'keep-alive',
	#'Referer' => 'http://www.google.com/search?q=sfa&hl=es&lr=&start=20&sa=N');


	#$request = HTTP::Request->new(POST=>$urlget,$hdr);
	#$response = $usr_agent->request($request);
	#$plain_html = $response->content;
	
	#pedir por post a translate_t   text="texto a traducir" langpair="es|de"  hl="en" ie="UTF8"

	my $ua = LWP::UserAgent->new;
	$ua->agent("Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
	$ua->timeout(10);
	$ua->max_size(60000);
	
	local $query="text=$str&langpair=$lang&hl=en&ie=UTF8";
	local $req = HTTP::Request->new(POST => $motordetraduccion);

	$req->content_type('application/x-www-form-urlencoded');
	$req->content($query); 

	local $response = $ua->request($req);
	sleep 1;
	local $content = $response->content(); #contenido de la respuesta

	#local @words = ($content =~ /\>[a-z]*|[A-Z]*|[0-9]*|[\b]*<\/textarea/g);
	local @words = ($content =~ /PHYSICAL\>.{1,255}<\/textarea/g);

	$words[0] =~ s/PHYSICAL\>//g;
	$words[0] =~ s/\<\/textarea//g;

	return "$words[0]";
}
