#!/usr/bin/perl
# $Id$
#
# Written by: John Laur <johnl@blurbco.com>
#
# Distributed under the terms of the GNU General Public License

use strict;
use CGI;

=item placecall.cgi

This application is written to demonstrate how you can do PC-to-phone
integration with the asterisk outgoing queue mechanism.

WARNING! Take note that this applicaiton provides no security by
default. Anyone using it can set up a call between any two extensions
in the list you specify, and thus it is not suitable for any kind of
real deployment! It is intended to serve only as an example.

Instructions:

Place the following entries into your extensions.conf:

exten => spooler,1,Answer
exten => spooler,2,Wait(1)
exten => spooler,3,Playback(transfer)
exten => spooler,4,SetCallerID(${OriginalCID})
exten => spooler,5,Goto(${ReqContext},${ReqExtension},${ReqPriority})

Next, edit the values below to correspond with your setup. If you
have changed the extension name 'spooler' above to anything else, or
placed the extension in a context other than 'default' you will need
to modify the values below.

The extensions hash corresponds to the phones you wish to support.
Asterisk needs to know the name of the channel, the extension, and the
name to use for callerid. See the defaults below for examples.

Finally, ensure that your outgoing spool directory is writable by the
user the CGI is running with (typically 'nobody', 'http', or 'www-data')
THIS IS A SECURITY RISK AND SHOULD NOT BE DONE ON A PRODUCTION SERVER!
A real implementation will require better security.

Usage:

Load placecall.cgi in your web browser. Select the extension from which
you want the call to originate. Then, select the extension you wish to
call. Your phone will ring and the caller id will show as the value you
configured below. When you pick up, you'll hear "Please hold while I try
that extension" and then your call will be connected to the party you
wish to call.

You should be able to apply the same basic technique seen here to write
your own click-to-call style application.

=cut

# Change these values to match the entry you have stored for the
# assistant in extensions.conf
#
# assistant_callerid is the callerid to show to the person calling out
# asterisk_extension assistant_context and assistant_priortiy:
#   These values are the name or number, priority, and context of the
#   assistant handler you defined in extensions.conf (see above)
#   
our $assistant_callerid  = 'Assistant <9999>';
our $assistant_extension = 'spooler';
our $assistant_context   = 'default';
our $assistant_priority  = '1';

# This is your asterisk outgoing spool directory (the directory that
# pbx_spool expects to find call set-up files in) Change this if you
# need to. This name must include a trailing slash
# 
our $spooldir = '/var/spool/asterisk/outgoing/'; #INCLUDE TRAILING SLASH!

# Change or extend this list to match your extensions
#
# dialstring is what gets passed to Dial to call this phone
# extension is the extension used to call this phone from another
# cidname is the callerid name to show when making a call from this phone
# 
my %extensions = (
  'Phone 1' => { dialstring => 'SIP/phoneone', 
                 extension  => '2001',
                 cidname    => 'Phone One' },
  'Phone 2' => { dialstring => 'Zap/2', 
                 extension  => '2002',
                 cidname    => 'Phone Two' },
  'Phone 3' => { dialstring => 'SIP/phonethree', 
                 extension  => '2003',
                 cidname    => 'Phone Three' },
		 );

#########################################################################
### It is not necessary to change anything below this line!
#########################################################################
my $http = new CGI;

#Get the two variables that could be passed to us
my $whoami     = $http->param('callfrom');
my $wanttocall = $http->param('extension');

#We are ready to make a call
if ($extensions{$wanttocall} && $extensions{$whoami}) {
	print "Content-Type: text/html\n\n";
	print "<html><head><title>Asterisk Assistant</title></head>";
	print "<body bgcolor=\"#ffffff\"><h1>Call in progress...</h1><hr>";
	
	&create_queuefile( extension => $extensions{$wanttocall}{extension},
			   endpoint  => $extensions{$whoami}{dialstring},
			   callerid  => "$extensions{$whoami}{cidname} <$extensions{$whoami}{extension}>"
			 );

	print "<p><a href=\"?callfrom=$whoami\">Return to make a new call</a>";
	print "</body></html>";

#We are ready to see who we want to call
} elsif ($extensions{$whoami}) {
	print "Content-Type: text/html\n\n";
	print "<html><head><title>Asterisk Assistant</title></head>";
	print "<body bgcolor=\"#ffffff\"><h1>Place a call from $whoami</h1><hr>";
	print "<ul>";
	foreach my $extension (sort keys %extensions) {
		next if $extension eq $whoami;
		print "<li><a href=\"?callfrom=$whoami&extension=$extension\">$extension</a> - $extensions{$extension}{extension}";
	}
	print "</ul>";
	print "</body></html>";

#We still need to find out who we are
} else {
	print "Content-Type: text/html\n\n";
	print "<html><head><title>Asterisk Assistant</title></head>";
	print "<body bgcolor=\"#ffffff\"><h1>Who are you?</h1><hr>";
	print "<ul>";
	foreach my $extension (sort keys %extensions) {
		print "<li><a href=\"?callfrom=$extension\">$extension</a> - $extensions{$extension}{extension}";
	}
	print "</ul>";
	print "</body></html>";
}

#Writes a queuefile into the spool directory
sub create_queuefile {

	my %params = @_;
	
	die unless $params{endpoint};
	
	$params{extension} ||= 'i';
	$params{callerid}  ||= 'Unknown <asterisk>';
	$params{retries}   ||= '0';
	$params{retrytime} ||= '60';
	$params{waittime}  ||= '30';
	$params{context}   ||= 'default';
	$params{priority}  ||= '1';
	
	my $queuefilename = ">$spooldir".time."-$params{extension}.call";
	#my $queuefilename = ">/tmp/".time."-$extension.call";

	#my $queuefile = new IO::File;
	open QUEUEFILE, $queuefilename or die "Can't open $queuefilename!\n";

	print "<br><br><b>QUEUEFILE WRITTEN AS $queuefilename</b>";

	print QUEUEFILE <<OUT;
Channel: $params{endpoint}
MaxRetries: $params{retries}
RetryTime: $params{retrytime}
WaitTime: $params{waittime}

Context: $assistant_context
Extension: $assistant_extension
Priority: $assistant_priority
Callerid: $assistant_callerid

SetVar: OriginalCID=$params{callerid}
SetVar: ReqExtension=$params{extension}
SetVar: ReqContext=$params{context}
SetVar: ReqPriority=$params{priority}
OUT

	close QUEUEFILE;
}
__END__

