From 2ee1f8cb4ed29aebad42c89f6321558c08780c87 Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Tue, 30 Jul 2013 13:26:55 -0700 Subject: [PATCH] Add some create mail handlers for paste and files Summary: Fixes T1144. Though actually I think T1144 wanted some handy way to email from the command-line / arc, this is cooler. :D Test Plan: set conf properly and then ./bin/mail receive-test --as btrahan --to pasties@phabricator.dev | README --> it worked...! couldn't test files as easily but verified exception thrown when I tried to test. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T1144 Differential Revision: https://secure.phabricator.com/D6622 --- src/__phutil_library_map__.php | 6 ++ .../PhabricatorConfigEditController.php | 1 + .../config/PhabricatorFilesConfigOptions.php | 5 ++ .../files/mail/FilesCreateMailReceiver.php | 71 ++++++++++++++++++ .../config/PhabricatorPasteConfigOptions.php | 27 +++++++ .../paste/mail/PasteCreateMailReceiver.php | 72 +++++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 src/applications/files/mail/FilesCreateMailReceiver.php create mode 100644 src/applications/paste/config/PhabricatorPasteConfigOptions.php create mode 100644 src/applications/paste/mail/PasteCreateMailReceiver.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index aeda184aa6..32d0dde37d 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -589,6 +589,7 @@ phutil_register_library_map(array( 'FeedPublisherHTTPWorker' => 'applications/feed/worker/FeedPublisherHTTPWorker.php', 'FeedPublisherWorker' => 'applications/feed/worker/FeedPublisherWorker.php', 'FeedPushWorker' => 'applications/feed/worker/FeedPushWorker.php', + 'FilesCreateMailReceiver' => 'applications/files/mail/FilesCreateMailReceiver.php', 'HarbormasterDAO' => 'applications/harbormaster/storage/HarbormasterDAO.php', 'HarbormasterObject' => 'applications/harbormaster/storage/HarbormasterObject.php', 'HarbormasterRunnerWorker' => 'applications/harbormaster/worker/HarbormasterRunnerWorker.php', @@ -756,6 +757,7 @@ phutil_register_library_map(array( 'PackageDeleteMail' => 'applications/owners/mail/PackageDeleteMail.php', 'PackageMail' => 'applications/owners/mail/PackageMail.php', 'PackageModifyMail' => 'applications/owners/mail/PackageModifyMail.php', + 'PasteCreateMailReceiver' => 'applications/paste/mail/PasteCreateMailReceiver.php', 'PasteEmbedView' => 'applications/paste/view/PasteEmbedView.php', 'Phabricator404Controller' => 'applications/base/controller/Phabricator404Controller.php', 'PhabricatorAWSConfigOptions' => 'applications/config/option/PhabricatorAWSConfigOptions.php', @@ -1362,6 +1364,7 @@ phutil_register_library_map(array( 'PhabricatorPHPMailerConfigOptions' => 'applications/config/option/PhabricatorPHPMailerConfigOptions.php', 'PhabricatorPagedFormExample' => 'applications/uiexample/examples/PhabricatorPagedFormExample.php', 'PhabricatorPaste' => 'applications/paste/storage/PhabricatorPaste.php', + 'PhabricatorPasteConfigOptions' => 'applications/paste/config/PhabricatorPasteConfigOptions.php', 'PhabricatorPasteController' => 'applications/paste/controller/PhabricatorPasteController.php', 'PhabricatorPasteDAO' => 'applications/paste/storage/PhabricatorPasteDAO.php', 'PhabricatorPasteEditController' => 'applications/paste/controller/PhabricatorPasteEditController.php', @@ -2591,6 +2594,7 @@ phutil_register_library_map(array( 'FeedPublisherHTTPWorker' => 'FeedPushWorker', 'FeedPublisherWorker' => 'FeedPushWorker', 'FeedPushWorker' => 'PhabricatorWorker', + 'FilesCreateMailReceiver' => 'PhabricatorMailReceiver', 'HarbormasterDAO' => 'PhabricatorLiskDAO', 'HarbormasterObject' => 'HarbormasterDAO', 'HarbormasterRunnerWorker' => 'PhabricatorWorker', @@ -2761,6 +2765,7 @@ phutil_register_library_map(array( 'PackageDeleteMail' => 'PackageMail', 'PackageMail' => 'PhabricatorMail', 'PackageModifyMail' => 'PackageMail', + 'PasteCreateMailReceiver' => 'PhabricatorMailReceiver', 'PasteEmbedView' => 'AphrontView', 'Phabricator404Controller' => 'PhabricatorController', 'PhabricatorAWSConfigOptions' => 'PhabricatorApplicationConfigOptions', @@ -3401,6 +3406,7 @@ phutil_register_library_map(array( 1 => 'PhabricatorTokenReceiverInterface', 2 => 'PhabricatorPolicyInterface', ), + 'PhabricatorPasteConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorPasteController' => 'PhabricatorController', 'PhabricatorPasteDAO' => 'PhabricatorLiskDAO', 'PhabricatorPasteEditController' => 'PhabricatorPasteController', diff --git a/src/applications/config/controller/PhabricatorConfigEditController.php b/src/applications/config/controller/PhabricatorConfigEditController.php index 0d7736d7a6..d34a635b73 100644 --- a/src/applications/config/controller/PhabricatorConfigEditController.php +++ b/src/applications/config/controller/PhabricatorConfigEditController.php @@ -64,6 +64,7 @@ final class PhabricatorConfigEditController ->setConfigKey($this->key) ->setNamespace('default') ->setIsDeleted(true); + $config_entry->setPHID($config_entry->generatePHID()); } $e_value = null; diff --git a/src/applications/files/config/PhabricatorFilesConfigOptions.php b/src/applications/files/config/PhabricatorFilesConfigOptions.php index 2db9a328ad..1d3b57ad14 100644 --- a/src/applications/files/config/PhabricatorFilesConfigOptions.php +++ b/src/applications/files/config/PhabricatorFilesConfigOptions.php @@ -154,6 +154,11 @@ final class PhabricatorFilesConfigOptions "Specify this limit in bytes, or using a 'K', 'M', or 'G' ". "suffix.")) ->addExample('10M', pht("Allow Uploads 10MB or Smaller")), + $this->newOption( + 'metamta.files.public-create-email', + 'string', + null) + ->setDescription(pht('Allow uploaded files via email.')), $this->newOption('files.enable-imagemagick', 'bool', false) ->setBoolOptions( array( diff --git a/src/applications/files/mail/FilesCreateMailReceiver.php b/src/applications/files/mail/FilesCreateMailReceiver.php new file mode 100644 index 0000000000..c477fb0577 --- /dev/null +++ b/src/applications/files/mail/FilesCreateMailReceiver.php @@ -0,0 +1,71 @@ +getToAddresses() as $to_address) { + if ($this->matchAddresses($create_address, $to_address)) { + return true; + } + } + + return false; + } + + protected function processReceivedMail( + PhabricatorMetaMTAReceivedMail $mail, + PhabricatorUser $sender) { + + $attachment_phids = $mail->getAttachments(); + if (empty($attachment_phids)) { + throw new PhabricatorMetaMTAReceivedMailProcessingException( + MetaMTAReceivedMailStatus::STATUS_UNHANDLED_EXCEPTION, + 'Ignoring email to create files that did not include attachments.'); + } + $first_phid = head($attachment_phids); + $mail->setRelatedPHID($first_phid); + + $attachment_count = count($attachment_phids); + if ($attachment_count > 1) { + $subject = pht( + 'You successfully uploaded %d files.', + $attachment_count); + } else { + $subject = pht('You successfully uploaded a file.'); + } + + $file_uris = array(); + foreach ($attachment_phids as $phid) { + $file_uris[] = + PhabricatorEnv::getProductionURI('/file/info/'.$phid.'/'); + } + + $body = new PhabricatorMetaMTAMailBody(); + $body->addRawSection($subject); + $body->addTextSection(pht('FILE LINKS'), implode("\n", $file_uris)); + + id(new PhabricatorMetaMTAMail()) + ->addTos(array($sender->getPHID())) + ->setSubject('[Files] '.$subject) + ->setFrom($sender->getPHID()) + ->setRelatedPHID($first_phid) + ->setBody($body->render()) + ->saveAndSend(); + } + +} diff --git a/src/applications/paste/config/PhabricatorPasteConfigOptions.php b/src/applications/paste/config/PhabricatorPasteConfigOptions.php new file mode 100644 index 0000000000..311cee73e1 --- /dev/null +++ b/src/applications/paste/config/PhabricatorPasteConfigOptions.php @@ -0,0 +1,27 @@ +newOption( + 'metamta.paste.public-create-email', + 'string', + null) + ->setDescription(pht('Allow creating pastes via email.')) + ); + } + +} diff --git a/src/applications/paste/mail/PasteCreateMailReceiver.php b/src/applications/paste/mail/PasteCreateMailReceiver.php new file mode 100644 index 0000000000..ef8b0236e5 --- /dev/null +++ b/src/applications/paste/mail/PasteCreateMailReceiver.php @@ -0,0 +1,72 @@ +getToAddresses() as $to_address) { + if ($this->matchAddresses($create_address, $to_address)) { + return true; + } + } + + return false; + } + + protected function processReceivedMail( + PhabricatorMetaMTAReceivedMail $mail, + PhabricatorUser $sender) { + + $title = $mail->getSubject(); + if (!$title) { + $title = pht('Pasted via email.'); + } + $paste_file = PhabricatorFile::newFromFileData( + $mail->getCleanTextBody(), + array( + 'name' => $title, + 'mime-type' => 'text/plain; charset=utf-8', + 'authorPHID' => $sender->getPHID(), + )); + + $paste = id(new PhabricatorPaste()) + ->setAuthorPHID($sender->getPHID()) + ->setTitle($title) + ->setFilePHID($paste_file->getPHID()) + ->setLanguage('') // auto-detect + ->setViewPolicy(PhabricatorPolicies::POLICY_USER) + ->save(); + + $mail->setRelatedPHID($paste->getPHID()); + + $subject = pht('You successfully created a paste.'); + $paste_uri = PhabricatorEnv::getProductionURI($paste->getURI()); + $body = new PhabricatorMetaMTAMailBody(); + $body->addRawSection($subject); + $body->addTextSection(pht('PASTE LINK'), $paste_uri); + + id(new PhabricatorMetaMTAMail()) + ->addTos(array($sender->getPHID())) + ->setSubject('[Paste] '.$subject) + ->setFrom($sender->getPHID()) + ->setRelatedPHID($paste->getPHID()) + ->setBody($body->render()) + ->saveAndSend(); + + } + +}