Resources.DocsCoreOps HistoryHide minor edits - Show changes to markup April 24, 2013, at 10:24 PM
by
- Changed lines 1-201 from:
Resources -> Documentation -> CookBooks -> Script statements(: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 'expr':
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"); } 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(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. whilewhile statement Example of usage: $var(i) = 0; while($var(i) < 10) { xlog("counter: $var(i)\n"); $var(i) = $var(i) + 1; } Script OperationsAssignments plus string and arithmetic operations can be done directly in the configuration file. AssignmentAssignments can be done like in C, via '=' (equal). The following pseudo-variables can be used in left side of an assignment:
$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 operationsFor strings, '+' is available to concatenate. $var(a) = "test"; $var(b) = "sip:" + $var(a) + "@" + $fd; Arithmetic and bitwise operationsFor numbers, one can use:
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"); to:
(:redirect Documentation/Script-Statements quiet=1:) February 01, 2011, at 03:56 AM
by
- Changed line 27 from:
to:
October 08, 2008, at 09:45 AM
by
- Changed lines 1-2 from:
Resources -> Documentation -> Script statementsto:
Resources -> Documentation -> CookBooks -> Script statementsOctober 03, 2008, at 01:55 PM
by
- Deleted lines 2-3:
Script statementsOctober 03, 2008, at 01:53 PM
by
- Changed lines 130-131 from:
Script Operationsto:
Script OperationsOctober 03, 2008, at 01:52 PM
by
- Added lines 3-4:
(:toc-float Table of Content:) July 19, 2008, at 09:12 PM
by
- Added lines 1-199:
Script statementsStatements 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 'expr':
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"); } 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(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. whilewhile statement Example of usage: $var(i) = 0; while($var(i) < 10) { xlog("counter: $var(i)\n"); $var(i) = $var(i) + 1; } Script OperationsAssignments plus string and arithmetic operations can be done directly in the configuration file. AssignmentAssignments can be done like in C, via '=' (equal). The following pseudo-variables can be used in left side of an assignment:
$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 operationsFor strings, '+' is available to concatenate. $var(a) = "test"; $var(b) = "sip:" + $var(a) + "@" + $fd; Arithmetic and bitwise operationsFor numbers, one can use:
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"); |