]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/translate.pl
fix unsetting autojoin when trying to join invite-only channel
[infobot.git] / src / Modules / translate.pl
1 # This program is distributed under the same terms as infobot.
2 # hacked by Tim@Rikers.org
3
4 package translate;
5 use strict;
6
7 my $no_translate;
8 my $url = 'http://translate.google.com/translate_a/t';
9
10 BEGIN {
11         eval "use URI::Escape"; # utility functions for encoding the
12         if ($@) { $no_translate++ }
13         eval "use LWP::UserAgent";
14         if ($@) { $no_translate++ }
15         eval "use JSON";
16         if ($@) { $no_translate++ }
17 }
18
19 sub translateParam {
20         return '' if $no_translate;
21         my ( $from, $to, $phrase ) = @_;
22         &::DEBUG("translate($from, $to, $phrase)");
23
24         my $ua = new LWP::UserAgent;
25         $ua->proxy( 'http', $::param{'httpProxy'} ) if ( &::IsParam('httpProxy') );
26
27         # Let's pretend
28         $ua->agent( "Mozilla/5.0 " . $ua->agent );
29         $ua->timeout(5);
30
31         my $req = HTTP::Request->new('GET', 'http://translate.google.com/translate_a/t?client=t&ie=UTF-8&oe=UTF-8&sl='.$from.'&tl='.$to.'&text='.$phrase);
32
33         $req->header('Accept-Language' => 'en-us');
34         $req->header('Accept-Charset' => 'UTF-8,*');
35         my $json = JSON->new->utf8;
36         my $json_text=$ua->request($req)->content;
37         $json_text =~ s/,,/,"",/g;
38         $json_text =~ s/,,/,"",/g;
39         #&::DEBUG($json_text);
40         my @decoded_json = from_json($json_text);
41
42         return $decoded_json[0][0][0][0];
43 }
44
45 sub translate {
46         my ($message) = @_;
47         if ($message =~ m{(\S*)\s+(\S*)\s+(.+)}xoi) {
48                 &::performStrictReply( &translateParam( lc $1, lc $2, lc $3 ) );
49         }
50         return;
51 }
52
53 1;
54
55 # vim:ts=4:sw=4:expandtab:tw=80