@pmndre/pine 0.22.0 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/changes.json +265 -0
- package/docs/SKILL.md +31 -7
- package/docs/references/agents.md +245 -21
- package/docs/references/coding.md +1 -1
- package/docs/references/computer.md +1 -1
- package/docs/references/configuration.md +14 -5
- package/docs/references/mcp.md +9 -0
- package/docs/references/operations.md +20 -9
- package/docs/references/overview.md +3 -1
- package/docs/references/security.md +83 -24
- package/docs/references/setup.md +55 -9
- package/docs/references/source-types.md +1418 -0
- package/docs/references/sources.md +333 -19
- package/docs/references/stack.md +1 -0
- package/docs/references/tui.md +86 -12
- package/examples/home/AGENTS.md +4 -0
- package/examples/home/TOOLS.md +5 -0
- package/examples/home/agents/main/workspace/AGENTS.md +1 -1
- package/examples/home/agents/main/workspace/TOOLS.md +5 -0
- package/mcp/browser.d.ts +6 -0
- package/mcp/browser.js +19 -0
- package/mcp/render.d.ts +1 -0
- package/mcp/render.js +237 -0
- package/package.json +3 -1
- package/src/actions.d.ts +6 -1
- package/src/actions.js +17 -4
- package/src/admin.d.ts +4 -0
- package/src/admin.js +25 -6
- package/src/agents.d.ts +167 -2
- package/src/agents.js +912 -45
- package/src/catalogue.d.ts +60 -0
- package/src/catalogue.js +791 -0
- package/src/cli.js +10 -7
- package/src/config.d.ts +53 -4
- package/src/config.js +30 -5
- package/src/goals.d.ts +1 -1
- package/src/goals.js +14 -1
- package/src/manual.js +24 -2
- package/src/mime.d.ts +78 -0
- package/src/mime.js +342 -0
- package/src/net.d.ts +11 -0
- package/src/net.js +29 -2
- package/src/pine.js +10 -1
- package/src/router.js +3 -3
- package/src/sandbox-linux.d.ts +3 -1
- package/src/sandbox-linux.js +5 -0
- package/src/security.d.ts +22 -6
- package/src/security.js +44 -13
- package/src/slots.d.ts +106 -0
- package/src/slots.js +250 -0
- package/src/source-types.d.ts +20 -0
- package/src/sources/calendar.d.ts +24 -0
- package/src/sources/calendar.js +265 -0
- package/src/sources/command.js +109 -4
- package/src/sources/discord.d.ts +4 -0
- package/src/sources/discord.js +298 -0
- package/src/sources/feed.d.ts +19 -0
- package/src/sources/feed.js +257 -0
- package/src/sources/http.js +154 -103
- package/src/sources/mail.d.ts +51 -0
- package/src/sources/mail.js +1013 -0
- package/src/sources/matrix.d.ts +4 -0
- package/src/sources/matrix.js +232 -0
- package/src/sources/modbus.d.ts +15 -0
- package/src/sources/modbus.js +240 -0
- package/src/sources/onvif.d.ts +35 -0
- package/src/sources/onvif.js +326 -0
- package/src/sources/presence.d.ts +80 -0
- package/src/sources/presence.js +480 -0
- package/src/sources/serial.d.ts +21 -0
- package/src/sources/serial.js +220 -0
- package/src/sources/slack.d.ts +16 -0
- package/src/sources/slack.js +1062 -0
- package/src/sources/smtp.d.ts +4 -0
- package/src/sources/smtp.js +360 -0
- package/src/sources/stream.d.ts +68 -0
- package/src/sources/stream.js +534 -0
- package/src/sources/syslog.d.ts +32 -0
- package/src/sources/syslog.js +243 -0
- package/src/sources/system.d.ts +14 -0
- package/src/sources/system.js +236 -0
- package/src/sources/system.swift +102 -0
- package/src/sources/telegram.js +34 -1
- package/src/sources.d.ts +41 -1
- package/src/sources.js +147 -24
- package/src/tools.d.ts +1 -1
- package/src/tools.js +304 -23
- package/src/tui/app.d.ts +35 -2
- package/src/tui/app.js +306 -43
- package/src/tui/levels.d.ts +68 -0
- package/src/tui/levels.js +273 -0
- package/src/tui/picture.d.ts +17 -1
- package/src/tui/picture.js +72 -5
- package/src/types.d.ts +3 -0
- package/src/update.d.ts +9 -1
- package/src/update.js +75 -3
- package/src/watching.d.ts +156 -0
- package/src/watching.js +609 -4
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { keepFiles, parseMail, pruneFiles } from "../mime.js";
|
|
6
|
+
import { timingSafeEqual } from "../security.js";
|
|
7
|
+
import { errText } from "../util.js";
|
|
8
|
+
import { capped, clip } from "../watching.js";
|
|
9
|
+
/**
|
|
10
|
+
* A mail drop: a small mail server that accepts messages and nothing else. For devices and
|
|
11
|
+
* programs whose only way of reporting is "send an e-mail": a NAS, a printer or scanner, a UPS,
|
|
12
|
+
* a camera, a router, cron on a server.
|
|
13
|
+
*
|
|
14
|
+
* options: { port, user?, password?, host?, allow?, to?, maxMb?, keepDays? }
|
|
15
|
+
* port, user, password what you enter in the device. The password must have 8 characters or more.
|
|
16
|
+
* allow only these client addresses, as prefixes: ["192.168.1.40", "192.168.2."]. With it
|
|
17
|
+
* and without a password, no login is asked for (for devices that cannot log in)
|
|
18
|
+
* host address to listen on. Default "0.0.0.0": devices on your network have to reach it.
|
|
19
|
+
* to recipient addresses it accepts; mail for any other is refused. Default: any
|
|
20
|
+
* maxMb largest message accepted (default 10)
|
|
21
|
+
* keepDays received attachments are deleted after this many days (default 7)
|
|
22
|
+
*
|
|
23
|
+
* event "mail": { from, fromName?, to, subject, text, attachments: [{ file, path?, type, size }], images?, client }
|
|
24
|
+
*
|
|
25
|
+
* It speaks plain SMTP, as these devices do: the password and the mail cross your network
|
|
26
|
+
* unencrypted. It only receives. Nothing is passed on to anybody (it is no relay), nothing can be
|
|
27
|
+
* read back, and every attachment lands under a name of pine's choosing inside the source's own folder.
|
|
28
|
+
*/
|
|
29
|
+
/** How many messages a minute are taken; a device that sends more is told to try again later. */
|
|
30
|
+
const PER_MINUTE = 30;
|
|
31
|
+
const LINE_MAX = 4096;
|
|
32
|
+
const ADDRESS = /^[^\s@<>,;"]+@[^\s@<>,;"]+$/;
|
|
33
|
+
export const type = {
|
|
34
|
+
summary: "a mail drop: a small mail server that only accepts. For a NAS, printer, scanner, UPS, camera or server whose only way of reporting is \"send an e-mail\"",
|
|
35
|
+
options: Type.Object({
|
|
36
|
+
port: Type.Number({ description: "port to listen on, e.g. 2525" }),
|
|
37
|
+
user: Type.Optional(Type.String({ description: 'user name the device logs in with. Default "device"' })),
|
|
38
|
+
password: Type.Optional(Type.String({ description: 'password the device logs in with, 8 characters or more. Leave it out and pine makes one (or, with "allow", no login is asked for)', secret: true })),
|
|
39
|
+
allow: Type.Optional(Type.Array(Type.String(), { description: 'only these client addresses, as prefixes: ["192.168.1.40", "192.168.2."]. For a device that cannot log in, give its address here and no password' })),
|
|
40
|
+
host: Type.Optional(Type.String({ description: 'address to listen on. Default "0.0.0.0", so that devices on the network reach it' })),
|
|
41
|
+
to: Type.Optional(Type.Array(Type.String(), { description: 'recipient addresses it accepts, e.g. ["alerts@pine.home"]; mail for any other is refused. Default: any recipient' })),
|
|
42
|
+
maxMb: Type.Optional(Type.Number({ description: "largest message accepted, in MB. Default 10" })),
|
|
43
|
+
keepDays: Type.Optional(Type.Number({ description: "received attachments are deleted after this many days. Default 7" })),
|
|
44
|
+
}, { additionalProperties: false }),
|
|
45
|
+
plan(o, setup) {
|
|
46
|
+
if (!Number.isInteger(o.port) || o.port < 1 || o.port > 65535)
|
|
47
|
+
throw new Error(`"port" must be a port number, for example 2525`);
|
|
48
|
+
if (o.password !== undefined && String(o.password).length < 8)
|
|
49
|
+
throw new Error("the password must have 8 characters or more");
|
|
50
|
+
const allow = (Array.isArray(o.allow) ? o.allow : []).map((a) => String(a).trim()).filter(Boolean);
|
|
51
|
+
for (const a of allow)
|
|
52
|
+
if (!/^[0-9a-fA-F.:]+$/.test(a))
|
|
53
|
+
throw new Error(`"${a}" in "allow" is not an address or the beginning of one. It looks like "192.168.1.40" or "192.168.2."`);
|
|
54
|
+
const to = (Array.isArray(o.to) ? o.to : []).map((a) => String(a).trim().toLowerCase()).filter(Boolean);
|
|
55
|
+
for (const a of to)
|
|
56
|
+
if (!ADDRESS.test(a))
|
|
57
|
+
throw new Error(`"${a}" in "to" is not an e-mail address`);
|
|
58
|
+
for (const key of ["maxMb", "keepDays"])
|
|
59
|
+
if (o[key] !== undefined && !(Number(o[key]) > 0))
|
|
60
|
+
throw new Error(`"${key}" must be a number above 0`);
|
|
61
|
+
if (o.maxMb !== undefined && o.maxMb > 50)
|
|
62
|
+
throw new Error('"maxMb" is at most 50');
|
|
63
|
+
// Either the device proves itself, or it is known by where it stands: without one of the two anyone
|
|
64
|
+
// on the network could hand text to an agent.
|
|
65
|
+
const login = o.password !== undefined || !allow.length;
|
|
66
|
+
const made = login && o.password === undefined;
|
|
67
|
+
const user = String(o.user ?? "device");
|
|
68
|
+
if (!login && o.user !== undefined)
|
|
69
|
+
throw new Error('"user" without a "password" does nothing: leave both out to accept the addresses on "allow" without a login, or leave only the password out and pine makes one');
|
|
70
|
+
const options = { ...o, ...(allow.length ? { allow } : {}), ...(to.length ? { to } : {}) };
|
|
71
|
+
if (login)
|
|
72
|
+
Object.assign(options, { user, password: o.password ?? setup.random(9) });
|
|
73
|
+
return {
|
|
74
|
+
options,
|
|
75
|
+
does: [
|
|
76
|
+
`open a mail server on port ${o.port} of this computer that accepts mail${login ? ` from the user "${user}"${made ? " with a password made now" : ""}` : " without a login"}${allow.length ? `${login ? "," : ""} from the addresses ${allow.join(", ")}` : ""}${to.length ? `, addressed to ${to.join(", ")}` : ""}, and passes nothing on to anybody`,
|
|
77
|
+
`speak plain SMTP as these devices do: ${login ? "the password and " : ""}the mail cross your network unencrypted`,
|
|
78
|
+
"treat every mail as outside content, whoever it says it is from",
|
|
79
|
+
],
|
|
80
|
+
opens: true,
|
|
81
|
+
tell: `In the device, enter this computer's address as the mail (SMTP) server, port ${o.port}, without encryption (no SSL, no TLS)${login ? `, and the user "${user}". The password is in pine's .env file as ${setup.envName("password")}, where your owner reads it` : ", and no login"}. ${to.length ? `The recipient must be ${to.join(" or ")}` : "Any recipient address will do"}; the sender address can be anything.`,
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
export default async function smtp(source) {
|
|
86
|
+
const o = source.options;
|
|
87
|
+
const port = Number(o.port);
|
|
88
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
89
|
+
throw new Error(`needs "port" (for example 2525)`);
|
|
90
|
+
const allow = (Array.isArray(o.allow) ? o.allow : []).map(String);
|
|
91
|
+
const user = String(o.user ?? "");
|
|
92
|
+
const password = String(o.password ?? "");
|
|
93
|
+
const login = password !== "";
|
|
94
|
+
if (login ? password.length < 8 || !user : !allow.length)
|
|
95
|
+
throw new Error(`needs a "user" and a "password" of 8 characters or more, or "allow" with the addresses of the devices: without either anyone on the network could hand mail to your agents`);
|
|
96
|
+
source.secret(password || undefined);
|
|
97
|
+
const host = String(o.host ?? "0.0.0.0");
|
|
98
|
+
const accept = (Array.isArray(o.to) ? o.to : []).map((a) => String(a).toLowerCase());
|
|
99
|
+
const maxBytes = Math.min(Number(o.maxMb ?? 10), 50) * 1024 * 1024;
|
|
100
|
+
const keepMs = Number(o.keepDays ?? 7) * 86_400_000;
|
|
101
|
+
const dir = path.join(source.dataDir, "files");
|
|
102
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
103
|
+
source.media(dir);
|
|
104
|
+
pruneFiles(dir, keepMs);
|
|
105
|
+
const pruner = setInterval(() => pruneFiles(dir, keepMs), 6 * 3_600_000);
|
|
106
|
+
pruner.unref?.();
|
|
107
|
+
const plain = (addr) => (addr ?? "").replace(/^::ffff:/, "");
|
|
108
|
+
const open = new Set();
|
|
109
|
+
const cap = capped(PER_MINUTE);
|
|
110
|
+
let received = 0;
|
|
111
|
+
let refused = 0;
|
|
112
|
+
const say = () => source.status(`accepting mail on port ${port}${login ? ` from the user ${user}` : ""}${allow.length ? ` from ${allow.join(", ")}` : ""}. ${received} received${refused ? `, ${refused} refused` : ""}`);
|
|
113
|
+
say();
|
|
114
|
+
/** The address in "MAIL FROM:<a@b> SIZE=1" or "RCPT TO:<a@b>". */
|
|
115
|
+
const pathOf = (rest) => (/^\s*<([^<>]*)>/.exec(rest)?.[1] ?? /^\s*(\S+)/.exec(rest)?.[1] ?? "").trim().toLowerCase();
|
|
116
|
+
const server = net.createServer((sock) => {
|
|
117
|
+
const remote = plain(sock.remoteAddress);
|
|
118
|
+
if (open.size >= 20 || (allow.length && !allow.some((a) => remote === a || (/[.:]$/.test(a) && remote.startsWith(a))))) {
|
|
119
|
+
refused++;
|
|
120
|
+
say();
|
|
121
|
+
return void sock.destroy();
|
|
122
|
+
}
|
|
123
|
+
open.add(sock);
|
|
124
|
+
sock.setTimeout(60_000, () => sock.destroy());
|
|
125
|
+
sock.on("close", () => open.delete(sock));
|
|
126
|
+
sock.on("error", () => { });
|
|
127
|
+
const reply = (line) => sock.writable && sock.write(`${line}\r\n`);
|
|
128
|
+
let authed = !login;
|
|
129
|
+
let failures = 0;
|
|
130
|
+
let from;
|
|
131
|
+
let to = [];
|
|
132
|
+
/** What the next line is: a command, part of a login, or (in `data`) the message itself. */
|
|
133
|
+
let expect = "command";
|
|
134
|
+
let name = "";
|
|
135
|
+
const reset = () => {
|
|
136
|
+
from = undefined;
|
|
137
|
+
to = [];
|
|
138
|
+
};
|
|
139
|
+
const auth = (u, p) => {
|
|
140
|
+
authed = timingSafeEqual(u, user) && timingSafeEqual(p, password);
|
|
141
|
+
if (authed)
|
|
142
|
+
return reply("235 Authenticated");
|
|
143
|
+
refused++;
|
|
144
|
+
say();
|
|
145
|
+
if (++failures >= 3)
|
|
146
|
+
return void (reply("535 Bye"), sock.end());
|
|
147
|
+
return reply("535 Login incorrect");
|
|
148
|
+
};
|
|
149
|
+
const decode = (b64) => Buffer.from(b64.trim(), "base64").toString("utf8");
|
|
150
|
+
const take = (raw) => {
|
|
151
|
+
if (raw.length > maxBytes) {
|
|
152
|
+
refused++;
|
|
153
|
+
say();
|
|
154
|
+
return reply("552 Message too large");
|
|
155
|
+
}
|
|
156
|
+
// More than a device has to say: it is told to come back, which a mail program does by itself.
|
|
157
|
+
if (!cap.ok())
|
|
158
|
+
return void (refused++, say(), reply("451 Too many messages, try again later"));
|
|
159
|
+
let m;
|
|
160
|
+
try {
|
|
161
|
+
m = parseMail(raw, { attachmentBytes: maxBytes });
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
source.log.debug("could not read a message", errText(e));
|
|
165
|
+
return reply("554 Could not read the message");
|
|
166
|
+
}
|
|
167
|
+
const kept = keepFiles(dir, m);
|
|
168
|
+
received++;
|
|
169
|
+
say();
|
|
170
|
+
try {
|
|
171
|
+
// Who it is from is what the message says, and failing that what the device said when it handed it over.
|
|
172
|
+
source.emit("mail", { from: m.from || clip(from ?? "", 320), ...(m.fromName ? { fromName: clip(m.fromName, 200) } : {}), to, subject: clip(m.subject, 300), text: clip(m.text, 6000), attachments: kept.attachments, ...(kept.images ? { images: kept.images } : {}), client: remote });
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
source.log.warn("could not report a mail", errText(e));
|
|
176
|
+
}
|
|
177
|
+
return reply("250 Accepted");
|
|
178
|
+
};
|
|
179
|
+
const handle = (line) => {
|
|
180
|
+
if (expect === "plain") {
|
|
181
|
+
expect = "command";
|
|
182
|
+
const [, u = "", p = ""] = decode(line).split("\0");
|
|
183
|
+
return auth(u, p);
|
|
184
|
+
}
|
|
185
|
+
if (expect === "login-user") {
|
|
186
|
+
name = decode(line);
|
|
187
|
+
expect = "login-pass";
|
|
188
|
+
return reply("334 UGFzc3dvcmQ6");
|
|
189
|
+
}
|
|
190
|
+
if (expect === "login-pass") {
|
|
191
|
+
expect = "command";
|
|
192
|
+
return auth(name, decode(line));
|
|
193
|
+
}
|
|
194
|
+
const cmd = line.trim().split(/\s+/, 1)[0].toUpperCase();
|
|
195
|
+
const rest = line.trim().slice(cmd.length).trim();
|
|
196
|
+
if (cmd === "QUIT")
|
|
197
|
+
return void (reply("221 Bye"), sock.end());
|
|
198
|
+
if (cmd === "NOOP")
|
|
199
|
+
return reply("250 OK");
|
|
200
|
+
if (cmd === "RSET")
|
|
201
|
+
return void (reset(), reply("250 OK"));
|
|
202
|
+
if (cmd === "HELO")
|
|
203
|
+
return void (reset(), reply("250 pine"));
|
|
204
|
+
if (cmd === "EHLO")
|
|
205
|
+
return void (reset(), reply([`250-pine`, `250-SIZE ${maxBytes}`, "250-8BITMIME", ...(login ? ["250-AUTH PLAIN LOGIN"] : []), "250 OK"].join("\r\n")));
|
|
206
|
+
if (cmd === "AUTH") {
|
|
207
|
+
if (!login)
|
|
208
|
+
return reply("503 No login is needed");
|
|
209
|
+
if (authed)
|
|
210
|
+
return reply("503 Already logged in");
|
|
211
|
+
const [mechanism = "", initial] = rest.split(/\s+/);
|
|
212
|
+
if (mechanism.toUpperCase() === "PLAIN") {
|
|
213
|
+
if (initial === undefined)
|
|
214
|
+
return void ((expect = "plain"), reply("334 "));
|
|
215
|
+
const [, u = "", p = ""] = decode(initial).split("\0");
|
|
216
|
+
return auth(u, p);
|
|
217
|
+
}
|
|
218
|
+
if (mechanism.toUpperCase() === "LOGIN") {
|
|
219
|
+
if (initial !== undefined) {
|
|
220
|
+
name = decode(initial);
|
|
221
|
+
expect = "login-pass";
|
|
222
|
+
return reply("334 UGFzc3dvcmQ6");
|
|
223
|
+
}
|
|
224
|
+
expect = "login-user";
|
|
225
|
+
return reply("334 VXNlcm5hbWU6");
|
|
226
|
+
}
|
|
227
|
+
return reply("504 Only AUTH PLAIN and AUTH LOGIN");
|
|
228
|
+
}
|
|
229
|
+
if (cmd === "MAIL") {
|
|
230
|
+
if (!authed)
|
|
231
|
+
return reply("530 Log in first");
|
|
232
|
+
if (!/^FROM:/i.test(rest))
|
|
233
|
+
return reply("501 MAIL FROM:<address>");
|
|
234
|
+
const size = Number(/\bSIZE=(\d+)/i.exec(rest)?.[1] ?? 0);
|
|
235
|
+
if (size > maxBytes)
|
|
236
|
+
return void (refused++, say(), reply("552 Message too large"));
|
|
237
|
+
reset();
|
|
238
|
+
from = pathOf(rest.slice(5));
|
|
239
|
+
return reply("250 OK");
|
|
240
|
+
}
|
|
241
|
+
if (cmd === "RCPT") {
|
|
242
|
+
if (!authed)
|
|
243
|
+
return reply("530 Log in first");
|
|
244
|
+
if (from === undefined)
|
|
245
|
+
return reply("503 MAIL first");
|
|
246
|
+
if (!/^TO:/i.test(rest))
|
|
247
|
+
return reply("501 RCPT TO:<address>");
|
|
248
|
+
const rcpt = pathOf(rest.slice(3));
|
|
249
|
+
// Nothing is passed on: a recipient is only a name the device gave, and where a list says which names count, the others are turned away.
|
|
250
|
+
if (!rcpt || (accept.length && !accept.includes(rcpt)))
|
|
251
|
+
return void (refused++, say(), reply("550 No such recipient here"));
|
|
252
|
+
if (to.length >= 20)
|
|
253
|
+
return reply("452 Too many recipients");
|
|
254
|
+
to.push(clip(rcpt, 320));
|
|
255
|
+
return reply("250 OK");
|
|
256
|
+
}
|
|
257
|
+
if (cmd === "DATA") {
|
|
258
|
+
if (!authed)
|
|
259
|
+
return reply("530 Log in first");
|
|
260
|
+
if (from === undefined || !to.length)
|
|
261
|
+
return reply("503 MAIL and RCPT first");
|
|
262
|
+
inData = true;
|
|
263
|
+
chunks = [];
|
|
264
|
+
size = 0;
|
|
265
|
+
return reply("354 Send it, end with a line that holds only a dot");
|
|
266
|
+
}
|
|
267
|
+
// STARTTLS, VRFY, EXPN, TURN and whatever else: not here.
|
|
268
|
+
return reply("502 Not implemented");
|
|
269
|
+
};
|
|
270
|
+
let buffer = Buffer.alloc(0);
|
|
271
|
+
let inData = false;
|
|
272
|
+
let chunks = [];
|
|
273
|
+
let size = 0;
|
|
274
|
+
let tail = Buffer.alloc(0);
|
|
275
|
+
const END = Buffer.from("\r\n.\r\n");
|
|
276
|
+
const feed = (chunk) => {
|
|
277
|
+
while (chunk.length) {
|
|
278
|
+
if (!inData) {
|
|
279
|
+
buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk;
|
|
280
|
+
chunk = Buffer.alloc(0);
|
|
281
|
+
for (;;) {
|
|
282
|
+
if (inData) {
|
|
283
|
+
// What follows the DATA command in the same packet is the message already.
|
|
284
|
+
chunk = buffer;
|
|
285
|
+
buffer = Buffer.alloc(0);
|
|
286
|
+
// The line break that ended "DATA" counts as the one before a first line of only a dot.
|
|
287
|
+
tail = Buffer.from("\r\n");
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
const i = buffer.indexOf("\n");
|
|
291
|
+
if (i < 0) {
|
|
292
|
+
if (buffer.length > LINE_MAX)
|
|
293
|
+
return void sock.destroy();
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
const line = buffer.subarray(0, i).toString("utf8").replace(/\r$/, "");
|
|
297
|
+
buffer = buffer.subarray(i + 1);
|
|
298
|
+
handle(line);
|
|
299
|
+
}
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
// The end of a message is searched for where the last piece ended, so that an end cut in two is found.
|
|
303
|
+
const joined = Buffer.concat([tail, chunk]);
|
|
304
|
+
const at = joined.indexOf(END);
|
|
305
|
+
if (at < 0) {
|
|
306
|
+
const keep = Math.max(0, joined.length - (END.length - 1));
|
|
307
|
+
const body = joined.subarray(0, keep);
|
|
308
|
+
size += body.length;
|
|
309
|
+
// A message that is too large is read to its end and not kept: the device gets its answer, and no memory is spent on it.
|
|
310
|
+
if (size <= maxBytes + END.length)
|
|
311
|
+
chunks.push(body);
|
|
312
|
+
else
|
|
313
|
+
chunks = [];
|
|
314
|
+
if (size > maxBytes * 2 + 1024 * 1024)
|
|
315
|
+
return void sock.destroy();
|
|
316
|
+
tail = joined.subarray(keep);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const body = joined.subarray(0, at);
|
|
320
|
+
size += body.length;
|
|
321
|
+
const whole = size <= maxBytes + END.length ? Buffer.concat([...chunks, body]) : undefined;
|
|
322
|
+
chunk = joined.subarray(at + END.length);
|
|
323
|
+
inData = false;
|
|
324
|
+
chunks = [];
|
|
325
|
+
tail = Buffer.alloc(0);
|
|
326
|
+
if (!whole) {
|
|
327
|
+
refused++;
|
|
328
|
+
say();
|
|
329
|
+
reply("552 Message too large");
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
// The first two bytes are the line break that was put in front; a line that began with two dots began with one.
|
|
333
|
+
take(Buffer.from(whole.subarray(2).toString("latin1").replace(/(^|\r\n)\.\./g, "$1."), "latin1"));
|
|
334
|
+
}
|
|
335
|
+
reset();
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
sock.on("data", (chunk) => {
|
|
339
|
+
try {
|
|
340
|
+
feed(chunk);
|
|
341
|
+
}
|
|
342
|
+
catch (e) {
|
|
343
|
+
source.log.debug("smtp connection failed", errText(e));
|
|
344
|
+
sock.destroy();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
reply("220 pine mail drop");
|
|
348
|
+
});
|
|
349
|
+
await new Promise((resolve, reject) => {
|
|
350
|
+
server.once("error", reject);
|
|
351
|
+
server.listen(port, host, () => (server.removeListener("error", reject), resolve()));
|
|
352
|
+
});
|
|
353
|
+
source.onStop(() => new Promise((resolve) => {
|
|
354
|
+
clearInterval(pruner);
|
|
355
|
+
for (const s of open)
|
|
356
|
+
s.destroy();
|
|
357
|
+
server.close(() => resolve());
|
|
358
|
+
}));
|
|
359
|
+
source.log.info(`accepting mail on ${host}:${port}`);
|
|
360
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Agent } from "undici";
|
|
2
|
+
import type { SourceType } from "../source-types.ts";
|
|
3
|
+
import type { SourceApi } from "../sources.ts";
|
|
4
|
+
export declare const clock: (ts?: number) => string;
|
|
5
|
+
/** The first pause before connecting again to a host: a second, or a twentieth of one for this computer. */
|
|
6
|
+
export declare const firstPause: (host: string) => 50 | 1000;
|
|
7
|
+
/**
|
|
8
|
+
* What a service said, as a status line may repeat it. A status line is read by agents without
|
|
9
|
+
* the mark that outside content gets, so only a code or a single word is passed on.
|
|
10
|
+
*/
|
|
11
|
+
export declare const word: (v: unknown) => string;
|
|
12
|
+
/** An error pine words itself: what it says may be shown where the sources are listed. */
|
|
13
|
+
export declare class Plain extends Error {
|
|
14
|
+
}
|
|
15
|
+
export interface Held {
|
|
16
|
+
/** Aborted when the source stops. */
|
|
17
|
+
signal: AbortSignal;
|
|
18
|
+
/** Say the status line again, when what `note` returns has changed. */
|
|
19
|
+
refresh(): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Keep a connection: `run` makes one and returns (or throws) when it has ended, and is then
|
|
23
|
+
* called again after a pause that starts short and doubles with every attempt that did not last,
|
|
24
|
+
* up to five minutes. `run` calls `up` once the connection works. The status line says how
|
|
25
|
+
* things stand; with `report` an address that cannot be reached is an event ("down", once, after
|
|
26
|
+
* three attempts in a row) and so is its return ("up").
|
|
27
|
+
*/
|
|
28
|
+
export declare function hold(source: SourceApi, o: {
|
|
29
|
+
host: string;
|
|
30
|
+
report?: string;
|
|
31
|
+
note?: () => string;
|
|
32
|
+
run: (signal: AbortSignal, up: () => void, wait: (ms: number) => void) => Promise<void>;
|
|
33
|
+
}): Held;
|
|
34
|
+
/** Connections that have to stay on the public internet, or none for a host that is local by its name or by the owner's word. */
|
|
35
|
+
export declare function guardFor(source: SourceApi, host: string, local: unknown, timeoutMs?: number): Agent | undefined;
|
|
36
|
+
export interface Wire {
|
|
37
|
+
send(message: unknown): void;
|
|
38
|
+
close(code?: number): void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* One WebSocket, from open to close. Resolves with the close code once a connection that was
|
|
42
|
+
* open has closed; rejects when it could not be opened. `guard` keeps it on the public internet.
|
|
43
|
+
* A message is handed on as text; text frames and binary ones alike.
|
|
44
|
+
*/
|
|
45
|
+
export declare function wire(address: string | URL, o: {
|
|
46
|
+
guard?: Agent;
|
|
47
|
+
headers?: Record<string, string>;
|
|
48
|
+
signal: AbortSignal;
|
|
49
|
+
onOpen?: (w: Wire) => void;
|
|
50
|
+
onMessage: (text: string, w: Wire) => void;
|
|
51
|
+
}): Promise<number>;
|
|
52
|
+
export declare const type: SourceType;
|
|
53
|
+
/** What Server-Sent Events are made of: fields line by line, an empty line ends an event. */
|
|
54
|
+
export declare function sseParser(on: (e: {
|
|
55
|
+
event: string;
|
|
56
|
+
data: string;
|
|
57
|
+
id?: string;
|
|
58
|
+
}) => void, retry: (ms: number) => void): (chunk: string) => void;
|
|
59
|
+
export default function stream(source: SourceApi): void;
|
|
60
|
+
/**
|
|
61
|
+
* The address of a service's API when the owner runs one of their own in its place: reached over
|
|
62
|
+
* https, or on this machine. Returned without a slash at the end.
|
|
63
|
+
*/
|
|
64
|
+
export declare function ownServer(raw: unknown, usual: string): string;
|
|
65
|
+
/** A list an agent may have written as one text with commas: trimmed entries, no empty ones. */
|
|
66
|
+
export declare const listOf: (v: unknown) => string[];
|
|
67
|
+
/** The keyword a text contains, capitals aside. */
|
|
68
|
+
export declare const keywordIn: (keywords: string[], text: string) => string | undefined;
|