IMAP library doesn't encode parameters to the required charset.
The library is useful, but when it comes to complex mailbox names, the user needs to convert strings to and from the custom imap_utf7 encoding.
I think this conversion could be implemented in the library and applied transparently to all the arguments that need it.
Example:
IMAP4.select(mailbox='INBOX', readonly=False):
For the method to work, the mailbox argument needs to be encoded to imap_utf7 and if it has spaces it needs to be double quoted. All this hassle could be handled by the library.
The same applies to every function that uses a mailbox or directory argument.
When it comes to the mailbox argument I can identify the following cases:
a. bytes: It should be treated as an already imap_utf7 encoded string. If necessary it can be converted to string using ascii charset.
b. string:
b.1: It's a valid imap_utf7 string without '&' -> doesn't need encoding. Eg.: INBOX
b.2: An already encoded imap_utf7 string with '&' character -> doesn't need encoding. Eg.: Test&AOk-
b.3: Any other case (invalid imap_utf7 string) -> needs to be encoded
Proposal:
1. Impelement an imap_utf7_encode() method
2. Implement a strict imap_utf7_decode() method, it must return an error if the input doesn't conform to imap_utf7 encoding.
3. Implement a method to ensure arguments are in imap_utf7 encoding:
* bytes -> arg.decode('ascii')
* string && imap_utf7_decode(arg) -> arg
* otherwise -> imap_utf7_encode(arg)
* In every case if it has spaces double quote the whole string
5. In every method that receives a mailbox or directory argument call this new method to ensure it's imap_utf7 encoded.
|