#!/usr/bin/perl -w ############################################################################ ## Astbot ## ## By Da Beave (beave@vistech.org) ## ## ## ## Updated: June 30th, 2005 - Released. Version "1.0". ## ## ## ## Description: This is a simple IRC "bot" that displays information ## ## about a Asterisk PBX. For example, you can use it to ## ## found out how many channels are up, how many people ## ## people are in a conference, etc. ## ## ## ## Check out : http://www.telephreak.org ## ## ## ############################################################################ use POE; use POE::Component::IRC; use Net::Telnet; # Used for connecting to the asterisk manager port. use DBI; use warnings; use strict; my $count; my $dbh; my $outpt; my $name = "Telephreak PBX"; # Your PBX's named ############################################################################ ## IRC server/nick/port information ## ############################################################################ my $ircserver = "irc.telephreak.org"; my $nickname = "AstBot"; my $channel = "#testing"; my $port = "6667"; my $debug = "1"; # Set to 0 for no debug output ############################################################################ ## Asterisk manager port authentication information ## ############################################################################ my $username = "myusername"; my $secret = "mypassword"; my $serverip = "127.0.0.1"; ############################################################################ ## Asterisk MySQL information (used to retreive # of VMBs ## ############################################################################ my $myusername = "asterisk"; my $mysecret = "mysecret"; my $myhost = "127.0.0.1"; my $myvm = "asterisk_vm"; # Asterisk Voicemail DB ############################################################################ ## Where to look for help ## ############################################################################ my $help = "Please see: http://www.telephreak.org/astbot.txt"; # Various var's that probably don't concern you. my $dbase; my @dataout; my $channelpattern="Seconds:"; my @outarray; my $outmsg; my $i; my $stripmsg; my $nick; my $ts; my $kicker; my $kicked; my $tn = new Net::Telnet (Port => 5038, Prompt => '/.*[\$%#>] $/', Output_record_separator => '', Errmode => 'return' ); sub CHANNEL () { "$channel" } # Create the component that will represent an IRC network. POE::Component::IRC->new("astbot"); # Create the bot session. The new() call specifies the events the bot # knows about and the functions that will handle those events. POE::Session->create( inline_states => { _start => \&bot_start, irc_001 => \&on_connect, irc_public => \&on_public, }, ); # The bot session has started. Register this bot with the "astbot" # IRC component. Select a nickname. Connect to a server. sub bot_start { my $kernel = $_[KERNEL]; my $heap = $_[HEAP]; my $session = $_[SESSION]; $kernel->post( astbot => register => "all"); $kernel->post( astbot => connect => { Nick => $nickname, Username => $nickname, Ircname => $nickname, Server => $ircserver, Port => $port, Debug => $debug, } ); } # The bot has successfully connected to a server. Join a channel. sub on_connect { $_[KERNEL]->post( astbot => join => CHANNEL ); } # The bot has received a public message. Parse it for commands, and # respond to interesting things. sub on_public { my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ]; my $nick = &remove_unwanted(( split /!/, $who )[0]); my $channel = $where->[0]; my $ts = scalar localtime; # Time for output ############################################################################ ## Get the number of active channels ## ############################################################################ if ( $msg eq ".channels" ) { print " [$ts] <$nick:$channel> $msg\n"; &connect_manager(); $tn->print("Action: Status\n\n"); @dataout = $tn->waitfor('/Event: StatusComplete/'); $tn->print("Action: Logoff\n\n"); $_ = $dataout[0]; $count=s/($channelpattern)/$1/g; if ( $count eq "" ) { $count="0"; } $outmsg="There are currently $count active channels up and operational on the $name"; $kernel->post( astbot => 'privmsg', $channel , $outmsg ); } ############################################################################ ## Get total number of voice mail boxes ## ############################################################################ if ( $msg eq ".voicemail" ) { print " [$ts] <$nick:$channel> $msg\n"; $dbase = "DBI:mysql:database=$myvm;host=$myhost"; $dbh = DBI->connect($dbase, $myusername, $mysecret) || die "Can't connect to asterisk_vm"; $outpt = $dbh->prepare("select count(mailbox) from users"); $outpt->execute || die "Can do query"; $count=$outpt->fetchrow_array; $outmsg="There are currently $count voicemail boxes on the $name."; $kernel->post( astbot => 'privmsg', $channel, $outmsg); print "$outmsg \n"; } ########################################################################### ## Get information about conferences ## ########################################################################### if ( $msg eq ".meetme" ) { print " [$ts] <$nick:$channel> $msg\n"; &connect_manager(); $tn->print("Action: Command\nCommand: meetme\n\n"); @dataout = $tn->waitfor('/--END COMMAND--/'); $tn->print("Action: Logoff\n\n"); @outarray = split('\n', $dataout[0]); foreach $i (4,5,6,7) { $kernel->post( astbot => 'privmsg', $channel , $outarray[$i] ); print "$outarray[$i] \n"; } } ########################################################################### ## Get system uptime (last reload, etc) ## ########################################################################### if ( $msg eq ".uptime" ) { print " [$ts] <$nick:$channel> $msg\n"; &connect_manager(); $tn->print("Action: Command\nCommand: show uptime\n\n"); @dataout = $tn->waitfor('/--END COMMAND--/'); $tn->print("Action: Logoff\n\n"); @outarray = split('\n', $dataout[0]); $outmsg = "$name Uptime:"; $kernel->post( astbot => 'privmsg', $channel , $outmsg ); foreach $i (4,5) { $kernel->post( astbot => 'privmsg', $channel, $outarray[$i] ); print "$outarray[$i] \n"; } } ############################################################################ ## Get version of Asterisk thats running ## ############################################################################ if ( $msg eq ".version" ) { print " [$ts] <$nick:$channel> $msg\n"; &connect_manager(); $tn->print("Action: Command\nCommand: show version\n\n"); @dataout = $tn->waitfor('/--END COMMAND--/'); $tn->print("Action: Logoff\n\n"); @outarray = split('\n', $dataout[0]); foreach $i (4,5) { $kernel->post( astbot => 'privmsg', $channel, $outarray[$i] ); print "$outarray[$i] \n"; } } ############################################################################ ## HALP! HALP! ## ############################################################################ if ( $msg eq ".help" ) { print " [$ts] <$nick:$channel> $msg\n"; $kernel->post( astbot => 'privmsg', $channel, $help ); } } ############################################################################# ## Trust nobody. Ever. ## ############################################################################# sub remove_unwanted { our $s; local($s) = @_; $s =~ s/[^A-Za-z0-9]//g if defined $s; $s =~ tr/A-Z/a-z/; return $s; } ############################################################################ ## Connect to the manager port, and log in please..... ## ############################################################################ sub connect_manager { $tn->open("$serverip"); $tn->waitfor('/0\n$/'); $tn->print("Action: Login\nUsername: $username\nSecret: $secret\n\n"); $tn->waitfor('/Authentication accept*/') || die "Can't login in"; } ############################################################################ ## Ready! Aim! FIRE! ## ############################################################################ $poe_kernel->run(); exit 0;