2011-05-01 04:40:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-14 07:22:27 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-05-01 04:40:05 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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)) {
|
|
|
|
throw new Exception("Receiver is not a DifferentialRevision!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
$text .= 'Reply to comment';
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2011-05-10 01:31:26 +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
|
|
|
}
|
|
|
|
|
2011-05-16 21:31:18 +02:00
|
|
|
public function receiveEmail(PhabricatorMetaMTAReceivedMail $mail) {
|
2011-06-22 21:41:19 +02:00
|
|
|
$this->receivedMail = $mail;
|
2011-05-16 21:31:18 +02:00
|
|
|
$this->handleAction($mail->getCleanTextBody());
|
|
|
|
}
|
|
|
|
|
2011-05-01 04:40:05 +02:00
|
|
|
public function handleAction($body) {
|
|
|
|
// 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':
|
|
|
|
$this->unsubscribeUser($this->getMailReceiver(), $actor);
|
|
|
|
// TODO: Send the user a confirmation email?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-05-01 04:40:05 +02:00
|
|
|
try {
|
|
|
|
$editor = new DifferentialCommentEditor(
|
2011-05-12 18:01:16 +02:00
|
|
|
$this->getMailReceiver(),
|
2011-05-01 04:40:05 +02:00
|
|
|
$actor->getPHID(),
|
|
|
|
$command);
|
|
|
|
|
2011-06-22 21:41:19 +02:00
|
|
|
// 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) {
|
Track content sources (email, web, conduit, mobile) for replies
Summary:
When an object is updated, record the content source for the update. This mostly
isn't terribly useful but one concrete thing I want to do with it is let admins
audit via-email replies more easily since there are a bunch of options which let
you do hyjinx if you intentionally configure them insecurely. I think having a
little more auditability around this feature is generally good. At some point
I'm going to turn this into a link admins can click to see details.
It also allows us to see how frequently different mechanisms are used, and lets
you see if someone is at their desk or on a mobile or whatever, at least
indirectly.
The "tablet" and "mobile" sources are currently unused but I figured I'd throw
them in anyway. SMS support should definitely happen at some point.
Not 100% sure about the design for this, I might change it to plain text at some
point.
Test Plan: Updated objects and saw update sources rendered.
Reviewers: jungejason, tuomaspelkonen, aran
Reviewed By: jungejason
CC: aran, epriestley, jungejason
Differential Revision: 844
2011-08-22 19:25:45 +02:00
|
|
|
$content_source = PhabricatorContentSource::newForSource(
|
|
|
|
PhabricatorContentSource::SOURCE_EMAIL,
|
|
|
|
array(
|
|
|
|
'id' => $this->receivedMail->getID(),
|
|
|
|
));
|
|
|
|
$editor->setContentSource($content_source);
|
2011-06-22 21:41:19 +02:00
|
|
|
$editor->setParentMessageID($this->receivedMail->getMessageID());
|
|
|
|
}
|
2011-05-01 04:40:05 +02:00
|
|
|
$editor->setMessage($body);
|
|
|
|
$comment = $editor->save();
|
|
|
|
|
|
|
|
return $comment->getID();
|
|
|
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$exception_mail = new DifferentialExceptionMail(
|
2011-05-12 18:01:16 +02:00
|
|
|
$this->getMailReceiver(),
|
2011-05-01 04:40:05 +02:00
|
|
|
$ex,
|
2012-07-12 22:33:26 +02:00
|
|
|
$this->receivedMail->getRawTextBody());
|
2011-05-01 04:40:05 +02:00
|
|
|
|
|
|
|
$exception_mail->setToPHIDs(array($this->getActor()->getPHID()));
|
|
|
|
$exception_mail->send();
|
|
|
|
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 23:53:03 +02:00
|
|
|
private function unsubscribeUser(
|
|
|
|
DifferentialRevision $revision,
|
|
|
|
PhabricatorUser $user) {
|
|
|
|
|
|
|
|
$revision->loadRelationships();
|
|
|
|
DifferentialRevisionEditor::removeCCAndUpdateRevision(
|
|
|
|
$revision,
|
|
|
|
$user->getPHID(),
|
|
|
|
$user->getPHID());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 04:40:05 +02:00
|
|
|
}
|