Path: blob/master/src/applications/differential/mail/DifferentialCreateMailReceiver.php
12262 views
<?php12final class DifferentialCreateMailReceiver3extends PhabricatorApplicationMailReceiver {45protected function newApplication() {6return new PhabricatorDifferentialApplication();7}89protected function processReceivedMail(10PhabricatorMetaMTAReceivedMail $mail,11PhutilEmailAddress $target) {1213$author = $this->getAuthor();1415$attachments = $mail->getAttachments();16$files = array();17$errors = array();18if ($attachments) {19$files = id(new PhabricatorFileQuery())20->setViewer($author)21->withPHIDs($attachments)22->execute();23foreach ($files as $index => $file) {24if ($file->getMimeType() != 'text/plain') {25$errors[] = pht(26'Could not parse file %s; only files with mimetype text/plain '.27'can be parsed via email.',28$file->getName());29unset($files[$index]);30}31}32}3334$diffs = array();35foreach ($files as $file) {36$call = new ConduitCall(37'differential.createrawdiff',38array(39'diff' => $file->loadFileData(),40));41$call->setUser($author);42try {43$result = $call->execute();44$diffs[$file->getName()] = $result['uri'];45} catch (Exception $e) {46$errors[] = pht(47'Could not parse attachment %s; only attachments (and mail bodies) '.48'generated via "diff" commands can be parsed.',49$file->getName());50}51}5253$body = $mail->getCleanTextBody();54if ($body) {55$call = new ConduitCall(56'differential.createrawdiff',57array(58'diff' => $body,59));60$call->setUser($author);61try {62$result = $call->execute();63$diffs[pht('Mail Body')] = $result['uri'];64} catch (Exception $e) {65$errors[] = pht(66'Could not parse mail body; only mail bodies (and attachments) '.67'generated via "diff" commands can be parsed.');68}69}7071$subject_prefix = pht('[Differential]');72if (count($diffs)) {73$subject = pht(74'You successfully created %d diff(s).',75count($diffs));76} else {77$subject = pht(78'Diff creation failed; see body for %s error(s).',79phutil_count($errors));80}81$body = new PhabricatorMetaMTAMailBody();82$body->addRawSection($subject);83if (count($diffs)) {84$text_body = '';85$html_body = array();86$body_label = pht('%s DIFF LINK(S)', phutil_count($diffs));87foreach ($diffs as $filename => $diff_uri) {88$text_body .= $filename.': '.$diff_uri."\n";89$html_body[] = phutil_tag(90'a',91array(92'href' => $diff_uri,93),94$filename);95$html_body[] = phutil_tag('br');96}97$body->addTextSection($body_label, $text_body);98$body->addHTMLSection($body_label, $html_body);99}100101if (count($errors)) {102$body_section = new PhabricatorMetaMTAMailSection();103$body_label = pht('%s ERROR(S)', phutil_count($errors));104foreach ($errors as $error) {105$body_section->addFragment($error);106}107$body->addTextSection($body_label, $body_section);108}109110id(new PhabricatorMetaMTAMail())111->addTos(array($author->getPHID()))112->setSubject($subject)113->setSubjectPrefix($subject_prefix)114->setFrom($author->getPHID())115->setBody($body->render())116->saveAndSend();117}118119}120121122