-
Notifications
You must be signed in to change notification settings - Fork 97
/
DoCommand.pm
327 lines (273 loc) · 10.4 KB
/
DoCommand.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$
# 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>.
#
# 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::Spindle::DoCommand;
use strict;
use warnings;
use English qw(-no_match_vars);
use Sympa;
use Conf;
use Sympa::Log;
use Sympa::Spindle::ProcessMessage;
use base qw(Sympa::Spindle::ProcessIncoming); # Derives _splicing_to().
my $log = Sympa::Log->instance;
# Old name: (part of) DoCommand() in sympa_msg.pl.
# Partially moved to Sympa::Request::Message::_load().
sub _twist {
my $self = shift;
my $message = shift;
# Fail-safe: Skip messages with unwanted types.
return 0 unless $self->_splicing_to($message) eq __PACKAGE__;
my ($list, $robot);
if (ref $message->{context} eq 'Sympa::List') {
$list = $message->{context};
$robot = $list->{'domain'};
} elsif ($message->{context} and $message->{context} ne '*') {
$robot = $message->{context};
} else {
$robot = '*';
}
my $messageid = $message->{'message_id'};
$log->syslog(
'debug',
"Processing command with priority %s, %s",
$Conf::Conf{'sympa_priority'}, $messageid
);
my $sender = $message->{'sender'};
# Preventing loops based on Sympa processes sending DSN to each other.
if ($message->{envelope_sender} and $message->{envelope_sender} eq '<>'
or grep {/multipart\/report/i} $message->get_header('Content-type')) {
$log->syslog(
'notice',
'%s: Ignoring message which would cause a loop; message appears to be DSN report',
$message
);
return undef;
}
if ($message->{'spam_status'} eq 'spam') {
$log->syslog(
'notice',
'Message for %s ignored, because tagged as spam (message ID: %s)',
$message->{context},
$messageid
);
return undef;
}
## Detect loops
if ($self->{_msgid}{'sympa@' . $robot}{$messageid}) {
$log->syslog('err',
'Found known Message-ID, ignoring command which would cause a loop'
);
$log->db_log(
'robot' => $robot,
#'list' => 'sympa',
'action' => 'DoCommand',
'parameters' => $message->get_id,
'target_email' => '',
'msg_id' => $messageid,
'status' => 'error',
'error_type' => 'known_message',
'user_email' => $sender
);
# Clean old files from spool.
return undef;
}
# Keep track of known message IDs...if any.
$self->{_msgid}{'sympa@' . $robot}{$messageid} = time;
my $spindle = Sympa::Spindle::ProcessMessage->new(
message => $message,
scenario_context =>
{sender => $message->{sender}, message => $message}
);
unless ($spindle and $spindle->spin) {
# No command found.
$log->syslog('info', 'No command found in message %s', $message);
Sympa::send_dsn($robot, $message, {}, '5.6.1');
$log->db_log(
'robot' => $robot,
#'list' => 'sympa',
'action' => 'DoCommand',
'parameters' => $message->get_id,
'target_email' => '',
'msg_id' => $messageid,
'status' => 'error',
'error_type' => 'no_cmd_found',
'user_email' => $sender
);
return undef;
}
my $status = $spindle->{success} if $spindle;
# Mail back the result.
if (@{$spindle->{stash} || []}) {
# Loop prevention.
## Count reports sent to $sender
$self->{_loop_info}{$sender}{'count'}++;
## Sampling delay
if ((time - ($self->{_loop_info}{$sender}{'date_init'} || 0)) <
$Conf::Conf{'loop_command_sampling_delay'}) {
# Notify listmaster of first rejection.
if ($self->{_loop_info}{$sender}{'count'} ==
$Conf::Conf{'loop_command_max'}) {
## Notify listmaster
Sympa::send_notify_to_listmaster($robot, 'loop_command',
{'msg' => $message});
}
# Too many reports sent => message skipped !!
if ($self->{_loop_info}{$sender}{'count'} >=
$Conf::Conf{'loop_command_max'}) {
$log->syslog(
'err',
'Ignoring message which would cause a loop, %d messages sent to %s; loop_command_max exceeded',
$self->{_loop_info}{$sender}{'count'},
$sender
);
return undef;
}
} else {
# Sampling delay is over, reinit.
$self->{_loop_info}{$sender}{'date_init'} = time;
# We apply Decrease factor if a loop occurred.
$self->{_loop_info}{$sender}{'count'} *=
$Conf::Conf{'loop_command_decrease_factor'};
}
# Send the reply message.
my $reports = $spindle->{stash};
if (grep { $_ and $_->[1] eq 'user' and $_->[2] eq 'unknown_command' }
@{$spindle->{stash} || []}
) {
$log->db_log(
'robot' => $robot,
#'list' => 'sympa',
'action' => 'DoCommand',
'parameters' => $message->get_id,
'target_email' => '',
'msg_id' => $message->{message_id},
'status' => 'error',
'error_type' => 'not_understood',
'user_email' => $sender,
);
}
_send_report($robot, $sender, $reports);
$log->db_log(
'robot' => $robot,
#'list' => 'sympa',
'action' => 'DoCommand',
'parameters' => $message->get_id,
'target_email' => "",
'msg_id' => $message->{message_id},
'status' => 'success',
'error_type' => '',
'user_email' => $sender
);
}
return $status;
}
# Send the template command_report to $sender with list of reports.
# Old name: Sympa::Report::send_report_cmd().
sub _send_report {
my $robot = shift;
my $sender = shift;
my $reports = shift;
my @reports;
foreach my $report (@{$reports || []}) {
my ($request, $type, $error, $params) = @$report;
my %data = %{$params || {}};
# Special case for "reject(tt2=TEMPLATE)".
if ( $type eq 'auth'
and $data{template}
and Sympa::send_file(
$request->{context}, $data{template},
$sender, {auto_submitted => 'auto-replied'}
)
) {
next;
}
$data{type} = $type;
$data{cmd} = $request->cmd_line;
$data{email} ||= $request->{email};
$data{entry} = ($type eq 'intern') ? 'intern_error' : ($error || '');
my $that = $request->{context};
if (ref $that eq 'Sympa::List') {
$data{listname} = $that->{'name'};
} elsif ($request->{localpart}) {
$data{listname} = $request->{localpart};
}
push @reports, \%data;
}
my @notice_cmd = grep { $_->{type} eq 'notice' } @reports;
my @auth_reject_cmd = grep { $_->{type} eq 'auth' } @reports;
my @user_error_cmd = grep { $_->{type} eq 'user' } @reports;
my @intern_error_cmd = grep { $_->{type} eq 'intern' } @reports;
# for mail layout
my $before_auth = @notice_cmd ? 1 : 0;
my $before_user_err = ($before_auth or @auth_reject_cmd) ? 1 : 0;
my $before_intern_err = ($before_user_err or @user_error_cmd) ? 1 : 0;
# Ensure 1 second elapsed since last message.
Sympa::send_file(
$robot,
'command_report',
$sender,
{ to => $sender,
nb_notice => scalar(@notice_cmd),
nb_auth => scalar(@auth_reject_cmd),
nb_user_err => scalar(@user_error_cmd),
nb_intern_err => scalar(@intern_error_cmd),
#nb_global => scalar(@global_error_cmd),
before_auth => $before_auth,
before_user_err => $before_user_err,
before_intern_err => $before_intern_err,
notices => [@notice_cmd],
auths => [@auth_reject_cmd],
user_errors => [@user_error_cmd],
intern_errors => [@intern_error_cmd],
#globals => [@global_error_cmd],
},
date => time + 1
);
}
1;
__END__
=encoding utf-8
=head1 NAME
Sympa::Spindle::DoCommand - Workflow to handle command messages
=head1 DESCRIPTION
L<Sympa::Spindle::DoCommand> handles command messages bound for sympa,
[list]-subscribe or [list]-unsubscribe address.
If a message has one of types above, commands in the message will be parsed
and executed. Otherwise messages will be skipped.
=head2 Public methods
See also L<Sympa::Spindle::ProcessIncoming/"Public methods">.
=over
=item new ( key =E<gt> value, ... )
=item spin ( )
In most cases, L<Sympa::Spindle::ProcessIncoming> splices messages
to this class. These methods are not used in ordinal case.
=back
=head1 SEE ALSO
L<Sympa::Message>, L<Sympa::Spindle::ProcessIncoming>,
L<Sympa::Spindle::ProcessMessage>.
=head1 HISTORY
L<Sympa::Spindle::DoCommand> appeared on Sympa 6.2.13.
=cut