diff --git a/Lua-Examples.md b/Lua-Examples.md index 47d86b1..011ddf8 100644 --- a/Lua-Examples.md +++ b/Lua-Examples.md @@ -2,7 +2,7 @@ By default Inbucket will load `inbucket.lua`, but you may use the `INBUCKET_LUA_ ## Event trigger: after message stored -Prints metadata of stored messages to STDOUT. +Prints metadata of stored messages to STDOUT: ```lua function inbucket.after.message_stored(msg) @@ -22,7 +22,7 @@ function inbucket.after.message_stored(msg) end ``` -Makes a JSON encoded POST to a web service. +Makes a JSON encoded POST to a web service: ```lua local http = require("http") @@ -41,4 +41,26 @@ function inbucket.after.message_stored(msg) body = request, })) end +``` + +Writes data to temporary file and runs external shell command: + +```lua +function inbucket.after.message_stored(msg) + local content = string.format("%q,%q", msg.from, msg.subject) + + -- Write POST data to temporary file. + local fnam = os.tmpname() + local f = assert(io.open(fnam, "w+")) + assert(f:write(content)) + f:close() + + local cmd = string.format("cat %q", fnam) + print(string.format("\n### running %s ###", cmd)) + local status = os.execute(cmd) + if status ~= 0 then + error("command failed: " .. cmd) + end + print("\n") +end ``` \ No newline at end of file