META.yml000064400000000000000000000005401111767315400123320ustar00rootroot00000000000000--- distribution_type: module generated_by: Module::Install version 0.67 license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 name: Asterisk-FastAGI no_index: directory: - t - inc - t package: - inc requires: Asterisk::AGI: 0 Net::Server::PreFork: 0 perl: 5.6.0 version: 0.01 Makefile.PL000064400000000000000000000004131111767315400130320ustar00rootroot00000000000000use inc::Module::Install 0.46; name('Asterisk-FastAGI'); license('perl'); perl_version('5.006'); version_from('lib/Asterisk/FastAGI.pm'); requires('Asterisk::AGI'); requires('Net::Server::PreFork'); no_index directory => 't'; no_index package => 'inc'; WriteAll; README000064400000000000000000000015021111767315400117400ustar00rootroot00000000000000Asterisk-FastAGI version 0.01 ============================= Asterisk::FastAGI provides a preforking daemon for handling FastAGI requests from Asterisk. Read the L for more information about the logging facilities, configuration, etc. INSTALLATION This module requires Asterisk::AGI to work. However this module isn't currently up on CPAN. This may change shortly. To download the asterisk-perl modules. Go to: http://asterisk.gnuinter.net/ To install this module type the following: perl Makefile.PL make make test make install COPYRIGHT AND LICENCE Copyright (C) 2006-2007 by Jason Yates This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. example/000075500000000000000000000000001111767315400125155ustar00rootroot00000000000000example/MyAGI.pm000064400000000000000000000002221111767315400137550ustar00rootroot00000000000000package MyAGI; use strict; use base 'Asterisk::FastAGI'; sub agi_handler { my $self = shift; sleep 2; $self->agi->say_number(54321); } 1; example/agid.pl000064400000000000000000000001321111767315400137520ustar00rootroot00000000000000#!/usr/bin/perl use strict; use lib '../lib'; use MyAGI; MyAGI->run( port => '4573' ); lib/000075500000000000000000000000001111767315400116305ustar00rootroot00000000000000lib/Asterisk/000075500000000000000000000000001111767315400134155ustar00rootroot00000000000000lib/Asterisk/FastAGI.pm000064400000000000000000000104461111767315400151760ustar00rootroot00000000000000package Asterisk::FastAGI; use 5.006; use strict; use warnings; $Asterisk::FastAGI::VERSION = '0.02'; use Asterisk::AGI; use Net::Server::PreFork; our @ISA = qw(Net::Server::PreFork); =head1 NAME Asterisk::FastAGI - Module for FastAGI handling. =head1 SYNOPSIS use base 'Asterisk::FastAGI'; sub fastagi_handler { my $self = shift; my $param = $self->param('foo'); my $callerid = $self->input('calleridname'); $self->agi->say_number(1000); } =head1 DESCRIPTION Asterisk::FastAGI provides a preforking daemon for handling FastAGI requests from Asterisk. Read the L for more information about the logging facilities, configuration, etc. =head1 USAGE EXAMPLE First you need a module containing all of your AGI handlers. package MyAGI; use base 'Asterisk::FastAGI'; sub agi_handler { my $self = shift; $self->agi->say_number(8675309); } Then you simply need to have a script that runs the daemon. #!/usr/bin/perl use MyAGI; MyAGI->run(); When it is run it creates a preforking daemon on port '4573'. That is the default port for FastAGI. Read the L documentation on how to change this and many other options. =head1 METHODS =head2 param Returns parsed parameters sent in from the AGI script. Inside extensions.conf: exten => 1111,1,Agi(agi://${SERVER}/fastagi_handler?foo=bar&blam=blah You can access those parameters from inside your AGI script. Much like you would if those were URL parameters on a CGI script. my $foo = $self->param('foo'); =cut sub param { my($self, $param) = @_; return $param ? $self->{server}{params}{$param} : $self->{server}{params}; } =head2 input Returns a hash containing the input from the AGI script. my %hash = $self->input() If given a key. It will return that particular value. my $uniqueid = $self->input('uniqueid'); =cut sub input { my($self, $param) = @_; if( not defined $self->{server}{input} ) { $self->parse_request(); } return $param ? $self->{server}{input}{$param} : $self->{server}{input}; } =head2 agi Will return the Asterisk::AGI object. =cut sub agi { my($self, $agi) = @_; if(defined $agi) { $self->{server}{agi} = $agi } return $self->{server}{agi}; } =head2 process_request This will process the agi request from asterisk. =cut sub process_request { my $self = shift; $self->_agi_parse(); $self->_parse_request(); $self->dispatch_request(); } =head2 dispatch_request Method used to dispatch the FastAGI request. =cut sub dispatch_request { my $self = shift; if( $self->can( $self->{server}{method} ) ) { my $method = $self->{server}{method}; $self->log(4, "Handling request: $method"); $self->$method(); } else { # Can't find that method. $self->log(2, "No method found for: " . $self->{server}{method}); $self->agi->execute( 'NOOP' ); #NOTE is this the right thing todo? } } =head2 child_init_hook This is called by Net::Server during child initialization. This is the method to override if you are going to be creating database connections for instance. sub child_init_hook { my $self = shift; $self->{server}{dbi} = DBI->connect(); } =cut ##### # Internal methods. ##### sub _agi_parse { my $self = shift; # Setup AGI object. $self->agi(Asterisk::AGI->new); # Parse the request. my %input = $self->agi->ReadParse(); $self->{server}{input} = \%input; } sub _parse_request { my $self = shift; # Grab the method and optional path. my($method, $path) = $self->{server}{input}{request} =~ m/\/(\w+)\?*([^\/]*)$/; # Parse each parameter. Format is ?blah=foo&asdf=blah # Turns into: { blah => foo, asdf => blah }. my %params; my(@pairs) = split(/[&;]/,$path); foreach (@pairs) { my($p,$v) = split('=',$_,2); $params{$p} = $v; } # Setup instance variables. $self->{server}{params} = \%params; $self->{server}{method} = $method; $self->{server}{path} = $path; } =head1 SEE ALSO L, L =head1 AUTHOR Jason Yates =head1 COPYRIGHT AND LICENSE Copyright (C) 2006-2007 by Jason Yates This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut 1; t/000075500000000000000000000000001111767315400113255ustar00rootroot00000000000000t/00-load.t000064400000000000000000000001311111767315400126410ustar00rootroot00000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 1; use_ok('Asterisk::FastAGI'); t/01-parse.t000064400000000000000000000021661111767315400130470ustar00rootroot00000000000000#!/usr/bin/perl -w use strict; use Asterisk::FastAGI; use Test::More tests => 12; { my %args1; $args1{server}{input} = { 'callerid' => 'jaywhy', 'context' => 'default', 'request' => 'agi://localhost/agi_handler?foo=bar&BLAH=blam&', }; my $test1 = bless \%args1, "Asterisk::FastAGI"; $test1->_parse_request(); ok( $test1->{server}{method} eq 'agi_handler' ); ok( $test1->param('foo') eq 'bar' ); ok( $test1->param('BLAH') eq 'blam' ); my $params = $test1->param(); ok( $params->{foo} eq 'bar' ); ok( $params->{BLAH} eq 'blam' ); ok( $test1->input('callerid') eq 'jaywhy' ); ok( $test1->input('context') eq 'default' ); my $input = $test1->input(); ok( $input->{callerid} eq 'jaywhy' ); ok( $input->{context} eq 'default' ); } { my %args2; $args2{server}{input} = { 'callerid' => 'jaywhy', 'request' => 'agi://192.168.1.100/other_handler', }; my $test2 = bless \%args2, "Asterisk::FastAGI"; $test2->_parse_request(); ok( $test2->{server}{method} eq 'other_handler' ); ok( $test2->input('callerid') eq 'jaywhy' ); my $input = $test2->input(); ok( $input->{callerid} eq 'jaywhy' ); }