source: tags/5.2.1/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Buildlistes.pm @ 2626

Last change on this file since 2626 was 1802, checked in by nanardon, 8 years ago

Don't use ACL for task module

  • Property svn:executable set to *
File size: 5.6 KB
Line 
1package LATMOS::Accounts::Task::Buildlistes;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use Config::IniFiles;
8use LATMOS::Accounts::Log;
9use LATMOS::Accounts::Utils;
10
11=head1 NAME
12
13LATMOS::Accounts::Task::Buildlistes - Task to generate list of mail address
14usable by mail robot such as mailman.
15
16=head1 DESCRITPTION
17
18This module is designed to automatically build mailing list members file. The
19configuration is handle by F<la-sync-list.ini> file.
20
21=cut
22
23sub runDelay { 60 * 60 } # 1 hour
24
25sub init {
26    my ($self) = @_;
27    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
28    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
29    $labase && $labase->load or die "Cannot load base";
30
31    my $listconfig = $self->{listconfig} || join('/', $LA->_configdir, 'la-sync-list.ini');
32    my $listcfg = Config::IniFiles->new(
33        -file => $listconfig,
34        -default => '_default_',
35    ) or do {
36        la_log LA_ERR, "Cannot open list config file $listconfig";
37        return;
38    };
39
40    $self->{_la} = $LA;
41    $self->{_base} = $labase;
42    $self->{_listcfg} = $listcfg;
43
44    1;
45}
46
47sub run {
48    my ($self) = @_;
49    my $listcfg = $self->{_listcfg};
50    my $labase = $self->{_base};
51
52    my %cache;
53    if (my $cmd = $listcfg->val('_default_', 'pre')) {
54        exec_command(
55            $cmd,
56            {
57                DIRECTORY => $listcfg->val('_default_', 'destdir'),
58                HOOK_TYPE => 'PRE',
59            },
60        );
61    } 
62
63    foreach my $list ($listcfg->Sections) {
64        la_log LA_DEBUG, "Start to process %s", $list;
65        my %content = ();
66        $list eq '_default_' and next;
67        $listcfg->val($list, 'ignore') and do {
68            la_log LA_DEBUG, "list %s taggued 'ignored'", $list;
69            next;
70        };
71        my $fmt = $listcfg->val($list, 'fmt', '%{mail}');
72        my $otype = $listcfg->val($list, 'objects', 'user');
73        foreach (grep { $_ } $listcfg->val($list, 'addtolist')) {
74            $content{$_} = 1;
75        }
76        my %ids;
77        # finding /^filter/ as search results
78        # adding to list results
79        foreach my $param ($listcfg->Parameters($list)) {
80            $param =~ /^filter/ or next;
81
82            foreach my $id (sort $labase->search_objects(
83                    $listcfg->val($list, 'objects', 'user'),
84                    $listcfg->val($list, $param),)) {
85                $ids{$id} = 1;
86            }
87        }
88        # finding /^excludefilter/ as search results
89        # deleting from list results
90        my %done;
91        foreach my $param ($listcfg->Parameters($list),
92                           $listcfg->Parameters('_default_')) {
93            $done{$param} and next;
94            $done{$param} = 1;
95            $param =~ /^excludefilter/ or next;
96            $listcfg->val($list, $param) or next;
97
98            foreach my $id (sort $labase->search_objects(
99                    $listcfg->val($list, 'objects', 'user'),
100                    $listcfg->val($list, $param),)) {
101                delete($ids{$id});
102            }
103        }
104        foreach my $id (sort keys %ids) {
105            if (!$cache{$otype}{$fmt}{$id}) {
106                my $obj = $labase->get_object(
107                    $otype,
108                    $id,
109                );
110
111                $cache{$otype}{$fmt}{$id} = $obj->queryformat($fmt);
112            }
113            $content{ $cache{$otype}{$fmt}{$id} } = 1;
114        }
115        # No destdir, no cmd, will do nothing...
116        if (my $destdir = $listcfg->val('_default_', 'destdir')) {
117            if (open(my $handle, '>', "$destdir/$list")) {
118                foreach (sort keys %content) {
119                    print $handle $_ ."\n";
120                }
121                close($handle);
122                la_log LA_NOTICE, "%s written", "$destdir/$list";
123                if (my $cmd = $listcfg->val('_default_', 'post_file')) {
124                    exec_command(
125                        $cmd,
126                        {
127                            DIRECTORY => $destdir,
128                            OUTPUT_FILE => $list,
129                            HOOK_TYPE => 'POSTFILE',
130                        },
131                    );
132                } 
133            } else {
134                la_log LA_ERR, "Can't open %s: %s", "$destdir/$list", $!;
135            }
136        }
137        # compat
138        if (my $cmd = $listcfg->val($list, 'cmd')) {
139            $cmd =~ s/%%/$list/g;
140            if (open(my $handle, '|' . $cmd)) {
141                foreach (sort keys %content) {
142                    print $handle $_ ."\n";
143                }
144                close($handle);
145                if ($?) {
146                    la_log(LA_ERR, "Command `%s' exit with status %d", $cmd, $?);
147                } else {
148                    la_log(LA_NOTICE, "Command `%s' done", $cmd);
149                }
150            } else {
151                la_log LA_ERR, "Can run command `%s': %s", $cmd, $!;
152            } 
153        }
154    }
155    if (my $cmd = $listcfg->val('_default_', 'post')) {
156        exec_command(
157            $cmd,
158            {
159                DIRECTORY => $listcfg->val('_default_', 'destdir'),
160                HOOK_TYPE => 'POST',
161            },
162        );
163    } 
164
165    1;
166}
167
1681;
169
170=head1 SEE ALSO
171
172Configuraiton file: L<la-sync-list.ini>
173
174L<LATMOS::Accounts::Task>
175
176=head1 AUTHOR
177
178Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
179
180=head1 COPYRIGHT AND LICENSE
181
182Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
183
184This library is free software; you can redistribute it and/or modify
185it under the same terms as Perl itself, either Perl version 5.10.0 or,
186at your option, any later version of Perl 5 you may have available.
187
188=cut
Note: See TracBrowser for help on using the repository browser.