Lua: lib-lua
Dovecot provides a lib-lua internal helper as part of libdovecot.so. It has facilities for loading scripts from various sources, and also helps with reusing scripts by keeping track of which scripts are loaded. Each script has it's own memory pool, which is guaranteed to be released when script is unloaded.
DANGER
Never use os.exit()
from a Lua script. This will cause the whole process to exit instead of just the script.
Initialization
When script is loaded, script_init()
function is called, if found.
Changed: 2.4.0
script_init()
return value is no longer checked. Use error() instead if necessary.
De-initialization
When script is being unloaded, script_deinit()
function is called, if found.
C API
void dlua_dovecot_register(struct dlua_script *script)
Register dovecot variable. This item can also be extended by context specific tables, like authentication database adds dovecot.auth
.
void dlua_push_event(struct event *event)
Pushes an Dovecot Event to stack.
Lua API
Base Functions
dovecot.i_debug()
Signature | dovecot.i_debug(text) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Log debug level message.
dovecot.i_info()
Signature | dovecot.i_info(text) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Log info level message.
dovecot.i_warning()
Signature | dovecot.i_warning(text) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Log warning level message.
dovecot.i_error()
Signature | dovecot.i_error(text) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Log error level message.
dovecot.event()
Signature | dovecot.event(parent) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Generate new event. If parent
is not specified, the lua script is used
as the parent.
dovecot.restrict_global_variables()
Signature | dovecot.restrict_global_variables(toggle) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Enable or disable restricting new global variables.
If enabled, the rest of the script won't be allowed to declare global non-function variables but they can declare local variables and use already defined global variables.
If a script needs to define a variable, they must declare them as local i.e.
instead of my_var = "some value"
, do local my_var = "some value"
.
Restrictions will remain in place until the end of the execution of the
script or until they are lifted by calling
dovecot.restrict_global_variables(false)
.
Default is permissive mode, i.e., same as lua's default, global variables are not restricted.
HTTP Functions
dovecot.http.client()
Signature | dovecot.http.client({ debug=boolean, no_auto_redirect=boolean, no_auto_retry=boolean, connect_backoff_time_msecs=int, connect_backoff_max_time_msecs=int, connect_timeout_msecs=int, event_parent=event, max_attempts=int, max_auto_retry_delay_secs=int, max_connect_attempts=int, max_idle_time_msecs=int, max_redirects=int, proxy_url=string, request_absolute_timeout_msecs=int, request_timeout_msecs=int, soft_connect_timeout_msecs=int, rawlog_dir=string, user_agent=string }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Arguments |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns | An http_client object. |
Create a new http client object that can be used to submit requests to remote servers.
Object http_client
http_client.request()
Signature | http_client.request({ url=string, method=string }) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
| |||||||||
Returns | An http_request object. |
Create a new request object.
By default, the request has Host
, and Date
headers with relevant
values, as well as Connection: Keep-Alive
.
Object http_request
http_request.add_header()
Signature | http_request.add_header(name, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a header to the request.
http_request.remove_header()
Signature | http_request.remove_header(name) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Do a lookup of the header in the request and remove it if found.
http_request.set_payload()
Signature | http_request.set_payload(value, synchronous) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Set payload data to the request.
Optionally you can set synchronous
, which will cause "100 Continue"
header to be sent.
http_request.submit()
Signature | http_request.submit() |
---|---|
Returns | An http_response object. |
Connect to the remote server and submit the request.
This function blocks until the HTTP response is fully received.
Object http_response
http_response.status()
Signature | http_response.status() |
---|---|
Returns | Status code of the http response. |
Get the status code of the HTTP response.
The codes contain error codes as well as HTTP codes, e.g., '200 HTTP_OK' and error code that denote connection to remote server failed.
A human-readable string of the error can then be read using reason()
function.
http_response.reason()
Signature | http_response.reason() |
---|---|
Returns | String representation of the status. |
Returns a human-readable string of HTTP status codes, e.g. "OK", "Bad Request", "Service Unavailable", as well as connection errors, e.g., "connect(...) failed: Connection refused".
http_response.header()
Signature | http_response.header(name) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
| ||||||
Returns | Value of the HTTP response header. |
Get value of a header in the HTTP request.
If header is not found from the response, an empty string is returned.
http_response.payload()
Signature | http_response.payload() |
---|---|
Returns | Payload of the HTTP response as a string. |
Get the payload of the HTTP response.
Example HTTP Client Code
local json = require "json"
local http_client = dovecot.http.client {
timeout = 10000;
max_attempts = 3;
debug = true;
}
function auth_password_verify(request, password)
local auth_request = http_client:request {
url = "https://endpoint/";
method = "POST";
}
local req = {user=request.user, password=password}
auth_request:set_payload(json.encode(req))
local auth_response = auth_request:submit()
local resp_status = auth_response:status()
if resp_status == 200
then
return dovecot.auth.PASSDB_RESULT_OK, ""
else
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
end
end
Object event
event.append_log_prefix()
Signature | event.append_log_prefix(prefix) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Set prefix to append into log messages.
event.replace_log_prefix()
Signature | event.replace_log_prefix(prefix) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Append prefix for log messages.
event.set_name()
Signature | event.set_name(name) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Set name for event.
event.add_str()
Signature | event.add_str(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event.add_int()
Signature | event.add_int(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event.add_timeval()
Signature | event.add_timeval(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event.inc_int()
Signature | event.inc_int(key, diff) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Increment key-value pair.
event.log_debug()
Signature | event.log_debug(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit debug message.
event.log_info()
Signature | event.log_info(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit info message.
event.log_warning()
Signature | event.log_warning(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit warning message.
event.log_error()
Signature | event.log_error(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit error message.
event.passthrough_event()
Returns an passthrough event.
A log message must be logged or else a panic will occur.
Object event_passthrough
event_passthrough.append_log_prefix()
Signature | event_passthrough.append_log_prefix(prefix) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Set prefix to append into log messages.
event_passthrough.replace_log_prefix()
Signature | event_passthrough.replace_log_prefix(prefix) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Append prefix for log messages.
event_passthrough.set_name()
Signature | event_passthrough.set_name(name) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Set name for event.
event_passthrough.add_str()
Signature | event_passthrough.add_str(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event_passthrough.add_int()
Signature | event_passthrough.add_int(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event_passthrough.add_timeval()
Signature | event_passthrough.add_timeval(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Add a key-value pair to event.
event_passthrough.inc_int()
Signature | event_passthrough.inc_int(key, diff) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Increment key-value pair.
event_passthrough.log_debug()
Signature | event_passthrough.log_debug(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit debug message.
event_passthrough.log_info()
Signature | event_passthrough.log_info(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit info message.
event_passthrough.log_warning()
Signature | event_passthrough.log_warning(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit warning message.
event_passthrough.log_error()
Signature | event_passthrough.log_error(message) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Emit error message.
Object dict
Note
Currently, this object cannot be created within the Lua code itself.
dict.lookup()
Signature | dict.lookup(key[, username]) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Lookup key from dict. If key is found, returns a table with values.
If key is not found, returns nil
.
dict.iterate()
Signature | dict.iterate(path, flags[, username]) | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Returns an iteration step function and dict iter userdata. For example:
for key, values in dict:iterate(key_prefix, 0) do
dovecot.i_debug('key='..key..', first value='..values[1])
end
dict.transaction_begin()
Signature | dict.transaction_begin([username]) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Returns a new transaction object.
Object dict.transaction
dict.transaction.set()
Signature | dict.transaction.set(key, value) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Set key=value in the dict transaction.
dict.transaction.unset()
Signature | dict.transaction.unset(key) | ||||||
---|---|---|---|---|---|---|---|
Arguments |
|
Unset key in the dict transaction.
dict.transaction.set_timestamp()
Signature | dict.transaction.set_timestamp({ seconds=int, nanoseconds=int }) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
|
Set timestamp to the dict transaction.
This is currently used only with Cassandra.
dict.transaction.commit()
Commit the transaction.
dict.transaction.rollback()
Rollback the transaction.
Object dns_client
Added: 2.4.0
Note
Currently, this object cannot be created within the Lua code itself.
dns_client.lookup()
Signature | dns_client.lookup(hostname[, event]) | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Arguments |
| |||||||||
Returns | On succesful DNS lookup, returns a table with IP addresses (which has at least one IP). On failure, returns nil, error string, net_gethosterror() compatible error code (similar to e.g. Lua io.* calls). |
Lookup hostname asynchronously via dns-client process.