Commit aacf8ed8 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

sympa.pl: Adding a new option --add. The option --import was deprecated.

parent ac5a081d
......@@ -7043,10 +7043,32 @@ sub do_import {
#);
#return $next_action unless $next_action eq '1';
 
unless ($list->is_subscription_allowed) {
wwslog('info', 'List %s not open', $list);
add_stash('user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
return $in{'previous_action'} || 'review';
}
my (@emails, @dnames);
foreach (split /\r\n|\r|\n/, $content) {
next unless /\S/;
next if /\A\s*#/; #FIXME: email address can contain '#'
my ($email, $dname) = m{\A\s*(\S+)(?:\s+(.*))?\s*\z};
push @emails, $email;
push @dnames, (length($dname // '') ? $dname : undef);
}
unless (@emails) {
add_stash('user', 'no_email');
return $in{'previous_action'} || 'review';
}
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'import',
dump => $content,
action => 'add',
email => [@emails],
gecos => [@dnames],
sender => $param->{'user'}{'email'},
quiet => $param->{'quiet'},
md5_check => 1,
......@@ -7100,6 +7122,13 @@ sub do_add {
);
return $next_action unless $next_action eq '1';
 
unless ($list->is_subscription_allowed) {
wwslog('info', 'List %s not open', $list);
add_stash('user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
return $in{'previous_action'} || 'review';
}
my $stash = [];
my $processed = 0;
foreach my $email (@emails) {
......@@ -7215,6 +7244,13 @@ sub do_del {
);
return $next_action unless $next_action eq '1';
 
unless ($list->is_subscription_allowed) {
wwslog('info', 'List %s not open', $list);
add_stash('user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
return $in{'previous_action'} || 'review';
}
my $stash = [];
my $processed = 0;
foreach my $email (@emails) {
......
......@@ -6,8 +6,8 @@
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
# Copyright 2017, 2018, 2019 The Sympa Community. See the AUTHORS.md file at
# the top-level directory of this distribution and at
# Copyright 2017, 2018, 2019, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
......@@ -100,7 +100,6 @@ nobase_modules_DATA = \
Sympa/Request/Handler/global_set.pm \
Sympa/Request/Handler/global_signoff.pm \
Sympa/Request/Handler/help.pm \
Sympa/Request/Handler/import.pm \
Sympa/Request/Handler/include.pm \
Sympa/Request/Handler/index.pm \
Sympa/Request/Handler/info.pm \
......
......@@ -3657,6 +3657,19 @@ sub is_included {
return $num;
}
# If a list is not 'open' and allow_subscribe_if_pending has been set to
# 'off', reject all changes.
sub is_subscription_allowed {
my $self = shift;
return (
'open' eq $self->{'admin'}{'status'}
or 'on' eq Conf::get_robot_conf(
$self->{'domain'}, 'allow_subscribe_if_pending'
)
);
}
# Old name: Sympa::List::get_nextdigest().
# Moved to Sympa::Spindle::ProcessDigest::_may_distribute_digest().
#sub may_distribute_digest;
......@@ -6541,6 +6554,12 @@ I<Instance method>.
Is a reception mode in the parameter reception of the available_user_options
section?
=item is_subscription_allowed ( )
I<Instance method>.
Is it allowed to add or remove subscribers of the list?
Added on Sympa 6.2.67b.1.
=item is_digest ( )
I<Instance method>.
......
......@@ -62,6 +62,13 @@ sub _twist {
my $comment = $request->{gecos};
my $ca = $request->{custom_attribute};
unless ($request->{force} or $list->is_subscription_allowed) {
$log->syslog('info', 'List %s not open', $list);
$self->add_stash($request, 'user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
return undef;
}
$language->set_lang($list->{'admin'}{'lang'});
unless (Sympa::Tools::Text::valid_email($email)) {
......@@ -89,21 +96,6 @@ sub _twist {
return undef;
}
unless ($request->{force}) {
# If a list is not 'open' and allow_subscribe_if_pending has been set
# to 'off' returns undef.
unless (
$list->{'admin'}{'status'} eq 'open'
or Conf::get_robot_conf($list->{'domain'},
'allow_subscribe_if_pending') eq 'on'
) {
$self->add_stash($request, 'user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
$log->syslog('info', 'List %s not open', $list);
return undef;
}
}
my $u;
my $defaults = $list->get_default_user_options();
%{$u} = %{$defaults};
......
......@@ -8,8 +8,8 @@
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
# Copyright 2017 The Sympa Community. See the AUTHORS.md file at the top-level
# directory of this distribution and at
# Copyright 2017, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
......@@ -57,6 +57,13 @@ sub _twist {
my $sender = $request->{sender};
my $who = $request->{email};
unless ($request->{force} or $list->is_subscription_allowed) {
$log->syslog('info', 'List %s not open', $list);
$self->add_stash($request, 'user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
return undef;
}
$language->set_lang($list->{'admin'}{'lang'});
# Check if we know this email on the list and remove it. Otherwise
......@@ -70,21 +77,6 @@ sub _twist {
return undef;
}
unless ($request->{force}) {
# If a list is not 'open' and allow_subscribe_if_pending has been set
# to 'off' returns undef.
unless (
$list->{'admin'}{'status'} eq 'open'
or Conf::get_robot_conf($list->{'domain'},
'allow_subscribe_if_pending') eq 'on'
) {
$self->add_stash($request, 'user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
$log->syslog('info', 'List %s not open', $list);
return undef;
}
}
# Really delete and rewrite to disk.
unless (
$list->delete_list_member(
......
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2017 The Sympa Community. See the AUTHORS.md file at the top-level
# directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package Sympa::Request::Handler::import;
use strict;
use warnings;
use Time::HiRes qw();
use Conf;
use Sympa::Log;
use Sympa::Spindle::ProcessRequest;
use base qw(Sympa::Request::Handler);
my $log = Sympa::Log->instance;
use constant _action_scenario => undef;
use constant _context_class => 'Sympa::List';
sub _twist {
my $self = shift;
my $request = shift;
my $list = $request->{context};
my $sender = $request->{sender};
unless ($request->{force}) {
# If a list is not 'open' and allow_subscribe_if_pending has been set
# to 'off' returns undef.
unless (
$list->{'admin'}{'status'} eq 'open'
or Conf::get_robot_conf($list->{'domain'},
'allow_subscribe_if_pending') eq 'on'
) {
$self->add_stash($request, 'user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}});
$log->syslog('info', 'List %s not open', $list);
return undef;
}
}
my @users = map {
my ($email, $gecos) = m{\A\s*(\S+)(?:\s+(.*))?\s*\z};
(defined $gecos and $gecos =~ /\S/)
? {email => $email, gecos => $gecos}
: {email => $email}
} grep {
/\S/ and !/\A\s*#/
} split /\r\n|\r|\n/, ($request->{dump} || '');
my $processed = 0;
foreach my $user (@users) {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'add',
email => $user->{email},
gecos => $user->{gecos},
quiet => $request->{quiet},
force => $request->{force},
sender => $sender,
md5_check => $request->{md5_check},
scenario_context => {
%{$self->{scenario_context} || {}},
sender => $sender,
email => $user->{email},
},
stash => $self->{stash},
);
$spindle and $processed += $spindle->spin;
last
if grep {
$_->[1] eq 'intern'
or $_->[1] eq 'user' and ($_->[2] eq 'list_not_open'
or $_->[2] eq 'max_list_members_exceeded')
} @{$self->{stash} || []};
}
unless ($processed) { # No message
$log->syslog('info', 'Import %s from %s failed, no e-mails to add',
$list, $sender);
$self->add_stash($request, 'user', 'no_email');
return undef;
}
$log->syslog(
'info', 'Import %s from %s finished (%.2f seconds)',
$list, $sender, Time::HiRes::time() - $self->{start_time},
);
return 1;
}
1;
__END__
=encoding utf-8
=head1 NAME
Sympa::Request::Handler::import - import request handler
=head1 DESCRIPTION
Add subscribers to the list.
E-mails and display names of subscribers are taken from {dump} parameter,
the text including lines describing users to be added.
=head2 Attributes
=over
=item {dump}
I<Mandatory>.
Text including information of users to be added.
=item {force}
I<Optional>.
If true value is specified,
users will be added even if the list is closed.
=back
=head1 SEE ALSO
L<Sympa::Request::Handler>, L<Sympa::Request::Handler::add>.
=head1 HISTORY
L<Sympa::Request::Handler::import> appeared on Sympa 6.2.19b.
=cut
......@@ -92,7 +92,7 @@ unless (
'dump_users', 'restore_users',
'open_list=s', 'show_pending_lists=s',
'notify', 'rebuildarc=s',
'del=s',
'add=s', 'del=s',
)
) {
pod2usage(-exitval => 1, -output => \*STDERR);
......@@ -499,37 +499,61 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
printf "md5 digest : %s \n", $md5;
exit 0;
} elsif ($main::options{'import'}) {
printf STDERR "The --import was deprecated. Use --add instead.\n";
exit 1;
} elsif ($main::options{'add'}) {
#FIXME The parameter should be a list address.
unless ($main::options{'import'} =~ /\@/) {
printf STDERR "Incorrect list address %s\n", $main::options{'import'};
unless (0 < index $main::options{'add'}, '@') {
printf STDERR "Incorrect list address %s\n", $main::options{'add'};
exit 1;
}
my $list;
unless ($list = Sympa::List->new($main::options{'import'})) {
printf STDERR "Unknown list name %s\n", $main::options{'import'};
unless ($list = Sympa::List->new($main::options{'add'})) {
printf STDERR "Unknown list name %s\n", $main::options{'add'};
exit 1;
}
unless ($main::options{force} or $list->is_subscription_allowed) {
printf STDERR "List %s not open\n", $main::options{'add'};
exit 1;
}
my (@emails, @dnames);
my $content = do { local $RS; <STDIN> };
foreach (split /\r\n|\r|\n/, $content) {
next unless /\S/;
next if /\A\s*#/; #FIXME: email address can contain '#'
my ($email, $dname) = m{\A\s*(\S+)(?:\s+(.*))?\s*\z};
push @emails, $email;
push @dnames, (length($dname // '') ? $dname : undef);
}
unless (@emails) {
print STDERR "No email addresses found in input.\n";
exit 1;
}
my $dump = do { local $RS; <STDIN> };
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'import',
dump => $dump,
action => 'add',
email => [@emails],
gecos => [@dnames],
force => 1,
quiet => $main::options{'quiet'},
notify => $main::options{'notify'},
sender => Sympa::get_address($list, 'listmaster'),
scenario_context => {skip => 1},
quiet => $main::options{quiet},
);
unless ($spindle and $spindle->spin) {
printf STDERR "Failed to add email addresses to %s\n", $list;
unless ($spindle and $spindle->spin and _report($spindle)) {
printf STDERR "Failed to add email addresses to %s\n", $list->get_id;
exit 1;
}
my $status = _report($spindle);
printf STDERR "Total imported subscribers: %d\n",
scalar(grep { $_->[1] eq 'notice' and $_->[2] eq 'now_subscriber' }
@{$spindle->{stash} || []});
exit($status ? 0 : 1);
exit 0;
} elsif ($main::options{'del'}) {
#FIXME The parameter should be a list address.
......@@ -542,37 +566,42 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
printf STDERR "Unknown list name %s\n", $main::options{'del'};
exit 1;
}
my $status;
foreach my $mail (<STDIN>) {
chomp $mail;
next unless $mail =~ /\S/;
unless ($main::options{force} or $list->is_subscription_allowed) {
printf STDERR "List %s not open\n", $main::options{'del'};
exit 1;
}
# Note: email address can contain "#" as of Sympa 6.2.65b.2.
unless (Sympa::Tools::Text::valid_email($mail)) {
printf STDERR "Skipping \"%s\"\n", $mail;
next;
}
printf STDERR "Deleting mail %s\n", $mail;
my @emails;
my $content = do { local $RS; <STDIN> };
foreach (split /\r\n|\r|\n/, $content) {
next unless /\S/;
next if /\A\s*#/; #FIXME: email address can contain '#'
my $notify = $main::options{'notify'} // 0;
my ($email) = m{\A\s*(\S+)};
push @emails, $email;
}
unless (@emails) {
print STDERR "No email addresses found in input.\n";
exit 1;
}
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'del',
email => $mail,
force => 1,
quiet => 1,
notify => $notify,
sender => Sympa::get_address($list, 'listmaster'),
scenario_context => {skip => 1},
);
unless ($spindle and $spindle->spin and _report($spindle)) {
printf STDERR "Failed to delete email addresses from %s\n",
$list->get_id;
exit 1;
}
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'del',
email => [@emails],
force => 1,
quiet => $main::options{'quiet'},
notify => $main::options{'notify'},
sender => Sympa::get_address($list, 'listmaster'),
scenario_context => {skip => 1},
);
unless ($spindle and $spindle->spin and _report($spindle)) {
printf STDERR "Failed to delete email addresses from %s\n",
$list->get_id;
exit 1;
}
exit 0;
} elsif ($main::options{'md5_encode_password'}) {
......@@ -1820,8 +1849,8 @@ C<sympa.pl> S<[ C<-d, --debug> ]> S<[ C<-f, --file>=I<another.sympa.conf> ]>
S<[ C<-l, --lang>=I<lang> ]> S<[ C<-m, --mail> ]>
S<[ C<-h, --help> ]> S<[ C<-v, --version> ]>
S<>
S<[ C<--del>=I<list>@I<domain> [--notify] ]>
S<[ C<--import>=I<listname> ]>
S<[ C<--add>=I<list>@I<domain> [--quiet] [--notify] [--force] ]>
S<[ C<--del>=I<list>@I<domain> [--quiet] [--notify] [--force] ]>
S<[ C<--open_list>=I<list>[I<@robot>] [--notify] ]>
S<[ C<--close_list>=I<list>[I<@robot>] ]>
S<[ C<--purge_list>=I<list>[I<@robot>] ]>
......@@ -1870,6 +1899,20 @@ With the following options F<sympa.pl> will run in batch mode:
=over 4
=item C<--add=>I<list>@I<domain> [ C<--quiet> ] [ C<--notify> ] [ C<--force> ]
Add email(s) from the list. Data are read from standard input.
The data should contain one email address per line.
Sample:
## emails to be added
john.steward@some.company.com John Steward
mary.blacksmith@another.company.com Mary Blacksmith
Note:
This options was added on Sympa 6.2.67b.1.
=item C<--add_list=>I<family_name> C<--robot=>I<robot_name>
C<--input_file=>I</path/to/file.xml>
......@@ -1905,18 +1948,20 @@ C<--input_file=>I</path/to/file.xml >
Create a list with the XML file under robot robot_name.
=item C<--del=>I<list>@I<domain> [ C<--notify> ]
=item C<--del=>I<list>@I<domain> [ C<--quiet> ] [ C<--notify> ] [ C<--force> ]
Delete email(s) from the list. Data are read from standard input.
The data should contain one email address per line.
This option is always "quiet".
Sample:
## emails to be deleted
john.steward@some.company.com
mary.blacksmith@another.company.com
Note:
This options was added on Sympa 6.2.67b.1.
=item C<--dump=>I<list>@I<domain>|C<ALL>
Obsoleted option. Use C<--dump_users>.
......@@ -1952,19 +1997,7 @@ are correct. If any errors occur, exits with non-zero status.
=item C<--import=>I<list>@I<dom>
Import subscribers in the list. Data are read from standard input.
The imported data should contain one entry per line : the first field
is an email address, the second (optional) field is the free form name.
Fields are spaces-separated.
Use C<--quiet> to prevent welcome emails.
Sample:
## Data to be imported
## email gecos
john.steward@some.company.com John - accountant
mary.blacksmith@another.company.com Mary - secretary
Deprecated. Use C<--add> instead.
=item C<--instantiate_family=>I<family_name> C<--robot=>I<robot_name>
C<--input_file=>I</path/to/file.xml> [ C<--close_unknown> ] [ C<--quiet> ]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment