Spam Free Email

Anti-spam ideas, tools and services

January 10th, 2007

Erlang’s binary matching rocks!

Okay, so for the past few days I’ve been playing with some binary data that I wasn’t looking forward to. It wasn’t the data so much as the last time I dealt this this particular type of data was in LISP and I was not happy with the results. In fact that experience was one of the major ones that got me to switch from LISP to Erlang.

When I started working with the binary data in Erlang I went back and re-read the documentation. It’s amazing how much easier the documentation is to read after working with Erlang for hundreds of hours, but I digress. After looking at the binary matching syntax I began to realize exactly how powerful it is and the project that I was working on was up and running in about 3 hours instead of my LISP attempt where I gave up on it after 3 days.

Then I started to realize where I could use binary pattern matching in my other application, specifically any where I use IP addresses. I often store IP addresses in their 32 bit decimal notation where the IP address 192.168.1.1 would turn out to be 3232235777. It’s easier to store and sort in databases that way, but how to convert from an IP Address tuple into an IP decimal.

With pattern matching it’s simple:

IPTuple = {192,168,1,1},
IPList = tuple_to_list(IPTuple),
IPBinary = list_to_binary(IPList),
<<IPDecimal:32>> = IPBinary,
IPDecimal.

believe me I was going through much more work to get this done when I wrote the original code for Spam Free Email to do this. I had no idea that I could do the whole thing with built in functions and I can’t wait to rewrite that section of code.

If you are doing anything with binary, or even if you are looking for simpler ways to do binary math, you really need to take a close look at Erlang’s binary pattern matching … really.

Update:

Joe Armstrong emailed me with a better version of the IP to decimal conversation. It uses more of the native binary pattern matching and I like it better then the one I wrote. Here it is:

IPTuple = {192,168,2,3},
{A,B,C,D} = IPTuple,
<<IPDecimal:32>> = <<A:8,B:8,C:8,D:8>>

Thanks Joe!

January 4th, 2007

IMAP Client extensions

Between last night and this morning I started to add some extensions to the IMAP client, mostly due the fact that I wanted to take advantage of my existing IMAP server and use it to sort the messages before my web-mail application gets them. So I started with the SORT command, I think this is still in the proposal stages, but it seems to be quite mature and easily implemented.

SORT is really an extension of the SEARCH command, allowing to to search the messages and then return them in a particular order. In the IMAP client the way the code currently works I took the time to check the capabilities and make sure that SORT is listed, if not it then defaults to SEARCH. That way you still get your results, they just aren’t in any defined order.

Along with the SORT command the THREAD command is defined. THREAD=REFERENCES looks most promising as it appears to be the way that Google’s Gmail is able to group email messages into conversations. Now that I know this I am planning on implementing this and adding a threaded email view as an option in my web-mail client.

I’m going to be looking into the other extensions that my existing IMAP server handles and deciding if I wish to implement them now or later. It will depend on how useful I think the extension is and how long I think it will take to implement it.

|