#!/usr/local/bin/perl -w # mutt_ldap_query.pl # version 2.2 # Copyright © 98,99 Marc de Courville # but feel free to redistribute it however you like. # # 11/02/99: merged modifications proposed by Warren Jones # # mutt_ldap_query.pl: perl script to parse the outputs of ldapsearch # (ldap server query tool present in ldap-3.3 distribution # http://www.umich.edu/~rsug/ldap) in order to pass the required # formatted data to mutt (mail client http://www.cs.hmc.edu/~me/mutt/) # using Brandon Blong's the "External Address Query" patch # (http://www.fiction.net/blong/programs/mutt/#query). use strict; # Please change the following 2 lines to match your site configuration my $ldap_server = "ldap"; my $BASEDN = "o=Motorola, c=US"; my @fields = qw(cn mail sn fn uid); # list of the fields that will be used for composing the answer my $expected_answers = "fn sn mail business_group telephonenumber"; my $LDAPSEARCH="/usr/local/bin/ldapsearch"; die print "$0 syntax error:\n\t Usage: $0 [[] ...]\n" if ! $ARGV[0]; my @results; $/ = ''; # Paragraph mode for input. foreach my $askfor ( @ARGV ) { # enable this if you want to include wildcard in your search with some huge # ldap databases you might want to avoid it # my $query = join '', map { "($_=$askfor*)" } @fields; my $query = join '', map { "($_=$askfor)" } @fields; my $command = "$LDAPSEARCH -h $ldap_server -b '$BASEDN' '(|$query)' $expected_answers"; open ( LDAPQUERY , "$command |") || die "LDAP query error: $!\n"; while ( ) { next if ! /^mail=(.*)$/im; my $email = $1; my $phone = /^telephonenumber=(.*)$/im ? $1 : ''; my $sector = /^business_group=(.*)$/im ? $1 : ''; my @name = ( /^fn=(.*)$/im, /^sn=(.*)$/im ); push @results, "<$email>\t@name\t($phone) $sector\n"; # for pretty printing # my $name = join ' ', ( /^fn=(.*)$/im, /^sn=(.*)$/im ); # my $answer = sprintf ("<%s>\t%-17.17s\t(%s) %s\n", $email, $name , $phone, $sector); # push(@results, $answer); } close(LDAPQUERY) || die "ldapsearch failed: $!\n"; } print "LDAP query: found ", scalar(@results), "\n", @results; exit 1 if ! @results;