Mutt Address book
#!/usr/bin/perl -w
# by Ben Collins , butchered by Marco d'Itri
# Hacked by Brad Marshall for use at PI
# to use, add to ~/.muttrc:
# set query_command="/mnt/home/linux/bin/pi-ldap-query %s"
use strict;
my @attrs = qw(sn cn uid);
my $base = 'ou=People, dc=pisoftware, dc=com';
my $server = 'morris';
my $port = 389;
die "Usage: $0 [...]\n" if not $ARGV[0];
eval 'require Net::LDAP;';
die "Could not load Net::LDAP: $@\n" if $@;
my $ldap = Net::LDAP->new($server, port => $port) or
die "Could not contact LDAP server $server:$port";
$ldap->bind or die 'Could not bind';
my @results = ();
foreach my $search (@ARGV) {
my $query = join '', map { "($_=*$search*)" } @attrs;
my $mesg = $ldap->search(base => $base, filter => "(|$query)")
or die 'Failed search';
foreach my $entry ($mesg->entries) {
my $uid = $entry->get('uid');
next unless (defined $uid);
my $fname = $entry->get('cn');
#my $lname = $entry->get('sn');
my $mail = $entry->get('mail');
push @results,
"<$$mail[0]>\t$$fname[0]\tPI\n";
}
}
$ldap->unbind;
print 'LDAP query: found ', scalar @results, "\n", @results;
exit 1 unless @results;
|