2011-06-01 17:33:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorMetaMTAEmailBodyParser {
|
|
|
|
|
2013-10-14 21:29:41 +02:00
|
|
|
/**
|
|
|
|
* Mails can have bodies such as
|
|
|
|
*
|
|
|
|
* !claim
|
|
|
|
*
|
|
|
|
* taking this task
|
|
|
|
*
|
|
|
|
* Or
|
|
|
|
*
|
|
|
|
* !assign epriestley
|
|
|
|
*
|
|
|
|
* please, take this task I took; its hard
|
|
|
|
*
|
|
|
|
* This function parses such an email body and returns a dictionary
|
|
|
|
* containing a clean body text (e.g. "taking this task"), a $command
|
|
|
|
* (e.g. !claim, !assign) and a $command_value (e.g. "epriestley" in the
|
|
|
|
* !assign example.)
|
|
|
|
*
|
|
|
|
* @return dict
|
|
|
|
*/
|
|
|
|
public function parseBody($body) {
|
|
|
|
$body = $this->stripTextBody($body);
|
|
|
|
$lines = explode("\n", trim($body));
|
|
|
|
$first_line = head($lines);
|
|
|
|
|
|
|
|
$command = null;
|
|
|
|
$command_value = null;
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match('/^!(\w+)\s*(\S+)?/', $first_line, $matches)) {
|
|
|
|
$lines = array_slice($lines, 1);
|
|
|
|
$body = implode("\n", $lines);
|
|
|
|
$body = trim($body);
|
|
|
|
|
|
|
|
$command = $matches[1];
|
|
|
|
$command_value = idx($matches, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'body' => $body,
|
|
|
|
'command' => $command,
|
|
|
|
'command_value' => $command_value);
|
|
|
|
}
|
|
|
|
|
2012-04-09 00:04:12 +02:00
|
|
|
public function stripTextBody($body) {
|
2013-10-14 21:29:41 +02:00
|
|
|
return trim($this->stripSignature($this->stripQuotedText($body)));
|
2011-06-01 17:33:14 +02:00
|
|
|
}
|
|
|
|
|
2012-04-09 00:04:12 +02:00
|
|
|
private function stripQuotedText($body) {
|
2013-12-07 00:47:37 +01:00
|
|
|
|
|
|
|
// Look for "On <date>, <user> wrote:". This may be split across multiple
|
|
|
|
// lines. We need to be careful not to remove all of a message like this:
|
|
|
|
//
|
|
|
|
// On which day do you want to meet?
|
|
|
|
//
|
|
|
|
// On <date>, <user> wrote:
|
|
|
|
// > Let's set up a meeting.
|
|
|
|
|
|
|
|
$start = null;
|
|
|
|
$lines = phutil_split_lines($body);
|
|
|
|
foreach ($lines as $key => $line) {
|
|
|
|
if (preg_match('/^\s*>?\s*On\b/', $line)) {
|
|
|
|
$start = $key;
|
|
|
|
}
|
|
|
|
if ($start !== null) {
|
|
|
|
if (preg_match('/\bwrote:/', $line)) {
|
|
|
|
$lines = array_slice($lines, 0, $start);
|
|
|
|
$body = implode('', $lines);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-01 17:33:14 +02:00
|
|
|
|
2011-12-19 17:40:18 +01:00
|
|
|
// Outlook english
|
|
|
|
$body = preg_replace(
|
2012-04-11 19:31:04 +02:00
|
|
|
'/^\s*-----Original Message-----.*?/imsU',
|
2011-12-19 17:40:18 +01:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
|
|
|
// Outlook danish
|
|
|
|
$body = preg_replace(
|
2012-04-11 19:31:04 +02:00
|
|
|
'/^\s*-----Oprindelig Meddelelse-----.*?/imsU',
|
2011-12-19 17:40:18 +01:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2013-05-21 00:52:54 +02:00
|
|
|
// See example in T3217.
|
|
|
|
$body = preg_replace(
|
|
|
|
'/^________________________________________\s+From:.*?/imsU',
|
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2012-04-09 00:04:12 +02:00
|
|
|
return rtrim($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function stripSignature($body) {
|
|
|
|
// Quasi-"standard" delimiter, for lols see:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=58406
|
|
|
|
$body = preg_replace(
|
|
|
|
'/^-- +$.*/sm',
|
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2012-04-03 00:21:15 +02:00
|
|
|
// HTC Mail application (mobile)
|
|
|
|
$body = preg_replace(
|
2012-04-09 00:04:12 +02:00
|
|
|
'/^\s*^Sent from my HTC smartphone.*/sm',
|
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
|
|
|
// Apple iPhone
|
|
|
|
$body = preg_replace(
|
|
|
|
'/^\s*^Sent from my iPhone\s*$.*/sm',
|
2012-04-03 00:21:15 +02:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2011-06-01 17:33:14 +02:00
|
|
|
return rtrim($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|