1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use Config::IniFiles; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | la-sync-list - Tools to sync mailing list |
---|
13 | |
---|
14 | =head1 SYNOPSIS |
---|
15 | |
---|
16 | la-sync-list |
---|
17 | |
---|
18 | =head1 OPTIONS |
---|
19 | |
---|
20 | =over 4 |
---|
21 | |
---|
22 | =item --config configfile |
---|
23 | |
---|
24 | Use this configuration file instead default |
---|
25 | |
---|
26 | =item --base basename |
---|
27 | |
---|
28 | Perform search in this specific base instead default |
---|
29 | |
---|
30 | =item --lc cfg |
---|
31 | |
---|
32 | la-list-sync config file |
---|
33 | |
---|
34 | =back |
---|
35 | |
---|
36 | =cut |
---|
37 | |
---|
38 | GetOptions( |
---|
39 | 'c|config=s' => \my $config, |
---|
40 | 'lc=s' => \my $listconfig, |
---|
41 | 'b|base=s' => \my $base, |
---|
42 | 'help' => sub { pod2usage(0) }, |
---|
43 | ) or pod2usage(); |
---|
44 | |
---|
45 | my $listcfg = Config::IniFiles->new( |
---|
46 | -file => $listconfig, |
---|
47 | -default => '_default_', |
---|
48 | ) or die "Cannot open list config file\n"; |
---|
49 | |
---|
50 | my $LA = LATMOS::Accounts->new($config); |
---|
51 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
52 | $labase && $labase->load or die "Cannot load base"; |
---|
53 | |
---|
54 | my %cache; |
---|
55 | |
---|
56 | foreach my $list ($listcfg->Sections) { |
---|
57 | my %content = (); |
---|
58 | $list eq '_default_' and next; |
---|
59 | $listcfg->val($list, 'ignore') and next; |
---|
60 | my $fmt = $listcfg->val($list, 'fmt', '%{mail}'); |
---|
61 | my $otype = $listcfg->val($list, 'objects', 'user'); |
---|
62 | foreach (grep { $_ } $listcfg->val($list, 'addtolist')) { |
---|
63 | $content{$_} = 1; |
---|
64 | } |
---|
65 | my %ids; |
---|
66 | foreach my $param ($listcfg->Parameters($list)) { |
---|
67 | $param =~ /^filter/ or next; |
---|
68 | |
---|
69 | foreach my $id (sort $labase->search_objects( |
---|
70 | $listcfg->val($list, 'objects', 'user'), |
---|
71 | $listcfg->val($list, $param),)) { |
---|
72 | $ids{$id} = 1; |
---|
73 | } |
---|
74 | } |
---|
75 | foreach my $id (sort keys %ids) { |
---|
76 | if (!$cache{$otype}{$fmt}{$id}) { |
---|
77 | my $obj = $labase->get_object( |
---|
78 | $otype, |
---|
79 | $id, |
---|
80 | ); |
---|
81 | |
---|
82 | $cache{$otype}{$fmt}{$id} = $obj->queryformat($fmt); |
---|
83 | } |
---|
84 | $content{ $cache{$otype}{$fmt}{$id} } = 1; |
---|
85 | } |
---|
86 | my $cmd = $listcfg->val($list, 'cmd', 'cat'); |
---|
87 | $cmd =~ s/%%/$list/g; |
---|
88 | if (open(my $handle, '|' . $cmd)) { |
---|
89 | foreach (keys %content) { |
---|
90 | print $handle $_ ."\n"; |
---|
91 | } |
---|
92 | close($handle); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | exit(0); |
---|