Documentation

Documentation.Script-Statements History

Hide minor edits - Show changes to markup

July 21, 2015, at 11:43 AM by liviu -
Changed lines 1-139 from:
Documentation -> Manuals -> Manual devel -> Script Statements

(:title Script Statements - ver devel :)


Page for other versions: devel 1.9 1.8 old versions: 1.7 1.6 1.5 1.4


Script Statements devel
PrevNext

(:toc-float Table of Content:)

Statements you can use in the OpenSIPS config file while building the routing logic.

if

IF-ELSE statement

Prototype:

    if (expr) {
       actions;
    } else {
       actions;
    }

The 'expr' should be a valid logical expression.

The logical operators that can be used in the logical expressions:

  • == - equal
  • != - not equal
  • =~ - regular expression matching (e.g. $rU =~ '^1800*' is "$rU begins with 1800" )
  • !~ - regular expression not-matching
  • > - greater
  • >= - greater or equal
  • < - less
  • <= - less or equal
  • && - logical AND
  • || - logical OR
  • ! - logical NOT
  • [ ... ] - test operator - inside can be any arithmetic expression

Example of usage:

    if ( is_method("INVITE") && $rp==5060 )
    {
        log("this sip message is an invite\n");
    } else {
        log("this sip message is not an invite\n");
    }

switch

SWITCH statement - it can be used to test the value of a pseudo-variable.

IMPORTANT NOTE: 'break' can be used only to mark the end of a 'case' branch (as it is in shell scripts). If you are trying to use 'break' outside a 'case' block the script will return error -- you must use 'return' there.

Example of usage:

    route {
        route(my_logic);
        switch($retcode)
        {
            case -1:
                log("process INVITE requests here\n");
            break;
            case 1:
                log("process REGISTER requests here\n");
            break;
            case 2:
            case 3:
                log("process SUBSCRIBE and NOTIFY requests here\n");
            break;
            default:
                log("process other requests here\n");
       }

        # switch of R-URI username
        switch($rU)
        {
            case "101":
                log("destination number is 101\n");
            break;
            case "102":
                log("destination number is 102\n");
            break;
            case "103":
            case "104":
                log("destination number is 103 or 104\n");
            break;
            default:
                log("unknown destination number\n");
       }
    }

    route[my_logic]{
        if(is_method("INVITE"))
        {
            return(-1);
        };
        if(is_method("REGISTER"))
            return(1);
        }
        if(is_method("SUBSCRIBE"))
            return(2);
        }
        if(is_method("NOTIFY"))
            return(3);
        }
        return(-2);
    }

Take care while using 'return' - 'return(0)' stops the execution of the script.

while

while statement

Example of usage:

    $var(i) = 0;
    while($var(i) < 10)
    {
        xlog("counter: $var(i)\n");
        $var(i) = $var(i) + 1;
    }
to:

(:redirect Documentation.Script-Statements-2-2 quiet=1:)

May 29, 2013, at 12:58 AM by 92.80.35.155 -
Changed lines 122-124 from:

NOTE: take care while using 'return' - 'return(0)' stops the execution of the script.

to:

Take care while using 'return' - 'return(0)' stops the execution of the script.

May 29, 2013, at 12:57 AM by 92.80.35.155 -
Changed lines 1-10 from:
Documentation -> Manuals -> Manual 1.9 -> Script Statements
to:
Documentation -> Manuals -> Manual devel -> Script Statements

(:title Script Statements - ver devel :)


Page for other versions: devel 1.9 1.8 old versions: 1.7 1.6 1.5 1.4


Script Statements devel
PrevNext
May 27, 2013, at 08:18 PM by 109.99.235.212 -
Changed line 1 from:
to:
Documentation -> Manuals -> Manual 1.9 -> Script Statements
Changed line 15 from:
    if(expr) {
to:
    if (expr) {
Changed lines 24-25 from:

The logical operators that can be used in 'expr':

to:

The logical operators that can be used in the logical expressions:

Changed line 42 from:
    if(is_method("INVITE"))
to:
    if ( is_method("INVITE") && $rp==5060 )
Changed line 60 from:
        route(1);
to:
        route(my_logic);
Changed line 95 from:
    route[1]{
to:
    route[my_logic]{
Changed line 121 from:
to:

[@

Deleted lines 127-153:

Script Operations

Assignments plus string and arithmetic operations can be done directly in the configuration file.

Assignment

Assignments can be done like in C, via '=' (equal). The following pseudo-variables can be used in left side of an assignment:

  • AVPs - to set the value of an AVP
  • script variables ($var(...)) - to set the value of a script variable
  • shared variables ($shv(...))
  • $ru - to set R-URI
  • $rd - to set domain part of R-URI
  • $rU - to set user part of R-URI
  • $rp - to set the port of R-URI
  • $du - to set dst URI
  • $fs - to set send socket
  • $br - to set branch
  • $mf - to set message flags value
  • $sf - to set script flags value
  • $bf - to set branch flags value

[@ $var(a) = 123;

Deleted lines 128-174:

There is a special assign operator ':=' (colon equal) that can be used with AVPs. If the right value is 'null', all AVPs with that name are deleted. If different, the new value will overwrite any existing values for the AVPs with than name (on other words, delete existing AVPs with same name, add a new one with the right side value).

$avp(i:3) := 123;

String operations

For strings, '+' is available to concatenate.

$var(a) = "test";
$var(b) = "sip:" + $var(a) + "@" + $fd;

Arithmetic and bitwise operations

For numbers, one can use:

  • + : plus
  • - : minus
  • / : divide
  • * : multiply
  • % : modulo
  • | : bitwise OR
  • & : bitwise AND
  • ^ : bitwise XOR
  • ~ : bitwise NOT
  • << : bitwise left shift
  • >> : bitwise right shift

Example:

$var(a) = 4 + ( 7 & ( ~2 ) );

NOTE: to ensure the priority of operands in expression evaluations do use __parenthesis__.

Arithmetic expressions can be used in condition expressions via test operator ' [ ... ] '.

if( [ $var(a) & 4 ] )
    log("var a has third bit set\n");
May 12, 2013, at 02:22 PM by 79.118.227.150 -
Changed lines 1-2 from:

Resources -> Documentation -> CookBooks -> Script statements

to:
April 24, 2013, at 10:23 PM by 92.80.24.181 -
Added lines 1-201:

Resources -> Documentation -> CookBooks -> Script statements

(:toc-float Table of Content:)

Statements you can use in the OpenSIPS config file while building the routing logic.

if

IF-ELSE statement

Prototype:

    if(expr) {
       actions;
    } else {
       actions;
    }

The 'expr' should be a valid logical expression.

The logical operators that can be used in 'expr':

  • == - equal
  • != - not equal
  • =~ - regular expression matching (e.g. $rU =~ '^1800*' is "$rU begins with 1800" )
  • !~ - regular expression not-matching
  • > - greater
  • >= - greater or equal
  • < - less
  • <= - less or equal
  • && - logical AND
  • || - logical OR
  • ! - logical NOT
  • [ ... ] - test operator - inside can be any arithmetic expression

Example of usage:

    if(is_method("INVITE"))
    {
        log("this sip message is an invite\n");
    } else {
        log("this sip message is not an invite\n");
    }

switch

SWITCH statement - it can be used to test the value of a pseudo-variable.

IMPORTANT NOTE: 'break' can be used only to mark the end of a 'case' branch (as it is in shell scripts). If you are trying to use 'break' outside a 'case' block the script will return error -- you must use 'return' there.

Example of usage:

    route {
        route(1);
        switch($retcode)
        {
            case -1:
                log("process INVITE requests here\n");
            break;
            case 1:
                log("process REGISTER requests here\n");
            break;
            case 2:
            case 3:
                log("process SUBSCRIBE and NOTIFY requests here\n");
            break;
            default:
                log("process other requests here\n");
       }

        # switch of R-URI username
        switch($rU)
        {
            case "101":
                log("destination number is 101\n");
            break;
            case "102":
                log("destination number is 102\n");
            break;
            case "103":
            case "104":
                log("destination number is 103 or 104\n");
            break;
            default:
                log("unknown destination number\n");
       }
    }

    route[1]{
        if(is_method("INVITE"))
        {
            return(-1);
        };
        if(is_method("REGISTER"))
            return(1);
        }
        if(is_method("SUBSCRIBE"))
            return(2);
        }
        if(is_method("NOTIFY"))
            return(3);
        }
        return(-2);
    }

NOTE: take care while using 'return' - 'return(0)' stops the execution of the script.

while

while statement

Example of usage:

    $var(i) = 0;
    while($var(i) < 10)
    {
        xlog("counter: $var(i)\n");
        $var(i) = $var(i) + 1;
    }

Script Operations

Assignments plus string and arithmetic operations can be done directly in the configuration file.

Assignment

Assignments can be done like in C, via '=' (equal). The following pseudo-variables can be used in left side of an assignment:

  • AVPs - to set the value of an AVP
  • script variables ($var(...)) - to set the value of a script variable
  • shared variables ($shv(...))
  • $ru - to set R-URI
  • $rd - to set domain part of R-URI
  • $rU - to set user part of R-URI
  • $rp - to set the port of R-URI
  • $du - to set dst URI
  • $fs - to set send socket
  • $br - to set branch
  • $mf - to set message flags value
  • $sf - to set script flags value
  • $bf - to set branch flags value
$var(a) = 123;

There is a special assign operator ':=' (colon equal) that can be used with AVPs. If the right value is 'null', all AVPs with that name are deleted. If different, the new value will overwrite any existing values for the AVPs with than name (on other words, delete existing AVPs with same name, add a new one with the right side value).

$avp(i:3) := 123;

String operations

For strings, '+' is available to concatenate.

$var(a) = "test";
$var(b) = "sip:" + $var(a) + "@" + $fd;

Arithmetic and bitwise operations

For numbers, one can use:

  • + : plus
  • - : minus
  • / : divide
  • * : multiply
  • % : modulo
  • | : bitwise OR
  • & : bitwise AND
  • ^ : bitwise XOR
  • ~ : bitwise NOT
  • << : bitwise left shift
  • >> : bitwise right shift

Example:

$var(a) = 4 + ( 7 & ( ~2 ) );

NOTE: to ensure the priority of operands in expression evaluations do use __parenthesis__.

Arithmetic expressions can be used in condition expressions via test operator ' [ ... ] '.

if( [ $var(a) & 4 ] )
    log("var a has third bit set\n");

Page last modified on July 21, 2015, at 11:43 AM