Commit 4ce9d6c2 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

Refactoring. Removing duplicate code.

A note:
Now create_list, create_automatic_list & update_automatic_list Request handlers won't take "listname" as an top-level parameter, but it must be included in an item of "parameters" parameter.
parent aa500ba4
......@@ -9735,9 +9735,9 @@ sub do_create_list {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $robot,
action => 'create_list',
listname => $in{'listname'},
parameters => {
owner => [
listname => $in{'listname'},
owner => [
{ email => $param->{'user'}{'email'},
gecos => $param->{'user'}{'gecos'},
}
......@@ -16977,8 +16977,7 @@ sub do_create_automatic_list {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $family,
action => 'create_automatic_list',
listname => $listname,
parameters => {},
parameters => {listname => $listname},
abort_on_error => 1,
sender => $param->{'user'}{'email'},
md5_check => 1,
......
......@@ -8,6 +8,9 @@
# 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 2019 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
......@@ -43,9 +46,15 @@ my $log = Sympa::Log->instance;
# -$fh : file handler on the xml file
#########################################
sub new {
$log->syslog('debug2', '(%s, %s)', @_);
my $class = shift;
my $fh = shift;
$log->syslog('debug2', '');
my $path = shift;
my $fh;
unless (open $fh, '<', $path) {
$log->syslog('err', 'Can\'t open %s: $m', $path);
return bless {} => $class;
}
my $self = {};
my $parser = XML::LibXML->new();
......@@ -54,28 +63,36 @@ sub new {
$self->{'root'} = $doc->documentElement();
bless $self, $class;
return $self;
return bless $self => $class;
}
################################################
# createHash
################################################
# Create a hash used to create a list. Check
# elements unicity when their are not
# declared multiple
#
# IN : -$self
# OUT : -1 or undef
################################################
sub createHash {
# Returns the hash structure.
sub as_hashref {
$log->syslog('debug2', '(%s)', @_);
my $self = shift;
return undef unless $self->{root};
return undef unless $self->_createHash;
my $phash = {%{$self->{config} || {}}};
# Compatibility: single topic on 6.2.24 or earlier.
$phash->{topics} ||= $phash->{topic};
# In old documentation "moderator" was single or multiple editors.
my $mod = $phash->{moderator};
$phash->{editor} ||=
(ref $mod eq 'ARRAY') ? $mod : (ref $mod eq 'HASH') ? [$mod] : [];
return $phash;
}
# Create a hash used to create a list. Check elements unicity when their are
# not declared multiple.
# Old name: Sympa::Config_XML::createHash().
sub _createHash {
my $self = shift;
$log->syslog('debug2', '');
unless ($self->{'root'}->nodeName eq 'list') {
$log->syslog('err',
"Config_XML::createHash() : the root element must be called \"list\" "
);
$log->syslog('err', 'The root element must be called "list"');
return undef;
}
......@@ -86,53 +103,21 @@ sub createHash {
if ($self->{'root'}->hasChildNodes()) {
my $hash = _getChildren($self->{'root'});
unless (defined $hash) {
unless ($hash) {
$log->syslog('err', 'Error in list elements');
return undef;
}
if (ref($hash) eq "HASH") {
foreach my $k (keys %$hash) {
if ($k eq "type") {
## the list template creation without family context
$self->{'type'} = $hash->{'type'};
} else {
$self->{'config'}{$k} = $hash->{$k};
}
}
} elsif ($hash ne "") { # a string
$log->syslog('err',
'Config_XML::createHash() : the list\'s children are not homogeneous'
);
} elsif (ref $hash eq 'HASH') {
$self->{config} = {%$hash};
} else { # a string
$log->syslog('err', 'The list\'s children are not homogeneous');
return undef;
}
}
return 1;
}
#########################################
# getHash
#########################################
# return the hash structure containing :
# type, config
#
# IN : -$self
# OUT : -$hash
#########################################
sub getHash {
my $self = shift;
$log->syslog('debug2', '');
my $hash = {};
## the list template creation without family context
$hash->{'type'} = $self->{'type'}
if (defined $self->{'type'});
$hash->{'config'} = $self->{'config'};
return $hash;
}
############################# PRIVATE METHODS ##############################
# Deprecated: No longer used.
#sub getHash;
#################################################################
# _getRequiredElements
......@@ -246,7 +231,7 @@ sub _getRequiredSingle {
return undef;
}
if ($nodeName eq "type") {
if ($nodeName eq 'type') {
## the list template creation without family context
my $value = $node->textContent;
......
This diff is collapsed.
......@@ -4,8 +4,8 @@
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2017, 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2017, 2018, 2019 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
......@@ -26,10 +26,12 @@ package Sympa::Request::Handler::create_automatic_list;
use strict;
use warnings;
use English qw(-no_match_vars);
use File::Copy qw();
use Sympa;
use Sympa::Aliases;
use Conf;
use Sympa::Config_XML;
use Sympa::List;
use Sympa::LockedFile;
use Sympa::Log;
......@@ -49,14 +51,30 @@ sub _twist {
my $request = shift;
my $family = $request->{context};
my $listname = lc($request->{listname} || '');
my $param = $request->{parameters};
my $abort_on_error = $request->{abort_on_error};
my $robot_id = $family->{'robot'};
my $path;
die 'bug in logic. Ask developer' unless ref $family eq 'Sympa::Family';
if ($param->{file}) {
$path = $param->{file};
# Get list data
$param = Sympa::Config_XML->new($path)->as_hashref;
unless ($param) {
$log->syslog('err',
"Error in representation data with these xml data");
$self->add_stash($request, 'user', 'XXX');
return undef;
}
}
$family->{'state'} = 'no_check';
my $listname = lc $param->{listname};
# Check new listname.
my @stash = Sympa::Aliases::check_new_listname($listname, $robot_id);
if (@stash) {
......@@ -75,7 +93,6 @@ sub _twist {
my $family_config =
Conf::get_robot_conf($robot_id, 'automatic_list_families');
$param->{'family_config'} = $family_config->{$family->{'name'}};
$param->{'listname'} = $listname;
my $config = '';
my $template =
......@@ -224,9 +241,10 @@ sub _twist {
$list->restore_users('owner');
$list->restore_users('editor');
if ($listname ne $request->{listname}) {
$self->add_stash($request, 'notice', 'listname_lowercased');
}
#FIXME
#if ($listname ne $request->{listname}) {
# $self->add_stash($request, 'notice', 'listname_lowercased');
#}
## Create shared if required.
#if (defined $list->{'admin'}{'shared_doc'}) {
......@@ -290,6 +308,17 @@ sub _twist {
{errors => $error});
}
# Copy files in the list directory : xml file
if ($path and $path ne $list->{'dir'} . '/instance.xml') {
unless (File::Copy::copy($path, $list->{'dir'} . '/instance.xml')) {
$list->set_status_error_config('error_copy_file',
$family->{'name'});
$self->add_stash($request, 'intern');
$log->syslog('err',
'Impossible to copy the XML file in the list directory');
}
}
## Synchronize list members if required
if ($list->has_include_data_sources()) {
$log->syslog('notice', "Synchronizing list members...");
......
......@@ -4,8 +4,8 @@
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2017, 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2017, 2018, 2019 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
......@@ -31,6 +31,7 @@ use English qw(-no_match_vars);
use Sympa;
use Sympa::Aliases;
use Conf;
use Sympa::Config_XML;
use Sympa::List;
use Sympa::LockedFile;
use Sympa::Log;
......@@ -49,12 +50,28 @@ sub _twist {
my $request = shift;
my $robot_id = $request->{context};
my $listname = lc($request->{listname} || '');
my $param = $request->{parameters};
my $pending = $request->{pending};
my $notify = $request->{notify};
my $sender = $request->{sender};
my $path;
if ($param->{file}) {
$path = $param->{file};
# Get list data
$param = Sympa::Config_XML->new($path)->as_hashref;
unless ($param) {
$log->syslog('err',
"Error in representation data with these xml data");
$self->add_stash($request, 'user', 'XXX');
return undef;
}
}
my $listname = lc $param->{listname};
# Obligatory parameters.
foreach my $arg (qw(subject template topics)) {
unless (defined $param->{$arg} and $param->{$arg} =~ /\S/) {
......@@ -212,9 +229,10 @@ sub _twist {
$list->restore_users('owner');
$list->restore_users('editor');
if ($listname ne $request->{listname}) {
$self->add_stash($request, 'notice', 'listname_lowercased');
}
#FIXME
#if ($listname ne $request->{listname}) {
# $self->add_stash($request, 'notice', 'listname_lowercased');
#}
if ($list->{'admin'}{'status'} eq 'open') {
# Install new aliases.
......
......@@ -4,8 +4,8 @@
# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2018, 2019 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
......@@ -26,9 +26,11 @@ package Sympa::Request::Handler::update_automatic_list;
use strict;
use warnings;
use English qw(-no_match_vars);
use File::Copy qw();
use Sympa;
use Conf;
use Sympa::Config_XML;
use Sympa::List;
use Sympa::LockedFile;
use Sympa::Log;
......@@ -51,6 +53,56 @@ sub _twist {
my $param = $request->{parameters};
my $robot_id = $family->{'robot'};
my $path;
if ($param->{file}) {
$path = $param->{file};
# get list data
$param = Sympa::Config_XML->new($path)->as_hashref;
unless ($param) {
$log->syslog('err',
"Error in representation data with these xml data");
$self->add_stash($request, 'user', 'XXX');
return undef;
}
}
# getting list
if ($list and $param->{listname}) {
unless ($list->get_id eq
sprintf('%s@%s', lc $param->{listname}, $family->{'robot'})) {
$log->syslog('err', 'The list %s and list name %s mismatch',
$list, $param->{listname});
$self->add_stash($request, 'user', 'XXX');
return undef;
}
} elsif ($list
or ($list = Sympa::List->new($param->{listname}, $family->{'robot'}, {just_try => 1})))
{
$param->{listname} = $list->{'name'};
} else {
$log->syslog('err', 'The list "%s" does not exist',
$param->{listname});
$self->add_stash($request, 'user', 'XXX');
return undef;
}
## check family name
if (defined $list->{'admin'}{'family_name'}) {
unless ($list->{'admin'}{'family_name'} eq $family->{'name'}) {
$log->syslog('err',
"The list $list->{'name'} already belongs to family $list->{'admin'}{'family_name'}."
);
$self->add_stash($request, 'user', 'listname_already_used');
return undef;
}
} else {
$log->syslog('err',
"The orphan list $list->{'name'} already exists.");
$self->add_stash($request, 'user', 'listname_already_used');
return undef;
}
# Get allowed and forbidden list customizing.
my $custom = _get_customizing($family, $list);
unless (defined $custom) {
......@@ -270,6 +322,45 @@ sub _twist {
[$family->{'name'}, $forbidden_param]);
}
# info parameters
$list->{'admin'}{'latest_instantiation'}{'email'} =
Sympa::get_address($family, 'listmaster');
$list->{'admin'}{'latest_instantiation'}{'date_epoch'} = time;
$list->save_config(Sympa::get_address($family, 'listmaster'));
$list->{'family'} = $family;
## check param_constraint.conf
$family->{'state'} = 'normal';
my $error = $family->check_param_constraint($list);
$family->{'state'} = 'no_check';
unless (defined $error) {
$list->set_status_error_config('no_check_rules_family',
$family->{'name'});
$self->add_stash($request, 'intern');
$log->syslog('err', 'Impossible to check parameters constraint');
return undef;
}
if (ref $error eq 'ARRAY') {
$list->set_status_error_config('no_respect_rules_family',
$family->{'name'});
$self->add_stash($request, 'user', 'not_respect_rules_family',
{errors => $error});
$log->syslog('err', 'The list does not respect the family rules : %s',
join ', ', @{$error});
}
# Copy files in the list directory : xml file
if ($path and $path ne $list->{'dir'} . '/instance.xml') {
unless (File::Copy::copy($path, $list->{'dir'} . '/instance.xml')) {
$list->set_status_error_config('error_copy_file',
$family->{'name'});
$self->add_stash($request, 'intern');
$log->syslog('err',
'Impossible to copy the XML file in the list directory');
}
}
return 1;
}
......
......@@ -247,8 +247,7 @@ sub _twist {
my $spindle_req = Sympa::Spindle::ProcessRequest->new(
context => $dyn_family,
action => 'create_automatic_list',
listname => $listname,
parameters => {},
parameters => {listname => $listname},
sender => $sender,
smime_signed => $message->{'smime_signed'},
md5_check => $message->{'md5_check'},
......
......@@ -32,6 +32,7 @@ use warnings;
use Cwd qw();
use Encode qw();
use English qw(-no_match_vars);
use File::Copy qw();
use MIME::Base64 qw();
use Time::Local qw();
......
......@@ -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, 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2017, 2018, 2019 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
......@@ -617,9 +617,9 @@ sub createList {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $robot,
action => 'create_list',
listname => $listname,
parameters => {
owner => [
listname => $listname,
owner => [
{ email => $sender,
gecos => ($user ? $user->{gecos} : undef),
}
......
......@@ -9,8 +9,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 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2017, 2018, 2019 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
......@@ -41,7 +41,6 @@ use POSIX qw();
use Sympa;
use Conf;
use Sympa::Config_XML;
use Sympa::Constants;
use Sympa::DatabaseManager;
use Sympa::Family;
......@@ -719,36 +718,16 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
print STDERR "Error : missing 'input_file' parameter\n";
exit 1;
}
my $fh;
unless (open $fh, '<', $main::options{'input_file'}) {
printf STDERR "Unable to open %s: %s\n", $main::options{'input_file'},
$ERRNO;
exit 1;
}
my $config = Sympa::Config_XML->new($fh);
close $fh;
unless (defined $config->createHash()) {
print STDERR "Error in representation data with these XML data\n";
exit 1;
}
my $hash = $config->getHash();
# Compatibility: single topic on 6.2.24 or earlier.
$hash->{config}{topics} ||= $hash->{config}{topic};
# In old documentation "moderator" was single or multiple editors.
my $mod = $hash->{config}{moderator};
$hash->{config}{editor} ||=
(ref $mod eq 'ARRAY') ? $mod : (ref $mod eq 'HASH') ? [$mod] : [];
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $robot,
action => 'create_list',
listname => $hash->{config}{listname},
parameters => {%{$hash->{config} || {}}, template => $hash->{type}},
sender => Sympa::get_address($robot, 'listmaster'),
context => $robot,
action => 'create_list',
parameters => {file => $main::options{'input_file'}},
sender => Sympa::get_address($robot, 'listmaster'),
scenario_context => {skip => 1}
);
unless ($spindle and $spindle->spin and _report($spindle)) {
printf STDERR "Could not create list %s\n", $hash->{config}{listname};
printf STDERR "Could not create list %s\n";
exit 1;
}
exit 0;
......@@ -793,7 +772,6 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
my %result;
my $err = $family->get_instantiation_results(\%result);
close INFILE;
unless ($main::options{'quiet'}) {
print STDOUT "@{$result{'info'}}";
......@@ -825,53 +803,20 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
print STDERR "Error : missing 'input_file' parameter\n";
exit 1;
}
my $fh;
unless (open $fh, '<', $main::options{'input_file'}) {
printf STDERR "\n Impossible to open input file : %s\n", $ERRNO;
exit 1;
}
# get list data
my $config = Sympa::Config_XML->new($fh);
close $fh;
unless (defined $config->createHash()) {
print STDERR "Error in representation data with these XML data\n";
exit 1;
}
my $hash = $config->getHash();
# Compatibility: single topic on 6.2.24 or earlier.
$hash->{config}{topics} ||= $hash->{config}{topic};
# In old documentation "moderator" was single or multiple editors.
my $mod = $hash->{config}{moderator};
$hash->{config}{editor} ||=
(ref $mod eq 'ARRAY') ? $mod : (ref $mod eq 'HASH') ? [$mod] : [];
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $family,
action => 'create_automatic_list',
listname => $hash->{config}{listname},
parameters => $hash->{config},
parameters => {file => $main::options{'input_file'}},
sender => Sympa::get_address($family, 'listmaster'),
scenario_context => {skip => 1},
);
unless ($spindle and $spindle->spin and _report($spindle)) {
printf STDERR "Impossible to add a list %s to the family %s\n",
$hash->{config}{listname}, $family_name;
printf STDERR "Impossible to add a list to the family %s\n",
$family_name;
exit 1;
}
my $list =