2011-06-01 08:33:14 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorMetaMTAEmailBodyParser {
|
|
|
|
|
2013-10-14 12:29:41 -07: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-08 15:04:12 -07:00
|
|
|
public function stripTextBody($body) {
|
2013-10-14 12:29:41 -07:00
|
|
|
return trim($this->stripSignature($this->stripQuotedText($body)));
|
2011-06-01 08:33:14 -07:00
|
|
|
}
|
|
|
|
|
2012-04-08 15:04:12 -07:00
|
|
|
private function stripQuotedText($body) {
|
2011-06-01 08:33:14 -07:00
|
|
|
$body = preg_replace(
|
2013-10-30 13:07:18 -07:00
|
|
|
'/^\s*>?\s*On\b.*\bwrote:.*?/msU',
|
2011-06-01 08:33:14 -07:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2011-12-19 08:40:18 -08:00
|
|
|
// Outlook english
|
|
|
|
$body = preg_replace(
|
2012-04-11 10:31:04 -07:00
|
|
|
'/^\s*-----Original Message-----.*?/imsU',
|
2011-12-19 08:40:18 -08:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
|
|
|
// Outlook danish
|
|
|
|
$body = preg_replace(
|
2012-04-11 10:31:04 -07:00
|
|
|
'/^\s*-----Oprindelig Meddelelse-----.*?/imsU',
|
2011-12-19 08:40:18 -08:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2013-05-20 15:52:54 -07:00
|
|
|
// See example in T3217.
|
|
|
|
$body = preg_replace(
|
|
|
|
'/^________________________________________\s+From:.*?/imsU',
|
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2012-04-08 15:04:12 -07: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-02 15:21:15 -07:00
|
|
|
// HTC Mail application (mobile)
|
|
|
|
$body = preg_replace(
|
2012-04-08 15:04:12 -07:00
|
|
|
'/^\s*^Sent from my HTC smartphone.*/sm',
|
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
|
|
|
// Apple iPhone
|
|
|
|
$body = preg_replace(
|
|
|
|
'/^\s*^Sent from my iPhone\s*$.*/sm',
|
2012-04-02 15:21:15 -07:00
|
|
|
'',
|
|
|
|
$body);
|
|
|
|
|
2011-06-01 08:33:14 -07:00
|
|
|
return rtrim($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|