| 1 |
=encoding utf8 |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
POE::Component::IRC::Cookbook::Resolver - A bot that can resolve DNS records |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
This bot uses L<POE::Component::Client::DNS|POE::Component::Client::DNS> |
| 10 |
to DNS records for channel members. |
| 11 |
|
| 12 |
=head1 DESCRIPTION |
| 13 |
|
| 14 |
#!/usr/bin/env perl |
| 15 |
|
| 16 |
use strict; |
| 17 |
use warnings; |
| 18 |
use IRC::Utils qw(parse_user); |
| 19 |
use POE; |
| 20 |
use POE::Component::Client::DNS; |
| 21 |
use POE::Component::IRC::State; |
| 22 |
use POE::Component::IRC::Plugin::AutoJoin; |
| 23 |
use POE::Component::IRC::Plugin::BotCommand; |
| 24 |
|
| 25 |
POE::Session->create( |
| 26 |
package_states => [ |
| 27 |
main => [ qw(_start irc_botcmd_resolve dns_response) ] |
| 28 |
], |
| 29 |
); |
| 30 |
|
| 31 |
$poe_kernel->run(); |
| 32 |
|
| 33 |
sub _start { |
| 34 |
my $heap = $_[HEAP]; |
| 35 |
my $irc = POE::Component::IRC::State->spawn( |
| 36 |
Nick => 'resolver_bot', |
| 37 |
Server => 'irc.freenode.net', |
| 38 |
); |
| 39 |
$heap->{irc} = $irc; |
| 40 |
|
| 41 |
$irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new( |
| 42 |
Channels => [ '#test_channel1', '#test_channel2' ] |
| 43 |
)); |
| 44 |
|
| 45 |
$irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new( |
| 46 |
Commands => { |
| 47 |
resolve => 'Usage: resolve <host>' |
| 48 |
} |
| 49 |
)); |
| 50 |
|
| 51 |
$heap->{dns} = POE::Component::Client::DNS->spawn(); |
| 52 |
|
| 53 |
$irc->yield(register => 'botcmd_resolve'); |
| 54 |
$irc->yield('connect'); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
sub irc_botcmd_resolve { |
| 59 |
my $dns = $_[HEAP]->{dns}; |
| 60 |
my $nick = parse_user( $_[ARG0] ); |
| 61 |
my ($channel, $host) = @_[ARG1, ARG2]; |
| 62 |
|
| 63 |
my $res = $dns->resolve( |
| 64 |
event => 'dns_response', |
| 65 |
host => $host, |
| 66 |
context => { |
| 67 |
channel => $channel, |
| 68 |
nick => $nick, |
| 69 |
}, |
| 70 |
); |
| 71 |
|
| 72 |
$poe_kernel->yield(dns_response => $res) if $res; |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
sub dns_response { |
| 77 |
my $irc = $_[HEAP]->{irc}; |
| 78 |
my $res = $_[ARG0]; |
| 79 |
my @answers = $res->{response} |
| 80 |
? map { $_->rdstring } $res->{response}->answer() |
| 81 |
: (); |
| 82 |
|
| 83 |
$irc->yield( |
| 84 |
'privmsg', |
| 85 |
$res->{context}->{channel}, |
| 86 |
$res->{context}->{nick} . (@answers |
| 87 |
? ": @answers" |
| 88 |
: ': no answers for "' . $res->{host} . '"') |
| 89 |
); |
| 90 |
|
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
=head1 AUTHOR |
| 95 |
|
| 96 |
Hinrik E<Ouml>rn SigurE<eth>sson, [email protected] |
| 97 |
|