#!/usr/bin/perl -Tw # talks to an asterisk server, calls two phone numbers # and joins the calls together # the only parameter: the second number # everything else is configured here # Based on callboth.php from Nerd Vittles # version 3.01 (c) Copyright Ward Mundy, 2005 # all kudos to those guys my $callerid='Jez <0855555555>'; my $sourceext="SIP/201"; # extension that's placing the call my $context='out-callboth'; sub web_usage { print "Specify phone number as num=parameter\n"; exit; } my $destnum; my $query_string=$ENV{'QUERY_STRING'}; if (defined($query_string)) { print "Content-type: text/html\n\n"; if (length($query_string) and $query_string =~ /\bnum=(\d+)/) { $destnum=$1; } else { &web_usage; } } else { $destnum=shift; } if ($query_string =~ /\bext=([^=]+)/) { # should decode ext $sourceext="SIP/$1"; } die "No phone number\n" unless defined $destnum and length $destnum; die "Invalid phone number\n" unless $destnum =~ /^(\+?[\s\d]+)$/; $destnum=$1; # untainted, yay my $server='asterserver:5038'; # name of server, optional port number or 5038 my $mgruser='manager'; my $mgrpass='managerpass'; #my $prefix='0'; # to get an outside line? my $prefix=''; # to get an outside line? my $timeout=30; # in seconds sub make_the_call { my $fh=shift; my $source=shift; my $dest=shift; return undef unless defined $fh; print $fh "Action: login\r\n"; print $fh "Username: $mgruser\r\n"; print $fh "Secret: $mgrpass\r\n"; print $fh "Events: off\r\n"; print $fh "\r\n"; sleep 1; # perhaps should check for error or disconnect print $fh "Action: Originate\r\n"; print $fh "Channel: $source\r\n"; print $fh "Context: $context\r\n"; print $fh "Exten: ${prefix}${dest}\r\n"; print $fh "Priority: 1\r\n"; print $fh "\r\n"; if (defined($callerid) and length($callerid)) { print $fh "Callerid: $callerid\r\n"; print $fh "\r\n"; } print $fh "Timeout: ${timeout}000\r\n"; print $fh "\r\n"; sleep 2; # Need to read the response } #use Socket; use IO::Socket; sub connect_to_server { my $server=shift; my $port=5038; if ($server =~ s/:(.*)// and length($1)) { $port=$1; $port=getservbyname($port,'tcp') if $port =~ /\D/; } my $remote=IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $server, PeerPort => $port, ) or die "Connect failed: $!"; return $remote; } $|=1; print "calling $sourceext->$destnum\n"; my $fh=connect_to_server($server); make_the_call($fh,$sourceext,$destnum); close($fh) or die "Error closing connection to Asterisk: $!"; print "done.\n";