/
client.js
1039 lines (899 loc) · 30.4 KB
/
client.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var Stream = require("stream").Stream,
utillib = require("util"),
net = require("net"),
tls = require("tls"),
oslib = require("os"),
xoauth2 = require("xoauth2"),
crypto = require("crypto"),
fs = require('fs');
// expose to the world
module.exports = function(port, host, options){
var connection = new SMTPClient(port, host, options);
process.nextTick(connection.connect.bind(connection));
return connection;
};
/**
* <p>Generates a SMTP connection object</p>
*
* <p>Optional options object takes the following possible properties:</p>
* <ul>
* <li><b>secureConnection</b> - use SSL</li>
* <li><b>name</b> - the name of the client server</li>
* <li><b>auth</b> - authentication object <code>{user:"...", pass:"..."}</code>
* <li><b>ignoreTLS</b> - ignore server support for STARTTLS</li>
* <li><b>tls</b> - options for createCredentials</li>
* <li><b>debug</b> - output client and server messages to console</li>
* <li><b>logFile</b> - output client and server messages to file</li>
* <li><b>instanceId</b> - unique instance id for debugging</li>
* <li><b>localAddress</b> - outbound address to bind to (see: http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener)</li>
* </ul>
*
* @constructor
* @namespace SMTP Client module
* @param {Number} [port=25] Port number to connect to
* @param {String} [host="localhost"] Hostname to connect to
* @param {Object} [options] Option properties
*/
function SMTPClient(port, host, options){
Stream.call(this);
this.writable = true;
this.readable = true;
this.stage = "init";
this.options = options || {};
this.port = port || (this.options.secureConnection ? 465 : 25);
this.host = host || "localhost";
this.options.secureConnection = !!this.options.secureConnection;
this.options.auth = this.options.auth || false;
this.options.maxConnections = this.options.maxConnections || 5;
if(!this.options.name){
// defaul hostname is machine hostname or [IP]
var defaultHostname = (oslib.hostname && oslib.hostname()) || "";
if(defaultHostname.indexOf('.')<0){
defaultHostname = "[127.0.0.1]";
}
if(defaultHostname.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)){
defaultHostname = "["+defaultHostname+"]";
}
this.options.name = defaultHostname;
}
this._init();
}
utillib.inherits(SMTPClient, Stream);
/**
* <p>Initializes instance variables</p>
*/
SMTPClient.prototype._init = function(){
/**
* Defines if the current connection is secure or not. If not,
* STARTTLS can be used if available
* @private
*/
this._secureMode = false;
/**
* Ignore incoming data on TLS negotiation
* @private
*/
this._ignoreData = false;
/**
* Store incomplete messages coming from the server
* @private
*/
this._remainder = "";
/**
* If set to true, then this object is no longer active
* @private
*/
this.destroyed = false;
/**
* The socket connecting to the server
* @publick
*/
this.socket = false;
/**
* Lists supported auth mechanisms
* @private
*/
this._supportedAuth = [];
/**
* Currently in data transfer state
* @private
*/
this._dataMode = false;
/**
* Keep track if the client sends a leading \r\n in data mode
* @private
*/
this._lastDataBytes = new Buffer(2);
/**
* Function to run if a data chunk comes from the server
* @private
*/
this._currentAction = false;
/**
* Timeout variable for waiting the greeting
* @private
*/
this._greetingTimeout = false;
/**
* Timeout variable for waiting the connection to start
* @private
*/
this._connectionTimeout = false;
if(this.options.ignoreTLS || this.options.secureConnection){
this._secureMode = true;
}
/**
* XOAuth2 token generator if XOAUTH2 auth is used
* @private
*/
this._xoauth2 = false;
if(typeof this.options.auth.XOAuth2 == "object" && typeof this.options.auth.XOAuth2.getToken == "function"){
this._xoauth2 = this.options.auth.XOAuth2;
}else if(typeof this.options.auth.XOAuth2 == "object"){
if(!this.options.auth.XOAuth2.user && this.options.auth.user){
this.options.auth.XOAuth2.user = this.options.auth.user;
}
this._xoauth2 = xoauth2.createXOAuth2Generator(this.options.auth.XOAuth2);
}
};
/**
* <p>Creates a connection to a SMTP server and sets up connection
* listener</p>
*/
SMTPClient.prototype.connect = function(){
var opts = {};
if(this.options.secureConnection){
if (this.options.tls) {
Object.keys(this.options.tls).forEach((function(key) {
opts[key] = this.options.tls[key];
}).bind(this));
}
if(!("rejectUnauthorized" in opts)){
opts.rejectUnauthorized = !!this.options.rejectUnauthorized;
}
if(this.options.localAddress){
opts.localAddress = this.options.localAddress;
}
this.socket = tls.connect(this.port, this.host, opts, this._onConnect.bind(this));
}else{
opts = {
port: this.port,
host: this.host
};
if(this.options.localAddress){
opts.localAddress = this.options.localAddress;
}
this.socket = net.connect(opts, this._onConnect.bind(this));
}
if(this.options.connectionTimeout){
this._connectionTimeout = setTimeout((function(){
var error = new Error("Connection timeout");
error.code = "ETIMEDOUT";
error.errno = "ETIMEDOUT";
error.stage = this.stage;
this.emit("error", error);
this.close();
}).bind(this), this.options.connectionTimeout);
}
this.socket.on("error", this._onError.bind(this));
};
/**
* <p>Upgrades the connection to TLS</p>
*
* @param {Function} callback Callback function to run when the connection
* has been secured
*/
SMTPClient.prototype._upgradeConnection = function(callback){
this._ignoreData = true;
this.socket.removeAllListeners("data");
this.socket.removeAllListeners("error");
var opts = {
socket: this.socket,
host: this.host,
rejectUnauthorized: !!this.options.rejectUnauthorized
};
Object.keys(this.options.tls || {}).forEach((function(key){
opts[key] = this.options.tls[key];
}).bind(this));
this.socket = tls.connect(opts, (function(){
this._ignoreData = false;
this._secureMode = true;
this.socket.on("data", this._onData.bind(this));
return callback(null, true);
}).bind(this));
this.socket.on("error", this._onError.bind(this));
};
/**
* <p>Connection listener that is run when the connection to
* the server is opened</p>
*
* @event
*/
SMTPClient.prototype._onConnect = function(){
this.stage = "connect";
clearTimeout(this._connectionTimeout);
if("setKeepAlive" in this.socket){
this.socket.setKeepAlive(true);
}
if("setNoDelay" in this.socket){
this.socket.setNoDelay(true);
}
this.socket.on("data", this._onData.bind(this));
this.socket.on("close", this._onClose.bind(this));
this.socket.on("end", this._onEnd.bind(this));
this.socket.setTimeout(3 * 3600 * 1000); // 1 hours
this.socket.on("timeout", this._onTimeout.bind(this));
this._greetingTimeout = setTimeout((function(){
// if strill waiting for greeting, give up
if(this.socket && !this._destroyed && this._currentAction == this._actionGreeting){
var error = new Error("Greeting never received");
error.code = "ETIMEDOUT";
error.errno = "ETIMEDOUT";
error.stage = this.stage;
this.emit("error", error);
this.close();
}
}).bind(this), this.options.greetingTimeout || 10000);
this._currentAction = this._actionGreeting;
};
/**
* <p>Destroys the client - removes listeners etc.</p>
*/
SMTPClient.prototype._destroy = function(){
if(this._destroyed)return;
this._destroyed = true;
this._ignoreData = true;
this.emit("end");
this.removeAllListeners();
};
/**
* <p>'data' listener for data coming from the server</p>
*
* @event
* @param {Buffer} chunk Data chunk coming from the server
*/
SMTPClient.prototype._onData = function(chunk){
var str;
if(this._ignoreData || !chunk || !chunk.length){
return;
}
// Wait until end of line
if(chunk.readUInt8(chunk.length-1) != 0x0A){
this._remainder += chunk.toString();
return;
}else{
str = (this._remainder + chunk.toString()).trim();
this._remainder = "";
}
// if this is a multi line reply, wait until the ending
if(str.match(/(?:^|\n)\d{3}-.+$/)){
this._remainder = str + "\r\n";
return;
}
if(this.options.debug){
console.log("SERVER"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+str.replace(/\r?\n/g,"\n "));
}
if(this.options.logFile){
this.log("SERVER"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+str.replace(/\r?\n/g,"\n ")
);
}
if(typeof this._currentAction == "function"){
this._currentAction.call(this, str);
}
};
/**
* <p>'error' listener for the socket</p>
*
* @event
* @param {Error} err Error object
* @param {String} type Error name
*/
SMTPClient.prototype._onError = function(err, type, data){
if(type && type != "Error"){
err.name = type;
}
if(data){
err.data = data;
}
err.stage = this.stage;
this.emit("error", err);
this.close();
};
/**
* <p>'close' listener for the socket</p>
*
* @event
*/
SMTPClient.prototype._onClose = function(){
this.stage = "close";
this._destroy();
};
/**
* <p>'end' listener for the socket</p>
*
* @event
*/
SMTPClient.prototype._onEnd = function(){
this.stage = "end";
this._destroy();
};
/**
* <p>'timeout' listener for the socket</p>
*
* @event
*/
SMTPClient.prototype._onTimeout = function(){
this.close();
};
/**
* <p>Passes data stream to socket if in data mode</p>
*
* @param {Buffer} chunk Chunk of data to be sent to the server
*/
SMTPClient.prototype.write = function(chunk){
// works only in data mode
if(!this._dataMode || this._destroyed){
// this line should never be reached but if it does, then
// say act like everything's normal.
return true;
}
if(typeof chunk == "string"){
chunk = new Buffer(chunk, "utf-8");
}
if(chunk.length > 2){
this._lastDataBytes[0] = chunk[chunk.length-2];
this._lastDataBytes[1] = chunk[chunk.length-1];
}else if(chunk.length == 1){
this._lastDataBytes[0] = this._lastDataBytes[1];
this._lastDataBytes[1] = chunk[0];
}
if(this.options.debug){
console.log("CLIENT (DATA)"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+chunk.toString().trim().replace(/\n/g,"\n "));
}
if(this.options.logFile){
this.log("CLIENT (DATA)"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+chunk.toString().trim().replace(/\n/g,"\n "));
}
// pass the chunk to the socket
return this.socket.write(chunk);
};
/**
* <p>Indicates that a data stream for the socket is ended. Works only
* in data mode.</p>
*
* @param {Buffer} [chunk] Chunk of data to be sent to the server
*/
SMTPClient.prototype.end = function(chunk){
// works only in data mode
if(!this._dataMode || this._destroyed){
// this line should never be reached but if it does, then
// say act like everything's normal.
return true;
}
if(chunk && chunk.length){
this.write(chunk);
}
// redirect output from the server to _actionStream
this._currentAction = this._actionStream;
// indicate that the stream has ended by sending a single dot on its own line
// if the client already closed the data with \r\n no need to do it again
if(this._lastDataBytes[0] == 0x0D && this._lastDataBytes[1] == 0x0A){
this.socket.write(new Buffer(".\r\n", "utf-8"));
}else if(this._lastDataBytes[1] == 0x0D){
this.socket.write(new Buffer("\n.\r\n"));
}else{
this.socket.write(new Buffer("\r\n.\r\n"));
}
// end data mode
this._dataMode = false;
};
/**
* <p>Send a command to the server, append \r\n</p>
*
* @param {String} str String to be sent to the server
*/
SMTPClient.prototype.sendCommand = function(str){
if(this._destroyed){
// Connection already closed, can't send any more data
return;
}
if(this.options.debug){
console.log("CLIENT"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+(str || "").toString().trim().replace(/\n/g,"\n "));
}
if(this.options.logFile){
this.log("CLIENT"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+(str || "").toString().trim().replace(/\n/g,"\n "));
}
this.socket.write(new Buffer(str+"\r\n", "utf-8"));
};
/**
* <p>Sends QUIT</p>
*/
SMTPClient.prototype.quit = function(){
this.sendCommand("QUIT");
this._currentAction = this.close;
};
/**
* <p>Closes the connection to the server</p>
*/
SMTPClient.prototype.close = function(){
if(this.options.debug){
console.log("Closing connection to the server");
}
if(this.options.logFile){
this.log("Closing connection to the server");
}
var closeMethod = "end";
if (this.stage === "init") {
// Clear connection timeout timer if other than timeout error occurred
clearTimeout(this._connectionTimeout);
// Close the socket immediately when connection timed out
closeMethod = "destroy";
}
if(this.socket && this.socket.socket && this.socket.socket[closeMethod] && !this.socket.socket.destroyed){
this.socket.socket[closeMethod]();
}
if(this.socket && this.socket[closeMethod] && !this.socket.destroyed){
this.socket[closeMethod]();
}
this._destroy();
};
/**
* <p>Initiates a new message by submitting envelope data, starting with
* <code>MAIL FROM:</code> command</p>
*
* @param {Object} envelope Envelope object in the form of
* <code>{from:"...", to:["..."]}</code>
*/
SMTPClient.prototype.useEnvelope = function(envelope){
this._envelope = envelope || {};
this._envelope.from = this._envelope.from || ("anonymous@"+this.options.name);
// clone the recipients array for latter manipulation
this._envelope.rcptQueue = JSON.parse(JSON.stringify(this._envelope.to || []));
this._envelope.rcptFailed = [];
this._currentAction = this._actionMAIL;
this.sendCommand("MAIL FROM:<"+(this._envelope.from)+">");
};
/**
* <p>If needed starts the authentication, if not emits 'idle' to
* indicate that this client is ready to take in an outgoing mail</p>
*/
SMTPClient.prototype._authenticateUser = function(){
this.stage = "auth";
if(!this.options.auth){
// no need to authenticate, at least no data given
this._currentAction = this._actionIdle;
this.emit("idle"); // ready to take orders
return;
}
var auth;
if(this.options.auth.XOAuthToken && this._supportedAuth.indexOf("XOAUTH")>=0){
auth = "XOAUTH";
}else if(this._xoauth2 && this._supportedAuth.indexOf("XOAUTH2")>=0){
auth = "XOAUTH2";
}else if(this.options.authMethod) {
auth = this.options.authMethod.toUpperCase().trim();
}else{
// use first supported
auth = (this._supportedAuth[0] || "PLAIN").toUpperCase().trim();
}
switch(auth){
case "XOAUTH":
this._currentAction = this._actionAUTHComplete;
if(typeof this.options.auth.XOAuthToken == "object" &&
typeof this.options.auth.XOAuthToken.generate == "function"){
this.options.auth.XOAuthToken.generate((function(err, XOAuthToken){
if(err){
return this._onError(err, "XOAuthTokenError");
}
this.sendCommand("AUTH XOAUTH " + XOAuthToken);
}).bind(this));
}else{
this.sendCommand("AUTH XOAUTH " + this.options.auth.XOAuthToken.toString());
}
return;
case "XOAUTH2":
this._currentAction = this._actionAUTHComplete;
this._xoauth2.getToken((function(err, token){
if(err){
this._onError(err, "XOAUTH2Error");
return;
}
this.sendCommand("AUTH XOAUTH2 " + token);
}).bind(this));
return;
case "LOGIN":
this._currentAction = this._actionAUTH_LOGIN_USER;
this.sendCommand("AUTH LOGIN");
return;
case "PLAIN":
this._currentAction = this._actionAUTHComplete;
this.sendCommand("AUTH PLAIN "+new Buffer(
//this.options.auth.user+"\u0000"+
"\u0000"+ // skip authorization identity as it causes problems with some servers
this.options.auth.user+"\u0000"+
this.options.auth.pass,"utf-8").toString("base64"));
return;
case "CRAM-MD5":
this._currentAction = this._actionAUTH_CRAM_MD5;
this.sendCommand("AUTH CRAM-MD5");
return;
}
this._onError(new Error("Unknown authentication method - "+auth), "UnknowAuthError");
};
/** ACTIONS **/
/**
* <p>Will be run after the connection is created and the server sends
* a greeting. If the incoming message starts with 220 initiate
* SMTP session by sending EHLO command</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionGreeting = function(str){
this.stage = "greeting";
clearTimeout(this._greetingTimeout);
if(str.substr(0,3) != "220"){
this._onError(new Error("Invalid greeting from server - "+str), false, str);
return;
}
this._currentAction = this._actionEHLO;
this.sendCommand("EHLO "+this.options.name);
};
/**
* <p>Handles server response for EHLO command. If it yielded in
* error, try HELO instead, otherwise initiate TLS negotiation
* if STARTTLS is supported by the server or move into the
* authentication phase.</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionEHLO = function(str){
this.stage = "ehlo";
if(str.substr(0, 3) == "421") {
this._onError(new Error("Server terminates connection - "+str), false, str);
return;
}
if(str.charAt(0) != "2"){
// Try HELO instead
this._currentAction = this._actionHELO;
this.sendCommand("HELO "+this.options.name);
return;
}
// Detect if the server supports STARTTLS
if(!this._secureMode && str.match(/[ \-]STARTTLS\r?$/mi)){
this.sendCommand("STARTTLS");
this._currentAction = this._actionSTARTTLS;
return;
}
// Detect if the server supports PLAIN auth
if(str.match(/AUTH(?:\s+[^\n]*\s+|\s+)PLAIN/i)){
this._supportedAuth.push("PLAIN");
}
// Detect if the server supports LOGIN auth
if(str.match(/AUTH(?:\s+[^\n]*\s+|\s+)LOGIN/i)){
this._supportedAuth.push("LOGIN");
}
// Detect if the server supports CRAM-MD5 auth
if(str.match(/AUTH(?:\s+[^\n]*\s+|\s+)CRAM-MD5/i)){
this._supportedAuth.push("CRAM-MD5");
}
// Detect if the server supports XOAUTH auth
if(str.match(/AUTH(?:\s+[^\n]*\s+|\s+)XOAUTH/i)){
this._supportedAuth.push("XOAUTH");
}
// Detect if the server supports XOAUTH2 auth
if(str.match(/AUTH(?:\s+[^\n]*\s+|\s+)XOAUTH2/i)){
this._supportedAuth.push("XOAUTH2");
}
this._authenticateUser.call(this);
};
/**
* <p>Handles server response for HELO command. If it yielded in
* error, emit 'error', otherwise move into the authentication phase.</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionHELO = function(str){
this.stage = "helo";
if(str.charAt(0) != "2"){
this._onError(new Error("Invalid response for EHLO/HELO - "+str), false, str);
return;
}
this._authenticateUser.call(this);
};
/**
* <p>Handles server response for STARTTLS command. If there's an error
* try HELO instead, otherwise initiate TLS upgrade. If the upgrade
* succeedes restart the EHLO</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionSTARTTLS = function(str){
this.stage = "starttls";
if(str.charAt(0) != "2"){
// Try HELO instead
this._currentAction = this._actionHELO;
this.sendCommand("HELO "+this.options.name);
return;
}
this._upgradeConnection((function(err, secured){
if(err){
this._onError(new Error("Error initiating TLS - "+(err.message || err)), "TLSError");
return;
}
if(this.options.debug){
console.log("Connection secured");
}
if(this.options.logFile){
this.log("Connection secured");
}
if(secured){
// restart session
this._currentAction = this._actionEHLO;
this.sendCommand("EHLO "+this.options.name);
}else{
this._authenticateUser.call(this);
}
}).bind(this));
};
/**
* <p>Handle the response for AUTH LOGIN command. We are expecting
* '334 VXNlcm5hbWU6' (base64 for 'Username:'). Data to be sent as
* response needs to be base64 encoded username.</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionAUTH_LOGIN_USER = function(str){
if(str != "334 VXNlcm5hbWU6"){
this._onError(new Error("Invalid login sequence while waiting for '334 VXNlcm5hbWU6' - "+str), false, str);
return;
}
this._currentAction = this._actionAUTH_LOGIN_PASS;
this.sendCommand(new Buffer(
this.options.auth.user, "utf-8").toString("base64"));
};
/**
* <p>Handle the response for AUTH CRAM-MD5 command. We are expecting
* '334 <challenge string>'. Data to be sent as response needs to be
* base64 decoded challenge string, MD5 hashed using the password as
* a HMAC key, prefixed by the username and a space, and finally all
* base64 encoded again.</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionAUTH_CRAM_MD5 = function(str) {
var challengeMatch = str.match(/^334\s+(.+)$/),
challengeString = "";
if (!challengeMatch) {
this._onError(new Error("Invalid login sequence while waiting for server challenge string - "+str), false, str);
return;
} else {
challengeString = challengeMatch[1];
}
// Decode from base64
var base64decoded = new Buffer(challengeString, 'base64').toString('ascii'),
hmac_md5 = crypto.createHmac('md5', this.options.auth.pass);
hmac_md5.update(base64decoded);
var hex_hmac = hmac_md5.digest('hex'),
prepended = this.options.auth.user + " " + hex_hmac;
this._currentAction = this._actionAUTH_CRAM_MD5_PASS;
this.sendCommand(new Buffer(prepended).toString("base64"));
};
/**
* <p>Handles the response to CRAM-MD5 authentication, if there's no error,
* the user can be considered logged in. Emit 'idle' and start
* waiting for a message to send</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionAUTH_CRAM_MD5_PASS = function(str) {
if (!str.match(/^235\s+/)) {
this._onError(new Error("Invalid login sequence while waiting for '235 go ahead' - "+str), false, str);
return;
}
this._currentAction = this._actionIdle;
this.emit("idle"); // ready to take orders
};
/**
* <p>Handle the response for AUTH LOGIN command. We are expecting
* '334 UGFzc3dvcmQ6' (base64 for 'Password:'). Data to be sent as
* response needs to be base64 encoded password.</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionAUTH_LOGIN_PASS = function(str){
if(str != "334 UGFzc3dvcmQ6"){
this._onError(new Error("Invalid login sequence while waiting for '334 UGFzc3dvcmQ6' - "+str), false, str);
return;
}
this._currentAction = this._actionAUTHComplete;
this.sendCommand(new Buffer(this.options.auth.pass, "utf-8").toString("base64"));
};
/**
* <p>Handles the response for authentication, if there's no error,
* the user can be considered logged in. Emit 'idle' and start
* waiting for a message to send</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionAUTHComplete = function(str){
var response;
if(this._xoauth2 && str.substr(0, 3) == "334"){
try{
response = str.split(" ");
response.shift();
response = JSON.parse(new Buffer(response.join(" "), "base64").toString("utf-8"));
if((!this._xoauth2.reconnectCount || this._xoauth2.reconnectCount < 200) && ['400','401'].indexOf(response.status)>=0){
this._xoauth2.reconnectCount = (this._xoauth2.reconnectCount || 0) + 1;
this._currentAction = this._actionXOAUTHRetry;
}else{
this._xoauth2.reconnectCount = 0;
this._currentAction = this._actionAUTHComplete;
}
this.sendCommand(new Buffer(0));
return;
}catch(E){}
}
this._xoauth2.reconnectCount = 0;
if(str.charAt(0) != "2"){
this._onError(new Error("Invalid login - "+str), "AuthError", str);
return;
}
this._currentAction = this._actionIdle;
this.emit("idle"); // ready to take orders
};
/**
* If XOAUTH2 authentication failed, try again by generating
* new access token
*/
SMTPClient.prototype._actionXOAUTHRetry = function(){
// ensure that something is listening unexpected responses
this._currentAction = this._actionIdle;
this._xoauth2.generateToken((function(err, token){
if(this._destroyed){
// Nothing to do here anymore, connection already closed
return;
}
if(err){
this._onError(err, "XOAUTH2Error");
return;
}
this._currentAction = this._actionAUTHComplete;
this.sendCommand("AUTH XOAUTH2 " + token);
}).bind(this));
};
/**
* <p>This function is not expected to run. If it does then there's probably
* an error (timeout etc.)</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionIdle = function(str){
this.stage = "idle";
if(Number(str.charAt(0)) > 3){
this._onError(new Error(str), false, str);
return;
}
// this line should never get called
};
/**
* <p>Handle response for a <code>MAIL FROM:</code> command</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionMAIL = function(str){
this.stage = "mail";
if(Number(str.charAt(0)) != "2"){
this._onError(new Error("Mail from command failed - " + str), "SenderError", str);
return;
}
if(!this._envelope.rcptQueue.length){
this._onError(new Error("Can't send mail - no recipients defined"), "RecipientError", str);
}else{
this._envelope.curRecipient = this._envelope.rcptQueue.shift();
this._currentAction = this._actionRCPT;
this.sendCommand("RCPT TO:<"+this._envelope.curRecipient+">"+this._getDSN());
}
};
/**
* <p>SetsUp DSN</p>
*/
SMTPClient.prototype._getDSN = function(){
var ret = "", n = [], dsn;
if (this.currentMessage && this.currentMessage.options && "dsn" in this.currentMessage.options){
dsn = this.currentMessage.options.dsn;
if(dsn.success){
n.push("SUCCESS");
}
if(dsn.failure){
n.push("FAILURE");
}
if(dsn.delay){
n.push("DELAY");
}
if(n.length > 0){
ret = " NOTIFY=" + n.join(",") + " ORCPT=rfc822;" + this.currentMessage._message.from;
}
}
return ret;
};
/**
* <p>Handle response for a <code>RCPT TO:</code> command</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionRCPT = function(str){
this.stage = "rcpt";
if(Number(str.charAt(0)) != "2"){
// this is a soft error
this._envelope.rcptFailed.push(this._envelope.curRecipient);
}
if(!this._envelope.rcptQueue.length){
if(this._envelope.rcptFailed.length < this._envelope.to.length){
if(this._envelope.rcptFailed.length){
this.emit("rcptFailed", this._envelope.rcptFailed);
}
this._currentAction = this._actionDATA;
this.sendCommand("DATA");
}else{
this._onError(new Error("Can't send mail - all recipients were rejected"), "RecipientError", str);
return;
}
}else{
this._envelope.curRecipient = this._envelope.rcptQueue.shift();
this._currentAction = this._actionRCPT;
this.sendCommand("RCPT TO:<"+this._envelope.curRecipient+">");
}
};
/**
* <p>Handle response for a <code>DATA</code> command</p>
*
* @param {String} str Message from the server
*/
SMTPClient.prototype._actionDATA = function(str){
this.stage = "data";
// response should be 354 but according to this issue https://github.com/eleith/emailjs/issues/24
// some servers might use 250 instead, so lets check for 2 or 3 as the first digit
if([2,3].indexOf(Number(str.charAt(0)))<0){
this._onError(new Error("Data command failed - " + str), false, str);
return;