Tech Tips: Unleashing Lua

Tom Mairs
Apr. 1, 2012 by Tom Mairs

Starting with Momentum Version 3.0, Message Systems has included an embedded copy of the Lua language in the bundle as a replacement for Sieve. For years Message Systems customers used Sieve as a policy engine to script actions based on message details, but Sieve, even in its modified Sieve++ form, is limited. Lua is a big improvement, offering the ability to iterate over data and work with tables, among other features. Just like Sieve, we have modified our embedded version of Lua to extend its functionality in numerous ways.

Not surprisingly, Lua has a large following in the programming world. It’s a well-developed, multi-purpose language that is often used by game developers for its simplicity and speed, as well as its inherent ability to expose and use C modules as well as its own native scripting. In Momentum, Lua has been extended with a number of local functions specifically to help with messaging rules. Aside from the extensive documentation on our custom functions, there is also a whole community of Lua users and developers on the web (links at the end of the article).

You will see in the sample below that the code itself is quite simple to follow, and anyone who has written a policy script in Sieve will see this as manna from heaven. So what does Lua look like in Momentum? I can show you using a very basic policy script that will assign messages to a binding based on an X-Header value. Here is the heavily commented policy script:

— This policy script is a common, but very simple example
–[[
You can use comments in Lua with a “–” for a single line or multi-line comments can be framed as this one is.
]]–
— As with many languages, you can “require” helper modules
— We have developed dozens of helpers that make message management easier

require(“msys.core”)
require(‘msys.extended.message’);


— We define the following variable as “local” and make it a “table” using braces
— IE: the variable “mod” is a local table

local mod = {};


— We have predefined function names for each message phase.
— This one is active specifically in the set_binding phase.

function mod:validate_set_binding(msg)


— getting the value of the X-Binding header is this easy:

   local mybinding = msg:header(“X-Binding” );


— Assigning a binding of the same name as the header is just as easy:

   local err = msg:binding(mybinding);


— proper etiquette requires us to “end” the function

end;


— and finally we register the script so Momentum can use it.

msys.registerModule(“policy”, mod);

Toggle Comments »

Enabling the script to run with Momentum just requires adding a reference to the ecelerity.conf file:

scriptlet scriptlet {
   script policy {
      source = “/opt/msys/ecelerity/etc/conf/default/lua/policy.lua”
   }
}

That is all there is to it. This is a very basic example but is probably the most common use of scripting in Momentum. Our clients have installed some extremely complex functionality with Lua including auto-responders, message cadence (recipient fatigue) protection and automated database list hygiene systems. If you do a little research you will find that Lua is embedded or core to many popular pieces of familiar code including Angry Birds, Civilization V, World of Warcraft, Far Cry, AutumnOut, the Sputnik wiki engine, and the Cisco Adaptive Security Appliance. We are not alone in thinking Lua is awesome.

If you’re interested in gaining a better understanding of the Lua language, I recommend the book Programming in Lua, Second Edition by Roberto Ierusalimschy — A.K.A. the Lua Bible. There’s also a ton of links on the Internet, and I’ve listed a few of them below. By the way, Lua is a proper name, and is not an acronym. Portuguese for “moon,” Lua was originally developed by a team at the Pontifical Catholic University of Rio de Janeiro in Brazil.

Related Content

5 Best Practices for Security Notifications

Learn the 5 best practices for security notification emails that product teams can use to build user trust and confidence.

read more

What GoT’s Casterly Rock Can Tell SaaS About Email Security

The defenses and vulnerabilities of castles in Game of Thrones should be a warning for SaaS providers about phishing and email security.

read more

Getting Started with SparkPost in Java

A quick and easy guide on how to use the SparkPost Java Client Library to integrate with SparkPost to allow users to send emails faster.

read more

Start sending email in minutes!

The world’s most powerful email delivery solution is now yours in a developer-friendly, quick to set up cloud service. Open a SparkPost account today and get started for free.

Get Started

Send this to a friend