2009-11-26

Gnus, IMAP and Exchange 2007

My workplace upgraded its Exchange installation to Exchange 2007 over the weekend. As usual when Exchange is upgraded, the new and improved version also has a new and "improved" relationship to whatever standards it pretends to support.

In this case it was IMAP that broke.

First problem: During startup of Gnus, my echo area would be spammed with messages saying BAD The specified message set is invalid. Some googling later I had set the variable imap-enable-exchange-bug-workaround to t, which does pretty much what it says on the tin.

So far, so good. But then I opened my inbox. Hm. Weren't there LOTS more mails here on Friday? Did the upgrade blat my mail? Hang on - didn't I see some junk in the echo area while Gnus worked. Let me try that again.

Yes; lots of messages about In imap-parse-body 2. So I have a look at that function; all 90-odd lines of it. Apparently, something Exchange was sending made it fail an assertion.

Unfortunately, the original function was hard to fix, not least because it was hard to tell what was failing. But isn't it really just reading a Lisp list? What if we let the reader help us?

Some hacking later I ended up with this:
(defun imap-parse-body ()
(when (eq (char-after) ?\()
(nsubst nil 'NIL
(imap-normalize-body-sexp (read (current-buffer))))))

(defun imap-normalize-body-sexp (body-sexp)
(dolist (m (imap-find-message-rfc822-parts body-sexp))
(setf (nth 7 m) (apply 'vector (nth 7 m))))
body-sexp)

(defun imap-find-message-rfc822-parts (body)
(flet ((rfc822-p (body)
(and (stringp (car body))
(string= "message" (car body))
(string= "rfc822" (car (cdr body)))
body)))
(cond ((and (consp body) (atom (car body)))
(rfc822-p body))
((consp body)
(remove nil (mapcar 'imap-find-message-rfc822-parts body))))))

The imap-normalize-body-sexp stuff isn't really necessary - all it does is turn a list into a vector, and since that vector is accessed using elt it works with a list, too. A vector is just more efficient.

This code, simplistic as it may be, works for me (so far). If it works for you too, good; if it doesn't, there is no warranty.

No comments: