yes i love technology

Tech Superstars: Bruce Schneier

February 9th, 2007 by Pete

Tech Superstars is a new segment that I’ll be running. In it I will be profiling individuals that have made an important contribution to the fields of Computer Science or technology. Readers of my Peteamania blog will know I love facts, I’m sure you’ll be delighted to hear my Tech Superstar Profiles will contain facts galore!

Bruce Schneir Looking Buff

Tech Superstar #1 is security guru Bruce Schneider. Regular blog commenter fat-faced Phil Evans sent me a link to this brilliant Brucey facts website a few months ago. This is my favourite fact from it:

  • FACT: Bruce Schneier can losslessly compress random data by 50%, with his fists

The people that have made the Bruce facts site seem to like Knuth a lot as well. Personally I was never too keen on Knuth, he just seemed too wrapped up in Theory not the application. I mean the man is supposed to be a Computer Science legend but he doesn’t even read his email!

Anway I got a bit sidelined with my Knuth ranting, lets all agree that he’s “no Bruce” and I’ll be happy. Back to Bruce: The Bruce Schneier book Secrets & Lies is an excellent read on computer security. Unusually for a technical book it’s very readable [1] and entertaining, while containing some great explanations of how digital security works. I’d give you a quote from the book but Phil Evans has stolen it from me. Bruce’s Applied Cryptography book is supposed to be extremely good but I haven’t read it yet, don’t worry it’s on my ever growing list of things to do!

Anyway hope you enjoyed this first Tech Superstar Profile, if you would like to see someone in particular covered then add your suggestions in the comments section.

/* Pete Graham */

[1] Technically nearly all books are readable; I mean in this case it reads like an actual book rather than a technical manual

Posted in Tech, Bruce Schneier, Tech Superstars, Knuth | 2 Comments »

Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)

February 9th, 2007 by Pete

One of the popular facilities on phuser.com is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!

Handling of emails replies has easy been the trickiest part of this process. Here is a very basic overview of the steps we have taken:

  1. Have postfix forward the email to a PHP command line script that saves the email to file. Details of how to do this can be found here.
  2. Cron runs a PHP script that processes the email files. It makes sure the emails are valid, strips the users reply from the body of the email, and then adds the email to a database table.
  3. Crons runs another script which queries the database table and processes the emails replies, this is where the actually interaction with the phuser system (the magic) happens!

Today I shall be describing the regular expression that we used for Step 3 as it took a bit of effort to get right. Step 2 will be covered at a later date.Warning: when I cover Step 2 I will be having a rant about my new found hatred of HTML emails!

Note: Steps 1 and 2 are separate because security precautions on our servers prevent the Postfix user from interacting with the Databases.

When someone gets a poll email from phuser the content looks like this:

pete (pete graham) wants your opinion:

Which is your favourite drink?

Vote for one of the options below:

1) Tea
2) Coffee
3) I love them both like they were my children
4) Urgh! I can’t stand either!

——————— YOUR REPLY BELOW THIS LINE ———————

——————— YOUR REPLY ABOVE THIS LINE ———————

Reply using one of the following methods:

o Reply to this email. Include the number of your vote at the start of your message. Any extra text will be added as a comment. (Please do not edit the Subject line).

Now due to the difference in the way email clients work and the ways people think here is a small selection of the possible replies we needed to be able to handle:

  • > 1 I like to drink tea since I am a English Gentleman!
  • 4 I’ll have whats going
  • 3 – I hate them all
  • > 2)
  • 1) tea because I love tea!!!
  • 1 I’m not a big drinker of either since I’m the no-caffs, but like a cup of decaff tea
  • > 1
  • 2

So what we required is a Regular Expression which would correctly grab the number if one was included at the beginning so we could log the voting option. We also needed to grab the text afterwards if any was included so it could be used as a comment. Here’s what we eventually came up with:

CODE:

  1. /^[^\w]*([0-9]+)[^\w]*(\w.*)/ms

And it works, yay! The ‘m’ flag is set so the Regex is multiline , the ’s’ flag makes the dot character match every character including newlines. Here is the page that describes all the PHP regular expression modifiers.

If you want to learn more about regular expressions (and who doesn’t) then loads of information can be found on this site. Another great site is the one that we used to testing our regular expressions, it lets you supply up to 10 different test strings at once.

If you’d like to see this system in action then go to phuser.com and request an invite we are still looking for Beta testers currently, if you sign-up now you can have a real input into the site as it develops and grows.

Ps. The regular expression is in an if statement. If it fails the entire message is put in the comments section on the poll.

# Pete Graham

Posted in Uncategorized, phuser, postfix, php, regex, regular expressions | 8 Comments »