Getting Started With Lamson

Lamson is designed to work like modern web application frameworks like Django, TurboGears,ASP.NET, Ruby on Rails, and whatever PHP is using these days. At every design decision Lamson tries to emulate terminology and features found in these frameworks. This Getting Starteddocument will help you get through that terminology, get you started running your firstlamson application, and walk you through the code you should read.

In total it should take you about 30 minutes to an hour to complete. If you just wantto try Lamson, at least go through the 30 second introduction given first.

You now have a working base Lamson setup ready for you to work on with the following installed:

Lamson and all dependencies (Jinja2, nosetests)

Code for your project in mymailserver. Look in app/handlers and config/settings.py.

Two initial tests that verify your server is not an open relay and forwards mail in tests/handlers/open_relay_tests.py.

A “logger” server running on port 8825 that dumps all of its mail into the run/queue maildir.

A config script for mutt (muttrc) that you can use to inspect the run/queue and also send mail using Lamson’s send command.

When you’re in mutt during the above test run, try sending an email. Theincluded muttrc is configured to use the run/queue as the mail queue, and touse the lamson sendmail command to deliver the mail. This tricks mutt intointeracting directly with your running Lamson server, so you can test the thingwith a real mail client and see how it will work without having to actuallydeploy the server.

Finally, if you wanted to stop all of above you would do:

$ lamson stop -ALL run

Which tells Lamson to stop all processes that have a .pid file in the run directory.

Important Terminology

If you are an old SMTP guru and/or you’ve never written a web application witha modern web framework, then some of the terminology used in Lamson may seemconfusing. Other terms may just confuse you or scare you because they soundcomplicated. I tried my best to make the concepts used in Lamsonunderstandable and the code that implements them easy to read. In fact, youcould probably read the code to Lamson in an evening and understand howeverything works.

Experience has taught me that nobody reads the code, even if it issmall. Therefore, here are the most important concepts you should know to geta grasp of Lamson and how it works.

MVC — Model View Controller is a design methodology used in web application frameworks where the data (model), presentation (view), and logic (controller) layers of the application are strictly separated.

FSM — Lamson uses the concept of a Finite State Machine to control how handlers execute. Each time it runs it will perform an action based on what it is send and what it was doing last. FSM in computer science class are overly complex, but in Lamson they are as easy to use as a return statement.

Template — Lamson generates the bodies of its messages using Templates, which are text files that have parts that get replaced with variables you pass in. Templates are converted to their final form with a process called rendering.

Relay — The relay for a Lamson server is where Lamson delivers its messages. Usually the Relay is a smart tougher server that’s not as smart, but very good at delivering mail. Lamson can also be run as a Relay for testing purposes.

Receiver — Lamson typically runs as the Receiver of email. If you are familiar with a web application setup, then Lamson is the inverse. Instead of Lamson runing “behind” an Apache or Nginx server, Lamson runs “in front” of an SMTP server like Postfix. It listens on port 25, handles the mail it should, and forwards the rest to the Relay. This makes Lamson much more of a Proxy or filter server.

Queue — Lamson can also do all of its processing off a queue. In this setup you would have your normal mail server dump all mail to a maildir queue, and then tell Lamson to process messages out of there. This can be combined with the usual Receiver+Relay configuration for processing messages that might take a long time.

Maildir — A standard created for the qmail project with stores mail in a directory such that you can access the mail atomically and store it on a shared disk without conflicts or locking.

Managing Your Server

Your Lamson application is now running inside the Lamson Python server. This is a very simple server based on Python’ssmtpd and asyncore libraries.

If you want to know more about how it operates, take a look at the lamson/server.py file in the source distribution.

You’ll need to use a few Lamson commands to manage the server. You already experienced them in the 30 second introduction, and you can review them all or see themby using the lamson help command.

Right now you have Lamson running on port 8823 and a “Lamson logger” running on 8825. This means thatyour lamson server (port 8823) will forward its messages to the logger (port 8825) thinking it’s yourreal relay server. The truth is the logger just logs its messages to logs/logger.log and dumps it intorun/queue so you can inspect the results.

Before we learn how to manage them and what they do, open up theconfig/settings.py file and take a look:

Your file probably has some comments telling you what these do, but it’s important to understandhow they work.

First, this file is just plain old Python variables. It is loaded by one oftwo other files in your config directory: config/boot.py orconfig/testing.py. The config/boot.py file is started whenever you use thelamson start command and its job is to read the config/settings.py andstart all the services you need, then assign them as variables back toconfig.settings so your handlers can get at them. The config/testing.py isalmost the same, except it configures config.settings so that your unit testscan run without any problems. Typically this means setting the spell checkerand not starting the real server.

The important thing to understand about this setup (where a boot file reads settings.py andthen configures config.settings) that it makes it easy for you to change Lamson’s operationsor start additional services you need and configure them. For the most part you won’t needto touch boot.py or testing.py until you need to add some new service, change the templatelibrary you want to use, setup a different database ORM, etc. Until then just ignore it.

settings.py Variables

The receiver_config variable is used by the lamson start command to figure out where to listenfor incoming SMTP connections. In a real installation this would be port 25 on your externalIP address. It’s where the internet talks to your server.

The relay_config setting is used by Lamson to figure out where to forward message replies (responses)for real delivery. Normally this would be a “smart host” running a more established serverlike Postfix or Exim to do the grunt workof delivering to the final recipients.

The handlers variable lists the modules (not files) of the handlers you want to load. Simply put them here and they’ll be loaded, even the lamson.handlersmodules will work here too.

The router_defaults are for the lamson.routing.Routerclass and configure the default routing regular expressions you plan on using. Typically you’ll at least configure the host regular expression since that is used in every route and shouldn’t change too often.

Finally, template_config contains the configuration values for the templating system you’llbe using. Lamson supports either Mako or Jinja2, but defaults to Jinja2.

Looking At config/boot.py

Programmers need to know how everything works before they trust it, so let’s look at the config/boot.pyfile and see how these variables are used:

Don’t be afraid that you see this much Python, you normally wouldn’t touch this fileunless it were to add your own services or to make a new version for a different configuration.For the most part, you can just edit the config/settings.py and go.

First you’ll see that config/boot.py sets up logging using the config/logging.conffile, which you can change to reconfigure how you want logs to be created.

Then it starts assigning variables to the config.settings module that ithas imported at the top. This is important because after config.boot runsyour lamson code and handlers will have access to all these services. Youcan get directly to the relay, receiver, database and anything else you needby simply doing:

from config import settings

After that config.boot sets up the settings.relay, settings.receiver,and settings.database. These three are used heavily in your own Lamson code,so knowing how to change them if you need to helps you later.

After this we configure the lamson.routing.Router to have your defaults,load up your handlers, and turn on RELOAD. Setting Router.RELOAD=Truetell the Router to reload all the handlers for each request. Very handy when youare doing development since you don’t need to reload the server so often.

If you deploy to production, then you’ll want to set this to False since it’sa performance hit.

Finally, the config.boot does the job os loading the template system you’ll use,in this case Jinja2. Jinja2 and Mako use the same API so you can configureMako here as well, as long as the object assigned to view.LOADER has the same APIit will work.

Developing With Lamson

Now that you’ve received a thorough introduction to how to manage Lamson, and how it is configured, you can get into actually writing some code for it.

Before you begin, you should know that writing an application for a mail servercan be a pain. The clients and servers that handle SMTP make a large number ofassumptions based on how the world was back in 1975. Everything is on defined portswith defined command line parameters and the concept of someone pointing theirmail client at a different server arbitrarily just doesn’t exist. The world of email is not like the web where you just take any old “client” and pointit at any old server and start messing with it.

Lucky for you, Lamson has solved most of these problems and provides you with a bunch of handy development tools and tricks so you can work with your Lamson serverwithout having to kill yourself in configuration hell.

Using Mutt

You probably don’t have another SMTP server running, and even if you did, it’dbe a pain to configure it for development purposes. You’d have to setup aliases, newmail boxes, restart it all the time, and other annoyances.

For development, what we want is our own little private SMTP relay, and since Lamson canalso deliver mail, that is what we get with the command:

$ lamson log

This tells Lamson to run as a “logging server”, which doesn’t actually deliverany mail. With this one command you have a server running on 8825 that takes everymail it receives and saves it to the run/queue Maildir and also logs it tologs/logger.log. It also logs the full protocol chat to logs/lamson.err soyou can inspect it.

Lamson uses Maildir by default since it is the most reliable and fastest mail queueformat available. It could also store mail messages to any queue supported by Python’smailbox library. If you wereadventurous you could also use a RDBMS, but that’s just silly.

You also have the file muttrc which is configured to trick mutt into talking to yourrunning Lamson server, and then read mail out of the run/queue maildir that is filledin by the lamson log server. Let’s take a look:

set mbox_type=Maildir
set folder=“run/queue”
set mask=”!\\.[.]”
set mbox=“run/queue”
set record=”+.Sent”
set postponed=”+.Drafts”
set spoolfile=“run/queue”
set sendmail=”/usr/bin/env lamson sendmail -port 8823 -host 127.0.0.1”

Notice that it’s configured sendmail to be “sendmail -port 8823 -host 127.0.0.1”which is a special lamson sendmail command that knows how to talk to lamson andread the arguments and input that mutt gives to deliver a mail.

Why does Lamson need its own sendmail? Because you actually have to configure mostmail server’s configuration files to change their ports before their sendmail commandwill use a different port. Yes, the average sendmail command line tool assumes that itis always talking to one and only one server on one and only one port for ever and alleternity. Without lamson sendmail you wouldn’t be able to send to an arbitraryserver.

With this setup (lamson start ; lamson log ; mutt -F muttrc) you can nowuse your mutt client as a test tool for working with your application.

Stopping Lamson

The PID files are stored in the run directory. Here’s a samplesession where I stop all the running servers:

You can also pass other options to the stop command to just stop one server. Uselamson help -for stop to see all the options.

Starting Lamson Again

Hopefully you’ve been paying attention and have figured out how to restart lamson and thelogging server. Just in case, here it is again:

$ lamson start
$ lamson log

You should also look in the logs/lamson.log file to see that it actually started. Theother files in the logs directory contain messages dumped to various output methods (likePython’s stdout and stderr). Periodically, if the information you want is not in logs/lamson.log then it is probably in the other files.

You can change your logging configuration by editing the logging line your config/settings.py file.

Other Useful Commands

You should read the available commands documentation to get anoverview, and you can also use lamson help to see them at any time.

send

The first useful command is lamson send, which lets you send mail to SMTP servers (notjust Lamson) and watch the full SMTP protocol chatter. Here’s a sample:

Using this helps you debug your Lamson server by showing you the exact protocol sentbetween you and the server. It is also a useful SMTP server debug command by itself.

When you use the supplied muttrc you’ll be configured to use Lamson’s sendmail(not *send) command as your delivery command. This lets you use mutt as a complete developmenttool with minimal configuration.

queue

The lamson queue command lets you investigate and manipulate the run/queue (or any maildir).You can pop a message off, get a message by its key, remove a message by its key, count the messages,clear the queue, list keys in the queue. It gives you a lower level view of the queue thanmutt would, and lets you manipulate it behind the scenes.

restart

Lamson does reload the code of your project when it receives a new request (probably toofrequently), but if you change the config/settings.py file then you need to restart.Easiest way to do that is with the restart command.

Walking Through The Code

You should actually know quite a lot about how to run and mess with Lamson, so you’llwant to start writing code. Before you do, go check out the API Documentationand take a look around. This document will guide you through where everything is and howto write your first handler, but when you start going out on your own you’ll need a goodset of reference material.

At the top level of your newly minted project you have these directories:

You don’t technically have to store your data in app/data. You are freeto put it anywhere you want, it’s just convenient for most situations tohave it there.

Your app/model directory could have anything in it from simple modules forworking various Maildir queues, to full blown SQLAlchemy configurations foryour database. The only restriction is that you load them in the modulesyourself (no magic here).

The app/templates directory can have any structure you want, and as yousaw from the config.boot discussion it is just configured into theJinja2 configuration as the default. If you have a lot of templates it mighthelp to have them match your app/handlers layout in some logical way.

That only leaves your app/handlers directory:

app/handlers/__init__.py
app/handlers/sample.py

This is where the world gets started. If you look at your config.settingsyou’ll see this line:

handlers = ['app.handlers.sample’]

Yep, that’s telling the lamson.routing.Routerto load your app.handlers.samplemodule to kick it into gear. It really is as simple as just putting the file inthat directory (in in sub-modules there) and then adding them to the handlerslist.

You can also add handlers from modules outside of your app.handlers:

handlers = ['app.handlers.sample’, 'lamson.handlers.log’]

This installs the handler (lamson.handlers.log) that lamson uses to log every email it receives.

Writing Your Handler

This document is for getting started quickly, so going into the depths of the cool stuffyou can do with Lamson handlers is outside the scope, but if you open the app/handlers/sample.pyfile and take a look you’ll how a handler is structured.

Since Lamson is changing so much the contents of the file aren’t included in this document. You’ll have to open it and take a look.

Notice that we include elements from the lamson.routing that are decoratorswe use to configure a route. Then you’ll see that we’re getting that settings.relayand settings.database we configured in the previous sections. Finally we bringin the lamson.view module directory to make rendering templates into email messagesa lot easier.

Now take a look at the rest of the file and you’ll how a handler is structured:

Each state is a separate function in CAPS. It doesn’t have to be, it just looks better.

Above each state function is a route, route_like, or stateless decorator to configure how lamson.routing.Router uses it.

The route decorator takes a pattern and then regex keyword arguments to fill it in. The words in the pattern string are replaced in the final more complex routing regex by the keyword arguments after. However, if you want to use regex directly you can, route just needs a string that eventually becomes a regex.

A state function changes state by returning the next function to call. You want to go to the RUNNING state, just return RUNNING.

If any state function throws an error it will go into the ERROR state, so if you make a state handler named ERROR it will get called on the next event and can recover.

If you want to run a state on this event rather than wait to have it run on the next, then simple call it and return what it returns. So to have RUNNING go now, just do return RUNNING(message, ...) and it will work.

If a state has the same regex as another state, just use route_like to say that.

If you have a stateless decorator after a route or route_like, then that handler will run for all addresses that match, not just if this handler is in that state.

That is pretty much the entire complexity of how you write a handler. Yousetup routes, and return the next step in your conversation as the nextfunction to run. The lamson.routing.Router then takes each message it receivesand runs it through a processing loop handing it to your states and handlers.

How States Are Run

The best way to see how states are processed is to look at the Router code that does it:

What this does is take all the handlers you’ve loaded, and then finds which handlers have a state function thatmatches the current message. It then goes through each potential match, and determines which of all the matchingstate functions is “in that state”. This means that, even though you have six state functions that answer to“(list_name)-(action)@(host)” only the one that matches the users current state (say PENDING) will be called next.As it goes through these functions it also loads up any that are marked “stateless” so they can be called as well.

Finally, it just calls them in order. If the message results in no methods to call, then it will take the messageand tell you this, or put it into an UNDELIVERABLE_QUEUE for you to review it later.

Slight design criticism: Currently the order of these calls is fairly deterministic, but you can’t rely on it.It’s also not clear if all matching states should run, or just the first. It currently only runs the first match,but it might be better to run each match from each handler. Suggestions welcome on this.

Debugging Routes

In the old way of doing routing you would edit a large table of “routes” in your config/settings.py file andthen that told Lamson how to run. The problem with this is it was too hard to maintain and too hard to indicate that different states needed a different route.

The new setup is great because all your routing for each handler module is right there, and it’s easy to seewhat will cause a particular state function to go off.

What sucks about the new setup is that you can’t find out what all the routes are doing globally in one place. That’s where lamson routes comes in. Simply run that command and you’ll get a debug dump ofall the full routing regex and the functions and modules they belong to:

This is telling you which regex is matched first, then what those regex are mapped to. This is very handy as you can copy-pastethat regex right into a python shell and then play with it to see if it would match what you want.

You can also pass in an email address to the -test option and it will tell you what routes would matchand which functions that will call:

If you’re working with Lamson this is incredibly helpful, because it tells you whatroutes you have, what functions they call, and then it’ll take an email address andtell you all the routes that match it.

THREADING!

Lamson takes a lighter approach to how it runs. It assumes that most of thetime you want lamson to keep itself sane with minimal locking, and that youwant each of your state functions to run in a thread lock that prevents othersfrom stepping on your operations. In 95% of the cases, this is what you want.

To accomplish this, Lamson’s router will acquire an internal lock foroperations that change its state, and a separate lock before it calls eachstate function. Since multiple state functions run inside each thread, but onethread handles each message, you’ll get multiple processing, but each statewon’t step on other states in the system.

However, it’s those 5% of the times that will kill your application, and if youknow what you’re doing, you should be able to turn this off. In order to tellthe Router not to lock your state function, simply decorate it withnolockingand Lamson will skip the locking and just run your state raw. This means thatother threads will run potentially stepping on your execution, so you must doyour own locking.

Now, don’t think that slapping anolockingon your state functions is some magic cure for performance issues. You onlyever want to do this if you really know your stuff, and you know how to makethat operation faster with better controlled locking.

The reality is, if you have an operation that takes so long it blockseverything else, then you are doing it wrong by trying to do it all in yourstate function. You should change your design so that this handler drops themessage into alamson.queue.Queueand that another Lamson server reads messages out of that to do the longrunning processing.

Using queues and separate Lamson servers you can solve most of your processingissues without a lot of thread juggling and process locking. In fact, sinceLamson uses maildir queues by default you can even spread these processors outto multiple machines reading off a shared disk and everything will be justfine.

But, since programmers will always want to just try turning off the locking,Lamson supports the nolocking decorator. Use with care.

What’s In A Unit Test

Writing unit tests is way outside the scope of this document, but you should read up on using nosetests, testunit, andyou should look at lamson.testing for a bunch of helper functions. Also look in the generated tests directoryto see some examples.

Spell Checking Your Email Templates

Another big help is that Lamson has support for PyEnchant so youcan spell check your templates. You can use lamson.testing.spelling function in your unit tests.

Installing PyEnchant is kind of a pain, but the trick is to get the dictionary you want and put it in your~/.enchant/myspell directory. You’ll also want to open the config/testing.py file and uncomment thelines at the bottom that tell PyEnchant where to find the enchant so (dylib).

PyEnchant is kind of hard to use, so if you have suggestions on a better Python spell checking lib for unit tests please let me know.