################################ # DNS Module # ################################ # Copyright 2010 Lenny Herold # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . package BotModules::DNS; use vars qw(@ISA); use Net::DNS; @ISA = qw(BotModules); 1; # RegisterConfig - Called when initialised, should call registerVariables sub RegisterConfig { my $self = shift; $self->SUPER::RegisterConfig(@_); $self->registerVariables( # [ name, save?, settable? ] ); } sub Help { my $self = shift; my ($event) = @_; return { '' => "This module performs hostname and reverse DNS lookups.", 'host, !host' => 'Perform a lookup for the specified hostname or IP.', 'mx, !mx' => 'Perform an MX record lookup for the specified hostname or IP.', }; } sub Told { my $self = shift; my ($event, $message) = @_; if ($message =~ /^(\s*!?host\s+)(.+)$/osi) { my $lookup = $2; my @result = host_lookup($lookup); foreach (@result) { $self->say($event, "$lookup $_") } } elsif ($message =~/^(\s*!?mx\s+)(.+)$/osi) { my $lookup = $2; my @result = mx_lookup($lookup); unless ($result[0] =~ /failed/) { foreach my $record (@result) { $self->say($event, "$lookup mail is handled by " . $record->preference . " " . $record->exchange . "\n") } } else { $self->say($event, $result[0]) } } else { return $self->SUPER::Told(@_); } return 0; } sub Heard { my $self = shift; my($event, $message) = @_; if ($message =~ /^(\s*!?host\s+)(.+)$/) { my $lookup = $2; my @result = host_lookup($lookup); foreach (@result) { $self->say($event, "$lookup $_") } } elsif ($message =~/^(\s*!?mx\s+)(.+)$/osi) { my $lookup = $2; my @result = mx_lookup($lookup); unless ($result[0] =~ /failed/) { foreach my $record (@result) { $self->say($event, "$lookup mail is handled by " . $record->preference . " " . $record->exchange . "\n") } } else { $self->say($event, $result[0]) } } else { return $self->SUPER::Heard(@_); } return 0; } sub host_lookup { my $host = shift; my @answer; my $res = Net::DNS::Resolver->new; my $query = $res->search("$host"); if ($query) { foreach my $rr ($query->answer) { if ($rr->type eq "A" ) { push (@answer, ("has address " . $rr->address . "\n")) } if ($rr->type eq "CNAME" ) { push (@answer, ("is an alias for " . $rr->cname . "\n")) } } } else { push (@answer, "Lookup failed: " . $res->errorstring . "\n"); } return @answer; } sub mx_lookup { my $host = shift; my $res = Net::DNS::Resolver->new; my @answer = mx($res, $host); unless (@answer) { push (@answer, "Lookup failed: " . $res->errorstring . "\n") } return @answer; }