# [SOLVED] offlineimap: exchange to courier maildir

## Vieri

I am trying to sync M$ Exchange maiboxes to Courier IMAP. I am using the latest offlineimap which seems faster than imapsync and even mailsync.

However I'm having just one last problem related to dotted folder names.

Suppose the Exchange mailbox has folders such as:

FOLDER1\messages

FOLDER2\messages

INBOX\messages

offlineimap syncs and creates the dirs:

~/testemail/FOLDER1/messages

~/testemail/FOLDER2/messages

~/testemail/INBOX/messages

What I am trying to achieve is Courier's dotted maildir structure:

~/testemail/.FOLDER1/messages

~/testemail/.FOLDER2/messages

~/testemail/.INBOX/messages

The solution is probably in nametrans. Can anyone please help?

$ cat .offlineimaprc

[general]

accounts = me

#[mbnames]

#

#enabled = yes

#filename = ~/testemail

#header = "mailboxes "

#peritem = "+%(accountname)s/%(foldername)s"

#sep = "."

#footer = "\n"

[Account me]

localrepository = Local

remoterepository = Remote

[Repository Local]

type = Maildir

#localfolders = ~/.maildir

localfolders = ~/testemail

sep = .

[Repository Remote]

type = IMAP

remotehost = aux2

remoteuser = me

remotepass = password

#nametrans = lambda foldername: re.sub('^ADSL', '.ADSL', foldername)

# The above rule correctly creates a "dotted" folder such as ~/testemail/.ADSL/messages which is what I'm looking for but would like to specify a wildcard expression.

#nametrans = lambda foldername: re.sub('^INBOX\.*', '.', foldername)

[EDIT]

I added:

nametrans = lambda foldername: "." + foldername

and that seems to work fine.

However, is there another way to do this?

[EDIT 2]

I changed ~/.offlineimaprc to:

[general]

accounts = me

[Account me]

localrepository = Local

remoterepository = Remote

[Repository Local]

type = Maildir

localfolders = ~/.maildir

sep = .

[Repository Remote]

type = IMAP

remotehost = aux2

remoteuser = me

remotepass = password

nametrans = lambda foldername: ( "." + foldername or re.sub('^INBOX\.*', '.', foldername) )

I am unfamiliar with Python and the lambda function. What I would like to do is combine these two rules into one call:

#nametrans = lambda foldername: re.sub('^INBOX\.*', '.', foldername)

#nametrans = lambda foldername: "." + foldername

How can this be done?

[EDIT 3]

I did the following and it "does the job" although I'm sure the code can be "optimized":

sh-3.1$ cat .offlineimaprc

[general]

pythonfile=~/.offlineimap_fhm.py

accounts = me

[Account me]

localrepository = Local

remoterepository = Remote

[Repository Local]

type = Maildir

localfolders = ~/.maildir

sep = .

[Repository Remote]

type = IMAP

remotehost = aux2

remoteuser = me

remotepassfile = ~/.maildir_fhm_pwd

nametrans = nametrans_fhm

sh-3.1$ cat .offlineimap_fhm.py

import re

def nametrans_fhm(foldername):

    if re.search('^INBOX\.*', foldername):

        foldername = re.sub('^INBOX\.*', '.', foldername)

    else:

        foldername = "." + foldername

    return foldername

[EDIT 4]

A simpler solution from John Goerzen is to make better use of the regular expression pattern:

.offlineimaprc:

[general]

accounts = me

[Account me]

localrepository = Local

remoterepository = Remote

[Repository Local]

type = Maildir

localfolders = ~/.maildir

sep = .

[Repository Remote]

type = IMAP

remotehost = aux2

remoteuser = me

remotepassfile = ~/.maildir_pwd

nametrans = lambda foldername: re.sub('^(INBOX)?', '.', foldername)

[EDIT 5]

You might notice that if you sync MS Exchange (type=IMAP) to Courier (type=Maildir) and then use a non-standard-compliant e-mail client such as Outlook 2003, you won't be able to correctly filter messages chronologically because they will appear to have been received on the same date and time (i.e. when you last ran offlineimap). This is apparently a "bug" in Outlook because it should take the timestamp out of each e-mail's headers and somehow it doesn't. However, if you use Mozilla Thunderbird (for example) it will correctly display the messages' timestamps.

If you absolutely need to use e.g. Outlook to view your offlineimapped messages on a Courier server then there's a simple solution: define both repositories as type=IMAP.

Check the files below.

If someone has a better solution, please post it.

# cat ~/.offlineimaprc

[general]

pythonfile=/OFFLINEIMAP/.offlineimap_fhm.py

accounts = me

[Account me]

localrepository = Local

remoterepository = Remote

[Repository Local]

# This is the Courier server

type = IMAP

remotehost = inf-bl07

remoteuser = me

remotepassfile = ~/.maildir_fhm_pwd

[Repository Remote]

# This is the Exchange server

type = IMAP

remotehost = aux2

remoteuser = me

remotepassfile = ~/.maildir_fhm_pwd

nametrans = nametrans_fhm

# cat /OFFLINEIMAP/.offlineimap_fhm.py

import re

def nametrans_fhm(foldername):

    if foldername == "INBOX":

        foldername = "INBOX"

    else:

        foldername = "INBOX." + foldername

    return foldername

[EDIT 6]

Problem:

with the settings in [EDIT 5] you may notice that folders WITHIN your Exchange inbox will appear in Courier under a different top-level folder. So the Exchange inbox messages will be in the Courier inbox folder as expected. However, Exchange inbox subfolders and messages will be in a new Courier folder. If someone has a quick solution for this please post it. Apart from this small inconvenience the rest of the structure is preserved.

----------

