source: LATMOS-Accounts/bin/la-sync-list @ 834

Last change on this file since 834 was 834, checked in by nanardon, 14 years ago
  • allow to several filters to build list, filters are merged, eg:

filter1=foo=bar
filter2=baz=tux
will perform seearch users having foo atributes set to bar, than adding user having baz set to tux (like OR).
Any keywords begining by 'filter' can be used

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use 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
24Use this configuration file instead default
25
26=item --base basename
27
28Perform search in this specific base instead default
29
30=item --lc cfg
31
32la-list-sync config file
33
34=back
35
36=cut
37
38GetOptions(
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
45my $listcfg = Config::IniFiles->new(
46    -file => $listconfig,
47    -default => '_default_',
48) or die "Cannot open list config file\n";
49
50my $LA = LATMOS::Accounts->new($config);
51my $labase = $base ? $LA->base($base) : $LA->default_base;
52$labase && $labase->load or die "Cannot load base";
53
54my %cache;
55
56foreach 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
96exit(0);
Note: See TracBrowser for help on using the repository browser.