After an upgrade to PHP v5.4.32, Mailhandler is failing to process downloaded messages.

There is a long series of errors but this is the first one:

Warning: preg_match(): Compilation failed: invalid range in character class at offset 15 in MailhandlerCommandsDefault->getCommands() (line 132 of /path/to/mailhandler/plugins/mailhandler/commands/MailhandlerCommandsDefault.class.php).

Here's line 132:

preg_match('/^([a-zA-Z0-9_-\s\.]+):{1}\s+(.+)$/', $line, $matches);

This suggests that the "_-\s" part might be a problem:

http://stackoverflow.com/questions/24764212/preg-match-compilation-faile...

CommentFileSizeAuthor
#2 mailhandler-fix-regex-2333087-2.patch790 bytesrclemings

Comments

rclemings’s picture

Followup: I'm not entirely clear on what this regex is supposed to do, but I found that putting a backslash in front of the hyphen solves the immediate problem and allows Mailhandler to process my commands correctly.

Was:
preg_match('/^([a-zA-Z0-9_-\s\.]+):{1}\s+(.+)$/', $line, $matches);

Now:
preg_match('/^([a-zA-Z0-9_\-\s\.]+):{1}\s+(.+)$/', $line, $matches);

If that sounds like a solution I can roll a patch.

rclemings’s picture

Assigned: Unassigned » rclemings
Status: Active » Needs review
StatusFileSize
new790 bytes

Here's the patch in case it's of use to anybody else. It has solved the problem for me at least.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

Came across the same issue and patch #2 resolved the issue.

bombjack’s picture

Yeah I have the same problem with PHP 5.5.30

Originally came across the problem using Open Atrium and filed an issue thinking the plugin was OA specific. https://www.drupal.org/node/2651534

Looking around the web some regex testers will accept either version, and some require the hyphen character to be escaped given it is sometimes used to separate a range of characters.

Seems to me that it is safe to escape the hyphen, and probably best practice given it does break the regex, but does solve problems with more picky regex parsers.

abarpetia’s picture

#2 patch worked for me. +1 to commit.

Anonymous’s picture

#2 fixed the problem.

In our case the problem was shown by the fact that the imported comments were set to unpublished. The mailhandler Extra commands are: /tls/novalidate-cert

Warning: preg_match(): Compilation failed: invalid range in character class at offset 15 in MailhandlerCommandsDefault->getCommands() (line 117 of /var/www/html/profiles/openatrium/modules/contrib/mailhandler/plugins/mailhandler/commands/MailhandlerCommandsDefault.class.php).

On our dev server where the mailhandler mailhandler Extra commands are set to the default /notls, there is no problem.

caspianroach’s picture

_-\s

in that regex is supposed to mean "literal underscore, literal hyphen, any whitespace". However, since the symbol for hyphen '-' is also used in character ranges (for example, 0-9 means "from 0 to 9", meaning all numerals) and character class subtractions, it can trip certain RegExp parsers.

All the symbols inside square brackets except for ^-[]\ are parsed as literal tokens. To make these literal you need to escape them with a backslash \

In short, this patch does exactly what is needed and should be commited.

alexh’s picture

The mailhandler commands did not work at all for me due to this bug. Patch in #2 worked and solved it for me.
I don't understand, why this simple but essential patch has not been committed in 4 years.

rclemings’s picture

Make that eight years. I guess this module is abandoned now.