Commit 3f601943 authored by sikeda's avatar sikeda
Browse files

[bug] A few bugs on email command processing:

- Long lines folded by flowed format (RFC 3676) were not handled correctly.
- mesaddressed_commands feature checked encoded body.


git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@12064 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent f2f62d33
......@@ -63,6 +63,7 @@ use MIME::Parser;
use MIME::Tools;
use Net::DNS;
use Scalar::Util qw();
use Text::LineFold;
use URI::Escape qw();
BEGIN { eval 'use Crypt::SMIME'; }
......@@ -3300,7 +3301,9 @@ sub _fix_utf8_parts {
I<Instance method>.
Gets decoded content of text/plain part.
The text will be converted to UTF-8.
Flowed text (see RFC 3676) will be conjuncted.
=back
......@@ -3315,7 +3318,7 @@ sub get_plain_body {
return undef unless $entity->bodyhandle;
my $body = $entity->bodyhandle->as_string;
## Get charset
# Get charset
my $cset =
MIME::Charset->new($entity->head->mime_attr('Content-Type.Charset')
|| 'NONE');
......@@ -3323,13 +3326,25 @@ sub get_plain_body {
# Charset is unknown. Detect 7-bit charset.
$cset = MIME::Charset->new(MIME::Charset::detect_7bit_charset($body));
}
if ($cset->decoder) {
$cset->encoder('UTF-8');
} else {
unless ($cset->decoder) {
$cset = MIME::Charset->new('US-ASCII');
}
return $cset->encode($body);
# Unfold flowed text if required.
my $format = lc($entity->head->mime_attr('Content-Type.Format') || '');
my $delsp = lc($entity->head->mime_attr('Content-Type.DelSp') || '');
if ($format eq 'flowed') {
my $linefold =
Text::LineFold->new(Charset => $cset, OutputCharset => 'UTF-8');
if ($delsp eq 'yes') {
return $linefold->unfold($body, 'FLOWED');
} else {
return $linefold->unfold($body, 'FLOWEDSP');
}
} else {
$cset->encoder('UTF-8');
return $cset->encode($body);
}
}
# Make multipart/alternative message to singlepart.
......
......@@ -82,53 +82,8 @@ sub by_date {
# Moved to Sympa::Mailer::_safefork().
#sub safefork ($i, $pid);
####################################################
# checkcommand
####################################################
# Checks for no command in the body of the message.
# If there are some command in it, it return true
# and send a message to $sender
#
# IN : -$msg (+): ref(MIME::Entity) - message to check
# -$sender (+): the sender of $msg
#
# OUT : -1 if there are some command in $msg
# -0 else
#
######################################################
sub checkcommand {
my ($msg, $sender) = @_;
my $i;
my $hdr = $msg->head;
## Check for commands in the subject.
my $subject = $msg->head->get('Subject');
$log->syslog('debug3', '(msg->head->get(subject) %s, %s)',
$subject, $sender);
if ($subject) {
if ($Conf::Conf{'misaddressed_commands_regexp'}
&& ($subject =~ /^$Conf::Conf{'misaddressed_commands_regexp'}$/im)
) {
return 1;
}
}
return 0 if ($#{$msg->body} >= 5); ## More than 5 lines in the text.
foreach $i (@{$msg->body}) {
if ($Conf::Conf{'misaddressed_commands_regexp'}
&& ($i =~ /^$Conf::Conf{'misaddressed_commands_regexp'}\b/im)) {
return 1;
}
## Control is only applied to first non-blank line
last unless $i =~ /^\s*$/;
}
return 0;
}
# Moved to _check_command in sympa_msg.pl.
#sub checkcommand;
## return a hash from the edit_list_conf file
sub load_edit_list_conf {
......
......@@ -49,7 +49,6 @@ use Sympa::Regexps;
use Sympa::Report;
use Sympa::Scenario;
use Sympa::Spool;
use tools;
use Sympa::Tools::Daemon;
use Sympa::Tools::Data;
use Sympa::Tools::File;
......@@ -1278,9 +1277,10 @@ sub DoMessage {
# Reject messages with commands
if ($Conf::Conf{'misaddressed_commands'} =~ /reject/i) {
## Check the message for commands and catch them.
if (tools::checkcommand($msg, $sender)) {
$log->syslog('err', 'Found command in message, ignoring message');
# Check the message for commands and catch them.
my $cmd = _check_command($message);
if (defined $cmd) {
$log->syslog('err', 'Found command "%s" in message, ignoring message', $cmd);
Sympa::Report::reject_report_msg('user', 'routing_error', $sender,
{'message' => $message},
$robot_id, $msg_string, $list);
......@@ -1781,6 +1781,49 @@ sub DoMessage {
}
}
# Checks command in the body of the message.
# If there are any commands in it, returns string.
#
# IN : -$message (+): ref(Sympa::Message) - message to check
#
# OUT : -string: Command found
# -undef: Else.
#
# Old name: tools::checkcommand().
sub _check_command {
my $message = shift;
my $commands_re = $Conf::Conf{'misaddressed_commands_regexp'};
return undef unless defined $commands_re and length $commands_re;
# Check for commands in the subject.
my $subject_field = $message->{'decoded_subject'};
$subject_field = '' unless defined $subject_field;
$subject_field =~ s/\n//mg; # multiline subjects
my $re_regexp = Sympa::Regexps::re();
$subject_field =~ s/^\s*(?:$re_regexp)?\s*(.*)\s*$/$1/i;
if ($subject_field =~ /^($commands_re)$/im) {
return $1;
}
my @body = map { s/\r\n|\n//; $_ } split /(?<=\n)/,
($message->get_plain_body || '');
# More than 5 lines in the text.
return undef if scalar @body > 5;
foreach my $line (@body) {
if ($line =~ /^($commands_re)\b/im) {
return $1;
}
# Control is only applied to first non-blank line.
last unless $line =~ /\A\s*\z/;
}
return undef;
}
############################################################
# DoCommand
############################################################
......
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