2011-05-10 01:31:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-07-04 22:04:22 +02:00
|
|
|
/**
|
|
|
|
* @group maniphest
|
|
|
|
*/
|
2011-05-10 01:31:26 +02:00
|
|
|
class ManiphestReplyHandler extends PhabricatorMailReplyHandler {
|
|
|
|
|
|
|
|
public function validateMailReceiver($mail_receiver) {
|
|
|
|
if (!($mail_receiver instanceof ManiphestTask)) {
|
|
|
|
throw new Exception("Mail receiver is not a ManiphestTask!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrivateReplyHandlerEmailAddress(
|
|
|
|
PhabricatorObjectHandle $handle) {
|
|
|
|
return $this->getDefaultPrivateReplyHandlerEmailAddress($handle, 'T');
|
|
|
|
}
|
|
|
|
|
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('T');
|
|
|
|
}
|
|
|
|
|
2011-05-10 01:31:26 +02:00
|
|
|
public function getReplyHandlerDomain() {
|
|
|
|
return PhabricatorEnv::getEnvConfig(
|
|
|
|
'metamta.maniphest.reply-handler-domain');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReplyHandlerInstructions() {
|
|
|
|
if ($this->supportsReplies()) {
|
2011-05-16 21:31:18 +02:00
|
|
|
return "Reply to comment or attach files, or !close, !claim, or ".
|
|
|
|
"!unsubscribe.";
|
2011-05-10 01:31:26 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 21:31:18 +02:00
|
|
|
public function receiveEmail(PhabricatorMetaMTAReceivedMail $mail) {
|
|
|
|
|
2011-07-04 18:45:42 +02:00
|
|
|
// NOTE: We'll drop in here on both the "reply to a task" and "create a
|
|
|
|
// new task" workflows! Make sure you test both if you make changes!
|
|
|
|
|
2011-05-16 21:31:18 +02:00
|
|
|
$task = $this->getMailReceiver();
|
2011-07-04 18:45:42 +02:00
|
|
|
|
|
|
|
$is_new_task = !$task->getID();
|
|
|
|
|
2011-05-16 21:31:18 +02:00
|
|
|
$user = $this->getActor();
|
|
|
|
|
|
|
|
$body = $mail->getCleanTextBody();
|
|
|
|
$body = trim($body);
|
|
|
|
|
2011-07-04 18:45:42 +02:00
|
|
|
$xactions = array();
|
|
|
|
|
|
|
|
$template = new ManiphestTransaction();
|
|
|
|
$template->setAuthorPHID($user->getPHID());
|
|
|
|
|
|
|
|
if ($is_new_task) {
|
|
|
|
// If this is a new task, create a "User created this task." transaction
|
|
|
|
// and then set the title and description.
|
|
|
|
$xaction = clone $template;
|
|
|
|
$xaction->setTransactionType(ManiphestTransactionType::TYPE_STATUS);
|
|
|
|
$xaction->setNewValue(ManiphestTaskStatus::STATUS_OPEN);
|
|
|
|
$xactions[] = $xaction;
|
2011-05-16 21:31:18 +02:00
|
|
|
|
2011-07-04 18:45:42 +02:00
|
|
|
$task->setAuthorPHID($user->getPHID());
|
|
|
|
$task->setTitle(nonempty($mail->getSubject(), 'Untitled Task'));
|
|
|
|
$task->setDescription($body);
|
|
|
|
|
|
|
|
} 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];
|
|
|
|
}
|
2011-05-16 21:31:18 +02:00
|
|
|
|
2011-07-04 18:45:42 +02:00
|
|
|
$ttype = ManiphestTransactionType::TYPE_NONE;
|
|
|
|
$new_value = null;
|
|
|
|
switch ($command) {
|
|
|
|
case 'close':
|
|
|
|
$ttype = ManiphestTransactionType::TYPE_STATUS;
|
|
|
|
$new_value = ManiphestTaskStatus::STATUS_CLOSED_RESOLVED;
|
|
|
|
break;
|
|
|
|
case 'claim':
|
|
|
|
$ttype = ManiphestTransactionType::TYPE_OWNER;
|
|
|
|
$new_value = $user->getPHID();
|
|
|
|
break;
|
|
|
|
case 'unsubscribe':
|
|
|
|
$ttype = ManiphestTransactionType::TYPE_CCS;
|
|
|
|
$ccs = $task->getCCPHIDs();
|
|
|
|
foreach ($ccs as $k => $phid) {
|
|
|
|
if ($phid == $user->getPHID()) {
|
|
|
|
unset($ccs[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$new_value = array_values($ccs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xaction = clone $template;
|
|
|
|
$xaction->setTransactionType($ttype);
|
|
|
|
$xaction->setNewValue($new_value);
|
|
|
|
$xaction->setComments($body);
|
|
|
|
|
|
|
|
$xactions[] = $xaction;
|
2011-05-16 21:31:18 +02:00
|
|
|
}
|
|
|
|
|
2011-07-04 18:45:42 +02:00
|
|
|
// TODO: We should look at CCs on the mail and add them as CCs.
|
2011-05-29 12:19:06 +02:00
|
|
|
|
|
|
|
$files = $mail->getAttachments();
|
|
|
|
if ($files) {
|
2011-07-04 18:45:42 +02:00
|
|
|
$file_xaction = clone $template;
|
2011-05-29 12:19:06 +02:00
|
|
|
$file_xaction->setTransactionType(ManiphestTransactionType::TYPE_ATTACH);
|
|
|
|
|
|
|
|
$phid_type = PhabricatorPHIDConstants::PHID_TYPE_FILE;
|
|
|
|
$new = $task->getAttached();
|
|
|
|
foreach ($files as $file_phid) {
|
|
|
|
$new[$phid_type][$file_phid] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_xaction->setNewValue($new);
|
|
|
|
$xactions[] = $file_xaction;
|
|
|
|
}
|
|
|
|
|
2011-05-16 21:31:18 +02:00
|
|
|
$editor = new ManiphestTransactionEditor();
|
2011-06-22 21:41:19 +02:00
|
|
|
$editor->setParentMessageID($mail->getMessageID());
|
2011-05-29 12:19:06 +02:00
|
|
|
$editor->applyTransactions($task, $xactions);
|
2011-05-16 21:31:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 01:31:26 +02:00
|
|
|
}
|