File Coverage

blib/lib/App/Addex/AddressBook/LDAP.pm
Criterion Covered Total %
statement 7 7 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 10 100.0


line stmt bran cond sub pod time code
1 1     1   852 use Modern::Perl;
  1         2  
  1         10  
2              
3             package App::Addex::AddressBook::LDAP;
4             BEGIN {
5 1     1   232 $App::Addex::AddressBook::LDAP::VERSION = '0.001';
6             }
7              
8             # ABSTRACT: LDAP address book for App::Addex
9              
10 1     1   906 use parent 'App::Addex::AddressBook';
  1         293  
  1         6  
11              
12             use App::Addex::Entry;
13             use App::Addex::Entry::EmailAddress;
14             use Carp;
15             use Net::LDAP;
16             use URI;
17              
18             sub new {
19             my ($class, $arg) = @_;
20              
21             my $uri = URI->new($arg->{uri});
22              
23             my $self = bless {
24             uri => $uri,
25             } => $class;
26              
27             my $ldap = Net::LDAP->new(
28             "$uri",
29             raw => qr/(?i:^jpegPhoto|;binary)/,
30             onerror => 'die'
31             ) or confess $@;
32             $ldap->bind();
33             $self->{ldap} = $ldap;
34              
35             return $self;
36             }
37              
38             sub _entrify {
39             my ($self, $entry) = @_;
40              
41             my @emails = map { App::Addex::Entry::EmailAddress->new($_) }
42             $entry->get_value('mail');
43             return unless @emails;
44              
45             return App::Addex::Entry->new(
46             {
47             name => $entry->get_value('cn'),
48             nick => $entry->get_value('uid'),
49             emails => \@emails,
50             }
51             );
52             }
53              
54             sub entries {
55             my ($self) = @_;
56              
57             my $mesg = $self->{ldap}->search(
58             base => $self->{uri}->dn,
59             filter => $self->{uri}->filter,
60             scope => $self->{uri}->scope,
61             );
62              
63             return map { $self->_entrify($_) } $mesg->entries;
64             }
65              
66             1;
67              
68              
69             __END__
70             =pod
71              
72             =head1 NAME
73              
74             App::Addex::AddressBook::LDAP - LDAP address book for App::Addex
75              
76             =head1 VERSION
77              
78             version 0.001
79              
80             =head1 SYNOPSIS
81              
82             This module implements the App::Addex::AddressBook interface by querying an
83             LDAP server for mail attributes. To use it, your App::Addex configuration might look like this:
84              
85             addressbook = App::Addex::AddressBook::LDAP
86             output = App::Addex::Output::Mutt
87              
88             [App::Addex::AddressBook::LDAP]
89             uri = ldap://yourldaphost/ou=People,dc=example,dc=org??sub
90              
91             [App::Addex::Output::Mutt]
92             filename = /home/mxey/.mutt_aliases
93              
94             The cn attribute is used as the name, the uid attribute is used as the nick and
95             mail attributes are used as e-mail addresses.
96              
97             =head1 BUGS
98              
99             Attributes in the LDAP URI are ignored. It might be nice to specify attributes
100             to use as mail addresses in the URI.
101              
102             Only anonymous binding is supported. If you need to specify a binddn, please
103             open a bugreport and/or send a patch :-)
104              
105             =head1 AUTHOR
106              
107             Maximilian Gass <mxey@ghosthacking.net>
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2011 by Maximilian Gass.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut
117