#!/usr/bin/perl
#
# Public translation tool. Gates text to google translations
# Versatile way: "!translate de|en String to translate"  - Convert German to English.
# Easy way: "!german string" to convert German to English. (Apologies for English-centric scripting).
# Also supported: !german !french, !dutch, !chinese, !japanese, !korean, !russian, !portugese, !arabic, !italian, !spanish !malay and etc.
# Usage: !translate french [Words Here], !malay [Words Here], or see to add new shortcuts.
# Freeware by Flash  - http://digdilem.org
#
# v.002 - Small change to reflect a change in google's html
# v.003 - Added polish
# v.004 - Google changed site, added workaround
# v.005 - More google changes - 140910, regexp reworked.
#
my $trigger='.tr';
# End user config
my $translate_url = "http://translate.google.com/translate_t";
my $ver = 'i005a';
my $title="Flash's Translate";
use LWP::UserAgent;
use URI::Escape;
use strict;
use vars qw($VERSION %IRSSI);
use Irssi;
$VERSION = $ver;
%IRSSI = (
    authors     => 'Flash',
    contact     => 'flash@digdilem.org',
    name        => 'Translate',
    description => 'A useful irc translation tool',
    license     => 'Do what thou wilt, and it harm none',
);
Irssi::signal_add("message public", \&translate);
Irssi::signal_add("message own_public", \&translate);
#

sub translate {
    my ($server, $data, $hunter, $mask, $chan) = @_;  #my ($server, $data, $channel) = @_;
	if (!defined $chan) { $chan = $hunter; }
	my @rowr = split(/ /,$data);
	my $itsforme=0;
	my $langs;
	my $text;
	my $trans;
	if ($rowr[0] eq $trigger) {
		$langs = $rowr[1]; # Get calling command
		$trans = join ' ', @rowr[2 .. @rowr];
		$itsforme=1;
		}
	if (lc($rowr[0]) eq '.german') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="de|en";
		}
	if (lc($rowr[0]) eq '.french') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="fr|en";
		}
	if (lc($rowr[0]) eq '.dutch') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="nl|en";
		}
	if (lc($rowr[0]) eq '.chinese') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="zh|en";
		}
	if (lc($rowr[0]) eq '.arabic') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ar|en";
		}
			if (lc($rowr[0]) eq '!greek') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="el|en";
		}
	if (lc($rowr[0]) eq '.italian') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="it|en";
		}
	if (lc($rowr[0]) eq '.polish') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="pl|en";
		}
	if (lc($rowr[0]) eq '!spanish') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="es|en";
		}
	if (lc($rowr[0]) eq '.russian') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ru|en";
		}
	if (lc($rowr[0]) eq '.japanese') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ja|en";
		}
	if (lc($rowr[0]) eq '.portugese') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="pt|en";
		}
	if (lc($rowr[0]) eq '.korean') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ko|en";
		}
        if (lc($rowr[0]) eq '.malay') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ms|en";
		}
        if (lc($rowr[0]) eq '.indonesian') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="ind|en";
		}
        if (lc($rowr[0]) eq '.turkey') {
		$trans = join ' ', @rowr[1 .. @rowr];
		$itsforme=1;
		$langs="tur|en";
		}
       if ($itsforme == 0) { return; } # I don't recognise any of this.

	chop($trans); # remove trailing space
	if (($langs eq '') or ($trans eq '')) {
		$server->command("msg $chan 0,1 Format Use $trigger fr|en [Words] vice versa! 12g4o7og3l4e 12t4r7an3s4l12a4t7e");
		return;
		}

		my $ua = new LWP::UserAgent;
		my $req = new HTTP::Request POST => 'http://translate.google.com/translate_t';
		$ua->agent("Translate/1.0");

		$req->content_type('application/x-www-form-urlencoded');
		my $text = "text=" . uri_escape($trans);
		my $options = "hl=en&ie=UTF8";

		my ($sl,$tl) = split(/\|/,$langs);
		$req->content($text . "&sl=$sl&tl=$tl" . "&" . $options);

		my $rv;
		my $res = $ua->request($req);
		if ($res->is_success) {
			if( $res->content =~ /this.style.backgroundColor='#fff'">(.*?)</ ) {
				#"this.style.backgroundColor='#fff'">Schweine sterben Hund</span>
			$rv = $1;
			$rv =~ s/\\ /\\\n/g
			}
		} else { $server->command("msg $chan Translation failed Pls refer http://translate.google.com"); return; }

		$rv =~ s/&amp;/&/g;
		$rv =~ s/&#39;/'/g;
		$rv =~ s/&quot;/"/g;


		$server->command("msg $chan 6Translation ($langs)14:". $rv);
		return;
}
