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

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

10 thoughts on “Blackberry IMAP Proxy – Working Sent and Trash Folders

  1. Excellent idea you got here!

    I run Courier IMAP and have similar issues to what you described.

    I tried running your PHP version and the server is running, etc. but when I get BIS to chat to it directly I get a “error has occurred, please check your settings”. I can chat to it OK with a normal IMAP client so my settings should be ok.

    Are you still using this and is it working for you?

  2. Yes it still works for me. Actually I am surprised how well it works. It runs without ever crashing.

    I have made a few tweaks since this version, notably I have figured out how to force BIS to check mail every 3 minutes rather than every 15. I will add another entry for this script in the next week or so.

    This version should still work. I suggest debugging the conversation between BIS and your IMAP server. To do that follow these instructions:
    1. Make sure the top line still reads:
    $X = true;
    2. run the script logging the output to some file like this:
    php -f BB-IMAP-Proxy.php > BB-IMAP-Proxy.log

    Then wait for a connection from BIS. After receiving the error check the log and see if you can figure out what the issue is.

  3. My apologies, all the time I tested against the wrong instance!

    I had some issues with talking ssl://localhost, as soon as I made it talk to my plaintext Courier (tcp://localhost on 143 vs 993) it started working properly, since it’s talking to localhost I’m not concerned about it having to talk SSL.

    BIS (in the EU at least) has removed the 15minute rule recently so mails are near-immediate (not as well as BES but pretty close!).

    Thanks for your proxy it works very very well for my needs! Looking forward for any changes you might make going forward!

  4. Glad it worked for you.

    I will have to check into the delay here in the US. The recently updated BIS to the “new” version. But I am still pretty sure I am seeing a 15 minute lag in emails.

  5. Hmm, interesting. I will have to check the logs on the proxy server and see if anything looks different.

    With a recent hack I have been getting my mail in no longer than 3 minutes.

  6. Typically around 90 seconds for me with your published version – that said, if you have a hack in place I’m interested to have that as a fallback *g*

  7. Interesting, I am still seeing:
    q logout
    sent from the BIS client at the end of a session. I would expect it to be sending:
    i IDLE
    Or something like that.

    I did see someone commented that they deleted their account and created the account again on the BIS setup page and then it worked. Maybe I have to do that?? It would seem odd but RIM does do stupid stuff like that.

  8. This is what I typically see:

    –Server sent– Resource id #8 — C/S Pair:2
    i OK IDLE completed

    –Client sent– Resource id #7 — C/S Pair:2
    f UID FETCH 20027:* (UID FLAGS)

    –Server sent– Resource id #8 — C/S Pair:2
    * 632 FETCH (UID 20027 FLAGS (\Seen NonJunk))
    f OK FETCH completed.

    –Client sent– Resource id #7 — C/S Pair:2
    i IDLE

    –Server sent– Resource id #8 — C/S Pair:2
    + entering idle mode
    * 601 FETCH (FLAGS (\Seen NonJunk))
    * 609 FETCH (FLAGS (\Seen \Deleted NonJunk))

  9. Damn, that is nice.

    Yeah that isn’t happening on my end. As I browse around the internet I see some people who have it and some who don’t. I can’t tell if this is a slow rollout or what.

    Some people even claim that they had it for a few days and now they don’t.

    I really would like that feature though, that is a nice touch.

Leave a Reply

Your email address will not be published. Required fields are marked *