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 runDelay { 60 * 60 } # 1 hour |
---|
24 | |
---|
25 | sub 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 | |
---|
47 | sub 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 | 'oalias=NULL', |
---|
86 | )) { |
---|
87 | $ids{$id} = 1; |
---|
88 | } |
---|
89 | } |
---|
90 | # finding /^excludefilter/ as search results |
---|
91 | # deleting from list results |
---|
92 | my %done; |
---|
93 | foreach my $param ($listcfg->Parameters($list), |
---|
94 | $listcfg->Parameters('_default_')) { |
---|
95 | $done{$param} and next; |
---|
96 | $done{$param} = 1; |
---|
97 | $param =~ /^excludefilter/ or next; |
---|
98 | $listcfg->val($list, $param) or next; |
---|
99 | |
---|
100 | foreach my $id (sort $labase->search_objects( |
---|
101 | $listcfg->val($list, 'objects', 'user'), |
---|
102 | $listcfg->val($list, $param), |
---|
103 | 'oalias=NULL', |
---|
104 | )) { |
---|
105 | delete($ids{$id}); |
---|
106 | } |
---|
107 | } |
---|
108 | foreach my $id (sort keys %ids) { |
---|
109 | if (!$cache{$otype}{$fmt}{$id}) { |
---|
110 | my $obj = $labase->get_object( |
---|
111 | $otype, |
---|
112 | $id, |
---|
113 | ); |
---|
114 | |
---|
115 | $cache{$otype}{$fmt}{$id} = $obj->queryformat($fmt); |
---|
116 | } |
---|
117 | $content{ $cache{$otype}{$fmt}{$id} } = 1; |
---|
118 | } |
---|
119 | # No destdir, no cmd, will do nothing... |
---|
120 | if (my $destdir = $listcfg->val('_default_', 'destdir')) { |
---|
121 | if (open(my $handle, '>', "$destdir/$list")) { |
---|
122 | foreach (sort keys %content) { |
---|
123 | print $handle $_ ."\n"; |
---|
124 | } |
---|
125 | close($handle); |
---|
126 | la_log LA_NOTICE, "%s written", "$destdir/$list"; |
---|
127 | if (my $cmd = $listcfg->val('_default_', 'post_file')) { |
---|
128 | exec_command( |
---|
129 | $cmd, |
---|
130 | { |
---|
131 | DIRECTORY => $destdir, |
---|
132 | OUTPUT_FILE => $list, |
---|
133 | HOOK_TYPE => 'POSTFILE', |
---|
134 | }, |
---|
135 | ); |
---|
136 | } |
---|
137 | } else { |
---|
138 | la_log LA_ERR, "Can't open %s: %s", "$destdir/$list", $!; |
---|
139 | } |
---|
140 | } |
---|
141 | # compat |
---|
142 | if (my $cmd = $listcfg->val($list, 'cmd')) { |
---|
143 | $cmd =~ s/%%/$list/g; |
---|
144 | if (open(my $handle, '|' . $cmd)) { |
---|
145 | foreach (sort keys %content) { |
---|
146 | print $handle $_ ."\n"; |
---|
147 | } |
---|
148 | close($handle); |
---|
149 | if ($?) { |
---|
150 | la_log(LA_ERR, "Command `%s' exit with status %d", $cmd, $?); |
---|
151 | } else { |
---|
152 | la_log(LA_NOTICE, "Command `%s' done", $cmd); |
---|
153 | } |
---|
154 | } else { |
---|
155 | la_log LA_ERR, "Can run command `%s': %s", $cmd, $!; |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | if (my $cmd = $listcfg->val('_default_', 'post')) { |
---|
160 | exec_command( |
---|
161 | $cmd, |
---|
162 | { |
---|
163 | DIRECTORY => $listcfg->val('_default_', 'destdir'), |
---|
164 | HOOK_TYPE => 'POST', |
---|
165 | }, |
---|
166 | ); |
---|
167 | } |
---|
168 | |
---|
169 | 1; |
---|
170 | } |
---|
171 | |
---|
172 | 1; |
---|
173 | |
---|
174 | =head1 SEE ALSO |
---|
175 | |
---|
176 | Configuraiton file: L<la-sync-list.ini> |
---|
177 | |
---|
178 | L<LATMOS::Accounts::Task> |
---|
179 | |
---|
180 | =head1 AUTHOR |
---|
181 | |
---|
182 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
183 | |
---|
184 | =head1 COPYRIGHT AND LICENSE |
---|
185 | |
---|
186 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
187 | |
---|
188 | This library is free software; you can redistribute it and/or modify |
---|
189 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
190 | at your option, any later version of Perl 5 you may have available. |
---|
191 | |
---|
192 | =cut |
---|