Page MenuHomePhabricator

D7307.diff
No OneTemporary

D7307.diff

Index: src/applications/conpherence/mail/ConpherenceReplyHandler.php
===================================================================
--- src/applications/conpherence/mail/ConpherenceReplyHandler.php
+++ 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,
Index: src/applications/files/mail/FileReplyHandler.php
===================================================================
--- src/applications/files/mail/FileReplyHandler.php
+++ src/applications/files/mail/FileReplyHandler.php
@@ -32,8 +32,8 @@
$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(
@@ -42,19 +42,8 @@
'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':
Index: src/applications/legalpad/mail/LegalpadReplyHandler.php
===================================================================
--- src/applications/legalpad/mail/LegalpadReplyHandler.php
+++ src/applications/legalpad/mail/LegalpadReplyHandler.php
@@ -37,8 +37,8 @@
$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(
@@ -47,19 +47,9 @@
'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':
Index: src/applications/maniphest/mail/ManiphestReplyHandler.php
===================================================================
--- src/applications/maniphest/mail/ManiphestReplyHandler.php
+++ 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;
Index: src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php
===================================================================
--- src/applications/metamta/parser/PhabricatorMetaMTAEmailBodyParser.php
+++ 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) {
Index: src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php
===================================================================
--- src/applications/metamta/parser/__tests__/PhabricatorMetaMTAEmailBodyParserTestCase.php
+++ 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 = ' ';
Index: src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php
===================================================================
--- src/applications/metamta/replyhandler/PhabricatorMailReplyHandler.php
+++ 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}') {
Index: src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php
===================================================================
--- src/applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php
+++ 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');
}
Index: src/applications/paste/mail/PasteReplyHandler.php
===================================================================
--- src/applications/paste/mail/PasteReplyHandler.php
+++ 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/plain
Expires
Sat, Jun 8, 5:36 AM (3 w, 17 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6283871
Default Alt Text
D7307.diff (10 KB)

Event Timeline