Added the possibility to change a groups description.
[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 if ($ENV{'DEBUG'})
28 {
29         require Data::Dumper;
30         print STDERR Data::Dumper->Dump ([$Config], ['Config']);
31 }
32
33 unless (defined ($Config->{'uri'}) and defined ($Config->{'bind_dn'})
34         and defined ($Config->{'password'}))
35 {
36         die (<<ERROR);
37 The configuration has not been found or is not complete. At least the options
38 uri, bind_dn and password are needed.
39 ERROR
40 }
41
42 $Config->{'base_dn'} = $Config->{'bind_dn'} unless (defined ($Config->{'base_dn'}));
43
44 our @Patterns = ();
45 for (@ARGV)
46 {
47         my $temp = $_;
48         $temp =~ s/[^\.\*\w\s]//g;
49
50         next unless ($temp);
51
52         $temp =~ s/\**$/*/;
53         push (@Patterns, [[lastname => $temp], [firstname => $temp], [mail => $temp]]);
54 }
55
56 die ('No (valid) patterns found.') unless (@Patterns);
57
58 LiCoM::Person->connect 
59 (
60         uri     => $Config->{'uri'},
61         base_dn => $Config->{'base_dn'},
62         bind_dn => $Config->{'bind_dn'},
63         password => $Config->{'password'}
64 ) or die;
65
66 our @Matches = LiCoM::Person->search (@Patterns, [[mail => '*']]);
67
68 print STDOUT scalar (@Matches), ' ', (scalar (@Matches) == 1 ? 'entry' : 'entries'), " found.\n";
69
70 for (sort { $a->name () cmp $b->name () } (@Matches))
71 {
72         my $person = $_;
73         my $cn = $person->name ();
74         my @mail = $person->get ('mail');
75         my @groups = $person->get ('group');
76         my $info = join (', ', sort (@groups));
77
78         $info = "($info)" if ($info);
79
80         for (sort (@mail))
81         {
82                 print "$_\t$cn\t$info\n";
83         }
84 }
85
86 LiCoM::Person->disconnect ();
87
88 exit (0);