@@ -23,7 +23,7 @@ const Stream = require("stream");
2323const Event = require ( "events" ) ;
2424const Uuid = require ( "node-uuid" ) ;
2525const Async = require ( "async" ) ;
26- const crypto = require ( "crypto" ) ;
26+ const Crypto = require ( "crypto" ) ;
2727const path_1 = require ( "path" ) ;
2828const os_1 = require ( "os" ) ;
2929const MAX_INT = 9007199254740992 ;
@@ -94,7 +94,7 @@ class ImapServerSwitchStream extends Stream.Transform {
9494 clearTimeout ( this . idleNextStop ) ;
9595 clearTimeout ( this . idleResponsrTime ) ;
9696 this . cmd = this . runningCommand = `DONE` ;
97- const cc = crypto . randomBytes ( 10 ) . toString ( 'base64' ) ;
97+ const cc = Crypto . randomBytes ( 10 ) . toString ( 'base64' ) ;
9898 this . debug ? debugOut ( this . cmd + `【${ cc } 】` , false ) : null ;
9999 if ( this . writable ) {
100100 this . idleResponsrTime = setTimeout ( ( ) => {
@@ -136,7 +136,7 @@ class ImapServerSwitchStream extends Stream.Transform {
136136 if ( / ^ o k $ / i. test ( cmdArray [ 1 ] ) ) {
137137 if ( / ^ I D L E $ / i. test ( cmdArray [ 0 ] ) )
138138 clearTimeout ( this . idleResponsrTime ) ;
139- this . doCommandCallback ( ) ;
139+ this . doCommandCallback ( null , commandLine ) ;
140140 return callback ( ) ;
141141 }
142142 this . doCommandCallback ( new Error ( cmdArray . slice ( 2 ) . join ( ' ' ) ) ) ;
@@ -285,7 +285,10 @@ class ImapServerSwitchStream extends Stream.Transform {
285285 haveMoreNewMail = _moreNew ;
286286 this . flagsDeleted ( newMailIds , next ) ;
287287 } ,
288- next => this . expunge ( next )
288+ ( data , next ) => {
289+ saveLog ( `Async.waterfall ( data[${ typeof data } ], next ${ typeof next } ) ` ) ;
290+ this . expunge ( next ) ;
291+ }
289292 ] , ( err , newMail ) => {
290293 this . runningCommand = null ;
291294 if ( err )
@@ -380,7 +383,7 @@ class ImapServerSwitchStream extends Stream.Transform {
380383 const name = this . imapServer . idleSupport ? 'IDLE' : 'NOOP' ;
381384 this . Tag = `${ name } ` ;
382385 this . cmd = `${ name } ${ name } ` ;
383- const cc = crypto . randomBytes ( 10 ) . toString ( 'base64' ) ;
386+ const cc = Crypto . randomBytes ( 10 ) . toString ( 'base64' ) ;
384387 this . debug ? debugOut ( this . cmd + `【${ cc } 】` , false ) : null ;
385388 if ( this . writable ) {
386389 this . idleResponsrTime = setTimeout ( ( ) => {
@@ -494,10 +497,10 @@ class ImapServerSwitchStream extends Stream.Transform {
494497 return this . logout_process ( this . waitLogoutCallBack ) ;
495498 }
496499 this . canDoLogout = false ;
497- this . doCommandCallback = err => {
500+ this . doCommandCallback = ( err , info ) => {
498501 this . canDoLogout = true ;
499502 this . checkLogout ( ( ) => {
500- CallBack ( err ) ;
503+ CallBack ( err , info ) ;
501504 } ) ;
502505 } ;
503506 let out = `Content-Type: application/octet-stream\r\nContent-Disposition: attachment\r\nMessage-ID:<${ Uuid . v4 ( ) } @>${ this . imapServer . domainName } \r\nContent-Transfer-Encoding: base64\r\nMIME-Version: 1.0\r\n\r\n${ text } ` ;
@@ -708,8 +711,10 @@ class ImapServerSwitchStream extends Stream.Transform {
708711 return this . imapServer . destroyAll ( null ) ;
709712 }
710713 expunge ( callback ) {
714+ console . trace ( callback . toString ( ) ) ;
711715 let newSwitchRet = false ;
712716 this . doCommandCallback = ( err ) => {
717+ console . trace ( callback . toString ( ) ) ;
713718 return callback ( err , newSwitchRet ) ;
714719 } ;
715720 this . commandProcess = ( text , cmdArray , next , _callback ) => {
@@ -796,9 +801,11 @@ class qtGateImap extends Event.EventEmitter {
796801 else {
797802 const jj = Tls . connect ( { rejectUnauthorized : ! this . IMapConnect . imapIgnoreCertificate , host : this . IMapConnect . imapServer , port : this . port } , ( ) => {
798803 jj . once ( 'error' , err => {
804+ saveLog ( `jj.once ( 'error' ) listenFolder[${ this . listenFolder } ] writeFolder [${ this . writeFolder } ]` ) ;
799805 this . destroyAll ( err ) ;
800806 } ) ;
801807 jj . once ( 'end' , ( ) => {
808+ saveLog ( `jj.once ( 'end' ) listenFolder[${ this . listenFolder } ] writeFolder [${ this . writeFolder } ]` ) ;
802809 this . destroyAll ( null ) ;
803810 } ) ;
804811 this . socket = jj ;
@@ -837,7 +844,7 @@ class qtGateImapwrite extends qtGateImap {
837844 this . canAppend = false ;
838845 this . appendPool = [ ] ;
839846 this . once ( 'ready' , ( ) => {
840- console . log ( `qtGateImapwrite [${ writeFolder } ] ready` ) ;
847+ console . log ( `qtGateImapWrite [${ writeFolder } ] ready` ) ;
841848 this . ready = this . canAppend = true ;
842849 } ) ;
843850 }
@@ -881,13 +888,67 @@ exports.getMailAttached = (email) => {
881888 const attachment = email . slice ( attachmentStart + 4 ) ;
882889 return Buffer . from ( attachment . toString ( ) , 'base64' ) ;
883890} ;
891+ exports . imapBasicTest = ( IMapConnect , CallBack ) => {
892+ saveLog ( `start imapBasicTest imap [${ JSON . stringify ( IMapConnect ) } ]` ) ;
893+ let callbackCall = false ;
894+ let startTime = null ;
895+ let wImap = null ;
896+ const listenFolder = Crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
897+ const ramdomText = Crypto . randomBytes ( 20 ) ;
898+ const timeout = setTimeout ( ( ) => {
899+ if ( rImap ) {
900+ rImap . logout ( ) ;
901+ }
902+ rImap = null ;
903+ if ( wImap ) {
904+ wImap . logout ( ) ;
905+ }
906+ wImap = null ;
907+ doCallBack ( new Error ( 'timeout' ) , null ) ;
908+ } , 15000 ) ;
909+ const doCallBack = ( err , ret ) => {
910+ if ( ! callbackCall ) {
911+ callbackCall = true ;
912+ clearTimeout ( timeout ) ;
913+ return CallBack ( err , ret ) ;
914+ }
915+ } ;
916+ let rImap = new qtGateImapRead ( IMapConnect , listenFolder , true , true , ( ) => {
917+ } ) ;
918+ rImap . once ( 'ready' , ( ) => {
919+ console . log ( `imapBasicTest rImap.once ( 'ready' )` ) ;
920+ rImap . logout ( ) ;
921+ rImap = null ;
922+ wImap = new qtGateImapwrite ( IMapConnect , listenFolder ) ;
923+ wImap . once ( 'ready' , ( ) => {
924+ console . log ( `imapBasicTest wImap.once ( 'ready' )` ) ;
925+ startTime = new Date ( ) . getTime ( ) ;
926+ wImap . append ( ramdomText . toString ( 'base64' ) , ( err , info ) => {
927+ if ( err ) {
928+ console . log ( ` imapBasicTest wImap.append err` , err ) ;
929+ }
930+ else {
931+ console . log ( `imapBasicTest wImap.append success [${ info } ]` ) ;
932+ }
933+ wImap . logout ( ) ;
934+ wImap = null ;
935+ } ) ;
936+ } ) ;
937+ } ) ;
938+ rImap . once ( 'end' , err => {
939+ doCallBack ( err , null ) ;
940+ } ) ;
941+ rImap . once ( 'error' , err => {
942+ doCallBack ( err , null ) ;
943+ } ) ;
944+ } ;
884945exports . imapAccountTest = ( IMapConnect , CallBack ) => {
885946 saveLog ( `start test imap [${ JSON . stringify ( IMapConnect ) } ]` ) ;
886947 let callbackCall = false ;
887948 let startTime = null ;
888949 let wImap = null ;
889950 const listenFolder = Uuid . v4 ( ) ;
890- const ramdomText = crypto . randomBytes ( 20 ) ;
951+ const ramdomText = Crypto . randomBytes ( 20 ) ;
891952 const timeout = setTimeout ( ( ) => {
892953 if ( rImap ) {
893954 rImap . logout ( ) ;
@@ -983,13 +1044,11 @@ class imapPeer extends Event.EventEmitter {
9831044 return setTimeout ( ( ) => {
9841045 saveLog ( `outlook replyPing ()` ) ;
9851046 this . pingUuid = null ;
986- if ( this . wImapReady )
987- this . replyPing ( uu ) ;
1047+ this . replyPing ( uu ) ;
9881048 return this . Ping ( ) ;
9891049 } , 5000 ) ;
9901050 }
991- if ( this . wImapReady )
992- this . replyPing ( uu ) ;
1051+ this . replyPing ( uu ) ;
9931052 saveLog ( `THIS peerConnect have not ready send ping!` ) ;
9941053 this . pingUuid = null ;
9951054 return this . Ping ( ) ;
@@ -1011,56 +1070,52 @@ class imapPeer extends Event.EventEmitter {
10111070 }
10121071 } ) ;
10131072 }
1014- sendToRemote ( text , CallBack ) {
1015- if ( this . wImap )
1016- return this . wImap . append ( text . toString ( 'base64' ) , CallBack ) ;
1073+ trySendToRemote ( email , CallBack ) {
1074+ if ( this . wImap ) {
1075+ return this . wImap . append ( email . toString ( 'base64' ) , err => {
1076+ if ( err ) {
1077+ saveLog ( `this.wImap.append ERROR: [${ err . message ? err . message : '' } ]` ) ;
1078+ }
1079+ return CallBack ( err ) ;
1080+ } ) ;
1081+ }
1082+ this . sendMailPool . push ( email ) ;
1083+ this . newWriteImap ( ) ;
1084+ return CallBack ( ) ;
10171085 }
10181086 replyPing ( uu ) {
10191087 return this . enCrypto ( JSON . stringify ( { pong : uu . ping } ) , ( err , data ) => {
1020- this . append ( data ) ;
1088+ this . trySendToRemote ( Buffer . from ( data ) , ( ) => {
1089+ } ) ;
10211090 } ) ;
10221091 }
10231092 Ping ( ) {
1024- if ( ! this . wImapReady || this . pingUuid !== null )
1025- return saveLog ( `Ping do nothing : this.wImapReady [${ this . wImapReady } ] || this.pingUuid [${ this . pingUuid } ]` ) ;
10261093 this . pingUuid = Uuid . v4 ( ) ;
1094+ saveLog ( `Ping!` ) ;
10271095 return this . enCrypto ( JSON . stringify ( { ping : this . pingUuid } ) , ( err , data ) => {
10281096 if ( err ) {
10291097 return saveLog ( `Ping enCrypto error: [${ err . message } ]` ) ;
10301098 }
1031- const doSend = ( ) => {
1032- this . sendToRemote ( Buffer . from ( data ) , err => {
1033- if ( err ) {
1034- this . emit ( 'wFolder' ) ;
1035- return saveLog ( `Ping doSend error! [${ err . message } ]` ) ;
1036- }
1037- saveLog ( `PING success!` ) ;
1038- } ) ;
1039- } ;
1040- doSend ( ) ;
1099+ this . trySendToRemote ( Buffer . from ( data ) , ( ) => { } ) ;
10411100 } ) ;
10421101 }
10431102 sendAllMail ( ) {
10441103 if ( ! this . sendMailPool . length || ! this . wImapReady )
10451104 return saveLog ( `sendAllMail do nothing! sendMailPool.length [${ this . sendMailPool . length } ] wImapReady [${ this . wImapReady } ]` ) ;
1046- const uu = Buffer . from ( this . sendMailPool . pop ( ) ) ;
1105+ const uu = this . sendMailPool . pop ( ) ;
10471106 if ( ! uu )
10481107 return saveLog ( `sendAllMail this.sendMailPool.pop () got nothing!` ) ;
1049- this . sendToRemote ( uu , err => {
1050- if ( err ) {
1051- saveLog ( `this.wImap.append error [${ err . message } ]! ` ) ;
1052- this . sendMailPool . push ( ) ;
1053- }
1054- saveLog ( `sendAllMail sendToRemote success!` ) ;
1055- } ) ;
1108+ this . trySendToRemote ( uu , ( ) => { } ) ;
10561109 }
10571110 newWriteImap ( ) {
10581111 this . wImap = new qtGateImapwrite ( this . imapData , this . writeBox ) ;
10591112 this . wImap . once ( 'end' , err => {
1060- saveLog ( `this.wImap.once end ! [${ err && err . message ? err . message : null } ]` ) ;
1113+ saveLog ( `this.wImap.once end ! [${ err && err . message ? err . message : null } ]! ` ) ;
10611114 this . wImap = null ;
1062- if ( ! this . doingDestroy )
1115+ if ( this . sendMailPool . length > 0 ) {
1116+ saveLog ( `this.wImap.once end ! sendMailPool.length > 0 [${ this . sendMailPool . length } ] newWriteImap () ` ) ;
10631117 return this . newWriteImap ( ) ;
1118+ }
10641119 } ) ;
10651120 this . wImap . once ( 'error' , err => {
10661121 if ( err && err . message && / A U T H | c e r t i f i c a t e / i. test ( err . message ) ) {
@@ -1070,6 +1125,7 @@ class imapPeer extends Event.EventEmitter {
10701125 this . wImap . destroyAll ( null ) ;
10711126 } ) ;
10721127 this . wImap . once ( 'ready' , ( ) => {
1128+ saveLog ( `wImap.once ( 'ready')` ) ;
10731129 this . wImapReady = true ;
10741130 if ( this . sendFirstPing ) {
10751131 this . sendAllMail ( ) ;
@@ -1107,6 +1163,7 @@ class imapPeer extends Event.EventEmitter {
11071163 this . rImap . destroyAll ( null ) ;
11081164 } ) ;
11091165 this . rImap . once ( 'end' , err => {
1166+ saveLog ( `rImap.once ( 'end' ) this.doingDestroy [${ this . doingDestroy } ]` ) ;
11101167 this . rImap = null ;
11111168 if ( ! this . doingDestroy )
11121169 return this . newReadImap ( ) ;
@@ -1147,14 +1204,10 @@ class imapPeer extends Event.EventEmitter {
11471204 this . exit = null ;
11481205 }
11491206 }
1150- append ( text ) {
1151- this . sendMailPool . unshift ( text ) ;
1152- return this . sendAllMail ( ) ;
1153- }
11541207 sendDone ( ) {
11551208 return Async . waterfall ( [
11561209 next => this . enCrypto ( JSON . stringify ( { done : new Date ( ) . toISOString ( ) } ) , next ) ,
1157- ( data , next ) => this . sendToRemote ( Buffer . from ( data ) , next )
1210+ ( data , next ) => this . trySendToRemote ( Buffer . from ( data ) , next )
11581211 ] , err => {
11591212 if ( err )
11601213 return saveLog ( `sendDone got error [${ err . message } ]` ) ;
0 commit comments