#!/usr/bin/perl use strict; use IO::Socket; use Time::HiRes; # this moudule will be used to time difference... in future.. use DBI; #$hash{$USER}{0}{1} = Time::HiRes::time(); #$hash{$USER}{1}{1} = Time::HiRes::time(); # $time = $hash{$USER}{0}{1} - $hash{$USER}{1}{1}; my $sec; my $min; my %hash = (); my %user_hash = (); # this hash table will maintain user Clone Line enable/disable status... my %call_hash = (); my $socket = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => 5038, Proto => 'tcp', ) or die "Couldn't connect to Server\n"; my $tmp; # login string... $tmp="Action: Login\r\nUsername: admin\r\nSecret: secret5\r\nActionID: 1\r\n\r\n"; my $recv_data; if($socket->send($tmp) == 0){ print "could not connect to Asterisk server"; exit(1); } #while(1){ # $socket->recv($recv_data,1024); # print $recv_data; # } #my $tmp = "Action: Originate\r\nChannel: LOCAL/600\r\nExten: 201\r\nContext: default\r\nCallerID: Music Stream\r\nPriority: 1\r\n\r\n"; #print $tmp; #$socket->send($tmp); # sleep(10); while(1){ $socket->recv($recv_data,1024); &tokenizepackets($recv_data); #print $recv_data; } sub tokenizepackets(){ my $recv_data = shift; #print $recv_data; my @lines = split(/\n/,$recv_data); #split the lines... my @tmp = split(/:/,$lines[0]); #split each lines : #print "@tmp[0]\n"; my $type = $tmp[1]; $type = trim($type); if($type eq "PeerStatus"){ @tmp = split(/:/,$lines[6]); my $status = $tmp[1]; $status = trim($status); #print "My Status is: $status\n"; @tmp = split(/:/,$lines[2]); #line3 hold username @tmp = split(/\//,$tmp[1]); #Since username= SIP/username so split the string on "/" basis. my $USER = $tmp[1]; # arr[0] is SIP arr[1] is username; $USER = trim($USER); # Remove forward or trailing whitespaces.. if( ($status eq "Registered") and (UserCloneLineStatus($USER) eq "enabled")){ @tmp = split(/:/,$lines[3]); my $IP = $tmp[1]; $IP = trim($IP); @tmp = split(/:/,$lines[4]); my $PORT = $tmp[1]; $PORT= trim($PORT); #print "USER:$USER\n"; #print "IP:$IP\n"; #print "Port:$PORT\n"; &ProcessQueue($USER,$IP,$PORT); } } =pod elsif($type eq "Newexten"){ #print $recv_data; print "\nI'm in Newexten.............. \n"; my @tmp = split(/:/,@lines[4]); #split each lines : my $ext = @tmp[1]; my $ext_lbl = @tmp[0]; $ext_lbl = trim($ext_lbl); $ext = trim($ext); #print $ext_lbl; if($ext_lbl eq "Extension"){ print "I am processing\n"; print $recv_data; my @tmp = split(/:/,@lines[8]); #split each lines : my $id = @tmp[1]; $id = trim($id); #print "ID: $id \n"; @tmp = split(/:/,@lines[2]); #split each lines : my $chan = @tmp[1]; $chan = trim($chan); #print "chan: $chan"; #print "ext: $ext"; #my $tmp = "Action: Originate\r\nChannel: SIP/Prayer \r\nExten: 800\r\nContext: default\r\nVariable: v1=123\r\nAsync: yes\r\nPriority: 1\r\n\r\n"; #my $tmp = "Action: Setvar\r\nChannel: $chan\r\nVariable: callerid(num)\r\nValue: 123\r\n\r\n"; #print $tmp; #$socket->send($tmp); } #print "\nI am sending call packet...\n"; #my $tmp = "Action: Originate\r\nChannel: SIP/700\r\nContext: default\r\nCallerID: AXV\r\nVariable: v1 = 123\r\nPriority: 2\r\n\r\n"; #my $tmp = "Action: Redirect\r\nChannel: SIP/700 \r\nExten: 700\r\rContext: default\r\nVariable: v1=123\r\nAsync: yes\r\nPriority: 1\r\n\r\n"; #$socket->send($tmp); #} } =cut elsif($type eq "dial"){ # implementation will be here.... } else{ return 0; } } sub UserCloneLineStatus(){ my $USER = shift; # my $ArStatus = SearchCloneLineBuffer($USER); # if($ArStatus eq "NOT_FOUND"){ # my $DBStatus = SearchDB($USER); # if($DBStatus eq "ENABLED"){ # ADDArrayBuffer($USER,"enabled"); # return "enabled"; # } # else{ # AddArrayBuffer($USER,"diabled"); # AddDB($USER,"disabled"); # return "disabled"; # } # } # elsif($ArStatus eq "enabled"){ # return "enabled"; # } return "enabled"; } sub ProcessQueue(){ my $USER= shift; #USERNAME, IP, Port <------ my $IP= shift; my $PORT= shift; my $ip1 = $hash{$USER}{0}{0}; # first IP:port my $ip2 = $hash{$USER}{1}{0}; # second IP:port # print "IP1:$ip1\n"; # print "IP2:$ip2\n"; if($hash{$USER}{0}{0} eq ""){ print "++++++++++++++++++++++++++++++++++++++++++\n"; $hash{$USER}{0}{0} ="$IP:$PORT"; $hash{$USER}{0}{1} = Time::HiRes::time(); print "First Registration for User: $USER\n"; print "IP1:Port ".$hash{$USER}{0}{0}."\n"; print "Time $hash{$USER}{0}{1} \n"; &DBPut($USER,$IP,$PORT); #&PutFile($USER,$IP,$PORT); } elsif($ip1 eq "$IP:$PORT"){ print "+++++++++++ Case - 1 ++++++++++++++\n"; print "User:$USER : Alread Registered @ $IP\n"; print "Updating Time Stamp\n"; $hash{$USER}{0}{1} = Time::HiRes::time(); print "IP1:".$hash{$USER}{0}{0}."\n"; my $tmp = $hash{$USER}{0}{1}; print "Time $tmp \n"; print "IP2:".$hash{$USER}{1}{0}."\n"; $tmp = $hash{$USER}{1}{1}; print "Time $tmp \n"; } elsif( ($ip2 eq "$IP:$PORT")){ # skip the packet processing.... print "++++++++++++ Case - 2 +++++++++++++++\n"; print "User:$USER : Alread Registered @ $IP\n"; print "Updating Time Stamp\n"; $hash{$USER}{1}{1} = Time::HiRes::time(); print "IP1:".$hash{$USER}{0}{0}."\n"; my $tmp = $hash{$USER}{0}{1}; print "Time $tmp \n"; print "IP2:".$hash{$USER}{1}{0}."\n"; $tmp = $hash{$USER}{1}{1}; print "Time $tmp \n"; print "++++++++++++++++++++++++++++++++++++++++++\n"; } else{ print "++++++++++++++++++++++++++++++++++++++++++\n"; print "New User:$USER found IP:Port@ $IP:$PORT\n"; $hash{$USER}{1}{0} = $hash{$USER}{0}{0}; $hash{$USER}{1}{1} = $hash{$USER}{0}{1}; $hash{$USER}{0}{0} = "$IP:$PORT"; $hash{$USER}{0}{1} = Time::HiRes::time(); print "IP1:".$hash{$USER}{0}{0}."\n"; my $tmp = $hash{$USER}{0}{1}; print "Time $tmp \n"; print "IP2:".$hash{$USER}{1}{0}."\n"; $tmp = $hash{$USER}{1}{1}; print "Time $tmp \n"; &DBUpdate_1($USER,$IP,$PORT); my @tmp = split(/:/,$hash{$USER}{1}{0}); &DBUpdate_3($USER,$IP,$PORT,$tmp[0],$tmp[1]); #UpdateFile($USER,$IP,$PORT,$tmp[0],$tmp[1]); print "++++++++++++++++++++++++++++++++++++++++++\n"; } } sub trim() { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } sub DBPut(){ my $USER = shift; my $IP = shift; my $PORT = shift; print $USER; print $IP; print $PORT; print "I am in DB insert\n"; my $dsn = "DBI:mysql:cloneline:localhost:3306"; #======================== my $dbh = DBI->connect($dsn,'cdrupdate','dbpass') or die "Couldn't connect to database: ".DBI->errstr;#connect to DB my $sth = $dbh->prepare("delete from users where username=?") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute($USER) or die "Couldn't execute statement: " .$sth->errstr; $sth = $dbh->prepare("insert into users(username, ip1, port1) values(?,?,?)") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute($USER,$IP,$PORT) or die "Couldn't execute statement: " .$sth->errstr; $sth->finish(); $dbh->disconnect or warn "Disconnection failed: $DBI::errstr\n"; } sub DBUpdate_1(){ my $USER = shift; my $IP = shift; my $PORT = shift; my $dbh; print "I am DB Update 1\n"; my $dsn = "DBI:mysql:cloneline:localhost:3306"; $dbh = DBI->connect($dsn,'cdrupdate','dbpass') or die "Couldn't connect to database: ".DBI->errstr;#connect to DB my $sth = $dbh->prepare('update users set ip1 = ?, port1 = ? where username = ?') or die "Couldn't prepare statement: ". $dbh->errstr; $sth->execute($IP,$PORT,$USER) or die "Couldn't execute statement: " . $sth->errstr; $sth->finish(); $dbh->disconnect or warn "Disconnection failed: $DBI::errstr\n"; print "data updated"; $dbh->disconnect(); } sub DBUpdate_2(){ my $USER = shift; my $IP = shift; my $PORT = shift; my $dbh; print "I am DB Update 2 \n"; my $dsn = "DBI:mysql:cloneline:localhost:3306"; $dbh = DBI->connect($dsn,'cdrupdate','dbpass') or die "Couldn't connect to database: ".DBI->errstr;#connect to DB my $sth = $dbh->prepare('update users set ip2 = ?, port2 = ? where username = ?') or die "Couldn't prepare statement: ". $dbh->errstr; $sth->execute($IP,$PORT,$USER) or die "Couldn't execute statement: " . $sth->errstr; $dbh->disconnect() or warn "Disconnection failed: $DBI::errstr\n"; $sth->finish(); $dbh->disconnect or warn "Disconnection failed: $DBI::errstr\n"; print "data Updated"; } sub DBUpdate_3(){ #$USER,$USER,$IP,$PORT,@tmp[0],@tmp[1] my $USER = shift; my $IP = shift; my $PORT = shift; my $IP2 = shift; my $PORT2 = shift; my $dbh; print "I am DB Update 3 \n"; my $dsn = "DBI:mysql:cloneline:localhost:3306"; $dbh = DBI->connect($dsn,'cdrupdate','dbpass') or die "Couldn't connect to database: ".DBI->errstr;#connect to DB my $sth = $dbh->prepare('update users set ip1 = ?, port1 = ?, ip2 = ?, port2 = ? where username = ?') or die "Couldn't prepare statement: ". $dbh->errstr; $sth->execute($IP,$PORT,$IP2,$PORT2,$USER) or die "Couldn't execute statement: " . $sth->errstr; $sth->finish(); $dbh->disconnect or warn "Disconnection failed: $DBI::errstr\n"; print "data Updated"; } sub PutFile(){ my $USER = shift; my $IP = shift; my $PORT = shift; my $str = "SIP/$USER@"."$IP:$PORT"; my $path = ">>/var/lib/asterisk/".$USER; open(FH,$path) or die "FAILED:: $!"; print FH $str; print FH "\n"; close(FH); } sub UpdateFile(){ my $USER = shift; my $IP = shift; my $PORT = shift; my $IP2 = shift; my $PORT2 = shift; my $str = "SIP/$USER@"."$IP:$PORT\n"."SIP/$USER@"."$IP2:$PORT2"; my $path = ">/var/lib/asterisk/".$USER; open(FH,$path) or die "FAILED:: $!"; print FH $str; print FH "\n"; close(FH); } #$manager_sock->send("Action: command\r\ncommand: reload\r\n\r\n"); #$manager_sock->close();