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

Refactoring: New module Sympa::Aliases to be base class for aliases management.

Sympa::Admin was deprecated.
parent 264a6c87
......@@ -30,8 +30,8 @@ nobase_dist_modules_DATA = \
Sympa/Internals/Workflow.pod
nobase_modules_DATA = \
Sympa.pm \
Sympa/Admin.pm \
Sympa/Alarm.pm \
Sympa/Aliases.pm \
Sympa/Archive.pm \
Sympa/Auth.pm \
Sympa/Bulk.pm \
......
......@@ -4,10 +4,6 @@
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# 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
# <https://github.com/sympa-community/sympa.git>.
......@@ -25,23 +21,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
=encoding utf-8
#=head1 NAME
#
#I<admin.pm> - This module includes administrative function for the lists.
=head1 DESCRIPTION
Central module for creating and editing lists.
=cut
package Sympa::Admin;
package Sympa::Aliases;
use strict;
use warnings;
use English qw(-no_match_vars);
BEGIN { eval 'use Net::SMTP'; }
use Conf;
use Sympa::Constants;
......@@ -49,70 +34,39 @@ use Sympa::Log;
my $log = Sympa::Log->instance;
=head1 SUBFUNCTIONS
This is the description of the subfunctions contained by admin.pm
=cut
# Moved to: Sympa::Request::Handler::create_list::_twist().
#sub create_list_old;
# Merged to: Sympa::Request::Handler::create_automatic_list::_twist().
#sub create_list;
# Moved to: Sympa::Family::_update_list().
#sub update_list;
# Moved to: Sympa::Request::Handler::move_list::_twist().
#sub rename_list;
# Moved to: (part of) Sympa::Request::Handler::move_list::_copy().
#sub clone_list_as_empty;
# Moved to: Sympa::Request::Handler::create_list::_check_owner_defined().
#sub check_owner_defined;
sub new {
bless {} => shift;
}
#####################################################
# list_check_smtp
#####################################################
# check if the requested list exists already using
# smtp 'rcpt to'
#
# IN : - $name : name of the list
# - $robot : list's robot
# OUT : - Net::SMTP object or 0
#####################################################
sub list_check_smtp {
$log->syslog('debug2', '(%s, %s)', @_);
my $name = shift;
my $robot = shift;
# OLd name: Sympa::Admin::list_check_smtp().
sub check {
$log->syslog('debug2', '(%s, %s, %s)', @_);
my $self = shift;
my $name = shift;
my $robot_id = shift;
my $conf = '';
my $smtp;
my (@suf, @addresses);
my $smtp_relay = Conf::get_robot_conf($robot, 'list_check_smtp');
my $smtp_helo = Conf::get_robot_conf($robot, 'list_check_helo')
my $smtp_relay = Conf::get_robot_conf($robot_id, 'list_check_smtp');
my $smtp_helo = Conf::get_robot_conf($robot_id, 'list_check_helo')
|| $smtp_relay;
$smtp_helo =~ s/:[-\w]+$// if $smtp_helo;
my $suffixes = Conf::get_robot_conf($robot, 'list_check_suffixes');
my $suffixes = Conf::get_robot_conf($robot_id, 'list_check_suffixes');
return 0
unless $smtp_relay and $suffixes;
my $host = Conf::get_robot_conf($robot, 'host');
$log->syslog('debug2', '(%s, %s)', $name, $robot);
@suf = split /\s*,\s*/, $suffixes;
return 0 unless @suf;
$log->syslog('debug2', '(%s, %s)', $name, $robot_id);
my @suf = split /\s*,\s*/, $suffixes;
return 0 unless @suf; #FIXME
foreach my $suffix (@suf) {
push @addresses, $name . '-' . $suffix . '@' . $host;
}
push @addresses, $name . '@' . $host;
my @addresses = (
$name . '@' . $robot_id,
map { $name . '-' . $_ . '@' . $robot_id } @suf
);
eval { require Net::SMTP; };
if ($EVAL_ERROR) {
unless ($Net::SMTP::VERSION) {
$log->syslog('err',
"Unable to use Net library, Net::SMTP required, install it (CPAN) first"
'Unable to use Net library, Net::SMTP required, install it first'
);
return undef;
}
......@@ -123,8 +77,8 @@ sub list_check_smtp {
)
) {
$smtp->mail('');
for (@addresses) {
$conf = $smtp->to($_);
foreach my $address (@addresses) {
$conf = $smtp->to($address);
last if $conf;
}
$smtp->quit();
......@@ -133,23 +87,15 @@ sub list_check_smtp {
return undef;
}
##########################################################
# install_aliases
##########################################################
# Install sendmail aliases for $list
#
# IN : - $list : object list
# - $robot : the list's robot ** No longer used
# OUT : - undef if not applicable or aliases not installed
# 1 (if ok) or
##########################################################
sub install_aliases {
$log->syslog('debug', '(%s)', @_);
# Old name: Sympa::Admin::install_aliases().
sub add {
$log->syslog('debug', '(%s, %s)', @_);
my $self = shift;
my $list = shift;
return 1
if Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') =~
/^none$/i;
if lc Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') eq
'none';
my $alias_manager = $Conf::Conf{'alias_manager'};
$log->syslog('debug2', '%s add %s %s', $alias_manager, $list->{'name'},
......@@ -210,72 +156,130 @@ sub install_aliases {
return undef;
}
#########################################################
# remove_aliases
#########################################################
# Remove sendmail aliases for $list
#
# IN : - $list : object list
# - $robot : the list's robot ** No longer used
# OUT : - undef if not applicable
# 1 (if ok) or
# $aliases : concated string of alias not removed
#########################################################
sub remove_aliases {
$log->syslog('info', '(%s)', @_);
# Old names: Sympa::Admin::remove_aliases() & Sympa::List::remove_aliases().
sub del {
$log->syslog('info', '(%s, %s)', @_);
my $self = shift;
my $list = shift;
return 1
if Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') =~
/^none$/i;
my $status = $list->remove_aliases();
my $suffix =
Conf::get_robot_conf($list->{'domain'}, 'return_path_suffix');
my $aliases;
unless ($status == 1) {
$log->syslog('err', 'Failed to remove aliases for list %s',
$list->{'name'});
## build a list of required aliases the listmaster should install
my $libexecdir = Sympa::Constants::LIBEXECDIR;
$aliases = <<EOF;
#----------------- $list->{'name'}
$list->{'name'}: "$libexecdir/queue $list->{'name'}"
$list->{'name'}-request: "|$libexecdir/queue $list->{'name'}-request"
$list->{'name'}$suffix: "|$libexecdir/bouncequeue $list->{'name'}"
$list->{'name'}-unsubscribe: "|$libexecdir/queue $list->{'name'}-unsubscribe"
# $list->{'name'}-subscribe: "|$libexecdir/queue $list->{'name'}-subscribe"
EOF
return $aliases;
if lc Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') eq
'none';
my $alias_manager = $Conf::Conf{'alias_manager'};
unless (-x $alias_manager) {
$log->syslog('err', 'Cannot run alias_manager %s', $alias_manager);
return undef;
}
$log->syslog('info', 'Aliases removed successfully');
my $status =
system($alias_manager, 'del', $list->{'name'},
$list->{'admin'}{'host'}) >> 8;
return 1;
if ($status == 0) {
$log->syslog('info', 'Aliases for list %s removed successfully',
$list);
return 1;
} else {
$log->syslog('err', 'Failed to remove aliases; status %d: %m',
$status);
return undef;
}
}
# No longer used.
#sub check_topics;
1;
__END__
# Moved to Sympa::Request::Handler::move_user::_twist().
#sub change_user_email;
=encoding utf-8
=pod
=head1 NAME
=head1 AUTHORS
Sympa::Aliases - Base class for alias management
=over
=head1 DESCRIPTION
=item * Serge Aumont <sa AT cru.fr>
TBD.
=item * Olivier Salaun <os AT cru.fr>
=head2 Methods
=back
=over
=cut
=item new ( )
1;
I<Constructor>.
Creates new instance of L<Sympa::Aliases>.
=item check ($listname, $robot_id)
I<Instance method>.
Checks if the requested list exists already using SMTP 'rcpt to'.
Parameters:
=over
=item $listname
Name of the list.
=item $robot_id
List's robot.
=back
Returns:
L<Net::SMTP> object or false value.
=item add ($list)
I<Instance method>.
Installs aliases for the list $list.
Parameters:
=over
=item $list
An instance of L<Sympa::List>.
=back
Returns:
C<undef> if not applicable or aliases not installed. or C<1> if OK.
=item del ($list)
I<Instance method>.
Removes aliases for the list $list.
Parameters:
=over
=item $list
An instance of L<Sympa::List>.
=back
Returns:
C<undef> if not applicable. C<1> (if ok) or concated string of alias not
removed.
=back
=head1 HISTORY
F<alias_manager.pl> as a program to automate alias management appeared on
Sympa 3.1b.13.
L<Sympa::Aliases> module as an OO-based class appeared on Sympa 6.2.23b,
and it obsoleted F<alias_manager.pl>.
=cut
......@@ -47,7 +47,7 @@ use Term::ProgressBar;
use XML::LibXML;
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Conf;
use Sympa::Config_XML;
use Sympa::DatabaseManager;
......@@ -457,16 +457,15 @@ sub modify_list {
if ($result->{'aliases'} == 1) {
push @{$return->{'string_info'}},
"The $list->{'name'} list has been modified.";
sprintf('The %s list has been modified.', $list->{'name'});
} elsif ($result->{'install_remove'} eq 'install') {
push @{$return->{'string_info'}},
"List $list->{'name'} has been modified, required aliases :\n $result->{'aliases'} ";
sprintf('List %s has been modified, required aliases.',
$list->{'name'});
} else {
push @{$return->{'string_info'}},
"List $list->{'name'} has been modified, aliases need to be removed : \n $result->{'aliases'}";
sprintf('List %s has been modified, aliases need to be removed.',
$list->{'name'});
}
## config_changes
......@@ -2113,15 +2112,12 @@ sub _update_existing_list {
if ($result->{'aliases'} == 1) {
push(@{$self->{'updated_lists'}{'aliases_ok'}}, $list->{'name'});
} elsif ($result->{'install_remove'} eq 'install') {
$self->{'updated_lists'}{'aliases_to_install'}{$list->{'name'}} =
$result->{'aliases'};
$list->{'name'};
} else {
$self->{'updated_lists'}{'aliases_to_remove'}{$list->{'name'}} =
$result->{'aliases'};
$list->{'name'};
}
## config_changes
......@@ -2353,15 +2349,14 @@ sub _set_status_changes {
if ($list->{'admin'}{'status'} eq 'open') {
unless ($old_status eq 'open') {
$result->{'install_remove'} = 'install';
$result->{'aliases'} = Sympa::Admin::install_aliases($list);
$result->{'aliases'} = Sympa::Aliases->new->add($list);
}
}
if ( ($list->{'admin'}{'status'} eq 'pending')
&& (($old_status eq 'open') || ($old_status eq 'error_config'))) {
if ( $list->{'admin'}{'status'} eq 'pending'
and ($old_status eq 'open' or $old_status eq 'error_config')) {
$result->{'install_remove'} = 'remove';
$result->{'aliases'} =
Sympa::Admin::remove_aliases($list, $self->{'robot'});
$result->{'aliases'} = Sympa::Aliases->new->del($list);
}
### subscribers
......
......@@ -9095,36 +9095,8 @@ sub remove_task {
#sub purge;
## Remove list aliases
sub remove_aliases {
my $self = shift;
return undef
unless $self
and $list_of_lists{$self->{'domain'}}{$self->{'name'}}
and Conf::get_robot_conf($self->{'domain'}, 'sendmail_aliases') !~
/^none$/i;
my $alias_manager = $Conf::Conf{'alias_manager'};
unless (-x $alias_manager) {
$log->syslog('err', 'Cannot run alias_manager %s', $alias_manager);
return undef;
}
my $status =
system($alias_manager, 'del', $self->{'name'},
$self->{'admin'}{'host'}) >> 8;
if ($status) {
$log->syslog('err', 'Failed to remove aliases; status %d: %m',
$status);
return undef;
}
$log->syslog('info', 'Aliases for list %s removed successfully',
$self->{'name'});
return 1;
}
# Deprecated. Use Sympa::Aliases::del().
#sub remove_aliases;
##
## bounce management actions
......
......@@ -28,7 +28,7 @@ use warnings;
use File::Path qw();
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Sympa::DatabaseManager;
use Sympa::Log;
use Sympa::Task;
......@@ -149,7 +149,7 @@ sub _close {
my $list = $request->{current_list};
my $sender = $request->{sender};
Sympa::Admin::remove_aliases($list);
Sympa::Aliases->new->del($list);
# Dump subscribers.
$list->_save_list_members_file(
......
......@@ -28,7 +28,7 @@ use warnings;
use English qw(-no_match_vars);
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Conf;
use Sympa::Constants;
use Sympa::List;
......@@ -93,7 +93,7 @@ sub _twist {
}
## Check listname on SMTP server
my $res = Sympa::Admin::list_check_smtp($listname, $robot_id);
my $res = Sympa::Aliases->new->check($listname, $robot_id);
unless (defined $res) {
$log->syslog('err', 'Can\'t check list %.128s on %s',
$listname, $robot_id);
......@@ -255,7 +255,7 @@ sub _twist {
$list->{'admin'}{'family_name'} = $family->{'name'};
if ($list->{'admin'}{'status'} eq 'open') {
Sympa::Admin::install_aliases($list);
Sympa::Aliases->new->add($list);
$self->add_stash($request, 'notice', 'auto_aliases');
} else {
;
......
......@@ -29,7 +29,7 @@ use Encode qw();
use English qw(-no_match_vars);
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Conf;
use Sympa::Constants;
use Sympa::List;
......@@ -118,7 +118,7 @@ sub _twist {
}
## Check listname on SMTP server
my $res = Sympa::Admin::list_check_smtp($listname, $robot_id);
my $res = Sympa::Aliases->new->check($listname, $robot_id);
unless (defined $res) {
$log->syslog('err', 'Can\'t check list %.128s on %s',
$listname, $robot_id);
......@@ -240,7 +240,7 @@ sub _twist {
if ($list->{'admin'}{'status'} eq 'open') {
# Install new aliases.
Sympa::Admin::install_aliases($list);
Sympa::Aliases->new->add($list);
$self->add_stash($request, 'notice', 'auto_aliases');
} elsif ($list->{'admin'}{'status'} eq 'pending') {
......
......@@ -28,7 +28,7 @@ use warnings;
use File::Copy qw();
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Sympa::Bulk;
use Conf;
use Sympa::Constants;
......@@ -96,7 +96,7 @@ sub _twist {
# Do not test if listname did not change.
my $res;
unless ($current_list->get_id eq $listname . '@' . $robot_id) {
$res = Sympa::Admin::list_check_smtp($listname, $robot_id);
$res = Sympa::Aliases->new->check($listname, $robot_id);
unless (defined $res) {
$log->syslog('err', 'Can\'t check list %.128s on %.128s',
$listname, $robot_id);
......@@ -171,7 +171,7 @@ sub _twist {
if ($list->{'admin'}{'status'} eq 'open') {
# Install new aliases.
Sympa::Admin::install_aliases($list);
Sympa::Aliases->new->add($list);
$self->add_stash($request, 'notice', 'auto_aliases');
} elsif ($list->{'admin'}{'status'} eq 'pending') {
......@@ -214,7 +214,7 @@ sub _move {
my $pending = $request->{pending};
# Remove aliases and dump subscribers.
Sympa::Admin::remove_aliases($current_list);
Sympa::Aliases->new->del($current_list);
$current_list->_save_list_members_file(
$current_list->{'dir'} . '/subscribers.closed.dump');
......
......@@ -28,7 +28,7 @@ use warnings;
use File::Path qw();
use Sympa;
use Sympa::Admin;
use Sympa::Aliases;
use Sympa::DatabaseManager;
use Sympa::Log;
use Sympa::Task;
......@@ -88,8 +88,7 @@ sub _twist {
$list->add_list_member(@users);
# Install new aliases.
my $aliases = Sympa::Admin::install_aliases($list);
if ($aliases and $aliases == 1) {
if (Sympa::Aliases->new->add($list)) {
$self->add_stash($request, 'notice', 'auto_aliases');
} else {
;
......
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