Blackberry IMAP Proxy – Working Sent and Trash Folders

Blackberry IMAP Proxy – Working Sent and Trash Folders

RIM has never fully supported IMAP servers in their Blackberry Internet Service(BIS) formerly Blackberry Web Client(BWC). Notably BIS does not recognize the NAMESPACE command from IMAP servers. The vast majority of IMAP servers place all user folders in a single parent folder(usually INBOX) therefore the Sent and Trash folders are actually INBOX.Sent and INBOX.Trash. Unfortunately BIS only recognizes Sent and Trash in the base directory.

This script sits between the BIS and your IMAP server. The proxy then makes INBOX.Sent and INBOX.Trash appear to be Sent and Trash. This enables BIS to save a copy of your sent emails into INBOX.Sent properly and a copy of deleted emails into INBOX.Trash properly.

Additionally this script allows you to use a non standard folder as your inbox as well.

The Language

This is all written in PHP because it was the only language I was able to get SSL to work in properly. Perl or python would probably be better suited for this project. In fact I found this script written in Perl which could be modified for this purpose imapforward.pl.

How It Works

When the script starts it binds itself to a port waiting for a connection from a client. When a connection is made to the proxy it immediately connects to the server. The proxy then forwards all data from the IMAP server to the client and forwards all data from the client to the IMAP server.

Concurrent Connections

The script loops through all incomming and outgoing sockets looking for data waiting to be passed on. This enables us to use multiple concurrent connections. I highly doubt it would perform very well under a heavy load. But it easily operates with 5-10 concurrent connections which should be more than sufficient for the light load of emails sent to your blackberry. It could probably work with multiple clients, but I can not gaurantee that it would work well for a large corporation. However, RIM’s blackberry corporate server likely fills this void.

Filtering and Editing Data

Because Blackberry’s only have a very limited IMAP functionality there were only 5 commands which needed to be caught and altered by the script:
l LOGIN "user" "password" – This is caught to see which folder should be used as the INBOX
s SELECT "INBOX" – This is caught and changed to an alternate folder if the above username is found
a APPEND "Sent" (\Seen) {287} – This is BIS attempting to save a sent message, change "Sent" to the proper folder
c UID COPY 8 "Trash" – This is BIS attempting to delete a message, change "Trash" to the proper folder.

Finally we need to make sure that "Sent" and "Trash" are listed when BIS requests a list of folders available. To do this we catch the final OK LIST response sent from the server and list the "Sent" and "Trash" folders before this:
OK LIST completed – List "Sent" and "Trash" before this line is sent to the BIS client.

Getting and Using the Script

I created this script mostly for my use, but I figure others will benefit from it too. I am not really inclined to figure out all the possible differences amongst all the IMAP servers out there. Also I use SSL on my IMAP server so i have little need to make this work for non-SSL servers. But I did my best to make at least my version usable. Just download the script and edit the variables at the top of the script.

Note: leaving the troubleshooting variable set to True, will slow down the proxy and add some overhead to yoru server. This is because I made extensive use of echo. So once you are sure this is running properly i highly recommend setting $X = false;

To run the script simply enter the following at the command line:

php -f BB-IMAP-Proxy.php

Enjoy! Download Link

Comments are closed.