The Sieve mail filtering language is great for filtering email messages on the server side.
I use it heavily for my IMAP account on my homeserver. There is a really good and short tutorial about the syntax with great real world examples. Go and check it out.
An introduction to Sieve, the mail filtering language. This tutorial introduces the basic building blocks of a Sieve filter and explains the conceps with many examples.