Fix the inline-documentation: LiCoM::Person doesn't provide a `group' method.
[licom.git] / mutt-licom.pl
index e3991f8..e7fbcbd 100755 (executable)
@@ -1,56 +1,88 @@
-#! /usr/bin/perl -Tw
-# 2005-02-24: Fixed for AD/Exchange 2003 & Unicode characters,
-# anders@bsdconsulting.no If you find this script useful, let me know. :-)
-#
-# 2000/2001: Original version obtained from Andreas Plesner Jacobsen at
-# World Online Denmark. Worked for me with Exchange versions prior to Exchange
-# 2000.
-#
-# Use it with mutt by putting in your .muttrc:
-# set query_command = "/home/user/bin/mutt-ldap.pl '%s'"
-#
-# Then you can search for your users by name directly from mutt. Press ^t
-# after having typed parts of the name. Remember to edit configuration
-# variables below.
+#!/usr/bin/perl
 
 use strict;
-use Encode qw/encode decode/;
-use vars qw { $ldapserver $domain $username $password $basedn };
-
-# --- configuration ---
-$ldapserver = "domaincontroller.yourdomain.com";
-$domain = "YOURDOMAIN";
-$username = "myuser";
-$password = "mypassword";
-$basedn = "ou=companyxy,dc=companyxy,dc=tld";
-# --- end configuration ---
-
-#my $search=shift;
-my $search=encode("UTF-8", join(" ", @ARGV));
-
-if (!$search=~/[\.\*\w\s]+/) {
-       print("Invalid search parameters\n");
-       exit 1;
+use warnings;
+
+use FindBin (qw($Bin));
+use lib ("$Bin/lib");
+
+use LiCoM::Config (qw(get_config));
+use LiCoM::Person;
+
+our $Config;
+
+if (-e $ENV{'HOME'} . '/.licomrc')
+{
+       $Config = get_config ($ENV{'HOME'} . '/.licomrc');
+}
+elsif (-e '/etc/licom/licom.conf')
+{
+       $Config = get_config ('/etc/licom/licom.conf');
+}
+else
+{
+       $Config = get_config ();
+}
+
+if ($ENV{'DEBUG'})
+{
+       require Data::Dumper;
+       print STDERR Data::Dumper->Dump ([$Config], ['Config']);
+}
+
+unless (defined ($Config->{'uri'}) and defined ($Config->{'bind_dn'})
+       and defined ($Config->{'password'}))
+{
+       die (<<ERROR);
+The configuration has not been found or is not complete. At least the options
+uri, bind_dn and password are needed.
+ERROR
 }
 
-use Net::LDAP;
+$Config->{'base_dn'} = $Config->{'bind_dn'} unless (defined ($Config->{'base_dn'}));
+
+our @Patterns = ();
+for (@ARGV)
+{
+       my $temp = $_;
+       $temp =~ s/[^\.\*\w\s]//g;
+
+       next unless ($temp);
+
+       $temp =~ s/\**$/*/;
+       push (@Patterns, [[lastname => $temp], [firstname => $temp], [mail => $temp]]);
+}
 
-my $ldap = Net::LDAP->new($ldapserver) or die "$@";
+die ('No (valid) patterns found.') unless (@Patterns);
 
-$ldap->bind("$domain\\$username", password=>$password);
+LiCoM::Person->connect 
+(
+       uri     => $Config->{'uri'},
+       base_dn => $Config->{'base_dn'},
+       bind_dn => $Config->{'bind_dn'},
+       password => $Config->{'password'}
+) or die;
 
-my $mesg = $ldap->search (base => $basedn,
-                          filter => "(|(cn=*$search*) (rdn=*$search*) (uid=*$search*) (mail=*$search*))",
-                         attrs => ['mail','cn']);
+our @Matches = LiCoM::Person->search (@Patterns, [[mail => '*']]);
 
-$mesg->code && die $mesg->error;
+print STDOUT scalar (@Matches), ' ', (scalar (@Matches) == 1 ? 'entry' : 'entries'), " found.\n";
 
-print(scalar($mesg->all_entries), " entries found\n");
+for (sort { $a->name () cmp $b->name () } (@Matches))
+{
+       my $person = $_;
+       my $cn = $person->name ();
+       my @mail = $person->get ('mail');
+       my @groups = $person->get ('group');
+       my $info = join (', ', sort (@groups));
 
-foreach my $entry ($mesg->all_entries) {
-       if ($entry->get_value('mail')) {
-               print($entry->get_value('mail'),"\t",
-                     decode("UTF-8", $entry->get_value('cn')),"\tFrom Exchange LDAP database\n");
-               }
+       $info = "($info)" if ($info);
+
+       for (sort (@mail))
+       {
+               print "$_\t$cn\t$info\n";
        }
-$ldap->unbind;
+}
+
+LiCoM::Person->disconnect ();
+
+exit (0);