Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # AGI Ldap CallerId Lookup for Asterisk
- # LDAP schema is mozillaAbPersonAlpha, Number format "0894711"
- # use exten => s,1,AGI(ldaplookupphone.agi) in extensions.conf
- #
- # V 0.01 2010-09-02 Reimer Prochnow, reimerp at gmx dot net
- # Credits to Matt Schmandt and duese
- # use as CC-BY-SA 3.0
- #
- # Done in Perl as Shell is not powerful enough for parsing output
- use strict;
- # far too slow: use Net::LDAP;
- use MIME::Base64 qw(decode_base64);
- use Encode qw(decode);
- $|=1;
- my $agi_callerid;
- my $num;
- if ($#ARGV >= 0) {
- $agi_callerid=$ARGV[0]; # for test
- $num="*$agi_callerid*"; # also allow wildcards around number
- } else {
- # Setup some variables
- my %AGI;
- while(<STDIN>) {
- chomp;
- last unless length($_);
- if (/^agi_(\w+)\:\s+(.*)$/) {
- $AGI{$1} = $2;
- }
- }
- #print STDERR "AGI Environment Dump:\n";
- #foreach my $i (sort keys %AGI) {
- # print STDERR " -- $i = $AGI{$i}\n";
- #}
- $agi_callerid=$AGI{'callerid'};
- $num=$agi_callerid;
- }
- my $cn='';
- my $ldapcmd="/usr/bin/ldapsearch -h server -x -D cn=asterisk,dc=xxx,dc=yyy -w ***** " .
- "-b ou=personal,dc=xxx,dc=yyy -LLL -s one " .
- "\"(&(objectClass=mozillaAbPersonAlpha)(|(homePhone=$num)(mobile=$num)(pager=$num)(telephoneNumber=$num)))\"" .
- ' | sed -n -e "H;\${g;s/\n //g;p}"'; # does unfold, not that easy in perl
- open (LDAP, "$ldapcmd |") || die ("Unable to open $ldapcmd: $!");
- while (<LDAP>){
- chop;
- if (/([^:]*):: (.*)/) {
- $_="$1: " . Encode::decode('utf8', decode_base64($2)); # decodes UTF-8 for :: vars
- }
- if (/^dn[^=]*=([^,]*).*/) { $cn=$1; }
- elsif (/^([^:]*): .*$agi_callerid/) { $cn.='/'.substr($1,0,3);} # builds: "peter falk/mob"
- #elsif (/^$/) { print "$cn\n"; }
- #print "$cn\n";
- }
- print 'SET CALLERID "' . $cn . '"<' . $agi_callerid . '>' . "\n";
- #exit 0;
Add Comment
Please, Sign In to add comment