heimi

AGI asterisk LDAP callerId lookup

Apr 25th, 2021
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.87 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # AGI Ldap CallerId Lookup for Asterisk
  4. # LDAP schema is mozillaAbPersonAlpha, Number format "0894711"
  5. # use exten => s,1,AGI(ldaplookupphone.agi) in extensions.conf
  6. #
  7. # V 0.01 2010-09-02 Reimer Prochnow, reimerp at gmx dot net
  8. # Credits to Matt Schmandt and duese
  9. # use as CC-BY-SA 3.0
  10. #
  11. # Done in Perl as Shell is not powerful enough for parsing output
  12.  
  13. use strict;
  14.  
  15. # far too slow: use Net::LDAP;
  16. use MIME::Base64 qw(decode_base64);
  17. use Encode qw(decode);
  18.  
  19. $|=1;
  20.  
  21. my $agi_callerid;
  22. my $num;
  23. if ($#ARGV >= 0) {
  24.     $agi_callerid=$ARGV[0];         # for test
  25.     $num="*$agi_callerid*";         # also allow wildcards around number
  26. } else {
  27.  
  28. # Setup some variables
  29. my %AGI;
  30.  
  31. while(<STDIN>) {
  32.     chomp;
  33.     last unless length($_);
  34.     if (/^agi_(\w+)\:\s+(.*)$/) {
  35.         $AGI{$1} = $2;
  36.     }
  37. }
  38.  
  39. #print STDERR "AGI Environment Dump:\n";
  40. #foreach my $i (sort keys %AGI) {
  41. #    print STDERR " -- $i = $AGI{$i}\n";
  42. #}
  43.  
  44. $agi_callerid=$AGI{'callerid'};
  45. $num=$agi_callerid;
  46. }
  47.  
  48. my $cn='';
  49.  
  50. my $ldapcmd="/usr/bin/ldapsearch -h server -x -D cn=asterisk,dc=xxx,dc=yyy -w ***** " .
  51.         "-b ou=personal,dc=xxx,dc=yyy -LLL -s one " .
  52.         "\"(&(objectClass=mozillaAbPersonAlpha)(|(homePhone=$num)(mobile=$num)(pager=$num)(telephoneNumber=$num)))\"" .
  53.         ' | sed -n -e "H;\${g;s/\n //g;p}"';    # does unfold, not that easy in perl
  54.  
  55. open (LDAP, "$ldapcmd |") || die ("Unable to open $ldapcmd: $!");
  56. while (<LDAP>){
  57.     chop;
  58.     if (/([^:]*):: (.*)/) {
  59.         $_="$1: " . Encode::decode('utf8', decode_base64($2));  # decodes UTF-8 for :: vars
  60.     }
  61.     if (/^dn[^=]*=([^,]*).*/) { $cn=$1; }
  62.     elsif (/^([^:]*): .*$agi_callerid/) { $cn.='/'.substr($1,0,3);}     # builds: "peter falk/mob"
  63.     #elsif (/^$/) { print "$cn\n"; }
  64.     #print "$cn\n";
  65. }
  66.  
  67. print 'SET CALLERID "' . $cn . '"<' . $agi_callerid . '>' . "\n";
  68. #exit 0;
  69.  
Add Comment
Please, Sign In to add comment