@blazetrails/net-smtp 0.0.0-stage → 0.1.0-alpha0.1
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/LICENSE +21 -0
- package/dist/auth-cram-md5.d.ts +15 -0
- package/dist/auth-cram-md5.js +35 -0
- package/dist/auth-cram-md5.js.map +1 -0
- package/dist/auth-login.d.ts +6 -0
- package/dist/auth-login.js +12 -0
- package/dist/auth-login.js.map +1 -0
- package/dist/auth-plain.d.ts +6 -0
- package/dist/auth-plain.js +10 -0
- package/dist/auth-plain.js.map +1 -0
- package/dist/auth-xoauth2.d.ts +8 -0
- package/dist/auth-xoauth2.js +15 -0
- package/dist/auth-xoauth2.js.map +1 -0
- package/dist/authenticator.d.ts +20 -0
- package/dist/authenticator.js +41 -0
- package/dist/authenticator.js.map +1 -0
- package/dist/constants.generated.d.ts +2 -0
- package/dist/constants.generated.js +25 -0
- package/dist/constants.generated.js.map +1 -0
- package/dist/protocol.d.ts +11 -0
- package/dist/protocol.js +2 -0
- package/dist/protocol.js.map +1 -0
- package/dist/smtp.d.ts +224 -0
- package/dist/smtp.js +724 -0
- package/dist/smtp.js.map +1 -0
- package/package.json +55 -3
- package/README.md +0 -3
package/dist/smtp.js
ADDED
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
import { checkServerIdentity, rootCertificates } from "node:tls";
|
|
3
|
+
import SMTPConnection from "nodemailer/lib/smtp-connection/index.js";
|
|
4
|
+
import { binaryBytes, binaryString, ArgumentError, EOFError, IOError, include, isModuleIncluded, OpenSSL, rbBlockGivenP, rbFArray, rbModName, } from "@blazetrails/ruby-compat";
|
|
5
|
+
import { Authenticator } from "./authenticator.js";
|
|
6
|
+
import { Net, OpenTimeout, Protocol, ReadTimeout, ProtoAuthError, ProtoFatalError, ProtocolError, ProtoServerError, ProtoSyntaxError, ProtoUnknownError, } from "./protocol.js";
|
|
7
|
+
export const SMTPError = {
|
|
8
|
+
get message() {
|
|
9
|
+
const self = this;
|
|
10
|
+
return self._message ?? self.response.message;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
export class SMTPAuthenticationError extends ProtoAuthError {
|
|
14
|
+
response;
|
|
15
|
+
/** @internal */
|
|
16
|
+
_message;
|
|
17
|
+
constructor(response, { message = null } = {}) {
|
|
18
|
+
super();
|
|
19
|
+
this.response = response instanceof Response ? response : null;
|
|
20
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
include(SMTPAuthenticationError, SMTPError);
|
|
24
|
+
Net.SMTPAuthenticationError = SMTPAuthenticationError;
|
|
25
|
+
export class SMTPServerBusy extends ProtoServerError {
|
|
26
|
+
response;
|
|
27
|
+
/** @internal */
|
|
28
|
+
_message;
|
|
29
|
+
constructor(response, { message = null } = {}) {
|
|
30
|
+
super();
|
|
31
|
+
this.response = response instanceof Response ? response : null;
|
|
32
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
include(SMTPServerBusy, SMTPError);
|
|
36
|
+
export class SMTPSyntaxError extends ProtoSyntaxError {
|
|
37
|
+
response;
|
|
38
|
+
/** @internal */
|
|
39
|
+
_message;
|
|
40
|
+
constructor(response, { message = null } = {}) {
|
|
41
|
+
super();
|
|
42
|
+
this.response = response instanceof Response ? response : null;
|
|
43
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
include(SMTPSyntaxError, SMTPError);
|
|
47
|
+
export class SMTPFatalError extends ProtoFatalError {
|
|
48
|
+
response;
|
|
49
|
+
/** @internal */
|
|
50
|
+
_message;
|
|
51
|
+
constructor(response, { message = null } = {}) {
|
|
52
|
+
super();
|
|
53
|
+
this.response = response instanceof Response ? response : null;
|
|
54
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
include(SMTPFatalError, SMTPError);
|
|
58
|
+
export class SMTPUnknownError extends ProtoUnknownError {
|
|
59
|
+
response;
|
|
60
|
+
/** @internal */
|
|
61
|
+
_message;
|
|
62
|
+
constructor(response, { message = null } = {}) {
|
|
63
|
+
super();
|
|
64
|
+
this.response = response instanceof Response ? response : null;
|
|
65
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
include(SMTPUnknownError, SMTPError);
|
|
69
|
+
export class SMTPUnsupportedCommand extends ProtocolError {
|
|
70
|
+
response;
|
|
71
|
+
/** @internal */
|
|
72
|
+
_message;
|
|
73
|
+
constructor(response, { message = null } = {}) {
|
|
74
|
+
super();
|
|
75
|
+
this.response = response instanceof Response ? response : null;
|
|
76
|
+
this._message = response instanceof Response ? message : (message ?? response);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
include(SMTPUnsupportedCommand, SMTPError);
|
|
80
|
+
export class Response {
|
|
81
|
+
static parse(str) {
|
|
82
|
+
return new Response(str.slice(0, 3), str);
|
|
83
|
+
}
|
|
84
|
+
status;
|
|
85
|
+
string;
|
|
86
|
+
constructor(status, string) {
|
|
87
|
+
this.status = status;
|
|
88
|
+
this.string = string;
|
|
89
|
+
}
|
|
90
|
+
statusTypeChar() {
|
|
91
|
+
return this.status.slice(0, 1);
|
|
92
|
+
}
|
|
93
|
+
isSuccess() {
|
|
94
|
+
return this.statusTypeChar() === "2";
|
|
95
|
+
}
|
|
96
|
+
isContinue() {
|
|
97
|
+
return this.statusTypeChar() === "3";
|
|
98
|
+
}
|
|
99
|
+
get message() {
|
|
100
|
+
return this.string.split(/(?<=\n)/)[0];
|
|
101
|
+
}
|
|
102
|
+
cramMd5Challenge() {
|
|
103
|
+
return atob(this.string.split(" ")[1]);
|
|
104
|
+
}
|
|
105
|
+
get capabilities() {
|
|
106
|
+
if (this.string.slice(3, 4) !== "-")
|
|
107
|
+
return {};
|
|
108
|
+
const h = {};
|
|
109
|
+
for (const line of this.string.split(/(?<=\n)/).slice(1)) {
|
|
110
|
+
const [k, ...v] = line.slice(4).trim().split(/\s+/);
|
|
111
|
+
h[k] = v;
|
|
112
|
+
}
|
|
113
|
+
return h;
|
|
114
|
+
}
|
|
115
|
+
get exceptionClass() {
|
|
116
|
+
if (/^4/.test(this.status))
|
|
117
|
+
return SMTPServerBusy;
|
|
118
|
+
if (/^50/.test(this.status))
|
|
119
|
+
return SMTPSyntaxError;
|
|
120
|
+
if (/^53/.test(this.status))
|
|
121
|
+
return SMTPAuthenticationError;
|
|
122
|
+
if (/^5/.test(this.status))
|
|
123
|
+
return SMTPFatalError;
|
|
124
|
+
return SMTPUnknownError;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export class Address {
|
|
128
|
+
address;
|
|
129
|
+
parameters;
|
|
130
|
+
constructor(address, ...args) {
|
|
131
|
+
const parameters = address instanceof Address ? address.parameters : [];
|
|
132
|
+
this.address = address instanceof Address ? address.address : address;
|
|
133
|
+
const params = [...parameters, ...args].flatMap((param) => rbFArray(param));
|
|
134
|
+
this.parameters = [
|
|
135
|
+
...new Set(params.map((param) => rbFArray(param)
|
|
136
|
+
.filter((p) => p != null)
|
|
137
|
+
.join("="))),
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
toString() {
|
|
141
|
+
return this.address;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export class SMTP extends Protocol {
|
|
145
|
+
static VERSION = "0.5.1";
|
|
146
|
+
static DEFAULT_AUTH_TYPE = "plain";
|
|
147
|
+
static Response = Response;
|
|
148
|
+
static Address = Address;
|
|
149
|
+
static Authenticator = Authenticator;
|
|
150
|
+
static defaultPort() {
|
|
151
|
+
return 25;
|
|
152
|
+
}
|
|
153
|
+
static defaultSubmissionPort() {
|
|
154
|
+
return 587;
|
|
155
|
+
}
|
|
156
|
+
static defaultTlsPort() {
|
|
157
|
+
return 465;
|
|
158
|
+
}
|
|
159
|
+
static defaultSslPort() {
|
|
160
|
+
return SMTP.defaultTlsPort();
|
|
161
|
+
}
|
|
162
|
+
static defaultSslContext(sslContextParams = null) {
|
|
163
|
+
const context = new OpenSSL.SSL.SSLContext();
|
|
164
|
+
context.setParams(sslContextParams ?? {});
|
|
165
|
+
return context;
|
|
166
|
+
}
|
|
167
|
+
static async start(address, port = null, ...args) {
|
|
168
|
+
const block = rbBlockGivenP(args.at(-1)) ? args.pop() : null;
|
|
169
|
+
const last = args.at(-1);
|
|
170
|
+
const isOptions = last !== null && typeof last === "object";
|
|
171
|
+
const options = (isOptions ? args.pop() : {});
|
|
172
|
+
const { tls = false, starttls = "auto", tlsVerify = true, tlsHostname = null } = options;
|
|
173
|
+
const { sslContextParams = null } = options;
|
|
174
|
+
if (args.length > 4) {
|
|
175
|
+
throw new ArgumentError(`wrong number of arguments (given ${args.length + 2}, expected 1..6)`);
|
|
176
|
+
}
|
|
177
|
+
const helo = options.helo ?? args[0] ?? "localhost";
|
|
178
|
+
const user = options.user ?? args[1] ?? null;
|
|
179
|
+
const secret = options.secret ?? options.password ?? args[2] ?? null;
|
|
180
|
+
const authtype = options.authtype ?? args[3] ?? null;
|
|
181
|
+
const smtp = new this(address, port, {
|
|
182
|
+
tls,
|
|
183
|
+
starttls,
|
|
184
|
+
tlsVerify,
|
|
185
|
+
tlsHostname,
|
|
186
|
+
sslContextParams,
|
|
187
|
+
});
|
|
188
|
+
return block
|
|
189
|
+
? smtp.start({ helo, user, secret, authtype }, block)
|
|
190
|
+
: smtp.start({ helo, user, secret, authtype });
|
|
191
|
+
}
|
|
192
|
+
address;
|
|
193
|
+
port;
|
|
194
|
+
esmtp = true;
|
|
195
|
+
capabilities = null;
|
|
196
|
+
tlsVerify;
|
|
197
|
+
tlsHostname;
|
|
198
|
+
sslContextParams;
|
|
199
|
+
openTimeout = 30;
|
|
200
|
+
/** @internal */
|
|
201
|
+
_socket = null;
|
|
202
|
+
_started = false;
|
|
203
|
+
_readTimeout = 60;
|
|
204
|
+
_errorOccurred = false;
|
|
205
|
+
_debugOutput = null;
|
|
206
|
+
_tls;
|
|
207
|
+
_starttls;
|
|
208
|
+
_sslContextTls = null;
|
|
209
|
+
_sslContextStarttls = null;
|
|
210
|
+
constructor(address, port = null, { tls = false, starttls = "auto", tlsVerify = true, tlsHostname = null, sslContextParams = null, } = {}) {
|
|
211
|
+
super();
|
|
212
|
+
this.address = address;
|
|
213
|
+
this.port = port ?? SMTP.defaultPort();
|
|
214
|
+
this._tls = tls;
|
|
215
|
+
this._starttls = starttls;
|
|
216
|
+
this.tlsVerify = tlsVerify;
|
|
217
|
+
this.tlsHostname = tlsHostname;
|
|
218
|
+
this.sslContextParams = sslContextParams;
|
|
219
|
+
}
|
|
220
|
+
inspect() {
|
|
221
|
+
return `#<${rbModName(this.constructor)} ${this.address}:${this.port} started=${this._started}>`;
|
|
222
|
+
}
|
|
223
|
+
isEsmtp() {
|
|
224
|
+
return this.esmtp;
|
|
225
|
+
}
|
|
226
|
+
isCapableStarttls() {
|
|
227
|
+
return this.isCapable("STARTTLS");
|
|
228
|
+
}
|
|
229
|
+
isCapable(key) {
|
|
230
|
+
if (this.capabilities == null)
|
|
231
|
+
return null;
|
|
232
|
+
return this.capabilities[key] != null;
|
|
233
|
+
}
|
|
234
|
+
isCapablePlainAuth() {
|
|
235
|
+
return this.isAuthCapable("PLAIN");
|
|
236
|
+
}
|
|
237
|
+
isCapableLoginAuth() {
|
|
238
|
+
return this.isAuthCapable("LOGIN");
|
|
239
|
+
}
|
|
240
|
+
isCapableCramMd5Auth() {
|
|
241
|
+
return this.isAuthCapable("CRAM-MD5");
|
|
242
|
+
}
|
|
243
|
+
isAuthCapable(type) {
|
|
244
|
+
if (this.capabilities == null)
|
|
245
|
+
return null;
|
|
246
|
+
if (this.capabilities["AUTH"] == null)
|
|
247
|
+
return false;
|
|
248
|
+
return this.capabilities["AUTH"].includes(type);
|
|
249
|
+
}
|
|
250
|
+
capableAuthTypes() {
|
|
251
|
+
if (this.capabilities == null)
|
|
252
|
+
return [];
|
|
253
|
+
return this.capabilities["AUTH"] ?? [];
|
|
254
|
+
}
|
|
255
|
+
isTls() {
|
|
256
|
+
return this._tls;
|
|
257
|
+
}
|
|
258
|
+
isSsl() {
|
|
259
|
+
return this.isTls();
|
|
260
|
+
}
|
|
261
|
+
enableTls(context = null) {
|
|
262
|
+
if (this._starttls === "always") {
|
|
263
|
+
throw new ArgumentError("SMTPS and STARTTLS is exclusive");
|
|
264
|
+
}
|
|
265
|
+
this._tls = true;
|
|
266
|
+
this._sslContextTls = context;
|
|
267
|
+
}
|
|
268
|
+
enableSsl(context = null) {
|
|
269
|
+
this.enableTls(context);
|
|
270
|
+
}
|
|
271
|
+
disableTls() {
|
|
272
|
+
this._tls = false;
|
|
273
|
+
this._sslContextTls = null;
|
|
274
|
+
}
|
|
275
|
+
disableSsl() {
|
|
276
|
+
this.disableTls();
|
|
277
|
+
}
|
|
278
|
+
isStarttls() {
|
|
279
|
+
return this._starttls;
|
|
280
|
+
}
|
|
281
|
+
isStarttlsAlways() {
|
|
282
|
+
return this._starttls === "always";
|
|
283
|
+
}
|
|
284
|
+
isStarttlsAuto() {
|
|
285
|
+
return this._starttls === "auto";
|
|
286
|
+
}
|
|
287
|
+
enableStarttls(context = null) {
|
|
288
|
+
if (this._tls)
|
|
289
|
+
throw new ArgumentError("SMTPS and STARTTLS is exclusive");
|
|
290
|
+
this._starttls = "always";
|
|
291
|
+
this._sslContextStarttls = context;
|
|
292
|
+
}
|
|
293
|
+
enableStarttlsAuto(context = null) {
|
|
294
|
+
if (this._tls)
|
|
295
|
+
throw new ArgumentError("SMTPS and STARTTLS is exclusive");
|
|
296
|
+
this._starttls = "auto";
|
|
297
|
+
this._sslContextStarttls = context;
|
|
298
|
+
}
|
|
299
|
+
disableStarttls() {
|
|
300
|
+
this._starttls = false;
|
|
301
|
+
this._sslContextStarttls = null;
|
|
302
|
+
}
|
|
303
|
+
get readTimeout() {
|
|
304
|
+
return this._readTimeout;
|
|
305
|
+
}
|
|
306
|
+
set readTimeout(sec) {
|
|
307
|
+
this._socket?._socket?.setTimeout(sec * 1000);
|
|
308
|
+
this._readTimeout = sec;
|
|
309
|
+
}
|
|
310
|
+
set debugOutput(arg) {
|
|
311
|
+
this._debugOutput = arg;
|
|
312
|
+
}
|
|
313
|
+
setDebugOutput(arg) {
|
|
314
|
+
this._debugOutput = arg;
|
|
315
|
+
}
|
|
316
|
+
isStarted() {
|
|
317
|
+
return this._started;
|
|
318
|
+
}
|
|
319
|
+
async start(...args) {
|
|
320
|
+
const block = rbBlockGivenP(args.at(-1)) ? args.pop() : null;
|
|
321
|
+
const last = args.at(-1);
|
|
322
|
+
const options = (last !== null && typeof last === "object" ? args.pop() : {});
|
|
323
|
+
if (args.length > 4) {
|
|
324
|
+
throw new ArgumentError(`wrong number of arguments (given ${args.length}, expected 0..4)`);
|
|
325
|
+
}
|
|
326
|
+
const helo = options.helo ?? args[0] ?? "localhost";
|
|
327
|
+
const user = options.user ?? args[1] ?? null;
|
|
328
|
+
const secret = options.secret ?? options.password ?? args[2] ?? null;
|
|
329
|
+
const authtype = options.authtype ?? args[3] ?? null;
|
|
330
|
+
const sslContextParams = this.sslContextParams ?? {};
|
|
331
|
+
if (!Object.hasOwn(sslContextParams, "verifyMode")) {
|
|
332
|
+
sslContextParams["verifyMode"] = this.tlsVerify
|
|
333
|
+
? OpenSSL.SSL.VERIFY_PEER
|
|
334
|
+
: OpenSSL.SSL.VERIFY_NONE;
|
|
335
|
+
}
|
|
336
|
+
if (this._tls && this._sslContextTls == null) {
|
|
337
|
+
this._sslContextTls = SMTP.defaultSslContext(sslContextParams);
|
|
338
|
+
}
|
|
339
|
+
if (this._starttls !== false && this._sslContextStarttls == null) {
|
|
340
|
+
this._sslContextStarttls = SMTP.defaultSslContext(sslContextParams);
|
|
341
|
+
}
|
|
342
|
+
if (block) {
|
|
343
|
+
try {
|
|
344
|
+
await this.doStart(helo, user, secret, authtype);
|
|
345
|
+
return await block(this);
|
|
346
|
+
}
|
|
347
|
+
finally {
|
|
348
|
+
await this.doFinish();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
await this.doStart(helo, user, secret, authtype);
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
async finish() {
|
|
355
|
+
if (!this.isStarted())
|
|
356
|
+
throw new IOError("not yet started");
|
|
357
|
+
await this.doFinish();
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* @internal
|
|
361
|
+
* @inventedArm rescue — PERMANENT
|
|
362
|
+
*/
|
|
363
|
+
async doStart(heloDomain, user, secret, authtype) {
|
|
364
|
+
if (this._started)
|
|
365
|
+
throw new IOError("SMTP session already started");
|
|
366
|
+
if (user != null || secret != null || authtype != null) {
|
|
367
|
+
this.checkAuthArgs(authtype, user, secret);
|
|
368
|
+
}
|
|
369
|
+
const s = new SMTPConnection({
|
|
370
|
+
host: this.address,
|
|
371
|
+
port: this.port,
|
|
372
|
+
name: heloDomain,
|
|
373
|
+
secure: false,
|
|
374
|
+
connectionTimeout: this.openTimeout * 1000,
|
|
375
|
+
greetingTimeout: this._readTimeout * 1000,
|
|
376
|
+
socketTimeout: this._readTimeout * 1000,
|
|
377
|
+
});
|
|
378
|
+
s.startSsl = (options) => _startSsl(s, options);
|
|
379
|
+
this._socket = s;
|
|
380
|
+
const onConnect = s._onConnect;
|
|
381
|
+
try {
|
|
382
|
+
try {
|
|
383
|
+
await new Promise((resolve, reject) => {
|
|
384
|
+
s.once("error", reject);
|
|
385
|
+
s._onConnect = () => {
|
|
386
|
+
s.removeListener("error", reject);
|
|
387
|
+
resolve();
|
|
388
|
+
};
|
|
389
|
+
s.connect();
|
|
390
|
+
});
|
|
391
|
+
this.logging(`Connection opened: ${this.address}:${this.port}`);
|
|
392
|
+
if (this.isTls())
|
|
393
|
+
await this.tlsconnect(s, this._sslContextTls);
|
|
394
|
+
}
|
|
395
|
+
catch (err) {
|
|
396
|
+
if (err.code === "ETIMEDOUT") {
|
|
397
|
+
throw new OpenTimeout("execution expired");
|
|
398
|
+
}
|
|
399
|
+
throw err;
|
|
400
|
+
}
|
|
401
|
+
this.checkResponse(await this.critical(async () => {
|
|
402
|
+
let onError;
|
|
403
|
+
try {
|
|
404
|
+
return await new Promise((resolve, reject) => {
|
|
405
|
+
onError = reject;
|
|
406
|
+
s.once("error", onError);
|
|
407
|
+
s._actionGreeting = (str) => {
|
|
408
|
+
s._clearPhase();
|
|
409
|
+
resolve(Response.parse(`${str}\n`));
|
|
410
|
+
};
|
|
411
|
+
s._connectionDeadline = null;
|
|
412
|
+
onConnect.call(s);
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
catch (err) {
|
|
416
|
+
if (err.code === "ETIMEDOUT") {
|
|
417
|
+
throw new ReadTimeout();
|
|
418
|
+
}
|
|
419
|
+
if (err.code === "ECONNECTION") {
|
|
420
|
+
throw new EOFError("end of file reached");
|
|
421
|
+
}
|
|
422
|
+
throw err;
|
|
423
|
+
}
|
|
424
|
+
finally {
|
|
425
|
+
s.removeListener("error", onError);
|
|
426
|
+
}
|
|
427
|
+
}));
|
|
428
|
+
s.on("error", () => {
|
|
429
|
+
this._errorOccurred = true;
|
|
430
|
+
});
|
|
431
|
+
await this.doHelo(heloDomain);
|
|
432
|
+
if (!this.isTls() &&
|
|
433
|
+
(this.isStarttlsAlways() || (this.isCapableStarttls() && this.isStarttlsAuto()))) {
|
|
434
|
+
if (!this.isCapableStarttls()) {
|
|
435
|
+
throw new SMTPUnsupportedCommand("STARTTLS is not supported on this server");
|
|
436
|
+
}
|
|
437
|
+
await this.starttls();
|
|
438
|
+
await this.tlsconnect(s, this._sslContextStarttls);
|
|
439
|
+
await this.doHelo(heloDomain);
|
|
440
|
+
}
|
|
441
|
+
if (user != null) {
|
|
442
|
+
await this.authenticate(user, secret, authtype ?? SMTP.DEFAULT_AUTH_TYPE);
|
|
443
|
+
}
|
|
444
|
+
this._started = true;
|
|
445
|
+
}
|
|
446
|
+
finally {
|
|
447
|
+
if (!this._started) {
|
|
448
|
+
s.close();
|
|
449
|
+
this._socket = null;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
/** @internal */
|
|
454
|
+
sslSocket(socket, context) {
|
|
455
|
+
return new OpenSSL.SSL.SSLSocket(socket, context);
|
|
456
|
+
}
|
|
457
|
+
/** @internal */
|
|
458
|
+
async tlsconnect(s, context) {
|
|
459
|
+
let verified = false;
|
|
460
|
+
const ssl = this.sslSocket(s, context);
|
|
461
|
+
try {
|
|
462
|
+
this.logging("TLS connection started");
|
|
463
|
+
ssl.syncClose = true;
|
|
464
|
+
ssl.hostname = this.tlsHostname ?? this.address;
|
|
465
|
+
await this.sslSocketConnect(ssl, this.openTimeout);
|
|
466
|
+
verified = true;
|
|
467
|
+
return ssl;
|
|
468
|
+
}
|
|
469
|
+
finally {
|
|
470
|
+
if (!verified)
|
|
471
|
+
ssl.close();
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
/** @internal */
|
|
475
|
+
async doHelo(heloDomain) {
|
|
476
|
+
for (;;) {
|
|
477
|
+
try {
|
|
478
|
+
const res = this.esmtp ? await this.ehlo(heloDomain) : await this.helo(heloDomain);
|
|
479
|
+
this.capabilities = res.capabilities;
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
catch (err) {
|
|
483
|
+
if (this.esmtp && err instanceof Error && isModuleIncluded(err.constructor, SMTPError)) {
|
|
484
|
+
this.esmtp = false;
|
|
485
|
+
this._errorOccurred = false;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
throw err;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/** @internal */
|
|
493
|
+
async doFinish() {
|
|
494
|
+
const socket = this._socket;
|
|
495
|
+
try {
|
|
496
|
+
if (socket != null && !socket._destroyed && !this._errorOccurred) {
|
|
497
|
+
await this.quit();
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
finally {
|
|
501
|
+
this._started = false;
|
|
502
|
+
this._errorOccurred = false;
|
|
503
|
+
socket?.close();
|
|
504
|
+
this._socket = null;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/** @internal */
|
|
508
|
+
requiresSmtputf8(address) {
|
|
509
|
+
const str = address instanceof Address ? address.address : address;
|
|
510
|
+
return /[^\p{ASCII}]/u.test(str);
|
|
511
|
+
}
|
|
512
|
+
/** @internal */
|
|
513
|
+
anyRequireSmtputf8(addresses) {
|
|
514
|
+
return addresses.some((a) => this.requiresSmtputf8(a));
|
|
515
|
+
}
|
|
516
|
+
async sendMessage(msgstr, fromAddr, ...toAddrs) {
|
|
517
|
+
const to = toAddrs.flat(Infinity);
|
|
518
|
+
if (this._socket == null)
|
|
519
|
+
throw new IOError("closed session");
|
|
520
|
+
if (this.anyRequireSmtputf8(to) && this.isCapable("SMTPUTF8")) {
|
|
521
|
+
fromAddr = new Address(fromAddr, "SMTPUTF8");
|
|
522
|
+
}
|
|
523
|
+
await this.mailfrom(fromAddr);
|
|
524
|
+
return await this.rcpttoList(to, () => this.data(msgstr));
|
|
525
|
+
}
|
|
526
|
+
sendMail(msgstr, fromAddr, ...toAddrs) {
|
|
527
|
+
return this.sendMessage(msgstr, fromAddr, ...toAddrs);
|
|
528
|
+
}
|
|
529
|
+
sendmail(msgstr, fromAddr, ...toAddrs) {
|
|
530
|
+
return this.sendMessage(msgstr, fromAddr, ...toAddrs);
|
|
531
|
+
}
|
|
532
|
+
async openMessageStream(fromAddr, ...toAddrs) {
|
|
533
|
+
const block = toAddrs.pop();
|
|
534
|
+
const to = toAddrs.flat(Infinity);
|
|
535
|
+
if (this._socket == null)
|
|
536
|
+
throw new IOError("closed session");
|
|
537
|
+
if (this.anyRequireSmtputf8(to) && this.isCapable("SMTPUTF8")) {
|
|
538
|
+
fromAddr = new Address(fromAddr, "SMTPUTF8");
|
|
539
|
+
}
|
|
540
|
+
await this.mailfrom(fromAddr);
|
|
541
|
+
return await this.rcpttoList(to, () => this.data(null, block));
|
|
542
|
+
}
|
|
543
|
+
ready(fromAddr, ...toAddrs) {
|
|
544
|
+
return this.openMessageStream(fromAddr, ...toAddrs);
|
|
545
|
+
}
|
|
546
|
+
async authenticate(user, secret, authtype = SMTP.DEFAULT_AUTH_TYPE) {
|
|
547
|
+
this.checkAuthArgs(authtype, user, secret);
|
|
548
|
+
const authenticator = new (Authenticator.authClass(authtype))(this);
|
|
549
|
+
return await authenticator.auth(user, secret);
|
|
550
|
+
}
|
|
551
|
+
/** @internal */
|
|
552
|
+
checkAuthArgs(type, ...args) {
|
|
553
|
+
const klass = Authenticator.authClass(type ?? SMTP.DEFAULT_AUTH_TYPE);
|
|
554
|
+
if (klass == null)
|
|
555
|
+
throw new ArgumentError(`wrong authentication type ${type}`);
|
|
556
|
+
klass.checkArgs(...args);
|
|
557
|
+
}
|
|
558
|
+
async starttls() {
|
|
559
|
+
return await this.getok("STARTTLS");
|
|
560
|
+
}
|
|
561
|
+
async helo(domain) {
|
|
562
|
+
return await this.getok(`HELO ${domain}`);
|
|
563
|
+
}
|
|
564
|
+
async ehlo(domain) {
|
|
565
|
+
return await this.getok(`EHLO ${domain}`);
|
|
566
|
+
}
|
|
567
|
+
async rset() {
|
|
568
|
+
return await this.getok("RSET");
|
|
569
|
+
}
|
|
570
|
+
async mailfrom(fromAddr) {
|
|
571
|
+
const addr = this.requiresSmtputf8(fromAddr) && this.isCapable("SMTPUTF8")
|
|
572
|
+
? new Address(fromAddr, "SMTPUTF8")
|
|
573
|
+
: new Address(fromAddr);
|
|
574
|
+
return await this.getok([`MAIL FROM:<${addr.address}>`, ...addr.parameters].join(" "));
|
|
575
|
+
}
|
|
576
|
+
async rcpttoList(toAddrs, block) {
|
|
577
|
+
if (toAddrs.length === 0)
|
|
578
|
+
throw new ArgumentError("mail destination not given");
|
|
579
|
+
for (const addr of toAddrs.flat(Infinity))
|
|
580
|
+
await this.rcptto(addr);
|
|
581
|
+
return await block();
|
|
582
|
+
}
|
|
583
|
+
async rcptto(toAddr) {
|
|
584
|
+
const addr = new Address(toAddr);
|
|
585
|
+
return await this.getok([`RCPT TO:<${addr.address}>`, ...addr.parameters].join(" "));
|
|
586
|
+
}
|
|
587
|
+
async data(msgstr = null, block = null) {
|
|
588
|
+
if (msgstr != null && block != null) {
|
|
589
|
+
throw new ArgumentError("message and block are exclusive");
|
|
590
|
+
}
|
|
591
|
+
if (msgstr == null && block == null)
|
|
592
|
+
throw new ArgumentError("message or block is required");
|
|
593
|
+
const res = await this.critical(async () => {
|
|
594
|
+
this.checkContinue(await this.getResponse("DATA"));
|
|
595
|
+
const binary = msgstr instanceof Uint8Array;
|
|
596
|
+
const chunks = msgstr != null ? [binary ? binaryString(msgstr) : msgstr] : [];
|
|
597
|
+
if (block != null)
|
|
598
|
+
await block({ write: (str) => chunks.push(str) });
|
|
599
|
+
const body = chunks
|
|
600
|
+
.join("")
|
|
601
|
+
.replace(/\r\n|\n|\r/g, "\r\n")
|
|
602
|
+
.replace(/^\./gm, "..");
|
|
603
|
+
const tail = body.endsWith("\r\n") ? "" : "\r\n";
|
|
604
|
+
const message = `${body}${tail}.\r\n`;
|
|
605
|
+
this._socket._socket.write(binary ? binaryBytes(message) : message);
|
|
606
|
+
return await this.recvResponse();
|
|
607
|
+
});
|
|
608
|
+
this.checkResponse(res);
|
|
609
|
+
return res;
|
|
610
|
+
}
|
|
611
|
+
async quit() {
|
|
612
|
+
return await this.getok("QUIT");
|
|
613
|
+
}
|
|
614
|
+
async getResponse(reqline) {
|
|
615
|
+
this.validateLine(reqline);
|
|
616
|
+
this._socket._sendCommand(reqline);
|
|
617
|
+
return await this.recvResponse();
|
|
618
|
+
}
|
|
619
|
+
/** @internal */
|
|
620
|
+
validateLine(line) {
|
|
621
|
+
if (/[\r\n]/.test(line))
|
|
622
|
+
throw new ArgumentError("A line must not contain CR or LF");
|
|
623
|
+
}
|
|
624
|
+
/** @internal */
|
|
625
|
+
async getok(reqline) {
|
|
626
|
+
this.validateLine(reqline);
|
|
627
|
+
const res = await this.critical(async () => {
|
|
628
|
+
this._socket._sendCommand(reqline);
|
|
629
|
+
return await this.recvResponse();
|
|
630
|
+
});
|
|
631
|
+
this.checkResponse(res);
|
|
632
|
+
return res;
|
|
633
|
+
}
|
|
634
|
+
/** @internal */
|
|
635
|
+
recvResponse() {
|
|
636
|
+
const socket = this._socket;
|
|
637
|
+
return new Promise((resolve, reject) => {
|
|
638
|
+
const eof = () => reject(new EOFError("end of file reached"));
|
|
639
|
+
if (socket._destroyed)
|
|
640
|
+
return eof();
|
|
641
|
+
socket.once("end", eof);
|
|
642
|
+
const onError = (err) => err.code === "ECONNECTION" ? eof() : reject(err);
|
|
643
|
+
socket.once("error", onError);
|
|
644
|
+
socket._responseActions.push((str) => {
|
|
645
|
+
socket.removeListener("end", eof);
|
|
646
|
+
socket.removeListener("error", onError);
|
|
647
|
+
resolve(Response.parse(`${str}\n`));
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
/** @internal */
|
|
652
|
+
async critical(block) {
|
|
653
|
+
if (this._errorOccurred)
|
|
654
|
+
return Response.parse("200 dummy reply code");
|
|
655
|
+
try {
|
|
656
|
+
return await block();
|
|
657
|
+
}
|
|
658
|
+
catch (err) {
|
|
659
|
+
this._errorOccurred = true;
|
|
660
|
+
throw err;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
/** @internal */
|
|
664
|
+
checkResponse(res) {
|
|
665
|
+
if (!res.isSuccess())
|
|
666
|
+
throw new res.exceptionClass(res);
|
|
667
|
+
}
|
|
668
|
+
/** @internal */
|
|
669
|
+
checkContinue(res) {
|
|
670
|
+
if (!res.isContinue()) {
|
|
671
|
+
throw new SMTPUnknownError(res, {
|
|
672
|
+
message: `could not get 3xx (${res.status}: ${res.string})`,
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
/** @internal */
|
|
677
|
+
logging(msg) {
|
|
678
|
+
this._debugOutput?.write(`${msg}\n`);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
Net.SMTP = SMTP;
|
|
682
|
+
export const SMTPSession = SMTP;
|
|
683
|
+
async function _startSsl(io, options) {
|
|
684
|
+
const hostname = options.sslHostAddress;
|
|
685
|
+
const { ca, defaultPaths, minVersion, maxVersion } = options;
|
|
686
|
+
io.options.tls = {
|
|
687
|
+
...(ca != null ? { ca: defaultPaths ? [...rootCertificates, ...ca] : ca } : {}),
|
|
688
|
+
rejectUnauthorized: options.verifyPeer,
|
|
689
|
+
secureOptions: options.options,
|
|
690
|
+
...(minVersion !== undefined ? { minVersion } : {}),
|
|
691
|
+
...(maxVersion !== undefined ? { maxVersion } : {}),
|
|
692
|
+
...(isIP(hostname) === 0 ? { servername: hostname } : {}),
|
|
693
|
+
checkServerIdentity: (_host, cert) => options.verifyHostname ? checkServerIdentity(hostname, cert) : undefined,
|
|
694
|
+
};
|
|
695
|
+
try {
|
|
696
|
+
await new Promise((resolve, reject) => {
|
|
697
|
+
const cleanup = () => {
|
|
698
|
+
io.removeListener("error", reject);
|
|
699
|
+
io.removeListener("end", cleanup);
|
|
700
|
+
};
|
|
701
|
+
io.once("error", reject);
|
|
702
|
+
io.once("end", cleanup);
|
|
703
|
+
io._upgradeConnection((err) => {
|
|
704
|
+
cleanup();
|
|
705
|
+
if (err)
|
|
706
|
+
reject(err);
|
|
707
|
+
else
|
|
708
|
+
resolve();
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
catch (e) {
|
|
713
|
+
const err = e;
|
|
714
|
+
if (err.originalCode != null)
|
|
715
|
+
err.code = err.originalCode;
|
|
716
|
+
throw err;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
import "./auth-plain.js";
|
|
720
|
+
import "./auth-login.js";
|
|
721
|
+
import "./auth-cram-md5.js";
|
|
722
|
+
import "./auth-xoauth2.js";
|
|
723
|
+
import "./constants.generated.js";
|
|
724
|
+
//# sourceMappingURL=smtp.js.map
|