imap: Moved mailbox check from the imap_do() function

In preparation for the addition of the LIST command, moved the mailbox
check from imap_do() to imap_select() and imap_append().