| Documentation | Documentation.Script-Statements-3-2 HistoryHide minor edits - Show changes to markup December 17, 2021, at 07:52 PM 
        by  -  Changed lines 17-18 from: ifto: if 🔗Changed lines 60-61 from: switchto: switch 🔗Changed lines 125-126 from: whileto: while 🔗Changed lines 144-145 from: for eachto: Changed line 179 from: @] to: @] July 27, 2019, at 01:56 AM 
        by  -  Changed lines 70-73 from:         switch($retcode)
        {
            case -1:
                log("process INVITE requests here\n");
to:         switch ($retcode) {
        case -1:
            log("process INVITE requests here\n");
Changed lines 74-75 from:             case 1:
                log("process REGISTER requests here\n");
to:         case 1:
            log("process REGISTER requests here\n");
Changed lines 77-79 from:             case 2:
            case 3:
                log("process SUBSCRIBE and NOTIFY requests here\n");
to:         case 2:
        case 3:
            log("process SUBSCRIBE and NOTIFY requests here\n");
Changed lines 81-82 from:             default:
                log("process other requests here\n");
to:         default:
            log("process other requests here\n");
Changed lines 86-89 from:         switch($rU)
        {
            case "101":
                log("destination number is 101\n");
to:         switch ($rU) {
        case "101":
            log("destination number is 101\n");
Changed lines 90-91 from:             case "102":
                log("destination number is 102\n");
to:         case "102":
            log("destination number is 102\n"); # continue with 103 and 104
        case "103":
        case "104":
            log("destination number is 103 or 104\n");
Changed lines 96-101 from:             case "103":
            case "104":
                log("destination number is 103 or 104\n");
            break;
            default:
                log("unknown destination number\n");
to:         default:
            log("unknown destination number\n");
Changed lines 101-103 from:     route[my_logic]{
        if(is_method("INVITE"))
        {
to:     route [my_logic] {
        if (is_method("INVITE"))
Changed lines 104-105 from:         };
        if(is_method("REGISTER"))
to:         if (is_method("REGISTER"))
Changed lines 107-108 from:         }
        if(is_method("SUBSCRIBE"))
to:         if (is_method("SUBSCRIBE"))
Changed lines 110-111 from:         }
        if(is_method("NOTIFY"))
to:         if (is_method("NOTIFY"))
Changed line 113 from: } to: Changed lines 130-132 from:     while($var(i) < 10)
    {
        xlog("counter: $var(i)\n");
to:     $var(cli) = NULL;
    while ($var(i) < 10) {
        if ($(avp(valid_clis[$var(i)]) == $fU) {
            xlog("matched the From user!\n");
            $var(cli) = $fU;
            break;
        }
Changed line 166 from: for ($json(contact) in $(avp(res)[*])) to:     for ($json(contact) in $(avp(res)[*])) {
Added lines 168-173:         if ($json(contact/phone) =~ "^40") {
            xlog("found a cheap destination to dial\n");
            break;
        }
    }
April 24, 2019, at 06:00 PM 
        by  -  Changed line 143 from: for each statement - easy iteration over indexed pseudo variables to: for each statement - easy iteration over indexed variables or pseudo-variables July 16, 2015, at 03:18 PM 
        by  -  Changed lines 166-167 from:     for ($json(it) in $(avp(res)[*]))
        xlog("Found: $json(it/field1) $json(it/field2)\n");
to:     for ($json(contact) in $(avp(res)[*]))
        xlog("Found: $json(contact/phone) $json(contact/email)\n");
July 16, 2015, at 03:16 PM 
        by  -  Added lines 163-167:     # iterate through all JSON documents returned by a MongoDB query
    cache_raw_query("mongodb:location", "{... find ...}", "$avp(res)");
    for ($json(it) in $(avp(res)[*]))
        xlog("Found: $json(it/field1) $json(it/field2)\n");
March 20, 2014, at 08:29 PM 
        by  -  Added lines 1-164: Documentation -> Manuals -> Manual 3.2 -> Script Statements(:title Script Statements - 3.2:) (:allVersions Script-Statements 3.2:) 
 
 
 (:toc-float Table of Content:) Statements you can use in the OpenSIPS config file while building the routing logic. ifIF-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: 
 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");
    }
switchSWITCH 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. whilewhile statement Example of usage: 
    $var(i) = 0;
    while($var(i) < 10)
    {
        xlog("counter: $var(i)\n");
        $var(i) = $var(i) + 1;
    }
for eachfor each statement - easy iteration over indexed pseudo variables Example of usage: 
    $avp(arr) = 0;
    $avp(arr) = 1;
    $avp(arr) = 2;
    $avp(arr) = 3;
    $avp(arr) = 4;
    for ($var(it) in $(avp(arr)[*]))
        xlog("array value: $var(it)\n");
    # iterate through all Contact URIs from each Contact header
    for ($var(ct) in $(ct[*]))
        xlog("Contact: $var(ct)\n");
    # iterate through all Via headers of a SIP request
    for ($var(via) in $(hdr(Via)[*]))
        xlog("Found \"Via\" header: $var(via)\n");
 | 
 
  