Commit 62dcc309 authored by sikeda's avatar sikeda
Browse files

[-dev] Small refactoring: Splitting global_remind, global_set and...

[-dev] Small refactoring: Splitting global_remind, global_set and global_signoff from remind, set and signoff, respectively.


git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@12642 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent 54c3c50f
......@@ -106,6 +106,8 @@ sub compute_auth {
my $that = $options{context};
my $email = $options{email};
my $cmd = $options{action};
# Compat. <= 6.2.12.
$cmd = 'remind' if $cmd eq 'global_remind';
my ($list, $robot);
if (ref $that eq 'Sympa::List') {
......@@ -161,7 +163,7 @@ Recipient (the person who asked for the command)
=item action =E<gt> $cmd
'signoff', 'subscribe', 'add', 'del' or 'remind' if $that is List.
'remind' else.
'global_remind' else.
=item email =E<gt> $email
......@@ -284,10 +286,10 @@ sub request_auth {
}
} else {
if ($cmd eq 'remind') {
if ($cmd eq 'global_remind') {
my $keyauth =
Sympa::compute_auth(context => '*', action => 'remind');
$data->{'command'} = "auth $keyauth $cmd *";
Sympa::compute_auth(context => '*', action => 'global_remind');
$data->{'command'} = "auth $keyauth remind *";
$data->{'command_escaped'} =
tools::escape_url($data->{'command'});
$data->{'type'} = 'remind';
......
......@@ -31,7 +31,7 @@ use Conf;
use Sympa::Regexps;
my $_email_re = Sympa::Regexps::addrspec();
our %comms = (
our %comms = (
add => {
cmd_regexp => qr'add'i,
arg_regexp => qr{(\S+)\s+($_email_re)(?:\s+(.+))?\s*\z},
......@@ -102,8 +102,8 @@ our %comms = (
},
remind => {
cmd_regexp => qr'rem|remind'i,
arg_regexp => qr'(?:([*])|([^\s\@]+))(?:\@([-.\w]+))?\s*\z',
arg_keys => [qw(anylists localpart domainpart)],
arg_regexp => qr'([^\s\@]+)(?:\@([-.\w]+))?\s*\z',
arg_keys => [qw(localpart domainpart)],
filter => sub {
my $r = shift;
......@@ -118,7 +118,11 @@ our %comms = (
}
$r;
},
scenario => 'remind', # or global_remind scenario.
scenario => 'remind',
},
global_remind => {
cmd_regexp => qr'(?:rem|remind)\s+[*]'i,
scenario => 'global_remind',
},
review => {
cmd_regexp => qr'rev|review|who'i,
......@@ -129,8 +133,8 @@ our %comms = (
set => {
cmd_regexp => qr'set'i,
arg_regexp =>
qr'(?:([*])|(\S+))\s+(digest|digestplain|nomail|normal|not_me|each|mail|conceal|noconceal|summary|notice|txt|html|urlize)\s*\z'i,
arg_keys => [qw(anylists localpart mode)],
qr'(\S+)\s+(digest|digestplain|nomail|normal|not_me|each|mail|conceal|noconceal|summary|notice|txt|html|urlize)\s*\z'i,
arg_keys => [qw(localpart mode)],
filter => sub {
my $r = shift;
......@@ -143,6 +147,22 @@ our %comms = (
},
# No scenario. Only list members are allowed.
},
global_set => {
cmd_regexp => qr'set\s+[*]'i,
arg_regexp =>
qr'(digest|digestplain|nomail|normal|not_me|each|mail|conceal|noconceal|summary|notice|txt|html|urlize)\s*\z'i,
arg_keys => [qw(mode)],
filter => sub {
my $r = shift;
$r->{mode} = lc($r->{mode} || '');
# SET EACH is a synonym for SET MAIL.
$r->{mode} = 'mail'
if grep { $r->{mode} eq $_ }
qw(each eachmail nodigest normal);
$r;
},
},
stats => {
cmd_regexp => qr'sta|stats'i,
arg_regexp => qr'(.+)',
......@@ -162,11 +182,12 @@ our %comms = (
},
signoff => {
cmd_regexp => qr'sig|signoff|uns|unsub|unsubscribe'i,
arg_regexp =>
qr{(?:([*])|([^\s\@]+))(?:\@([-.\w]+))?(?:\s+($_email_re))?\z},
arg_keys => [qw(anylists localpart domainpart email)],
filter => sub {
arg_regexp => qr{([^\s\@]+)(?:\@([-.\w]+))?(?:\s+($_email_re))?\z},
arg_keys => [qw(localpart domainpart email)],
filter => sub {
my $r = shift;
# email is defined if command is "unsubscribe <listname> <e-mail>".
$r->{email} ||= $r->{sender};
if ($r->{domainpart}) {
......@@ -180,7 +201,19 @@ our %comms = (
}
$r;
},
scenario => 'unsubscribe', # global signoff allows any lists.
scenario => 'unsubscribe',
},
global_signoff => {
cmd_regexp => qr'(?:sig|signoff|uns|unsub|unsubscribe)\s+[*]'i,
arg_regexp => qr{($_email_re)?\z},
arg_keys => [qw(email)],
filter => sub {
my $r = shift;
# email is defined if command is "unsubscribe * <e-mail>".
$r->{email} ||= $r->{sender};
$r;
},
},
verify => {
cmd_regexp => qr'ver|verify'i,
......@@ -193,4 +226,3 @@ our %comms = (
1;
__END__
......@@ -1326,17 +1326,15 @@ sub info {
#
#
##############################################################
sub signoff {
sub global_signoff {
$log->syslog('debug2', '(%s)', @_);
my $request = shift;
my $message = $request->{message};
my $sender = $request->{sender};
my $email = $request->{email} || $request->{sender};
# $email is defined if command is "unsubscribe <listname> <e-mail>"
my $email = $request->{email};
unless (ref $request->{context} eq 'Sympa::List') {
my $success;
foreach my $list (
Sympa::List::get_which($email, $request->{context}, 'member')) {
......@@ -1375,7 +1373,7 @@ sub signoff {
}
my $req = $request->dup;
delete $req->{anylists};
$req->{action} = 'signoff';
$req->{context} = $list;
$req->{localpart} = $list->{'name'};
......@@ -1383,7 +1381,15 @@ sub signoff {
$success ||= $status;
}
return $success;
}
}
sub signoff {
my $request = shift;
my $message = $request->{message};
my $sender = $request->{sender};
my $email = $request->{email};
my $list = $request->{context};
my $which = $list->{'name'};
......@@ -1981,7 +1987,7 @@ sub invite {
#
#
##############################################################
sub remind {
sub global_remind {
$log->syslog('debug2', '(%s)', @_);
my $request = shift;
......@@ -1990,14 +1996,8 @@ sub remind {
my ($list, $listname, $robot);
if (ref $request->{context} eq 'Sympa::List') {
$list = $request->{context};
$listname = $list->{'name'};
$robot = $list->{'domain'};
} else {
$listname = '*';
$robot = $request->{context};
}
my $auth_method = get_auth_method($request);
return 'wrong_auth'
......@@ -2006,24 +2006,10 @@ sub remind {
my $action;
my $result;
unless (ref $request->{context} eq 'Sympa::List') {
$result =
Sympa::Scenario::request_action($robot, 'global_remind',
$auth_method, {'sender' => $sender});
$action = $result->{'action'} if (ref($result) eq 'HASH');
} else {
$language->set_lang($list->{'admin'}{'lang'});
$result = Sympa::Scenario::request_action(
$list, 'remind',
$auth_method,
{ 'sender' => $sender,
'message' => $message,
}
);
$action = $result->{'action'} if (ref($result) eq 'HASH');
}
unless (defined $action) {
my $error = "Unable to evaluate scenario 'remind' for list $listname";
......@@ -2054,12 +2040,11 @@ sub remind {
return 'not_allowed';
} elsif ($action =~ /request_auth/i) {
$log->syslog('debug2', 'Auth requested from %s', $sender);
unless (ref $request->{context} eq 'Sympa::List') {
unless (
Sympa::request_auth(
context => '*',
sender => $sender,
action => 'remind'
action => 'global_remind'
)
) {
my $error =
......@@ -2067,62 +2052,11 @@ sub remind {
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
} else {
unless (
Sympa::request_auth(
context => $list,
sender => $sender,
action => 'remind'
)
) {
my $error =
'Unable to request authentication for command "remind"';
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
}
$log->syslog('info',
'REMIND %s from %s, auth requested (%.2f seconds)',
$listname, $sender, Time::HiRes::time() - $time_command);
return 1;
} elsif ($action =~ /do_it/i) {
if (ref $request->{context} eq 'Sympa::List') {
# For each subscriber send a reminder.
my $total = 0;
my $user;
unless ($user = $list->get_first_list_member()) {
my $error = "Unable to get subscribers for list $listname";
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
do {
unless ($list->send_probe_to_user('remind', $user->{'email'}))
{
$log->syslog('notice',
'Unable to send "remind" probe to %s',
$user->{'email'});
Sympa::Report::reject_report_cmd($request,
'intern_quiet');
}
$total += 1;
} while ($user = $list->get_next_list_member());
Sympa::Report::notice_report_cmd($request, 'remind',
{'total' => $total});
$log->syslog(
'info',
'REMIND %s from %s accepted, sent to %d subscribers (%.2f seconds)',
$listname,
$sender,
$total,
Time::HiRes::time() - $time_command
);
return 1;
} else {
## Global REMIND
my %global_subscription;
my %global_info;
my $count = 0;
......@@ -2210,7 +2144,132 @@ sub remind {
}
Sympa::Report::notice_report_cmd($request, 'glob_remind',
{'count' => $count});
return 1;
} else {
$log->syslog(
'info',
'REMIND %s from %s aborted, unknown requested action in scenario',
$listname,
$sender
);
my $error = "Unknown requested action in scenario: $action.";
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
}
sub remind {
$log->syslog('debug2', '(%s)', @_);
my $request = shift;
my $message = $request->{message};
my $sender = $request->{sender};
my ($list, $listname, $robot);
$list = $request->{context};
$listname = $list->{'name'};
$robot = $list->{'domain'};
my $auth_method = get_auth_method($request);
return 'wrong_auth'
unless (defined $auth_method);
my $action;
my $result;
$language->set_lang($list->{'admin'}{'lang'});
$result = Sympa::Scenario::request_action(
$list, 'remind',
$auth_method,
{ 'sender' => $sender,
'message' => $message,
}
);
$action = $result->{'action'} if (ref($result) eq 'HASH');
unless (defined $action) {
my $error = "Unable to evaluate scenario 'remind' for list $listname";
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
if ($action =~ /reject/i) {
$log->syslog('info', 'Remind for list %s from %s refused',
$listname, $sender);
if (defined $result->{'tt2'}) {
unless (
Sympa::send_file(
$list || $robot,
$result->{'tt2'}, $sender, {}
)
) {
$log->syslog('notice', 'Unable to send template "%s" to %s',
$result->{'tt2'}, $sender);
Sympa::Report::reject_report_cmd($request, 'auth',
$result->{'reason'});
}
} else {
Sympa::Report::reject_report_cmd($request, 'auth',
$result->{'reason'});
}
return 'not_allowed';
} elsif ($action =~ /request_auth/i) {
$log->syslog('debug2', 'Auth requested from %s', $sender);
unless (
Sympa::request_auth(
context => $list,
sender => $sender,
action => 'remind'
)
) {
my $error =
'Unable to request authentication for command "remind"';
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
$log->syslog('info',
'REMIND %s from %s, auth requested (%.2f seconds)',
$listname, $sender, Time::HiRes::time() - $time_command);
return 1;
} elsif ($action =~ /do_it/i) {
# For each subscriber send a reminder.
my $total = 0;
my $user;
unless ($user = $list->get_first_list_member()) {
my $error = "Unable to get subscribers for list $listname";
Sympa::Report::reject_report_cmd($request, 'intern', $error);
return undef;
}
do {
unless ($list->send_probe_to_user('remind', $user->{'email'}))
{
$log->syslog('notice',
'Unable to send "remind" probe to %s',
$user->{'email'});
Sympa::Report::reject_report_cmd($request,
'intern_quiet');
}
$total += 1;
} while ($user = $list->get_next_list_member());
Sympa::Report::notice_report_cmd($request, 'remind',
{'total' => $total});
$log->syslog(
'info',
'REMIND %s from %s accepted, sent to %d subscribers (%.2f seconds)',
$listname,
$sender,
$total,
Time::HiRes::time() - $time_command
);
return 1;
} else {
$log->syslog(
'info',
......@@ -2402,7 +2461,7 @@ sub del {
#
#
#############################################################
sub set {
sub global_set {
$log->syslog('debug2', '(%s)', @_);
my $request = shift;
......@@ -2411,7 +2470,6 @@ sub set {
my $mode = $request->{mode};
# Recursive call to subroutine.
unless (ref $request->{context} eq 'Sympa::List') {
my $success;
foreach my $list (
Sympa::List::get_which($sender, $request->{context}, 'member')) {
......@@ -2450,7 +2508,7 @@ sub set {
}
my $req = $request->dup;
delete $req->{anylists};
$req->{action} = 'set';
$req->{context} = $list;
$req->{localpart} = $list->{'name'};
......@@ -2458,7 +2516,13 @@ sub set {
$success ||= $status;
}
return $success;
}
}
sub set {
my $request = shift;
my $sender = $request->{sender};
my $mode = $request->{mode};
my $list = $request->{context};
my $which = $list->{'name'};
......
......@@ -168,7 +168,10 @@ sub _parse {
my $quiet = 1 if $line =~ s/\Aquiet\s+(.+)\z/$1/i;
my $l = $line;
foreach my $action (sort keys %Sympa::CommandDef::comms) {
foreach my $action (
sort(grep /global_/, keys %Sympa::CommandDef::comms),
sort(grep !/global_/, keys %Sympa::CommandDef::comms)
) {
my $comm = $Sympa::CommandDef::comms{$action};
my $cmd_regexp = $comm->{cmd_regexp};
my $arg_regexp = $comm->{arg_regexp};
......@@ -195,7 +198,7 @@ sub _parse {
} @{$arg_keys}
);
if (not $args{anylists} and $args{localpart}) {
if ($args{localpart}) {
# Load the list if not already done.
$context =
Sympa::List->new($args{localpart}, $robot,
......@@ -229,9 +232,7 @@ sub _parse {
sign_mod => $sign_mod,
);
if ( not $args{anylists}
and $args{localpart}
and ref $request->{context} ne 'Sympa::List') {
if ($args{localpart} and ref $request->{context} ne 'Sympa::List') {
# Reject the command if this list is unknown to us.
$request->{error} = 'unknown_list';
} elsif ($filter and not $filter->($request)) {
......@@ -254,7 +255,16 @@ sub _parse {
use constant quarantine => 1;
use constant remove => 1;
use constant store => 0;
sub store {
my $self = shift;
my $request = shift;
$self->{_metadatas} ||= [];
unshift @{$self->{_metadatas}}, $request;
1;
}
1;
__END__
......
Markdown is supported
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