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
|
import { basename } from "@std/path";
import { getConfigValue } from "./config.ts";
import logger, { log } from "./logger.ts";
import {
Mail,
MailDeliverContext,
MailDeliverer,
MailDeliverReceiptResult,
ReceiptsFromHeadersHook,
} from "./mail.ts";
export class DovecotMailDeliverer extends MailDeliverer {
readonly name = "dovecot";
constructor() {
super();
this.preHooks.push(
new ReceiptsFromHeadersHook(),
);
}
protected override async doDeliver(
mail: Mail,
context: MailDeliverContext,
): Promise<void> {
const ldaPath = getConfigValue("ldaPath");
const ldaBinName = basename(ldaPath);
const utf8Stream = mail.toUtf8Bytes();
const receipts = [...context.receipts];
if (receipts.length === 0) {
throw new Error("No receipts found.");
}
log(`Deliver to ${receipts.join(", ")}.`);
for (const receipt of receipts) {
log(`Call ${ldaBinName} for ${receipt}...`);
const result: MailDeliverReceiptResult = {
kind: "done",
message: `${ldaBinName} exited with success.`,
};
try {
const ldaCommand = new Deno.Command(ldaPath, {
args: ["-d", receipt],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const ldaProcess = ldaCommand.spawn();
using logFiles = await logger.openLogForProgram(ldaBinName);
ldaProcess.stdout.pipeTo(logFiles.stdout.writable);
ldaProcess.stderr.pipeTo(logFiles.stderr.writable);
const stdinWriter = ldaProcess.stdin.getWriter();
await stdinWriter.write(utf8Stream);
await stdinWriter.close();
const status = await ldaProcess.status;
if (!status.success) {
result.kind = "fail";
result.message =
`${ldaBinName} exited with error code ${status.code}`;
if (status.signal != null) {
result.message += ` (signal ${status.signal})`;
}
// https://doc.dovecot.org/main/core/man/dovecot-lda.1.html
switch (status.code) {
case 67:
result.message += ", receipt user not known";
break;
case 75:
result.kind = "retry";
break;
}
result.message += ".";
}
} catch (e) {
result.kind = "fail";
result.message = "An error was thrown when running lda process: " + e;
result.cause = e;
}
context.result.set(receipt, result);
}
}
}
|