Changed structure of perl modules to be more useful.
[licom.git] / mutt-licom.pl
1 #! /usr/bin/perl -Tw
2 # 2005-02-24: Fixed for AD/Exchange 2003 & Unicode characters,
3 # anders@bsdconsulting.no If you find this script useful, let me know. :-)
4 #
5 # 2000/2001: Original version obtained from Andreas Plesner Jacobsen at
6 # World Online Denmark. Worked for me with Exchange versions prior to Exchange
7 # 2000.
8 #
9 # Use it with mutt by putting in your .muttrc:
10 # set query_command = "/home/user/bin/mutt-ldap.pl '%s'"
11 #
12 # Then you can search for your users by name directly from mutt. Press ^t
13 # after having typed parts of the name. Remember to edit configuration
14 # variables below.
15
16 use strict;
17 use Encode qw/encode decode/;
18 use vars qw { $ldapserver $domain $username $password $basedn };
19
20 # --- configuration ---
21 $ldapserver = "domaincontroller.yourdomain.com";
22 $domain = "YOURDOMAIN";
23 $username = "myuser";
24 $password = "mypassword";
25 $basedn = "ou=companyxy,dc=companyxy,dc=tld";
26 # --- end configuration ---
27
28 #my $search=shift;
29 my $search=encode("UTF-8", join(" ", @ARGV));
30
31 if (!$search=~/[\.\*\w\s]+/) {
32         print("Invalid search parameters\n");
33         exit 1;
34 }
35
36 use Net::LDAP;
37
38 my $ldap = Net::LDAP->new($ldapserver) or die "$@";
39
40 $ldap->bind("$domain\\$username", password=>$password);
41
42 my $mesg = $ldap->search (base => $basedn,
43                           filter => "(|(cn=*$search*) (rdn=*$search*) (uid=*$search*) (mail=*$search*))",
44                           attrs => ['mail','cn']);
45
46 $mesg->code && die $mesg->error;
47
48 print(scalar($mesg->all_entries), " entries found\n");
49
50 foreach my $entry ($mesg->all_entries) {
51         if ($entry->get_value('mail')) {
52                 print($entry->get_value('mail'),"\t",
53                       decode("UTF-8", $entry->get_value('cn')),"\tFrom Exchange LDAP database\n");
54                 }
55         }
56 $ldap->unbind;