Page MenuHomePhabricator

D7307.diff

diff --git a/src/applications/conpherence/mail/ConpherenceReplyHandler.php b/src/applications/conpherence/mail/ConpherenceReplyHandler.php
--- a/src/applications/conpherence/mail/ConpherenceReplyHandler.php
+++ b/src/applications/conpherence/mail/ConpherenceReplyHandler.php
@@ -69,7 +69,6 @@
->setParentMessageID($mail->getMessageID());
$body = $mail->getCleanTextBody();
- $body = trim($body);
$file_phids = $mail->getAttachments();
$body = $this->enhanceBodyWithAttachments(
$body,
diff --git a/src/applications/files/mail/FileReplyHandler.php b/src/applications/files/mail/FileReplyHandler.php
--- a/src/applications/files/mail/FileReplyHandler.php
+++ b/src/applications/files/mail/FileReplyHandler.php
@@ -32,29 +32,18 @@
$actor = $this->getActor();
$file = $this->getMailReceiver();
- $body = $mail->getCleanTextBody();
- $body = trim($body);
+ $body_data = $mail->parseBody();
+ $body = $body_data['body'];
$body = $this->enhanceBodyWithAttachments($body, $mail->getAttachments());
$content_source = PhabricatorContentSource::newForSource(
PhabricatorContentSource::SOURCE_EMAIL,
array(
'id' => $mail->getID(),
));
- $lines = explode("\n", trim($body));
- $first_line = head($lines);
-
$xactions = array();
- $command = null;
- $matches = null;
- if (preg_match('/^!(\w+)/', $first_line, $matches)) {
- $lines = array_slice($lines, 1);
- $body = implode("\n", $lines);
- $body = trim($body);
-
- $command = $matches[1];
- }
+ $command = $body_data['body'];
switch ($command) {
case 'unsubscribe':
diff --git a/src/applications/legalpad/mail/LegalpadReplyHandler.php b/src/applications/legalpad/mail/LegalpadReplyHandler.php
--- a/src/applications/legalpad/mail/LegalpadReplyHandler.php
+++ b/src/applications/legalpad/mail/LegalpadReplyHandler.php
@@ -37,29 +37,19 @@
$actor = $this->getActor();
$document = $this->getMailReceiver();
- $body = $mail->getCleanTextBody();
- $body = trim($body);
+ $body_data = $mail->parseBody();
+ $body = $body_data['body'];
$body = $this->enhanceBodyWithAttachments($body, $mail->getAttachments());
$content_source = PhabricatorContentSource::newForSource(
PhabricatorContentSource::SOURCE_EMAIL,
array(
'id' => $mail->getID(),
));
- $lines = explode("\n", trim($body));
- $first_line = head($lines);
$xactions = array();
- $command = null;
- $matches = null;
- if (preg_match('/^!(\w+)/', $first_line, $matches)) {
- $lines = array_slice($lines, 1);
- $body = implode("\n", $lines);
- $body = trim($body);
-
- $command = $matches[1];
- }
+ $command = $body_data['command'];
switch ($command) {
case 'unsubscribe':
diff --git a/src/applications/maniphest/mail/ManiphestReplyHandler.php b/src/applications/maniphest/mail/ManiphestReplyHandler.php
--- a/src/applications/maniphest/mail/ManiphestReplyHandler.php
+++ b/src/applications/maniphest/mail/ManiphestReplyHandler.php
@@ -45,8 +45,8 @@
$user = $this->getActor();
- $body = $mail->getCleanTextBody();
- $body = trim($body);
+ $body_data = $mail->parseBody();
+ $body = $body_data['body'];
$body = $this->enhanceBodyWithAttachments($body, $mail->getAttachments());
$xactions = array();
@@ -73,18 +73,9 @@
$task->setPriority(ManiphestTaskPriority::getDefaultPriority());
} else {
- $lines = explode("\n", trim($body));
- $first_line = head($lines);
- $command = null;
- $matches = null;
- if (preg_match('/^!(\w+)/', $first_line, $matches)) {
- $lines = array_slice($lines, 1);
- $body = implode("\n", $lines);
- $body = trim($body);
-
- $command = $matches[1];
- }
+ $command = $body_data['command'];
+ $command_value = $body_data['command_value'];
$ttype = PhabricatorTransactions::TYPE_COMMENT;
$new_value = null;
@@ -97,6 +88,23 @@
$ttype = ManiphestTransaction::TYPE_OWNER;
$new_value = $user->getPHID();
break;
+ case 'assign':
+ $ttype = ManiphestTransaction::TYPE_OWNER;
+ if ($command_value) {
+ $assign_users = id(new PhabricatorPeopleQuery())
+ ->setViewer($user)
+ ->withUsernames(array($command_value))
+ ->execute();
+ if ($assign_users) {
+ $assign_user = head($assign_users);
+ $new_value = $assign_user->getPHID();
+ }
+ }
+ // assign to the user by default
+ if (!$new_value) {
+ $new_value = $user->getPHID();
+ }
+ break;
case 'unsubscribe':
$is_unsub = true;
$ttype = ManiphestTransaction::TYPE_CCS;
diff --git a/src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php b/src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php
--- a/src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php
+++ b/src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php
@@ -2,8 +2,51 @@
final class PhabricatorMetaMTAEmailBodyParser {
+ /**
+ * 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);
+ }
+
public function stripTextBody($body) {
- return $this->stripSignature($this->stripQuotedText($body));
+ return trim($this->stripSignature($this->stripQuotedText($body)));
}
private function stripQuotedText($body) {
diff --git a/src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php b/src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php
--- a/src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php
+++ b/src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php
@@ -12,6 +12,44 @@
}
}
+ public function testEmailBodyCommandParsing() {
+ $bodies = $this->getEmailBodiesWithFullCommands();
+ foreach ($bodies as $body) {
+ $parser = new PhabricatorMetaMTAEmailBodyParser();
+ $body_data = $parser->parseBody($body);
+ $this->assertEqual('OKAY', $body_data['body']);
+ $this->assertEqual('whatevs', $body_data['command']);
+ $this->assertEqual('dude', $body_data['command_value']);
+ }
+ $bodies = $this->getEmailBodiesWithPartialCommands();
+ foreach ($bodies as $body) {
+ $parser = new PhabricatorMetaMTAEmailBodyParser();
+ $body_data = $parser->parseBody($body);
+ $this->assertEqual('OKAY', $body_data['body']);
+ $this->assertEqual('whatevs', $body_data['command']);
+ $this->assertEqual(null, $body_data['command_value']);
+ }
+ }
+
+ private function getEmailBodiesWithFullCommands() {
+ $bodies = $this->getEmailBodies();
+ $with_commands = array();
+ foreach ($bodies as $body) {
+ $with_commands[] = "!whatevs dude\n" . $body;
+ }
+ return $with_commands;
+ }
+
+ private function getEmailBodiesWithPartialCommands() {
+ $bodies = $this->getEmailBodies();
+ $with_commands = array();
+ foreach ($bodies as $body) {
+ $with_commands[] = "!whatevs\n" . $body;
+ }
+ return $with_commands;
+ }
+
+
private function getEmailBodies() {
$trailing_space = ' ';
diff --git a/src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php b/src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php
--- a/src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php
+++ b/src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php
@@ -307,7 +307,7 @@
return $this->getSingleReplyHandlerPrefix($address);
}
- final protected function enhanceBodyWithAttachments(
+ final protected function enhanceBodyWithAttachments(
$body,
array $attachments,
$format = '- {F%d, layout=link}') {
diff --git a/src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php b/src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php
--- a/src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php
+++ b/src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php
@@ -120,12 +120,17 @@
}
public function getCleanTextBody() {
- $body = idx($this->bodies, 'text');
-
+ $body = $this->getRawTextBody();
$parser = new PhabricatorMetaMTAEmailBodyParser();
return $parser->stripTextBody($body);
}
+ public function parseBody() {
+ $body = $this->getRawTextBody();
+ $parser = new PhabricatorMetaMTAEmailBodyParser();
+ return $parser->parseBody($body);
+ }
+
public function getRawTextBody() {
return idx($this->bodies, 'text');
}
diff --git a/src/applications/paste/mail/PasteReplyHandler.php b/src/applications/paste/mail/PasteReplyHandler.php
--- a/src/applications/paste/mail/PasteReplyHandler.php
+++ b/src/applications/paste/mail/PasteReplyHandler.php
@@ -32,8 +32,8 @@
$actor = $this->getActor();
$paste = $this->getMailReceiver();
- $body = $mail->getCleanTextBody();
- $body = trim($body);
+ $body_data = $mail->parseBody();
+ $body = $body_data['body'];
$body = $this->enhanceBodyWithAttachments($body, $mail->getAttachments());
$content_source = PhabricatorContentSource::newForSource(
@@ -46,15 +46,7 @@
$first_line = head($lines);
$xactions = array();
- $command = null;
- $matches = null;
- if (preg_match('/^!(\w+)/', $first_line, $matches)) {
- $lines = array_slice($lines, 1);
- $body = implode("\n", $lines);
- $body = trim($body);
-
- $command = $matches[1];
- }
+ $command = $body_data['command'];
switch ($command) {
case 'unsubscribe':

File Metadata

Mime Type
text/x-diff
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/d3/xg/upwplda6j733kksb
Default Alt Text
D7307.diff (10 KB)

Event Timeline