circuithub / mail-listener

Mail listener library for node.js. Get notification when new email arrived.
134 stars 32 forks source link

Add IMAP listeners on "close" and "error" #22

Open anotheri opened 11 years ago

anotheri commented 11 years ago

Add please two imap listeners and emits. Something like this:

After @emit "server:connected":

@imap.on "close", (err) =>
    # console.log "IMAP close"
    @emit "imap:close", err

@imap.on "error", (err) =>
    # console.log "IMAP error", err
    @emit "imap:err", err;

I think it may have another predefine handlers by default (stop mailListener, for example, or restart it, or something other). But i need opportunity to set handlers on this events from listeners for mail-listener class like this:

mailListener.on("imap:err", function(){
    console.log("imapError");
});

mailListener.on("imap:close", function(){
    mailListener.restart();
        // or something other code
});
chirag04 commented 11 years ago

Mail listener already emit these events. See if below example makes sense to you.

mailListener.on("server:error", function(error){
                console.log('imap error')
});
mailListener.on("server:disconnected", function(){
      //  restart here
});
anotheri commented 11 years ago

@chirag04 server:error and server:disconnected are not imap events, check the code. you can try to add this events and check it. imap is down after 1 hour of inactivity and only @imap.on "close", (err) => can catch it.

anotheri commented 11 years ago

@chirag04 this is actual for imap v. 0.7.8. I didn't test this with other versions of imap. but I'm testing now imap v.0.7.12.

chirag04 commented 11 years ago

Ok. got it. I will leave this to @podviaznikov

anotheri commented 11 years ago

@chirag04 It seems that fallings down after 1 hour of inactivity were fixed in imap. And that's great! It works already 1.5 hour. ) I'll leave my listener in active status for 24 hours and will describe how it works tomorrow.

@podviaznikov Could you update mail-listener in the npm please? Version based on imap 0.7.12 seems more stable.

anotheri commented 11 years ago

But... After ~2 hour I send a second mail, it was parsed correctly and listener working else few minute and than shutted down with unhandled error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:883:11)
    at TCP.onread (net.js:539:19)

And I think this error should be handled next way:

@imap.on "error", (err) =>
    # console.log "IMAP error", err
    @emit "imap:err", err;

And I started listener again...

chirag04 commented 11 years ago

@anotheri till then you might want to start your script with forever. Shall give you some good sleep. Haha :)

anotheri commented 11 years ago

@chirag04 thanks for realy good advice. ) but now i want test it without forever. this (forever) is my ace in the hole. =))

anotheri commented 11 years ago

@chirag04 And, as i sad yesterday, listener was shutted down with only output:

 --- IMAP CLOSE: false

that I define into this block:

@imap.on "close", (err) =>
    console.log " --- IMAP CLOSE:", err
    @emit "imap:close", err

In this case imap v. 0.7.12 works like previous. So add emit functions on imap close and error events please. And I'll use my ace — forever. =)

chirag04 commented 11 years ago

Not sure if this is relevant but i tried to start mail-listener and kept it on server. Its still connected and running from last 5hours.