#!/usr/bin/perl ### Antoine DELAPORTE - GPL(2) use strict; #use warnings; use IPC::Open3; use Sys::Syslog qw(:DEFAULT :standard :macros); use Getopt::Std; use File::Temp qw/ tempfile tempdir /; my $sipgw=""; my $cont=1; my ($cacuna,$cacuex,$cacude)=("","",""); my %opt; getopts('c:l:r:',\%opt); # c = config file # l = linphonec # r = quel script appeler en cas de ring "Exemaple -r "/usr/local/bin/log __CID__ __EXTENSION__ __DEVICE__" #print "Pour les tests aller sur la console de test hein : channel originate SIP/e62pr1fq extension SIP/toto\@10.0.10.21\n"; $opt{c}='~/.linphonerc' if(!defined($opt{c})); $opt{l}='linphonec' if(!defined($opt{l})); openlog("SipCliClaScr[$$]{".$opt{c}."}", 'ndelay', 'local2'); # Cet ensemble (et plus bas) de code est un AFFREUX cludge due au fait que linphonec : # 1. loggues les appels dans le fichier de conf # 2. ecris dans le fichier (et le regenere) a chaques fin d'appels # ce qui laisse presager d'un affreux bonheur cote contenu memoire my ($fh, $tmpfilename) = tempfile(UNLINK => 1,SUFFIX => "-$$.tel" ); my $null=$opt{c}; `cp $null $tmpfilename`; my $ringcmd; my $ringret; my $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, 'LANG=C '.$opt{'l'}.' -c '.$tmpfilename) or $cont=0; close($fh); while($cont) { # Cette methode de lecture de la sortie me permet en fait de sortir violement $_=; if(!defined($_)) { $_=""; } chomp(); #linphonec> "Anonymous" is contacting you. #linphonec> Receiving new incoming call from "Anonymous" , assigned id 1 # failed: Forbiden # failed: no response timeout if(/^$/) { syslog('info', "Sip client shut(ting) down"); $cont=0; print CHLD_IN "quit\n"; } elsif(/Registration on (.*)/) { $sipgw=$1; if($2 =~ /successful.$/) { syslog('info', "Sip client connected onto $sipgw sucessfully tmpfile $tmpfilename"); } else { syslog('Info',"Sip client close sip connection or unable to start connection [$2]"); ###$cont=0; ###print CHLD_IN "help\n"; } unlink($tmpfilename); } elsif(/\"(.*)\" is contacting you.$/) { $cacuna=$1; $cacuex=$2; $cacude=$3; syslog('Info',"Call received from $cacuna (Sip identity $cacuex\@$cacude)"); if(defined($opt{r})) { $ringcmd=$opt{r}; $ringcmd=~ s/__CID__/$cacuna/g; $ringcmd=~ s/__EXTENSION__/$cacuex/g; $ringcmd=~ s/__DEVICE__/$cacude/g; $ringret=1; syslog('Info',"?? Running $ringcmd"); open(RING,"$ringcmd |") or $ringret=0; while() {} close(RING); if($ringret) {syslog('Info',"OK $ringcmd"); } else {syslog('Info',"KO $ringcmd"); } } } elsif(/Call (terminated\.|ended)$/) { syslog('Info',"Call finished for $cacuna (Sip identity $cacuex\@$cacude)"); sleep(1); #Without this, no unlink :-/ ok cludge unlink($tmpfilename); ($cacuna,$cacuex,$cacude)=("","",""); } elsif(/^(linphonec> Receiving new incoming call from |linphonec> You have missed |linphonec> Call .* with)|Ready$|Warning: video is disabled in linphonec/) { # Trapping signals } else { syslog('Info',"Bad Analyse : $_"); } } unlink($tmpfilename); syslog('Info',"Sip client terminated, SipCliClaScr too, unlink of $tmpfilename");