Services

Services are external programs that can be used via the functions available in this module.

fn get(key)

Get the value of a key in a csv database.

Args

  • key - the key to query.

Return

  • Array of records - an array containing the results. If no record is found, an empty array is returned.

Effective smtp stage

All of them.

Example

import "services" as svc;

#{
    mail: [
       action "fetch database" || {
            let records = svc::my_database.get(mail_from());

            if records == [] {
                log("debug", `${mail_from()} is not in my database`);
            } else {
                log("debug", `${mail_from()} found in my database: ${records}`);
            }
       }
    ]
}


fn query(q)

Query a database.

Args

  • q - the query to execute.

Effective smtp stage

All of them.

Example

service my_db db:mysql = #{
    url: "mysql://localhost/",
    user: "guest",
};
import "services" as svc;

#{
    connect: [
       action "log database" || {
            let version_record = svc::my_db.query("SELECT version();");
            // Result is a array of maps: [ #{"version()": "'version-of-mysql'"} ]
            log("trace", `Database version is ${version_record[0]["version()"]}`);

            // Fetching a 'greylist' database with a 'sender' table containing the (user, domain, address) fields.
            let senders = svc::my_db.query("SELECT * FROM greylist.sender");

            // Iterate over rows.
            for sender in senders {
                // You can access the columns values using Rhai's Map syntax:
                print(`name: ${sender.user}, domain: ${sender.domain}, address: ${sender.address}`);
            }

            // Populate the database with a new record.
            svc::my_db.query(`INSERT INTO greylist.sender (user, domain, address) values ("john.doe", "example.com", "john.doe@example.com");`);
       }
    ]
}


fn rm(key)

Remove a record from a csv database.

Args

  • key - the key to remove.

Effective smtp stage

All of them.

Example

import "services" as svc;

#{
    mail: [
       action "remove sender from database" || {
            svc::my_database.rm(mail_from());
       }
    ]
}


fn run(args)

Run a command from a `cmd` service with arguments. This allows you to run a command with dynamic arguments.

Args

  • args - an array of strings that will replace current command arguments defined in the args field of a cmd service.

Effective smtp stage

All of them.

Example

// services.vsl
service echo cmd = #{
    timeout: "2s",
    command: "echo",
    args: ["-e", "using cmd to print to stdout\r\n"],
};
// main.vsl
import "services" as svc;

#{
    rcpt: [
       action "print recipient using command" || {
            // prints all recipients using the `echo` command.
            svc::echo.cmd_run(["-E", "-n", `new recipient: ${rcpt()}`]);
       }
    ]
}


fn set(record)

Set a record into a csv database.

Args

  • record - the record to set.

Effective smtp stage

All of them.

Example

import "services" as svc;

#{
    mail: [
       action "set sender in database" || {
            svc::my_database.set([ mail_from() ]);
       }
    ]
}