| 1 |
=encoding utf8 |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
POE::Component::IRC::Cookbook::Translator - A bot that can translate text |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
This bot uses L<POE::Component::Lingua::Translate|POE::Component::Lingua::Translate> |
| 10 |
to translate text for channel members. It makes use of the C<BotCommand> plugin |
| 11 |
to handle the translate command. |
| 12 |
|
| 13 |
=head1 DESCRIPTION |
| 14 |
|
| 15 |
#!/usr/bin/env perl |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
use Encode qw(decode); |
| 20 |
use Encode::Guess; |
| 21 |
use IRC::Utils qw(decode_irc parse_user); |
| 22 |
use POE; |
| 23 |
use POE::Component::IRC::State; |
| 24 |
use POE::Component::IRC::Plugin::AutoJoin; |
| 25 |
use POE::Component::IRC::Plugin::BotCommand; |
| 26 |
use POE::Component::Lingua::Translate; |
| 27 |
|
| 28 |
POE::Session->create( |
| 29 |
package_states => [ |
| 30 |
main => [ qw(_start irc_botcmd_trans translated) ] |
| 31 |
], |
| 32 |
heap => { |
| 33 |
translators => { }, |
| 34 |
} |
| 35 |
); |
| 36 |
|
| 37 |
$poe_kernel->run(); |
| 38 |
|
| 39 |
sub _start { |
| 40 |
my $heap = $_[HEAP]; |
| 41 |
my $irc = POE::Component::IRC::State->spawn( |
| 42 |
Nick => 'translator_bot', |
| 43 |
Server => 'irc.freenode.net', |
| 44 |
); |
| 45 |
$heap->{irc} = $irc; |
| 46 |
|
| 47 |
$irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new( |
| 48 |
Channels => [ '#test_channel1', '#test_channel2' ] |
| 49 |
)); |
| 50 |
|
| 51 |
$irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new( |
| 52 |
Commands => { |
| 53 |
trans => 'Usage: trans <from>,<to> <text>' |
| 54 |
} |
| 55 |
)); |
| 56 |
|
| 57 |
$irc->yield(register => 'botcmd_trans'); |
| 58 |
$irc->yield('connect'); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
sub irc_botcmd_trans { |
| 63 |
my $heap = $_[HEAP]; |
| 64 |
my $irc = $heap->{irc}; |
| 65 |
my $nick = parse_user( $_[ARG0] ); |
| 66 |
my $channel = $_[ARG1]; |
| 67 |
my ($from, $to, $text) = split /,|\s+/, $_[ARG2], 3; |
| 68 |
|
| 69 |
if (!exists $heap->{translators}->{$from . $to}) { |
| 70 |
eval { |
| 71 |
$heap->{translators}->{$from . $to} = POE::Component::Lingua::Translate->new( |
| 72 |
alias => $from . $to, |
| 73 |
back_end => 'Babelfish', |
| 74 |
src => $from, |
| 75 |
dest => $to, |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
if ($@) { |
| 80 |
$irc->yield(privmsg => $channel, "$nick: There was an error: $@"); |
| 81 |
return; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$poe_kernel->post($from . $to => translate => |
| 86 |
decode_irc($text), |
| 87 |
{ |
| 88 |
channel => $channel, |
| 89 |
nick => $nick, |
| 90 |
} |
| 91 |
); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
sub translated { |
| 96 |
my $irc = $_[HEAP]->{irc}; |
| 97 |
my ($text, $context, $error) = @_[ARG0, ARG1, ARG2]; |
| 98 |
|
| 99 |
if ($error) { |
| 100 |
$irc->yield( |
| 101 |
'privmsg', |
| 102 |
$context->{channel}, |
| 103 |
$context->{nick} . ": There was an error: $error", |
| 104 |
); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$irc->yield( |
| 109 |
'privmsg', |
| 110 |
$context->{channel}, |
| 111 |
$context->{nick} . ': ' . $text, |
| 112 |
); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
=head1 AUTHOR |
| 117 |
|
| 118 |
Hinrik E<Ouml>rn SigurE<eth>sson, [email protected] |
| 119 |
|