Bug #1713: IMAP NO response messages are unavailable
http://redmine.ruby-lang.org/issues/show/1713

Author: Eric Hodel
Status: Open, Priority: Normal
ruby -v: ruby 1.9.2dev (2009-06-30 trunk 23904) [i386-darwin9.7.0] 

In RFC 3501 the IMAP server may respond with information the user can use to retry their request.

For the COPY command (6.4.7):

      If the destination mailbox does not exist, a server SHOULD return
      an error.  It SHOULD NOT automatically create the mailbox.  Unless
      it is certain that the destination mailbox can not be created, the
      server MUST send the response code "[TRYCREATE]" as the prefix of
      the text of the tagged NO response.  This gives a hint to the
      client that it can attempt a CREATE command and retry the COPY if
      the CREATE is successful.

Also for the SEARCH command (6.4.4):

      If the server does not support the specified [CHARSET], it MUST
      return a tagged NO response (not a BAD).  This response SHOULD
      contain the BADCHARSET response code, which MAY list the
      [CHARSET]s supported by the server.

Net::IMAP doesn't easily give access to this information.  For example:

$ cat imap_copy.rb 
require 'net/imap'

Net::IMAP.debug = true
imap = Net::IMAP.new 'localhost', 143
imap.login 'test', 'test'
imap.select 'INBOX'
uids = imap.search 'ALL'
begin
  imap.copy uids, 'no such mailbox'
  puts 'copy successful'
rescue Net::IMAP::NoResponseError => e
  p e
end
$ ruby imap_copy.rb 
S: * OK Dovecot ready.
[...]
C: RUBY0004 COPY 1 "no such mailbox"
S: RUBY0004 NO [TRYCREATE] Mailbox doesn't exist: no such mailbox
#<Net::IMAP::NoResponseError:  Mailbox doesn't exist: no such mailbox>

Here the server told us we could probably fix this by sending CREATE first, so we could handle this intelligently.  Unfortunately, we can't tell if the mailbox outright doesn't exist or if it simply can't be selected:

irb(main):016:0> imap.list '', 'directory'
C: RUBY0012 LIST "" directory
S: * LIST (\Noselect \HasChildren) "/" "directory"
S: RUBY0012 OK List completed.

irb(main):015:0> imap.copy [1], 'directory'
C: RUBY0011 COPY 1 directory
S: RUBY0011 NO Mailbox isn't selectable: directory

The attached patch adds the response to Net::IMAP::ResponseError which allows us to behave intelligently:

$ cat imap_copy.rb 
require 'net/imap'

Net::IMAP.debug = true
imap = Net::IMAP.new 'localhost', 143
imap.login 'test', 'test'
imap.select 'INBOX'
uids = imap.search 'ALL'

begin
  imap.copy uids, 'no such mailbox'
  puts 'copy successful'
rescue Net::IMAP::NoResponseError => e
  raise unless e.response.data.code.name == 'TRYCREATE'
  imap.create 'no such mailbox'
  imap.copy uids, 'no such mailbox'
end
$ ruby imap_copy.rb 
S: * OK Dovecot ready.
[...]
C: RUBY0004 COPY 1 "no such mailbox"
S: RUBY0004 NO [TRYCREATE] Mailbox doesn't exist: no such mailbox
C: RUBY0005 CREATE "no such mailbox"
S: RUBY0005 OK Create completed.
C: RUBY0006 COPY 1 "no such mailbox"
S: RUBY0006 OK [COPYUID 1 64 1] Copy completed.


----------------------------------------
http://redmine.ruby-lang.org