================================================================================
Windows + XMail + UW IMAP + RoundCube HowTo              version 1 (2009-01-20)
================================================================================

The following should be taken as information about what I did, rather than the
way how it should be done properly. All I did was to reach my main goal, I may
have omitted things needed for other use. For example I didn't even look at SSL
yet.

Main goal:
==========
Add IMAP to XMail, primarily for use with local RoundCube
(http://www.roundcube.net) webmail on the same server.

Other requirements:
===================
- POP3 must stay functional, mail must not be moved before user actually
  connects with IMAP

Description of changes:
=======================
I started with UW IMAP, because it is the only standalone open-source IMAP
server I found, that cleanly compiles on Windows (without Cygwin).

The major change is for the authentication system. Original code worked with
system accounts. Because XMail doesn't use them, I completely removed whole
user-changing code. The new code authenticates users against XMail's
mailusers.tab (path taken from HKEY_LOCAL_MACHINE\SOFTWARE\GNU\XMail\MAIL_ROOT).
This will probably be problem with larger sites, because it means reading this
file over and over, not mentioning that XMail probably won't be very happy when
it needs to write to that file while it is opened by imapd.

By doing that, I created a wonderful security hole. ;) My code tries to make
sure that user won't be able to access any file outside of his/her mail
directory, but because I'm not very familiar with the whole UW IMAP code, I may
have missed something! If you decide to use this, do not at least ever run it
under system/admin account!

IMAP data are stored in MAIL_ROOT\domains\<domain>\<user>\imap\ (you have to
create it first).

The biggest trouble was with Inbox. The proper way to handle it, would be to
write a driver for XMail's mailbox format and access the message files directly.
Unfortunately it was too much for me and therefore I ended up using POP3 access
to XMail. The c-client library (part of UW IMAP) has native support for POP3, so
it wasn't very hard that way.

There are two ways how to do it:

1) tell client to use special name for inbox

UW IMAP has special namespace (#move) that allows to automatically move messages
from one folder to another. Combined with support for POP3, one can use
something like this:

#move+{[::1]/pop3/notls}+INBOX

to move all messages from POP3 server at localhost (using the same username/
password as for IMAP) into INBOX. Trouble with this approach is that no client
supports it. No problem for me, because RoundCube is open-source too and it's
only small change to add support for this.

2) handle it in imapd using special wrapper driver

In IMAP directory, create text file with extension "popbox" (e.g. INBOX.popbox)
and put the connection string at first line, for example:

{[::1]/pop3/notls}

IMAP server will then present it as another IMAP folder (read and delete only,
no writes). With some additional modifications, it would be possible to map any
POP3 account as IMAP folder. Currently not, because there doesn't seem to be
a way how to include password in connection string.
This approach also has some problems. First, the read and delete only access may
be a problem. Second, it doesn't allow to set flags, so all messages are always
"unread". And third, the mailbox has non-sticky-uids that doesn't seem to be
very common thing and some clients don't handle that well (e.g. RoundCube).
Also, for some reason I had problem with moving messages from such folder in
Outlook Express, XMail's logs showed failed POP3 connection, no idea why,
I didn't debug it much.

And that's it.

Files:
======
Patch for UW IMAP 2007e, patch for RoundCube, IMAPd binary for Windows >= XP:

http://web.hisoftware.cz/sob/download/xmail-imap.rar

Other notes:
============
If you want to compile it yourself (and you should, using binaries from
untrusted strangers is not good habit :), download IMAP 2007e source, apply the
patch and compile with:

nmake -f makefile.w2k

Resulting imapd.exe is inetd server, it means you can't run it directly, but you
need something like inetd. Original documentation recommends wsinetd and it
works well, only website address is a little different:

http://sourceforge.net/projects/wsinetd/

Remember, the whole thing is experimental, I'm the only one so far who tried to
use it, so if you decide to play with it, be VERY careful!

================================================================================
 Sob <sob@hisoftware.cz>
================================================================================
 