Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Connection.js #546

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lucas-pouchot-zz
Copy link

add some check on object _curReq and object box before use it
It's fix some uncaugth error like :

self._box.name = name;
         ^

TypeError: Cannot set property 'name' of undefined
at Connection. (/root/thethingbox/node_modules/node-red/node_modules/node-red-node-email/node_modules/imap/lib/Connection.js:422:22)

or

var msg = self._curReq.fetchCache[info.seqno], toget;
                      ^

TypeError: Cannot read property 'fetchCache' of undefined
at Parser. (/root/thethingbox/node_modules/node-red/node_modules/node-red-node-email/node_modules/imap/lib/Connection.js:196:27)

add some check on object _curReq and object box before use it
It's fix some uncaugth error like :

     self._box.name = name;
                     ^
TypeError: Cannot set property 'name' of undefined
    at Connection.<anonymous> (/root/thethingbox/node_modules/node-red/node_modules/node-red-node-email/node_modules/imap/lib/Connection.js:422:22)

or 

   var msg = self._curReq.fetchCache[info.seqno], toget;
                          ^
TypeError: Cannot read property 'fetchCache' of undefined
    at Parser.<anonymous> (/root/thethingbox/node_modules/node-red/node_modules/node-red-node-email/node_modules/imap/lib/Connection.js:196:27)
@mscdex
Copy link
Owner

mscdex commented Apr 20, 2016

I'd be more interested in knowing why there are body events when there is no request in progress. Do you have debug output you can share (e.g. set debug: console.log in your connection config)?

@lucas-pouchot-zz
Copy link
Author

lucas-pouchot-zz commented Apr 20, 2016

I'm not sure what you want I debug, I added

console.log(self)

just after the

parser.on('body', function (stream, info) {

when the error come, I obtain this log : ( removing the mail adress and password)

{ domain: null,
  _events: { error: [Function] },
  _maxListeners: 10,
  _config: 
   { socket: undefined,
     socketTimeout: 0,
     host: 'smtp.gmail.com',
     port: '993',
     tls: true,
     tlsOptions: { rejectUnauthorized: false },
     autotls: undefined,
     user: 'xxxxxxxxxxxxxxx@xxxxxxxxx',
     password: 'xxxxxxxxxxxxxx',
     xoauth: undefined,
     xoauth2: undefined,
     connTimeout: 7000,
     authTimeout: 7000,
     keepalive: true },
  _sock: 
   { _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: false,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: true,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: true,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        sslOutEnd: [Function],
        error: [Function],
        timeout: [Function],
        readable: [Function] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: true,
        ended: true,
        finished: true,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [],
        errorEmitted: false },
     writable: false,
     allowHalfOpen: true,
     pair: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        server: undefined,
        _secureEstablished: true,
        _isServer: false,
        _encWriteState: true,
        _clearWriteState: true,
        _doneFlag: false,
        _destroying: false,
        credentials: [Object],
        _rejectUnauthorized: false,
        _requestCert: true,
        ssl: [Object],
        servername: false,
        cleartext: [Circular],
        encrypted: [Object],
        fd: undefined,
        npnProtocol: undefined },
     _pending: null,
     _pendingEncoding: '',
     _pendingCallback: null,
     _doneFlag: false,
     _retryAfterPartial: false,
     _halfRead: false,
     _sslOutCb: null,
     _resumingSession: false,
     _reading: true,
     _destroyed: false,
     _ended: false,
     _finished: true,
     _opposite: 
      { _readableState: [Object],
        readable: false,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: true,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: true,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     _buffer: 
      { isFull: false,
        pool: <Buffer 4f 34 4e 43 53 71 73 56 6f 42 6b 46 5a 79 4b 0d 0a 55 2f 5a 69 66 78 2b 59 55 69 48 58 32 73 44 4d 38 42 45 36 6e 50 4a 47 46 2f 70 79 6c 6d 51 37 42 74 ...>,
        offset: 7809569,
        remaining: 2676191 },
     _handle: { readStop: [Function], readStart: [Function] },
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        onend: null,
        destroyed: false,
        bytesRead: 4530834,
        _bytesDispatched: 892,
        _pendingData: null,
        _pendingEncoding: '',
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1,
        read: [Function],
        _consuming: true },
     encrypted: 
      { _readableState: [Object],
        readable: false,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: true,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: true,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     authorized: true,
     _controlReleased: true,
     npnProtocol: undefined },
  _tagcount: 6,
  _tmrConn: 
   { _idleTimeout: -1,
     _idlePrev: null,
     _idleNext: null,
     _idleStart: 1461162868479,
     _monotonicStartTime: 399548,
     _onTimeout: null,
     _repeat: false,
     ontimeout: null },
  _tmrKeepalive: undefined,
  _tmrAuth: 
   { _idleTimeout: -1,
     _idlePrev: null,
     _idleNext: null,
     _idleStart: 1461162868780,
     _monotonicStartTime: 399849,
     _onTimeout: null,
     _repeat: false,
     ontimeout: null },
  _queue: [],
  _box: 
   { name: 'INBOX',
     flags: 
      [ '\\Answered',
        '\\Flagged',
        '\\Draft',
        '\\Deleted',
        '\\Seen',
        '$NotPhishing',
        '$Phishing' ],
     readOnly: false,
     uidvalidity: 1,
     uidnext: 1394,
     permFlags: [ '\\Answered', '\\Flagged', '\\Draft', '\\Deleted', '\\Seen' ],
     keywords: [ '$NotPhishing', '$Phishing' ],
     newKeywords: true,
     persistentUIDs: true,
     nomodseq: false,
     messages: { total: 1329, new: 0 },
     highestmodseq: '247546' },
  _idle: { started: undefined, enabled: false },
  _parser: 
   { domain: null,
     _events: 
      { untagged: [Function],
        tagged: [Function],
        body: [Function],
        continue: [Function],
        other: [Function] },
     _maxListeners: 10,
     _stream: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: true,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        npnProtocol: undefined },
     _body: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: {},
        _maxListeners: 10,
        _read: [Function] },
     _literallen: 630,
     _literals: [],
     _buffer: '* 1329 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532140301053352759 X-GM-LABELS ("\\\\Important" "\\\\Sent") UID 1393 MODSEQ (247525) INTERNALDATE "20-Apr-2016 14:34:14 +0000" FLAGS (\\Seen)  ',
     _ignoreReadable: false,
     debug: undefined,
     _cbReadable: [Function] },
  _curReq: undefined,
  delimiter: '/',
  namespaces: { personal: [ [Object] ], other: null, shared: null },
  state: 'authenticated',
  debug: undefined,
  _onError: [Function],
  _onSocketTimeout: [Function],
  _caps: 
   [ 'IMAP4REV1',
     'UNSELECT',
     'IDLE',
     'NAMESPACE',
     'QUOTA',
     'ID',
     'XLIST',
     'CHILDREN',
     'X-GM-EXT-1',
     'UIDPLUS',
     'COMPRESS=DEFLATE',
     'ENABLE',
     'MOVE',
     'CONDSTORE',
     'ESEARCH',
     'UTF8=ACCEPT',
     'APPENDLIMIT=35882577',
     'LIST-EXTENDED',
     'LIST-STATUS' ] }

@mscdex
Copy link
Owner

mscdex commented Apr 20, 2016

Ok, so the parser still thinks it's parsing the results from a fetch(). Can you set debug: console.log in the object you pass to the constructor (e.g. new Imap({ ...., debug: console.log })) and post the output here (censoring username and password in output)?

@lucas-pouchot-zz
Copy link
Author

I set debug: console.log
and I obtain this : (I replace my previous console.log(self) by console.log("ERROR HERE") )

[connection] Closed
[connection] Closed
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 er5mb6261250wjd'
=> 'A0 CAPABILITY'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 7mb23357432wjz'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! er5mb6261250wjd'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! 7mb23357432wjz'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* LIST (\Noselect) "/" "/"'
<= 'A3 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* LIST (\Noselect) "/" "/"'
<= 'A3 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
=> 'A5 SELECT "INBOX" (CONDSTORE)'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
=> 'A5 SELECT "INBOX" (CONDSTORE)'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 b132mb22777230wlg'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! b132mb22777230wlg'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A5 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
=> 'A6 FETCH 1331:* (X-GM-THRID X-GM-MSGID X-GM-LABELS MODSEQ UID FLAGS INTERNALDATE BODY[HEADER] BODY[TEXT])'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A5 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 la8mb24154505wjb'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! la8mb24154505wjb'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* LIST (\Noselect) "/" "/"'
<= 'A3 OK Success [THROTTLED]'
20 Apr 16:08:22 - [error] [e-mail in:7fc343b9.75209c] Failed to fetch folder: INBOX
=> 'A5 LOGOUT'
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) BODY[TEXT] {8956395}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx ERROR HERE xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
[connection] Ended
[connection] Closed
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) BODY[HEADER] {772}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx ERROR HERE xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) )'
<= 'A6 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* LIST (\Noselect) "/" "/"'
<= 'A3 OK Success [THROTTLED]'
20 Apr 16:08:26 - [error] [e-mail in:f05906e0.818b38] Failed to fetch folder: INBOX
=> 'A5 LOGOUT'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 v10mb24471738wjw'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! v10mb24471738wjw'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
[connection] Ended
[connection] Closed
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* LIST (\Noselect) "/" "/"'
<= 'A3 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing _)] Flags permitted.'
<= '_ OK [UIDVALIDITY 1] UIDs valid.'
<= '* 1331 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 1396] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 247793]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success) [THROTTLED]'
=> 'A5 FETCH 1331:* (X-GM-THRID X-GM-MSGID X-GM-LABELS MODSEQ UID FLAGS INTERNALDATE BODY[HEADER] BODY[TEXT])'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 e128mb22913791wle'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! e128mb22913791wle'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
[connection] Connected to host
<= '* OK Gimap ready for requests from 78.217.236.83 g126mb9175995wme'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! g126mb9175995wme'
=> 'A1 LOGIN "xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx" "xxxxxxxxxxxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS'
<= 'A1 OK xxxxxxxxxxxxxxxxxxxx@xxxxxxxxxx authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) BODY[TEXT] {8956395}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx ERROR HERE xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success [THROTTLED]'
=> 'A3 LIST "" ""'
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) BODY[HEADER] {772}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx ERROR HERE xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<= '* 1331 FETCH (X-GM-THRID 1532140282143983147 X-GM-MSGID 1532145950656002128 X-GM-LABELS ("\Important" "\Sent") UID 1395 MODSEQ (247766) INTERNALDATE "20-Apr-2016 16:04:02 +0000" FLAGS (\Seen) )'
<= 'A5 OK Success [THROTTLED]'
=> 'A4 SELECT "INBOX" (CONDSTORE)'

@mscdex
Copy link
Owner

mscdex commented Apr 20, 2016

Are you reusing client instances or? Can you show how you're using the module?

@lucas-pouchot-zz
Copy link
Author

It's come when I use the node mail of node-red
(https://github.com/node-red/node-red-nodes/tree/master/social/email)

so, each 10 seconds, this code is called : (line 180 of file 61-email.js)
I tried to surround with a try/catch the call of imap.openBox, but it doesn't work

imap.once('ready', function() {
    node.status({fill:"blue",shape:"dot",text:"email.status.fetching"});
    var pay = {};
    imap.openBox(node.box, false, function(err, box) {
        if (err) {
            node.status({fill:"red",shape:"ring",text:"email.status.foldererror"});
            node.error(RED._("email.errors.fetchfail",{folder:node.box}),err);
        }
        else {
            if (box.messages.total > 0) {
                //var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER.FIELDS (FROM SUBJECT DATE TO CC BCC)','TEXT'] });
                var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER','TEXT'] });
                f.on('message', function(msg, seqno) {
                    node.log(RED._("email.status.message",{number:seqno}));
                    var prefix = '(#' + seqno + ') ';
                    msg.on('body', function(stream, info) {
                        var buffer = '';
                        stream.on('data', function(chunk) {
                            buffer += chunk.toString('utf8');
                        });
                        stream.on('end', function() {
                            if (info.which !== 'TEXT') {
                                var head = Imap.parseHeader(buffer);
                                if (head.hasOwnProperty("from")) { pay.from = head.from[0]; }
                                if (head.hasOwnProperty("subject")) { pay.topic = head.subject[0]; }
                                if (head.hasOwnProperty("date")) { pay.date = head.date[0]; }
                                pay.header = head;
                            } else {
                                var parts = buffer.split("Content-Type");
                                for (var p = 0; p < parts.length; p++) {
                                    if (parts[p].indexOf("text/plain") >= 0) {
                                        pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
                                    }
                                    else if (parts[p].indexOf("text/html") >= 0) {
                                        pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
                                    } else {
                                        pay.payload = parts[0];
                                    }
                                }
                                //pay.body = buffer;
                            }
                        });
                    });
                    msg.on('end', function() {
                        //node.log('finished: '+prefix);
                    });
                });
                f.on('error', function(err) {
                    node.warn(RED._("email.errors.messageerror",{error:err}));
                    node.status({fill:"red",shape:"ring",text:"email.status.messageerror"});
                });
                f.on('end', function() {
                    delete(pay._msgid);
                    if (JSON.stringify(pay) !== oldmail) {
                        oldmail = JSON.stringify(pay);
                        node.send(pay);
                        node.log(RED._("email.status.newemail",{topic:pay.topic}));
                    }
                    else { node.log(RED._("email.status.duplicate",{topic:pay.topic})); }
                    //node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
                    node.status({});
                });
            }
            else {
                node.log(RED._("email.status.inboxzero"));
                //node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
                node.status({});
            }
        }
        imap.end();
    });
});
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"});
imap.connect();

@mscdex
Copy link
Owner

mscdex commented Apr 20, 2016

That looks relatively ok. Are you re-using imap though (e.g. only doing imap = new Imap({..}) once and calling imap.connect() each time), or are you doing imap = new Imap({...}) each time?

@lucas-pouchot-zz
Copy link
Author

this node-red module do imap = new Imap({..}) once and call imap.connect() each time.
But I have two instances of this module

@mscdex
Copy link
Owner

mscdex commented Apr 20, 2016

Ok looking at the debug output again and with what you just told me, I think it makes sense now. I believe imap.connect() is being called too often and that is causing issues. Please try applying this patch and let me know if that gets rid of this particular problem.

The underlying downstream problem still exists though, even with the aforementioned patch. A connection is being requested, but since the same instance is being reused, it's not waiting for the previous connection to be ready first (and possibly execute any fetch()s or any other commands) before trying to connect again. It just happens that occasionally the reconnection happens in the middle of a fetch() and some state isn't being reset.

So some possible suggestions for changes to the node-red-node-email module:

  • Keep the single instance but move the imap.connect(), imap.end(), and ready event handlers outside of the input event handler. If the instance gets disconnected, you could just add a close event handler that just calls imap.connect(). Then inside your input event handler, you're just making ordinary fetch() and whatever other calls.
  • Move the var imap = new Imap({...}) to inside the input event handler. This means every input event will create a new instance, guaranteeing that other input events do not interfere.
  • Use a connection pool to limit the number separate connections being made (to the same server) if using the previous solution.

@iby
Copy link

iby commented Aug 26, 2019

@mscdex This seems to be related to #591. The suggested patch works, but it seemed like the request got stuck in like 1 of 10 attempts at first, thought it easily might have been something else.

What do we do? Happy to work on the PR, but would appreciate some guidance – very new to the lib and the IMAP business. 👍

@iby
Copy link

iby commented Aug 26, 2019

My particular issue appears my own fault – reconnecting on any status other than connected, including authenticated. But I believe the issue and PR remain – perhaps the already connected error should be thrown or a disconnection be issued first. Or maybe an early return without resetting everything if the connection is established…

Garfonso added a commit to Garfonso/node-imap that referenced this pull request Mar 23, 2023
see PR mscdex#564 in original repo:
mscdex#546

These crashes might result from user errors... but still it makes sense to not just crash because of accessing uninitialized variables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants