08c25022834e0e244f2948d33b863e9765e754d3
[licom.git] / mutt-licom.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use FindBin (qw($Bin));
7 use lib ("$Bin/lib");
8
9 use LiCoM::Config (qw(get_config));
10 use LiCoM::Person;
11
12 our $Config;
13
14 if (-e $ENV{'HOME'} . '/.licomrc')
15 {
16         $Config = get_config ($ENV{'HOME'} . '/.licomrc');
17 }
18 elsif (-e '/etc/licom/licom.conf')
19 {
20         $Config = get_config ('/etc/licom/licom.conf');
21 }
22 else
23 {
24         $Config = get_config ();
25 }
26
27 unless (defined ($Config->{'uri'}) and defined ($Config->{'bind_dn'})
28         and defined ($Config->{'password'}))
29 {
30         die (<<ERROR);
31 The configuration has not been found or is not complete. At least the options
32 uri, bind_dn and password are needed.
33 ERROR
34 }
35
36 $Config->{'base_dn'} = $Config->{'bind_dn'} unless (defined ($Config->{'base_dn'}));
37
38 our @Patterns = ();
39 for (@ARGV)
40 {
41         my $temp = $_;
42         $temp =~ s/[^\.\*\w\s]//g;
43
44         next unless ($temp);
45
46         $temp =~ s/\**$/*/;
47         push (@Patterns, [[lastname => $temp], [firstname => $temp], [mail => $temp]]);
48 }
49
50 die ('No (valid) patterns found.') unless (@Patterns);
51
52 LiCoM::Person->connect 
53 (
54         uri     => $Config->{'uri'},
55         base_dn => $Config->{'base_dn'},
56         bind_dn => $Config->{'bind_dn'},
57         password => $Config->{'password'}
58 ) or die;
59
60 our @Matches = LiCoM::Person->search (@Patterns, [[mail => '*']]);
61
62 print STDOUT scalar (@Matches), ' ', (scalar (@Matches) == 1 ? 'entry' : 'entries'), " found.\n";
63
64 for (@Matches)
65 {
66         my $person = $_;
67         my $cn = $person->name ();
68         my @mail = $person->get ('mail');
69
70         for (@mail)
71         {
72                 print "$_\t$cn\tFound by LiCoM\n";
73         }
74 }
75
76 LiCoM::Person->disconnect ();
77
78 exit (0);