2011-05-01 04:40:05 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-26 22:01:45 +01:00
|
|
|
/**
|
|
|
|
* NOTE: Do not extend this!
|
|
|
|
*
|
|
|
|
* @concrete-extensible
|
|
|
|
*/
|
2012-03-15 22:16:32 +01:00
|
|
|
class DifferentialReplyHandler extends PhabricatorMailReplyHandler {
|
2011-05-10 01:31:26 +02:00
|
|
|
|
2011-06-22 21:41:19 +02:00
|
|
|
private $receivedMail;
|
|
|
|
|
2011-05-10 01:31:26 +02:00
|
|
|
public function validateMailReceiver($mail_receiver) {
|
|
|
|
if (!($mail_receiver instanceof DifferentialRevision)) {
|
2014-06-09 20:36:49 +02:00
|
|
|
throw new Exception('Receiver is not a DifferentialRevision!');
|
2011-05-10 01:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrivateReplyHandlerEmailAddress(
|
|
|
|
PhabricatorObjectHandle $handle) {
|
|
|
|
return $this->getDefaultPrivateReplyHandlerEmailAddress($handle, 'D');
|
|
|
|
}
|
|
|
|
|
Allow Phabricator to be configured to use a public Reply-To address
Summary:
We already support this (and Facebook uses it) but it is difficult to configure
and you have to write a bunch of code. Instead, provide a simple flag.
See the documentation changes for details, but when this flag is enabled we send
one email with a reply-to like "D2+public+23hf91fh19fh@phabricator.example.com".
Anyone can reply to this, and we figure out who they are based on their "From"
address instead of a unique hash. This is less secure, but a reasonable tradeoff
in many cases.
This also has the advantage over a naive implementation of at least doing object
hash validation.
@jungejason: I don't think this affects Facebook's implementation but this is an
area where we've had problems in the past, so watch out for it when you deploy.
Also note that you must set "metamta.public-replies" to true since Maniphest now
looks for that key specifically before going into public reply mode; it no
longer just tests for a public reply address being generateable (since it can
always generate one now).
Test Plan:
Swapped my local install in and out of public reply mode and commented on
objects. Got expected email behavior. Replied to public and private email
addresses.
Attacked public addresses by using them when the install was configured to
disallow them and by altering the hash and the from address. All this stuff was
rejected.
Reviewed By: jungejason
Reviewers: moskov, jungejason, tuomaspelkonen, aran
CC: aran, epriestley, moskov, jungejason
Differential Revision: 563
2011-06-30 22:01:35 +02:00
|
|
|
public function getPublicReplyHandlerEmailAddress() {
|
|
|
|
return $this->getDefaultPublicReplyHandlerEmailAddress('D');
|
|
|
|
}
|
|
|
|
|
2011-05-10 01:31:26 +02:00
|
|
|
public function getReplyHandlerDomain() {
|
|
|
|
return PhabricatorEnv::getEnvConfig(
|
|
|
|
'metamta.differential.reply-handler-domain');
|
|
|
|
}
|
2011-05-01 04:40:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate text like the following from the supported commands.
|
|
|
|
* "
|
|
|
|
*
|
|
|
|
* ACTIONS
|
|
|
|
* Reply to comment, or !accept, !reject, !abandon, !resign, !reclaim.
|
|
|
|
*
|
|
|
|
* "
|
|
|
|
*/
|
2011-05-10 01:31:26 +02:00
|
|
|
public function getReplyHandlerInstructions() {
|
|
|
|
if (!$this->supportsReplies()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-05-01 04:40:05 +02:00
|
|
|
$supported_commands = $this->getSupportedCommands();
|
|
|
|
$text = '';
|
|
|
|
if (empty($supported_commands)) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
$comment_command_printed = false;
|
|
|
|
if (in_array(DifferentialAction::ACTION_COMMENT, $supported_commands)) {
|
2013-01-24 22:18:44 +01:00
|
|
|
$text .= pht('Reply to comment');
|
2011-05-01 04:40:05 +02:00
|
|
|
$comment_command_printed = true;
|
|
|
|
|
|
|
|
$supported_commands = array_diff(
|
|
|
|
$supported_commands, array(DifferentialAction::ACTION_COMMENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($supported_commands)) {
|
|
|
|
if ($comment_command_printed) {
|
|
|
|
$text .= ', or ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$modified_commands = array();
|
|
|
|
foreach ($supported_commands as $command) {
|
|
|
|
$modified_commands[] = '!'.$command;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text .= implode(', ', $modified_commands);
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:36:49 +02:00
|
|
|
$text .= '.';
|
2011-05-01 04:40:05 +02:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSupportedCommands() {
|
2011-09-14 17:11:05 +02:00
|
|
|
$actions = array(
|
2011-05-01 04:40:05 +02:00
|
|
|
DifferentialAction::ACTION_COMMENT,
|
|
|
|
DifferentialAction::ACTION_REJECT,
|
|
|
|
DifferentialAction::ACTION_ABANDON,
|
|
|
|
DifferentialAction::ACTION_RECLAIM,
|
|
|
|
DifferentialAction::ACTION_RESIGN,
|
2011-05-16 21:31:18 +02:00
|
|
|
DifferentialAction::ACTION_RETHINK,
|
2011-05-31 23:53:03 +02:00
|
|
|
'unsubscribe',
|
2011-05-01 04:40:05 +02:00
|
|
|
);
|
2011-09-14 17:11:05 +02:00
|
|
|
|
|
|
|
if (PhabricatorEnv::getEnvConfig('differential.enable-email-accept')) {
|
|
|
|
$actions[] = DifferentialAction::ACTION_ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actions;
|
2011-05-01 04:40:05 +02:00
|
|
|
}
|
|
|
|
|
2012-08-28 23:09:37 +02:00
|
|
|
protected function receiveEmail(PhabricatorMetaMTAReceivedMail $mail) {
|
2011-06-22 21:41:19 +02:00
|
|
|
$this->receivedMail = $mail;
|
2012-11-01 23:18:06 +01:00
|
|
|
$this->handleAction($mail->getCleanTextBody(), $mail->getAttachments());
|
2011-05-16 21:31:18 +02:00
|
|
|
}
|
|
|
|
|
2012-11-01 23:18:06 +01:00
|
|
|
public function handleAction($body, array $attachments) {
|
2011-05-01 04:40:05 +02:00
|
|
|
// all commands start with a bang and separated from the body by a newline
|
|
|
|
// to make sure that actual feedback text couldn't trigger an action.
|
|
|
|
// unrecognized commands will be parsed as part of the comment.
|
|
|
|
$command = DifferentialAction::ACTION_COMMENT;
|
|
|
|
$supported_commands = $this->getSupportedCommands();
|
|
|
|
$regex = "/\A\n*!(" . implode('|', $supported_commands) . ")\n*/";
|
|
|
|
$matches = array();
|
|
|
|
if (preg_match($regex, $body, $matches)) {
|
|
|
|
$command = $matches[1];
|
|
|
|
$body = trim(str_replace('!' . $command, '', $body));
|
|
|
|
}
|
|
|
|
|
|
|
|
$actor = $this->getActor();
|
|
|
|
if (!$actor) {
|
|
|
|
throw new Exception('No actor is set for the reply action.');
|
|
|
|
}
|
|
|
|
|
2011-05-31 23:53:03 +02:00
|
|
|
switch ($command) {
|
|
|
|
case 'unsubscribe':
|
2014-02-25 00:57:26 +01:00
|
|
|
id(new PhabricatorSubscriptionsEditor())
|
|
|
|
->setObject($this->getMailReceiver())
|
|
|
|
->unsubscribe(array($actor->getPHID()))
|
|
|
|
->save();
|
2011-05-31 23:53:03 +02:00
|
|
|
// TODO: Send the user a confirmation email?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-11-01 23:18:06 +01:00
|
|
|
$body = $this->enhanceBodyWithAttachments($body, $attachments);
|
|
|
|
|
2014-03-07 17:10:18 +01:00
|
|
|
$xactions = array();
|
|
|
|
|
|
|
|
if ($command && ($command != DifferentialAction::ACTION_COMMENT)) {
|
|
|
|
$xactions[] = id(new DifferentialTransaction())
|
|
|
|
->setTransactionType(DifferentialTransaction::TYPE_ACTION)
|
|
|
|
->setNewValue($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($body)) {
|
|
|
|
$xactions[] = id(new DifferentialTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
|
|
|
|
->attachComment(
|
|
|
|
id(new DifferentialTransactionComment())
|
|
|
|
->setContent($body));
|
|
|
|
}
|
|
|
|
|
|
|
|
$editor = id(new DifferentialTransactionEditor())
|
|
|
|
->setActor($actor)
|
|
|
|
->setExcludeMailRecipientPHIDs($this->getExcludeMailRecipientPHIDs())
|
|
|
|
->setContinueOnMissingFields(true)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
|
|
|
// NOTE: We have to be careful about this because Facebook's
|
|
|
|
// implementation jumps straight into handleAction() and will not have
|
|
|
|
// a PhabricatorMetaMTAReceivedMail object.
|
|
|
|
if ($this->receivedMail) {
|
|
|
|
$content_source = PhabricatorContentSource::newForSource(
|
|
|
|
PhabricatorContentSource::SOURCE_EMAIL,
|
|
|
|
array(
|
|
|
|
'id' => $this->receivedMail->getID(),
|
|
|
|
));
|
|
|
|
$editor->setContentSource($content_source);
|
|
|
|
$editor->setParentMessageID($this->receivedMail->getMessageID());
|
|
|
|
} else {
|
|
|
|
$content_source = PhabricatorContentSource::newForSource(
|
|
|
|
PhabricatorContentSource::SOURCE_LEGACY,
|
|
|
|
array());
|
|
|
|
$editor->setContentSource($content_source);
|
|
|
|
}
|
|
|
|
|
When we fail to process mail, tell the user about it
Summary:
Ref T4371. Ref T4699. Fixes T3994.
Currently, we're very conservative about sending errors back to users. A concern I had about this was that mistakes could lead to email loops, massive amounts of email spam, etc. Because of this, I was pretty hesitant about replying to email with more email when I wrote this stuff.
However, this was a long time ago. We now have Message-ID deduplication, "X-Phabricator-Sent-This-Mail", generally better mail infrastructure, and rate limiting. Together, these mechanisms should reasonably prevent anything crazy (primarily, infinite email loops) from happening.
Thus:
- When we hit any processing error after receiving a mail, try to send the author a reply with details about what went wrong. These are limited to 6 per hour per address.
- Rewrite most of the errors to be more detailed and informative.
- Rewrite most of the errors in a user-facing voice ("You sent this mail..." instead of "This mail was sent..").
- Remove the redundant, less sophisticated code which does something similar in Differential.
Test Plan:
- Using `scripts/mail/mail_receiver.php`, artificially received a pile of mail.
- Hit a bunch of different errors.
- Saw reasonable error mail get sent to me.
- Saw other reasonable error mail get rate limited.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T3994, T4371, T4699
Differential Revision: https://secure.phabricator.com/D8692
2014-04-04 03:43:18 +02:00
|
|
|
$editor->applyTransactions($this->getMailReceiver(), $xactions);
|
2011-05-01 04:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|