# &adminguide;

## Overview

This module allows executing JavaScript scripts from the Kamailio configuration file. It exports all KEMI functions to JavaScript in order to access the currently processed SIP message. These functions are named within the JavaScript object 'KSR'.

The module is based on the Duktape JavaScript engine (http://www.duktape.org), which is a fast and easy to embed JavaScript interpreter. The exported API from Kamailio to JavaScript is documented in the wiki.

The module has two JavaScript contexts:

- first is used for functions jsdt_dofile() and jsdt_dostring().

- second is used for function jsdt_run() and parameter 'load' and 'loaddir'. Therefore jsdt_run() cannot execute functions from scripts loaded via jsdt_dofile() in config. This is kind of caching mode, avoiding reading file every time, but you must be sure you do not have something that is executed by default and requires access to SIP message. This environment is also used by KEMI framework for the config SIP routing functions.

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- none.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- libm - the math library (part of standard system libraries - it is needed to complile embedded Duktape JS interpreter).

## Parameters

### `load` (str)

Set the path to the JavaScript file to be loaded at startup. Then you can use jsdt_run(function, params) to execute a function from the script at runtime. If you use it for KEMI configuration, then it has to include the required functions.

Default value is null.

#### Set `load` parameter

    ...
    modparam("app_jsdt", "load", "/usr/local/etc/kamailio/js/myscript.js")
    ...

### `loaddir` (str)

Set the path to the folder containing JavaScript files to be loaded at startup. All .js files in the folder will be loaded and combined into a single javascript script. Then you can use jsdt_run(function, params) to execute a function from the script at runtime. If you use it for KEMI configuration, then it has to include the required functions.

Default value is null.

#### Set `loaddir` parameter

    ...
    modparam("app_jsdt", "loaddir", "/usr/local/etc/kamailio/js")
    ...

### `mode` (int)

Control if the API to load JavaScript module (nodejs interface) is initialized or not.

Default value is 1 (initialize).

#### Set `mode` parameter

    ...
    modparam("app_jsdt", "mode", 0)
    ...

## Functions

### `jsdt_dofile(path)`

Execute the JavaScript script stored in 'path'. The parameter can be a string with pseudo-variables evaluated at runtime.

#### `jsdt_dofile` usage

    ...
    jsdt_dofile("/usr/local/etc/kamailio/js/myscript.js");
    ...

### `jsdt_dostring(script)`

Execute the JavaScript script stored in parameter. The parameter can be a string with pseudo-variables.

#### `jsdt_dostring` usage

    ...
    if(!jsdt_dostring('KSR.dbg("test message\n")'))
    {
        xdbg("SCRIPT: failed to execute js script!\n");
    }
    ...

### `jsdt_run(function, params)`

Execute the JS function 'func' giving params as parameters. There can be up to 3 string parameters. The function must exist in the script loaded at startup via parameter 'load'. Parameters can be strings with pseudo-variables that are evaluated at runtime.

#### `jsdt_run` usage

    ...
    if(!jsdt_run("js_append_fu_to_reply"))
    {
        xdbg("SCRIPT: failed to execute js function!\n");
    }
    ...
    jsdt_run("js_funcx", "$rU", "2");
    ...

### `jsdt_runstring(script)`

Execute the JS script stored in parameter. The parameter can be a string with pseudo-variables. The script is executed in JS context specific to loaded JS files at startup.

#### `jsdt_runstring` usage

    ...
    if(!jsdt_runstring('KSR.dbg("Hello World from $fU\n")'))
    {
        xdbg("failed to execute js script!\n");
    }
    ...

## RPC Commands

### `app_jsdt.reload`

Marks the need to reload the js script. The actual reload is done by every working process when the next call to jsdt_run() function or KEMI config is executed.

Name: app_jsdt.reload

Parameters: none

Example:

    ...
    &kamctl; rpc app_jsdt.reload
    ...

### `app_jsdt.api_list`

List the functions available via Kemi framework.

Name: app_jsdt.api_list

Parameters: none

Example:

    ...
    &kamctl; rpc app_jsdt.api_list
    ...

## Example of usage

Create your JS script and store it on the file system, say: '/usr/local/etc/kamailio/js/myscript.js'.

    ...
    function sr_append_fu_to_reply()
    {
    	KSR.hdr.append_to_reply("P-From: " + KSR.pv.get("$fu") + "\r\n");
    }
    ...

Load the script via parameter 'load' and execute function via jsdt_run(...).

    ...
    modparam("app_jsdt", "load", "/usr/local/etc/kamailio/js/myscript.js")
    ...
    request_route {
        ...
        if(!jsdt_run("sr_append_fu_to_reply"))
        {
            xdbg("SCRIPT: failed to execute js function!\n");
        }
        ...
    }
    ...