forked from eleith/emailjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp.js
603 lines (506 loc) · 16.2 KB
/
smtp.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
* SMTP class written using python's (2.7) smtplib.py as a base
*/
var net = require('net');
var crypto = require('crypto');
var os = require('os');
var tls = require('tls');
var util = require('util');
var events = require('events');
var starttls = require('./tls');
var SMTPResponse = require('./response');
var SMTPError = require('./error');
var SMTP_PORT = 25;
var SMTP_SSL_PORT = 465;
var SMTP_TLS_PORT = 587;
var CRLF = "\r\n";
var AUTH_METHODS = {PLAIN:'PLAIN', CRAM_MD5:'CRAM-MD5', LOGIN:'LOGIN', XOAUTH2: 'XOAUTH2'};
var TIMEOUT = 5000;
var DEBUG = 0;
var log = function()
{
if(DEBUG)
{
Array.prototype.slice.call(arguments).forEach(function(d) { console.log(d); });
}
};
var quotedata = function(data)
{
// Quote data for email.
// Double leading '.', and change Unix newline '\\n', or Mac '\\r' into
// Internet CRLF end-of-line.
return data.replace(/(?:\r\n|\n|\r(?!\n))/g, CRLF).replace(/^\./gm, '..');
};
var caller = function(callback)
{
if(typeof(callback) == 'function')
{
var args = Array.prototype.slice.call(arguments);
args.shift();
callback.apply(null, args);
}
};
var SMTPState =
{
NOTCONNECTED: 0,
CONNECTING: 1,
CONNECTED: 2
};
var SMTP = function(options)
{
events.EventEmitter.call(this);
options = options || {};
this.sock = null;
this.timeout = options.timeout || TIMEOUT;
this.features = null;
this._state = SMTPState.NOTCONNECTED;
this._secure = false;
this.loggedin = (options.user && options.password) ? false : true;
this.domain = options.domain || os.hostname();
this.host = options.host || 'localhost';
this.port = options.port || (options.ssl ? SMTP_SSL_PORT : options.tls ? SMTP_TLS_PORT : SMTP_PORT);
this.ssl = options.ssl || false;
this.tls = options.tls || false;
this.monitor = null;
// keep these strings hidden when quicky debugging/logging
this.user = function() { return options.user; };
this.password = function() { return options.password; };
};
SMTP.prototype =
{
debug: function(level)
{
DEBUG = level;
},
state: function()
{
return this._state;
},
authorized: function()
{
return this.loggedin;
},
connect: function(callback, port, host, options)
{
options = options || {};
var self = this;
self.host = host || self.host;
self.port = port || self.port;
self.ssl = options.ssl || self.ssl;
if(self._state != SMTPState.NOTCONNECTED)
self.quit();
var connected = function(err)
{
if(!err)
{
log("connected: " + self.host + ":" + self.port);
if(self.ssl && !self.tls)
{
// if key/ca/cert was passed in, check if connection is authorized
if(typeof(self.ssl) != 'boolean' && !self.sock.authorized)
{
self.close(true);
caller(callback, SMTPError('could not establish an ssl connection', SMTPError.CONNECTIONAUTH, err));
}
else
self._secure = true;
}
}
else
{
self.close(true);
caller(callback, SMTPError("could not connect", SMTPError.COULDNOTCONNECT, err));
}
};
var response = function(err, msg)
{
if(err)
{
self.close(true);
caller(callback, err);
}
else if(msg.code == '220')
{
log(msg.data);
// might happen first, so no need to wait on connected()
self._state = SMTPState.CONNECTED;
caller(callback, null, msg.data);
}
else
{
log("response (data): " + msg.data);
self.quit();
caller(callback, SMTPError("bad response on connection", SMTPError.BADRESPONSE, err, msg.data));
}
};
self._state = SMTPState.CONNECTING;
log("connecting: " + self.host + ":" + self.port);
if(self.ssl)
{
self.sock = tls.connect(self.port, self.host, self.ssl, connected);
}
else
{
self.sock = new net.Socket();
self.sock.connect(self.port, self.host, connected);
}
self.monitor = SMTPResponse.monitor(self.sock, self.timeout, function() { self.close(true); });
self.sock.once('response', response);
},
send: function(str, callback)
{
var self = this;
if(self.sock && self._state == SMTPState.CONNECTED)
{
log(str);
var response = function(err, msg)
{
if(err)
{
caller(callback, err);
}
else
{
log(msg.data);
caller(callback, null, msg);
}
};
self.sock.once('response', response);
self.sock.write(str);
}
else
{
self.close(true);
caller(callback, SMTPError('no connection has been established', SMTPError.NOCONNECTION));
}
},
command: function(cmd, callback, codes, failed)
{
codes = Array.isArray(codes) ? codes : typeof(codes) == 'number' ? [codes] : [250];
var response = function(err, msg)
{
if(err)
{
caller(callback, err);
}
else
{
if(codes.indexOf(Number(msg.code)) != -1)
caller(callback, err, msg.data, msg.message);
else
caller(callback, SMTPError("bad response on command '" + cmd.split(' ')[0] + "'", SMTPError.BADRESPONSE, null, msg.data));
}
};
this.send(cmd + CRLF, response);
},
helo: function(callback, domain)
{
/*
* SMTP 'helo' command.
* Hostname to send for self command defaults to the FQDN of the local
* host.
*/
this.command("helo " + (domain || this.domain), callback);
},
starttls: function(callback)
{
var self = this,
response = function(err, msg)
{
if(err)
{
err.message += " while establishing a starttls session";
caller(callback, err);
}
else
{
var secured_socket = null;
var secured = function()
{
self._secure = true;
self.sock = secured_socket;
var error = function(err)
{
self.close(true);
caller(callback, err);
};
SMTPResponse.monitor(self.sock, self.timeout, function() { self.close(true); });
caller(callback, msg.data);
};
secured_socket = starttls.secure(self.sock, self.ssl, secured);
}
};
this.command("starttls", response, [220]);
},
ehlo: function(callback, domain)
{
var self = this,
response = function(err, data)
{
if(err)
{
caller(callback, err);
}
else
{
// According to RFC1869 some (badly written)
// MTA's will disconnect on an ehlo. Toss an exception if
// that happens -ddm
data.split("\n").forEach(function(ext)
{
var parse = ext.match(/^(?:\d+[\-=]?)\s*?([^\s]+)(?:\s+(.*)\s*?)?$/);
// To be able to communicate with as many SMTP servers as possible,
// we have to take the old-style auth advertisement into account,
// because:
// 1) Else our SMTP feature parser gets confused.
// 2) There are some servers that only advertise the auth methods we
// support using the old style.
if(parse)
{
// RFC 1869 requires a space between ehlo keyword and parameters.
// It's actually stricter, in that only spaces are allowed between
// parameters, but were not going to check for that here. Note
// that the space isn't present if there are no parameters.
self.features[parse[1].toLowerCase()] = parse[2] || true;
}
});
if(self.tls && !self._secure)
self.starttls(function() { self.ehlo(callback, domain); });
else
caller(callback, err, data);
}
};
this.features = {};
this.command("ehlo " + (domain || this.domain), response);
},
has_extn: function(opt)
{
return this.features[opt.toLowerCase()] === undefined;
},
help: function(callback, args)
{
// SMTP 'help' command, returns text from the server
this.command(args ? "help " + args : "help", callback, [211, 214]);
},
rset: function(callback)
{
this.command("rset", callback);
},
noop: function(callback)
{
this.send("noop", callback);
},
mail: function(callback, from)
{
this.command("mail FROM:" + from, callback);
},
rcpt: function(callback, to)
{
this.command("RCPT TO:" + to, callback, [250, 251]);
},
data: function(callback)
{
this.command("data", callback, [354]);
},
data_end: function(callback)
{
this.command(CRLF + ".", callback);
},
message: function(data)
{
log(data);
this.sock.write(data);
},
verify: function(address, callback)
{
// SMTP 'verify' command -- checks for address validity."""
this.command("vrfy " + address, callback, [250, 251, 252]);
},
expn: function(address, callback)
{
// SMTP 'expn' command -- expands a mailing list.
this.command("expn " + address, callback);
},
ehlo_or_helo_if_needed: function(callback, domain)
{
// Call self.ehlo() and/or self.helo() if needed.
// If there has been no previous EHLO or HELO command self session, self
// method tries ESMTP EHLO first.
var self = this;
if(!this.features)
{
var response = function(err, data)
{
caller(callback, err, data);
};
var attempt = function(err)
{
if(err)
self.helo(response, domain);
else
caller(callback, err);
};
self.ehlo(attempt, domain);
}
},
login: function(callback, user, password, options)
{
var self = this,
login =
{
user: user ? function() { return user; } : self.user,
password: password ? function() { return password; } : self.password,
method: options && options.method ? options.method.toUpperCase() : ''
},
domain = options && options.domain ? options.domain : this.domain,
initiate = function(err, data)
{
if(err)
{
caller(callback, err);
return;
}
/*
* Log in on an SMTP server that requires authentication.
*
* The arguments are:
* - user: The user name to authenticate with.
* - password: The password for the authentication.
*
* If there has been no previous EHLO or HELO command self session, self
* method tries ESMTP EHLO first.
*
* This method will return normally if the authentication was successful.
*/
var method = null,
encode_cram_md5 = function(challenge)
{
challenge = (new Buffer(challenge, "base64")).toString("ascii");
var hmac = crypto.createHmac('md5', login.password());
hmac.update(challenge);
return (new Buffer(login.user() + " " + hmac.digest('hex')).toString("base64"));
},
encode_plain = function()
{
return (new Buffer("\0" + login.user() + "\0" + login.password())).toString("base64");
},
encode_xoauth2 = function()
{
// console.log("user=" + login.user() + "\1auth=Bearer " + login.password()+"\1\1");
// see: https://developers.google.com/gmail/xoauth2_protocol
return (new Buffer("user=" + login.user() + "\1auth=Bearer " + login.password()+"\1\1")).toString("base64");
};
// List of authentication methods we support: from preferred to
// less preferred methods.
if(!method)
{
var preferred = [AUTH_METHODS.CRAM_MD5, AUTH_METHODS.LOGIN, AUTH_METHODS.PLAIN, AUTH_METHODS.XOAUTH2];
for(var i = 0; i < preferred.length; i++)
{
if((self.features.auth || "").indexOf(preferred[i]) != -1)
{
method = preferred[i];
break;
}
}
}
// handle bad responses from command differently
var failed = function(err, data)
{
self.loggedin = false;
caller(callback, SMTPError('authorization.failed', SMTPError.AUTHFAILED, err, data));
};
var response = function(err, data)
{
if(err)
{
failed(err, data);
}
else
{
self.loggedin = true;
caller(callback, err, data);
}
};
var attempt = function(err, data, msg)
{
if(err)
{
failed(err, data);
}
else
{
if(method == AUTH_METHODS.CRAM_MD5)
self.command(encode_cram_md5(msg), response, [235, 503]);
else if(method == AUTH_METHODS.LOGIN)
self.command((new Buffer(login.password())).toString("base64"), response, [235, 503]);
}
};
var attempt_user = function(err, data, msg)
{
if(err)
{
failed(err, data);
}
else
{
if(method == AUTH_METHODS.LOGIN)
self.command((new Buffer(login.user())).toString("base64"), attempt, [334]);
}
};
if(method == AUTH_METHODS.CRAM_MD5)
self.command("AUTH " + AUTH_METHODS.CRAM_MD5, attempt, [334]);
else if(method == AUTH_METHODS.LOGIN)
self.command("AUTH " + AUTH_METHODS.LOGIN, attempt_user, [334]);
else if(method == AUTH_METHODS.PLAIN)
self.command("AUTH " + AUTH_METHODS.PLAIN + " " + encode_plain(login.user(), login.password()), response, [235, 503]);
else if(method == AUTH_METHODS.XOAUTH2)
self.command("AUTH " + AUTH_METHODS.XOAUTH2 + " " + encode_xoauth2(login.user(), login.password()), response, [235, 503]);
else if(!method)
caller(callback, SMTPError('no form of authorization supported', SMTPError.AUTHNOTSUPPORTED, null, data));
};
self.ehlo_or_helo_if_needed(initiate, domain);
},
close: function(force)
{
if(this.sock)
{
if(force)
{
log("smtp connection destroyed!");
this.sock.destroy();
}
else
{
log("smtp connection closed.");
this.sock.end();
}
}
if(this.monitor)
{
this.monitor.stop();
this.monitor = null;
}
this._state = SMTPState.NOTCONNECTED;
this._secure = false;
this.sock = null;
this.features = null;
this.loggedin = !(this.user() && this.password());
},
quit: function(callback)
{
var self = this,
response = function(err, data)
{
caller(callback, err, data);
self.close();
};
this.command("quit", response, [221, 250]);
}
};
for(var each in events.EventEmitter.prototype)
{
SMTP.prototype[each] = events.EventEmitter.prototype[each];
}
exports.SMTP = SMTP;
exports.state = SMTPState;