Commit 7b14f8a8 authored by sikeda's avatar sikeda
Browse files

[dev] Use Sympa::Template instead of tt2.


git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@12037 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent 8725c6f8
......@@ -41,7 +41,6 @@ use warnings;
use Encode qw();
use English qw(-no_match_vars);
use File::Copy qw();
use IO::Scalar;
use Conf;
use Sympa::Constants;
......@@ -53,12 +52,13 @@ use Sympa::Regexps;
use Sympa::Robot;
use Sympa::Scenario;
use SDM;
use Sympa::Template;
use tools;
use Sympa::Tools::File;
use tt2;
use Sympa::User;
my $language = Sympa::Language->instance;
my $log = Sympa::Log->instance;
=head1 SUBFUNCTIONS
......@@ -90,7 +90,7 @@ Creates a list. Used by the create_list() sub in sympa.pl and the do_create_list
=back
=item * I<$template>, a string containing the list creation template
=item * I<$list_tpl>, a string containing the list creation template
=item * I<$robot>, a string containing the name of the robot the list will be hosted by.
......@@ -131,7 +131,7 @@ Creates a list. Used by the create_list() sub in sympa.pl and the do_create_list
# array of hash,with key email obligatory
# $param->{'owner_include'} array of hash :
# with key source obligatory
# - $template : the create list template
# - $list_tpl : the create list template
# - $robot : the list's robot
# - $origin : the source of the command : web, soap or command_line
# no longer used
......@@ -142,7 +142,7 @@ Creates a list. Used by the create_list() sub in sympa.pl and the do_create_list
# are not installed or 1(in status open)
#######################################################
sub create_list_old {
my ($param, $template, $robot, $origin, $user_mail) = @_;
my ($param, $list_tpl, $robot, $origin, $user_mail) = @_;
Log::do_log('debug', '(%s, %s)', $param->{'listname'}, $robot, $origin);
## obligatory list parameters
......@@ -161,8 +161,8 @@ sub create_list_old {
}
# template
unless ($template) {
Log::do_log('err', 'Missing param "template"', $template);
unless ($list_tpl) {
Log::do_log('err', 'Missing param "template"', $list_tpl);
return undef;
}
# robot
......@@ -223,9 +223,9 @@ sub create_list_old {
## Check the template supposed to be used exist.
my $template_file = tools::search_fullpath($robot, 'config.tt2',
subdir => 'create_list_templates/' . $template);
subdir => 'create_list_templates/' . $list_tpl);
unless (defined $template_file) {
Log::do_log('err', 'No template %s found', $template);
Log::do_log('err', 'No template %s found', $list_tpl);
return undef;
}
......@@ -235,9 +235,9 @@ sub create_list_old {
# a virtual robot
if (-d "$Conf::Conf{'home'}/$robot") {
unless (-d $Conf::Conf{'home'} . '/' . $robot) {
unless (mkdir($Conf::Conf{'home'} . '/' . $robot, 0777)) {
unless (mkdir $Conf::Conf{'home'} . '/' . $robot, 0777) {
Log::do_log('err', 'Unable to create %s/%s: %s',
$Conf::Conf{'home'}, $robot, $?);
$Conf::Conf{'home'}, $robot, $ERRNO);
return undef;
}
}
......@@ -248,9 +248,8 @@ sub create_list_old {
}
## Check the privileges on the list directory
unless (mkdir($list_dir, 0777)) {
Log::do_log('err', 'Unable to create %s: %s', $list_dir,
$CHILD_ERROR);
unless (mkdir $list_dir, 0777) {
Log::do_log('err', 'Unable to create %s: %s', $list_dir, $ERRNO);
return undef;
}
......@@ -272,21 +271,26 @@ sub create_list_old {
unless ($param->{'creation_email'});
$param->{'status'} = 'open' unless ($param->{'status'});
my $tt2_include_path = tools::get_search_path($robot,
subdir => 'create_list_templates/' . $template);
## Lock config before openning the config file
my $lock_fh = Sympa::LockedFile->new($list_dir . '/config', 5, '>');
unless ($lock_fh) {
Log::do_log('err', 'Impossible to create %s/config: %m', $list_dir);
return undef;
}
## Use an intermediate handler to encode to filesystem_encoding
my $config = '';
my $fd = IO::Scalar->new(\$config);
#FIXME: Check parse error
tt2::parse_tt2($param, 'config.tt2', $fd, $tt2_include_path);
# Encode::from_to($config, 'utf8', $Conf::Conf{'filesystem_encoding'});
my $template =
Sympa::Template->new($robot,
subdir => 'create_list_templates/' . $list_tpl);
unless ($template->parse($param, 'config.tt2', \$config)) {
my $error = $template->{last_error};
Log::do_log(
'err', 'Can\'t parse %s/config.tt2: %s',
$list_tpl, ($error and $error->info or 'Unknown error')
);
return undef;
}
print $lock_fh $config;
## Unlock config file
......@@ -323,13 +327,12 @@ sub create_list_old {
#log in stat_table to make statistics
if ($origin eq "web") {
Log::db_stat_log(
{ 'robot' => $robot,
'list' => $param->{'listname'},
'operation' => 'create_list',
'parameter' => '',
'mail' => $user_mail,
}
$log->add_stat(
'robot' => $robot,
'list' => $param->{'listname'},
'operation' => 'create_list',
'parameter' => '',
'mail' => $user_mail
);
}
......@@ -460,16 +463,18 @@ sub create_list {
Conf::get_robot_conf($robot, 'automatic_list_families');
$param->{'family_config'} = $family_config->{$family->{'name'}};
my $conf;
my $tt_result =
tt2::parse_tt2($param, 'config.tt2', \$conf, [$family->{'dir'}]);
unless (defined $tt_result || !$abort_on_error) {
my $template =
Sympa::Template->new(undef, include_path => [$family->{'dir'}]);
my $tt_result = $template->parse($param, 'config.tt2', \$conf);
if (not $tt_result and $abort_on_error) {
my $error = $template->{last_error};
Log::do_log(
'err',
'Abort on tt2 error. List %s from family %s@%s, file config.tt2 : %s',
$param->{'listname'},
$family->{'name'},
$robot,
tt2::get_error()->info()
($error and $error->info)
);
return undef;
}
......@@ -479,9 +484,9 @@ sub create_list {
if (-d "$Conf::Conf{'home'}/$robot") {
unless (-d $Conf::Conf{'home'} . '/' . $robot) {
unless (mkdir($Conf::Conf{'home'} . '/' . $robot, 0777)) {
unless (mkdir $Conf::Conf{'home'} . '/' . $robot, 0777) {
Log::do_log('err', 'Unable to create %s/%s: %s',
$Conf::Conf{'home'}, $robot, $?);
$Conf::Conf{'home'}, $robot, $ERRNO);
return undef;
}
}
......@@ -491,9 +496,8 @@ sub create_list {
$list_dir = $Conf::Conf{'home'} . '/' . $param->{'listname'};
}
unless (-r $list_dir || mkdir($list_dir, 0777)) {
Log::do_log('err', 'Unable to create %s: %s', $list_dir,
$CHILD_ERROR);
unless (-r $list_dir or mkdir($list_dir, 0777)) {
Log::do_log('err', 'Unable to create %s: %s', $list_dir, $ERRNO);
return undef;
}
......@@ -511,7 +515,6 @@ sub create_list {
Log::do_log('err', 'Impossible to create %s/config: %m', $list_dir);
return undef;
}
#tt2::parse_tt2($param, 'config.tt2', $lock_fh, [$family->{'dir'}]);
print $lock_fh $conf;
## Unlock config file
......@@ -543,9 +546,13 @@ sub create_list {
my $template_file = tools::search_fullpath($family, $file . ".tt2");
if (defined $template_file) {
my $file_content;
my $tt_result = tt2::parse_tt2($param, $file . ".tt2",
\$file_content, [$family->{'dir'}]);
my $template =
Sympa::Template->new(undef,
include_path => [$family->{'dir'}]);
my $tt_result =
$template->parse($param, $file . ".tt2", \$file_content);
unless (defined $tt_result) {
my $error = $template->{last_error};
Log::do_log(
'err',
'Tt2 error. List %s from family %s@%s, file %s : %s',
......@@ -553,7 +560,7 @@ sub create_list {
$family->{'name'},
$robot,
$file,
tt2::get_error()->info()
($error and $error->info)
);
}
unless (open FILE, '>', "$list_dir/$file") {
......@@ -665,11 +672,18 @@ sub update_list {
my $lock_fh = Sympa::LockedFile->new($list->{'dir'} . '/config', 5, '>');
unless ($lock_fh) {
Log::do_log('err', 'Impossible to create %s/config: %s',
$list->{'dir'}, $!);
$list->{'dir'}, $ERRNO);
return undef;
}
my $template =
Sympa::Template->new(undef, include_path => [$family->{'dir'}]);
unless ($template->parse($param, 'config.tt2', $lock_fh)) {
my $error = $template->{last_error};
Log::do_log('err', 'Can\'t parse %s/config.tt2: %s',
$family->{'dir'}, ($error and $error->info));
return undef;
}
#FIXME: Check parse error
tt2::parse_tt2($param, 'config.tt2', $lock_fh, [$family->{'dir'}]);
## Unlock config file
$lock_fh->close;
......@@ -708,12 +722,24 @@ sub update_list {
my $template_file = tools::search_fullpath($family, $file . ".tt2");
if (defined $template_file) {
my $file_content;
my $tt_result = tt2::parse_tt2($param, $file . ".tt2",
\$file_content, [$family->{'dir'}]);
unless (defined $tt_result) {
Log::do_log('err',
'Tt2 error. List %s from family %s@%s, file %s',
$param->{'listname'}, $family->{'name'}, $robot, $file);
my $template =
Sympa::Template->new(undef,
include_path => [$family->{'dir'}]);
my $tt_result =
$template->parse($param, $file . ".tt2", \$file_content);
unless ($tt_result) {
my $error = $template->{last_error};
Log::do_log(
'err',
'Template error. List %s from family %s@%s, file %s: %s',
$param->{'listname'},
$family->{'name'},
$robot,
$file,
($error and $error->info)
);
next; #FIXME: Abort processing and rollback.
}
unless (open FILE, '>', "$list->{'dir'}/$file") {
Log::do_log('err', 'Impossible to create %s/%s: %s',
......@@ -1299,7 +1325,8 @@ sub check_owner_defined {
}
} elsif (ref($owner) eq "HASH") {
unless ($owner->{'email'}) {
Log::do_log('err', 'Missing sub param "email" for param "owner"');
Log::do_log('err',
'Missing sub param "email" for param "owner"');
return undef;
}
} elsif (defined $owner) {
......@@ -1435,7 +1462,8 @@ sub install_aliases {
Log::do_log('err', 'Configuration file %s has errors',
Conf::get_sympa_conf());
} elsif ($status == 2) {
Log::do_log('err', 'Internal error: Incorrect call to alias_manager');
Log::do_log('err',
'Internal error: Incorrect call to alias_manager');
} elsif ($status == 3) {
# Won't occur
Log::do_log('err',
......
......@@ -62,6 +62,7 @@ use Sympa::Robot;
use Sympa::Scenario;
use SDM;
use Sympa::Task;
use Sympa::Template;
use tools;
use Sympa::Tools::Data;
use Sympa::Tools::File;
......@@ -69,7 +70,6 @@ use Sympa::Tools::Password;
use Sympa::Tools::SMIME;
use Sympa::Tools::Text;
use Sympa::Tracking;
use tt2;
use Sympa::User;
my @sources_providing_listmembers = qw/
......@@ -93,8 +93,8 @@ my @more_data_sources = qw/
my %config_in_admin_user_file = map +($_ => 1),
@sources_providing_listmembers;
# Language context
my $language = Sympa::Language->instance;
my $log = Sympa::Log->instance;
=encoding utf-8
......@@ -169,7 +169,6 @@ Returns a default option of the list for subscription.
Returns the number of subscribers to the list.
=item get_global_user ( USER )
Returns a hash with the information regarding the indicated
......@@ -1612,8 +1611,29 @@ sub distribute_msg {
## Virer eventuelle signature S/MIME
}
## Add Custom Subject
# Add Custom Subject
my $parsed_tag;
if ($self->{'admin'}{'custom_subject'}) {
my $custom_subject = $self->{'admin'}{'custom_subject'};
# Check if custom_subject parameter is parsable.
my $data = {
list => {
name => $self->{'name'},
sequence => $self->{'stats'}->[0],
},
};
my $template = Sympa::Template->new(undef);
unless ($template->parse($data, [$custom_subject], \$parsed_tag)) {
my $error = $template->{last_error};
Log::do_log('err', 'Can\'t parse custom_subject of list %s: %s',
$self, ($error and $error->info));
undef $parsed_tag;
}
}
if ($self->{'admin'}{'custom_subject'} and defined $parsed_tag) {
my $subject_field = $message->{'decoded_subject'};
$subject_field = '' unless defined $subject_field;
## Remove leading and trailing blanks
......@@ -1642,19 +1662,7 @@ sub distribute_msg {
## Takes spaces into account
$tag_regexp =~ s/\s+/\\s+/g;
## Add subject tag
#FIXME: Check parse error
$message->delete_header('Subject');
my $parsed_tag;
tt2::parse_tt2(
{ 'list' => {
'name' => $self->{'name'},
'sequence' => $self->{'stats'}->[0]
}
},
[$custom_subject],
\$parsed_tag
);
# Add subject tag
## If subject is tagged, replace it with new tag
## Splitting the subject in two parts :
......@@ -1671,7 +1679,7 @@ sub distribute_msg {
# truncate multiple "Re:" and equivalents.
my $re_regexp = Sympa::Regexps::re();
if ($subject_field =~ /^\s*($re_regexp\s*)($re_regexp\s*)*/) {
($before_tag, $after_tag) = ($1, $POSTMATCH); #'
($before_tag, $after_tag) = ($1, $POSTMATCH);
} else {
($before_tag, $after_tag) = ('', $subject_field);
}
......@@ -1702,6 +1710,7 @@ sub distribute_msg {
. $after_tag;
}
$message->delete_header('Subject');
$message->add_header('Subject', $subject_field);
}
......@@ -1918,15 +1927,12 @@ sub distribute_msg {
#ignore messages sent by robot
unless ($message->{sender} =~ /($self->{name})-request/) {
#ignore messages of requests
Log::db_stat_log(
{ 'robot' => $self->{'domain'},
'list' => $self->{'name'},
'operation' => 'send_mail',
'parameter' => $message->{size},
'mail' => $message->{sender},
'client' => '',
'daemon' => 'sympa_msg.pl'
}
$log->add_stat(
'robot' => $self->{'domain'},
'list' => $self->{'name'},
'operation' => 'send_mail',
'parameter' => $message->{size},
'mail' => $message->{sender},
);
}
}
......@@ -2752,8 +2758,8 @@ sub send_confirm_to_sender {
original => 1
);
unless ($marshalled) {
Log::do_log('err', 'Cannot create authkey %s for %s', $authkey,
$list);
Log::do_log('err', 'Cannot create authkey %s for %s',
$authkey, $list);
return undef;
}
$authkey = ${
......@@ -3246,7 +3252,8 @@ sub send_notify_to_editor {
sub send_notify_to_user {
my ($self, $operation, $user, $param) = @_;
Log::do_log('debug2', '(%s, %s, %s)', $self->{'name'}, $operation, $user);
Log::do_log('debug2', '(%s, %s, %s)', $self->{'name'}, $operation,
$user);
my $host = $self->{'admin'}->{'host'};
my $robot = $self->{'domain'};
......@@ -3397,12 +3404,11 @@ sub delete_list_member {
#log in stat_table to make statistics
if ($operation) {
Log::db_stat_log(
{ 'robot' => $self->{'domain'},
'list' => $name,
'operation' => $operation,
'mail' => $who,
}
$log->add_stat(
'robot' => $self->{'domain'},
'list' => $name,
'operation' => $operation,
'mail' => $who
);
}
......@@ -4637,7 +4643,7 @@ sub get_next_bouncing_list_member {
}
sub parse_list_member_bounce {
my ($self,$user) = @_;
my ($self, $user) = @_;
if ($user->{bounce}) {
$user->{'bounce'} =~ /^(\d+)\s+(\d+)\s+(\d+)(\s+(.*))?$/;
$user->{'first_bounce'} = $1;
......@@ -4666,7 +4672,8 @@ sub get_info {
my $info;
unless (open INFO, "$self->{'dir'}/info") {
Log::do_log('err', 'Could not open %s: %m', $self->{'dir'} . '/info');
Log::do_log('err', 'Could not open %s: %m',
$self->{'dir'} . '/info');
return undef;
}
......@@ -5221,13 +5228,12 @@ sub add_list_member {
$new_user->{'included'} ||= 0;
#Log in stat_table to make staistics
Log::db_stat_log(
{ 'robot' => $self->{'domain'},
'list' => $self->{'name'},
'operation' => 'add_or_subscribe',
'parameter' => '',
'mail' => $new_user->{'email'},
}
$log->add_stat(
'robot' => $self->{'domain'},
'list' => $self->{'name'},
'operation' => 'add_or_subscribe',
'parameter' => '',
'mail' => $new_user->{'email'}
);
## Update Subscriber Table
......@@ -6224,7 +6230,8 @@ sub _include_users_remote_sympa_list {
my %u;
## Check if user has already been included
if ($users->{$email}) {
Log::do_log('debug3', 'Ignore %s because already member', $email);
Log::do_log('debug3', 'Ignore %s because already member',
$email);
if ($tied) {
%u = split "\n", $users->{$email};
} else {
......@@ -6347,12 +6354,13 @@ sub _include_users_list {
# Run the test
my $result;
unless (tt2::parse_tt2($variables, \($filter), \$result)) {
my $template = Sympa::Template->new(undef);
unless ($template->parse($variables, \($filter), \$result)) {
Log::do_log(
'err',
'Error while applying filter "%s" : %s, aborting include',
$filter,
tt2::get_error()
$template->{last_error}
);
return undef;
}
......@@ -6644,8 +6652,8 @@ sub _include_users_remote_file {
#FIXME: Reset http credentials
Log::do_log('info', '%d included users from remote file %s', $total,
$url);
Log::do_log('info', '%d included users from remote file %s',
$total, $url);
return $total;
}
......@@ -6729,7 +6737,8 @@ sub _include_users_voot_group {
}
}
Log::do_log('info', '%d included users from VOOT group %s at provider %s',
Log::do_log('info',
'%d included users from VOOT group %s at provider %s',
$total, $param->{'group'}, $param->{'provider'});
return $total;
......@@ -7772,12 +7781,10 @@ sub _load_include_admin_user_file {
my $vars = {'param' => \@data};
my $output = '';
unless (
tt2::parse_tt2(
$vars, $parsing->{'template'},
\$output, [$parsing->{'include_path'}]
)
) {
my $template =
Sympa::Template->new(undef,
include_path => [$parsing->{'include_path'}]);
unless ($template->parse($vars, $parsing->{'template'}, \$output)) {
Log::do_log('err', 'Failed to parse %s', $parsing->{'template'});
return undef;
}
......@@ -7842,16 +7849,16 @@ sub _load_include_admin_user_file {
## Look for first valid line
unless ($paragraph[0] =~ /^\s*([\w-]+)(\s+.*)?$/) {
Log::do_log('info', 'Bad paragraph "%s" in %s', @paragraph,
$file);
Log::do_log('info', 'Bad paragraph "%s" in %s',
@paragraph, $file);
next;
}
$pname = $1;
unless ($config_in_admin_user_file{$pname}) {
Log::do_log('info', 'Unknown parameter "%s" in %s', $pname,
$file);
Log::do_log('info', 'Unknown parameter "%s" in %s',
$pname, $file);
next;
}
......@@ -8284,7 +8291,8 @@ sub sync_include {
unless (defined($new_subscribers->{$email})) {
## User is also subscribed, update DB entry
if ($old_subscribers{$email}{'subscribed'}) {
Log::do_log('debug', 'Updating %s to list %s', $email, $self);
Log::do_log('debug', 'Updating %s to list %s', $email,
$self);
unless (
$self->update_list_member(
$email,
......@@ -8330,7 +8338,8 @@ sub sync_include {
}
}
if ($users_removed > 0) {
Log::do_log('notice', '(%s) %d users removed', $self, $users_removed);
Log::do_log('notice', '(%s) %d users removed', $self,
$users_removed);
}
## Go through new users
......@@ -8386,7 +8395,8 @@ sub sync_include {
## User was already subscribed, update
## include_sources_subscriber in DB
} else {
Log::do_log('debug', 'Updating %s to list %s', $email, $self);
Log::do_log('debug', 'Updating %s to list %s', $email,
$self);
unless (
$self->update_list_member(
$email,
......@@ -8721,8 +8731,8 @@ sub sync_include_admin {
if ($#add_tab >= 0) {
unless ($admin_users_added =
$self->add_list_admin($role, @add_tab)) {
Log::do_log('err', '(%s) Failed to add new %ss', $role,
$name);
Log::do_log('err', '(%s) Failed to add new %ss',
$role, $name);
return undef;
}
}
......@@ -9327,7 +9337,8 @@ sub get_lists {
push @keys_perl, sprintf '$a->{"total"} <=> $b->{"total"}';
}
} else {
Log::do_log('err', 'bug in logic. Ask developer: $key=%s', $key);
Log::do_log('err', 'bug in logic. Ask developer: $key=%s',
$key);
return undef;
}
......@@ -10872,8 +10883,11 @@ sub store_subscription_request {
. int(rand(1000));
unless (opendir SUBSPOOL, "$Conf::Conf{'queuesubscribe'}") {
Log::do_log('err', 'Could not open %s',
$Conf::Conf{'queuesubscribe'});
Log::do_log(
'err',
'Could not open %s',
$Conf::Conf{'queuesubscribe'}
);
return undef;
}
......@@ -11179,8 +11193,11 @@ sub remove_task {
my $task = shift;
unless (opendir(DIR, $Conf::Conf{'queuetask'})) {
Log::do_log('err', 'Can\'t open dir %s: %m',
$Conf::Conf{'queuetask'});
Log::do_log(
'err',
'Can\'t open dir %s: %m',
$Conf::Conf{'queuetask'}
);