You could do worse than start with the code to Courier-IMAP. Reportedly, it has a nice modular architecture, so you would only need to replace the bit that dances on maildir.
It's not all that hard to write one from the RFC, BTW. You just have to make sure you test with common clients. Outlook is known to be very fussy about whitespace in the IMAP responses.
Wade.