#!/usr/bin/perl use strict; use Getopt::Long; use lib '/usr/share/sympa/lib'; use Conf; use List; use Log; use Data::Dumper; my %options; unless (&GetOptions(\%main::options, 'import=s')) { &fatal_err("Unknown options."); } my $config_file = $main::options{'config'} || Sympa::Constants::CONFIG; ## Load configuration file. Ignoring database config for now unless (Conf::load($config_file,1)) { &fatal_err("Configuration file $config_file has errors."); } ## Probe Db if defined if ($Conf{'db_name'} and $Conf{'db_type'}) { unless (&Upgrade::probe_db()) { &fatal_err('Database %s defined in sympa.conf has not the right structure', $Conf{'db_name'}); } } ## Now trying to load full config (including database) unless (Conf::load($config_file)) { &fatal_err("Configuration file $config_file has errors."); } do_openlog($Conf::Conf{'syslog'}, $Conf::Conf{'log_socket_type'}, 'bulk'); if ($main::options{'import'}) { my ($list); ## The parameter should be a list address unless ($main::options{'import'} =~ /\@/) { &do_log('err','Incorrect list address %s', $main::options{'import'}); exit; } unless ($list = new List ($main::options{'import'})) { fatal_err('Unknown list name %s', $main::options{'import'}); } my %subscribers = (); ## Read imported data from STDIN while () { next if /^\s*$/; next if /^\s*\#/; unless (/^\s*((\S+|\".*\")@\S+)(\s*(\S.*))?\s*$/) { printf STDERR "Not an email address: %s\n", $_; } my $email = lc($1); my $gecos = ""; my $u; my $defaults = $list->get_default_user_options(); %{$u} = %{$defaults}; $u->{'email'} = $email; $u->{'gecos'} = $gecos; if (!$list->is_user($email)) { unless($list->add_user($u)) { printf STDERR "\nCould not add %s\n", $email; next; } } $subscribers{$email} = 1; } ## Remove existing subscribers not found in STDIN for (my $user = $list->get_first_user(); $user; $user = $list->get_next_user() ) { my $email = lc($user->{'email'}); if (!exists($subscribers{$email})) { unless ( $list->delete_user('users' => [$email]) ) { printf STDERR "\nCould not delete %s\n", $email; next; } } } exit 0; }