From a5487732091773bf0a0098a2ae77522fa5708941 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 17 May 2013 03:51:33 -0700 Subject: [PATCH] Make "Receive Test" mail form use MailReceivers Summary: Currently this is fairly hard-coded. Instead, make it use available receivers. Ref T1205. Test Plan: Used mail form to send mail to various objects (Dnn, Tnn, Cnn, etc.). Only some of these work right now because the receiver thing still hard-codes a bunch of junk. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T1205 Differential Revision: https://secure.phabricator.com/D5944 --- .../diffusion/query/DiffusionCommitQuery.php | 13 +++++ .../PhabricatorMetaMTAReceiveController.php | 51 +++++++++++++++++-- .../PhabricatorObjectMailReceiver.php | 4 ++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/applications/diffusion/query/DiffusionCommitQuery.php b/src/applications/diffusion/query/DiffusionCommitQuery.php index c9f757aee8..05c908e838 100644 --- a/src/applications/diffusion/query/DiffusionCommitQuery.php +++ b/src/applications/diffusion/query/DiffusionCommitQuery.php @@ -3,6 +3,7 @@ final class DiffusionCommitQuery extends PhabricatorCursorPagedPolicyAwareQuery { + private $ids; private $identifiers; private $phids; private $defaultRepository; @@ -32,6 +33,11 @@ final class DiffusionCommitQuery return $this; } + public function withIDs(array $ids) { + $this->ids = $ids; + return $this; + } + public function withPHIDs(array $phids) { $this->phids = $phids; return $this; @@ -164,6 +170,13 @@ final class DiffusionCommitQuery $where[] = '('.implode(' OR ', $sql).')'; } + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'id IN (%Ld)', + $this->ids); + } + if ($this->phids) { $where[] = qsprintf( $conn_r, diff --git a/src/applications/metamta/controller/PhabricatorMetaMTAReceiveController.php b/src/applications/metamta/controller/PhabricatorMetaMTAReceiveController.php index d113e776ac..91c12c2617 100644 --- a/src/applications/metamta/controller/PhabricatorMetaMTAReceiveController.php +++ b/src/applications/metamta/controller/PhabricatorMetaMTAReceiveController.php @@ -19,23 +19,64 @@ final class PhabricatorMetaMTAReceiveController if (!empty($from)) { $header_content['from'] = $from; + } else { + // If the user doesn't provide a "From" address, use their primary + // address. + $header_content['from'] = $user->loadPrimaryEmail()->getAddress(); } if (preg_match('/.+@.+/', $to)) { $header_content['to'] = $to; } else { - $receiver = PhabricatorMetaMTAReceivedMail::loadReceiverObject($to); + + // We allow the user to use an object name instead of a real address + // as a convenience. To build the mail, we build a similar message and + // look for a receiver which will accept it. + $pseudohash = PhabricatorObjectMailReceiver::computeMailHash('x', 'y'); + $pseudomail = id(new PhabricatorMetaMTAReceivedMail()) + ->setHeaders( + array( + 'to' => $to.'+1+'.$pseudohash, + )); + + $receivers = id(new PhutilSymbolLoader()) + ->setAncestorClass('PhabricatorMailReceiver') + ->loadObjects(); + + $receiver = null; + foreach ($receivers as $possible_receiver) { + if (!$possible_receiver->isEnabled()) { + continue; + } + if (!$possible_receiver->canAcceptMail($pseudomail)) { + continue; + } + $receiver = $possible_receiver; + break; + } if (!$receiver) { - throw new Exception(pht("No such task or revision!")); + throw new Exception( + "No configured mail receiver can accept mail to '{$to}'."); + } + + if (!($receiver instanceof PhabricatorObjectMailReceiver)) { + $class = get_class($receiver); + throw new Exception( + "Receiver '{$class}' accepts mail to '{$to}', but is not a ". + "subclass of PhabricatorObjectMailReceiver."); + } + + $object = $receiver->loadMailReceiverObject($to, $user); + if (!$object) { + throw new Exception("No such object '{$to}'!"); } $hash = PhabricatorObjectMailReceiver::computeMailHash( - $receiver->getMailKey(), + $object->getMailKey(), $user->getPHID()); - $header_content['to'] = - $to.'+'.$user->getID().'+'.$hash.'@'; + $header_content['to'] = $to.'+'.$user->getID().'+'.$hash.'@test.com'; } $received->setHeaders($header_content); diff --git a/src/applications/metamta/receiver/PhabricatorObjectMailReceiver.php b/src/applications/metamta/receiver/PhabricatorObjectMailReceiver.php index f429106f7d..7dc37661ab 100644 --- a/src/applications/metamta/receiver/PhabricatorObjectMailReceiver.php +++ b/src/applications/metamta/receiver/PhabricatorObjectMailReceiver.php @@ -26,6 +26,10 @@ abstract class PhabricatorObjectMailReceiver extends PhabricatorMailReceiver { */ abstract protected function loadObject($pattern, PhabricatorUser $viewer); + public function loadMailReceiverObject($pattern, PhabricatorUser $viewer) { + return $this->loadObject($pattern, $viewer); + } + public function validateSender( PhabricatorMetaMTAReceivedMail $mail,