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

Unable to call any command because of 'Uknown command <something>' #16

Closed
sennoy opened this issue Aug 1, 2014 · 2 comments
Closed

Comments

@sennoy
Copy link

sennoy commented Aug 1, 2014

So, the code is:

var BrowserBox = require('browserbox'),
    email = 'some.email.address@gmail.com',
    password = 'some.password',
    mailbox = 'INBOX';

var client = new BrowserBox('imap.gmail.com', 993, {
    auth: {
        user: email,
        pass: password
    },
    useSSL: true
});

client.connect();
client.listMailboxes(function (err, mailboxes) {
    console.log(err || mailboxes);
});
client.selectMailbox(mailbox, function (err, mailbox) {
    console.log(err || 'Mailbox was successfully changed to ' + mailbox);
});

When I'm trying to execute it, I'm getting something like this:

[2014-08-01T15:24:24.564Z][browserbox] connecting to imap.gmail.com:993
[2014-08-01T15:24:24.566Z][browserbox] entering state: false
[Error: Unknown command j1mb3779317lae]
[Error: Unknown command j1mb3779317lae]
[Error: Unknown command j1mb3779317lae]

Note, that this value j1mb3779317lae changes all the time.
May be, it should give at least more friendly response?

UPD:
May be it will by useful. Executing this one:

    client.listMessages('1:10', ['uid', 'body[]'], function (err, messages) {
        console.log(err);
        messages.forEach(function (message) { // this is 30 line;
                ...
        });
    });

gives me following stackstrace:

TypeError: Cannot call method 'forEach' of undefined
at /path/nodeEmails.js:30:18
    at BrowserBox.<anonymous> (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox.js:680:17)
    at BrowserBox.<anonymous> (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox.js:280:17)
    at ImapClient._processServerResponse (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox-imap.js:529:45)
    at ImapClient._processServerQueue (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox-imap.js:472:14)
    at ImapClient._addToServerQueue (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox-imap.js:419:14)
    at ImapClient._onData (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/src/browserbox-imap.js:363:22)
    at TCPSocket._emit (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/node_modules/tcp-socket/src/tcp-socket.js:332:9)
    at CleartextStream.<anonymous> (/Users/AK/ncryptedcloud/NccWebBuildout/node_modules/browserbox/node_modules/tcp-socket/src/tcp-socket.js:90:22)
    at CleartextStream.EventEmitter.emit (events.js:95:17)
@felixhammerl
Copy link
Contributor

There is a working implementation on top of browserbox that accumulates many of its features in a somewhat opinionated high-level api, so you might want to have a look at that: https://github.com/whiteout-io/imap-client
(We did not really get around to write a proper documentation for the imap-client), but you should be able to figure it out pretty quickly from the inline documentation)

Regarding your code examples: client.connect(); is not synchronous, you need to wait for the onauth callback. Here's an example. Otherwise your queries end up somewhere in oblivion.

Second: useSSL is deprecated, you should use useSecureTransport instead. I'll adapt the README.

Please let me know if that helped.

@andris9
Copy link
Member

andris9 commented Aug 1, 2014

The module is not synchronous, just as Felix already stated – you have to wait for the callback before you can make the next command.

What happens with your code is the following:

S: * OK Gimap ready for requests from X.X.X.X j1mb3779317lae ← session idendifier
C: W1 LIST "" "*" ← already calling LIST command even though you're not logged in
S: W1 BAD Unknown command j1mb3779317lae ← error response with session idendifier
                                           the command is 'unknown' because LIST is
                                           available only in the 'authenticated' state

To fix this, your code should be like this:

client.connect();
client.onauth = function() {
    client.listMailboxes(function(err, mailboxes) {
        console.log(err || mailboxes);
        client.selectMailbox(mailbox, function(err, mailbox) {
            console.log(err || 'Mailbox was successfully changed to ' + mailbox);
        });
    });
};

In this case the commands are completely different:

S: * OK Gimap ready for requests from X.X.X.X j1mb3779317lae
C: W1 CAPABILITY
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE ...
S: W1 OK Thats all she wrote! j1mb3779317lae
C: W2 ID NIL
S: * ID ("name" "GImap" "vendor" "Google, Inc." ...
S: W2 OK Success j1mb3779317lae
C: W3 login "some.email.address@gmail.com" "some.password"
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE ...
S: W3 OK some.email.address@gmail.com Some Email authenticated (Success)
C: W4 LIST "" "*" ← now we send the LIST after successful auth
S: * LIST (\Noselect \HasChildren) "/" "[Gmail]" ← and no error anymore
...

Remember that in addition of onauth you should also set onerror handler, otherwise you might end up with uncaught errors, and the onclose handler to detect if the connection is already closed.

I'll close this issue as it is not a bug but feel free to ask if you have any further questions.

@andris9 andris9 closed this as completed Aug 1, 2014
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

No branches or pull requests

3 participants