forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifferentialCreateMailReceiver.php
120 lines (109 loc) · 3.39 KB
/
DifferentialCreateMailReceiver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
final class DifferentialCreateMailReceiver
extends PhabricatorApplicationMailReceiver {
protected function newApplication() {
return new PhabricatorDifferentialApplication();
}
protected function processReceivedMail(
PhabricatorMetaMTAReceivedMail $mail,
PhutilEmailAddress $target) {
$author = $this->getAuthor();
$attachments = $mail->getAttachments();
$files = array();
$errors = array();
if ($attachments) {
$files = id(new PhabricatorFileQuery())
->setViewer($author)
->withPHIDs($attachments)
->execute();
foreach ($files as $index => $file) {
if ($file->getMimeType() != 'text/plain') {
$errors[] = pht(
'Could not parse file %s; only files with mimetype text/plain '.
'can be parsed via email.',
$file->getName());
unset($files[$index]);
}
}
}
$diffs = array();
foreach ($files as $file) {
$call = new ConduitCall(
'differential.createrawdiff',
array(
'diff' => $file->loadFileData(),
));
$call->setUser($author);
try {
$result = $call->execute();
$diffs[$file->getName()] = $result['uri'];
} catch (Exception $e) {
$errors[] = pht(
'Could not parse attachment %s; only attachments (and mail bodies) '.
'generated via "diff" commands can be parsed.',
$file->getName());
}
}
$body = $mail->getCleanTextBody();
if ($body) {
$call = new ConduitCall(
'differential.createrawdiff',
array(
'diff' => $body,
));
$call->setUser($author);
try {
$result = $call->execute();
$diffs[pht('Mail Body')] = $result['uri'];
} catch (Exception $e) {
$errors[] = pht(
'Could not parse mail body; only mail bodies (and attachments) '.
'generated via "diff" commands can be parsed.');
}
}
$subject_prefix = pht('[Differential]');
if (count($diffs)) {
$subject = pht(
'You successfully created %d diff(s).',
count($diffs));
} else {
$subject = pht(
'Diff creation failed; see body for %s error(s).',
phutil_count($errors));
}
$body = new PhabricatorMetaMTAMailBody();
$body->addRawSection($subject);
if (count($diffs)) {
$text_body = '';
$html_body = array();
$body_label = pht('%s DIFF LINK(S)', phutil_count($diffs));
foreach ($diffs as $filename => $diff_uri) {
$text_body .= $filename.': '.$diff_uri."\n";
$html_body[] = phutil_tag(
'a',
array(
'href' => $diff_uri,
),
$filename);
$html_body[] = phutil_tag('br');
}
$body->addTextSection($body_label, $text_body);
$body->addHTMLSection($body_label, $html_body);
}
if (count($errors)) {
$body_section = new PhabricatorMetaMTAMailSection();
$body_label = pht('%s ERROR(S)', phutil_count($errors));
foreach ($errors as $error) {
$body_section->addFragment($error);
}
$body->addTextSection($body_label, $body_section);
}
id(new PhabricatorMetaMTAMail())
->addTos(array($author->getPHID()))
->setSubject($subject)
->setSubjectPrefix($subject_prefix)
->setFrom($author->getPHID())
->setBody($body->render())
->saveAndSend();
}
}