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