Writing an e-mail application with Lamson - II

In the last
post,
we saw how to create the skeleton of a basic email application using
Lamson. In this part, we'll see how to write a handler (in the
controller) to open a ticket in Unfuddle, the project management tool we
use at Agiliq.

If you look at the config/settings.py file, you'll see a handlers
attribute that should be updated to match the file that contains the
rules for mail routing. In this case, let us create a unfuddle.py
under the app/handlers directory and update the config/settings.py:

For a conventional application, e-mail is routed on the basis of rules
mentioned in configuration files (aliases, macros etc) and then dropped
off to a separate process (like fetchmail or procmail) which finally
delivers the e-mail to it's destination. In lamson, all this routing is
implemented through 'State
Machines'. This
makes for cleaner code (unified at one place and process) and easy to
develop and debug. Each function generally represents a state and
returns the next state (could be the same state itself) where the
application should proceed. Lamson uses data stores to store the state.
By default, it uses the MemoryStorage but it's very easy to write a
custom data storage to say store the mails in a database etc.

In our example app, there are two states to the state machine. They are
'START' and 'ERROR'. The ERROR state is a special state in which the
application finds itself when there's any error. The 'START' state
accepts all mails with the from address in the regexp format
(?P<subdomain>\w+)\+(?P<project>\w+)@(?P<host>.+). These captured
regular expression groups are passed as keyword arguments to the
'START' function. Based on these keyword arguments, we use the
Unfuddle API (which uses HTTP Basic
Auth) to create a ticket if it doesn't exist else attach a comment to
the ticket.

We'll have to restart the lamson server to be able to reflect the new
changes. You can then use the local mutt config file to be able to send
mails to the server and try out your changes (for example, send a mail
to <[email protected]>):

mutt -F muttrc

Keep a watch at the logs/lamson.log files while developing because
this information may be invaluable.