Browse code

misc/examples/kemi/kamailio-basic-kemi-sqlang.js: use functions to match methods and myself

Daniel-Constantin Mierla authored on 06/12/2018 09:54:14
Showing 1 changed files
... ...
@@ -20,7 +20,6 @@ local FLAGS = {
20 20
 // equivalent of request_route{}
21 21
 function ksr_request_route()
22 22
 {
23
-	local METHOD = KSR.pv.get("$rm");
24 23
 	// KSR.sl.sl_send_reply(100,"Intelligent trying");
25 24
 	// KSR.info("===== request - from kamailio SQLang script\n");
26 25
 
... ...
@@ -31,7 +30,7 @@ function ksr_request_route()
31 31
 	ksr_route_natdetect();
32 32
 
33 33
 	// CANCEL processing
34
-	if (METHOD == "CANCEL") {
34
+	if (KSR.is_CANCEL()) {
35 35
 		if(KSR.tm.t_check_trans()>0) {
36 36
 			ksr_route_relay();
37 37
 		}
... ...
@@ -44,11 +43,13 @@ function ksr_request_route()
44 44
 	// -- only initial requests (no To tag)
45 45
 
46 46
 	// handle retransmissions
47
-	if (KSR.tmx.t_precheck_trans()>0) {
48
-		KSR.tm.t_check_trans();
49
-		return;
47
+	if (!KSR.is_ACK()) {
48
+		if (KSR.tmx.t_precheck_trans()>0) {
49
+			KSR.tm.t_check_trans();
50
+			return;
51
+		}
52
+		if (KSR.tm.t_check_trans()==0) { return; }
50 53
 	}
51
-	if (KSR.tm.t_check_trans()==0) { return; }
52 54
 
53 55
 	// authentication
54 56
 	ksr_route_auth();
... ...
@@ -56,12 +57,12 @@ function ksr_request_route()
56 56
 	// record routing for dialog forming requests (in case they are routed)
57 57
 	// - remove preloaded route headers
58 58
 	KSR.hdr.remove("Route");
59
-	if (METHOD=="INVITE" || METHOD=="SUBSCRIBE") {
59
+	if (KSR.is_method_in("IS")) {
60 60
 		KSR.rr.record_route();
61 61
 	}
62 62
 
63 63
 	// account only INVITEs
64
-	if (METHOD=="INVITE") {
64
+	if (KSR.is_INVITE()) {
65 65
 		KSR.setflag(FLAGS.FLT_ACC); // do accounting
66 66
 	}
67 67
 
... ...
@@ -73,7 +74,7 @@ function ksr_request_route()
73 73
 	// handle registrations
74 74
 	ksr_route_registrar();
75 75
 
76
-	if (KSR.pv.is_null("$rU")) {
76
+	if (KSR.corex.has_ruri_user() < 0) {
77 77
 		// request with no Username in RURI
78 78
 		KSR.sl.sl_send_reply(484, "Address Incomplete");
79 79
 		return;
... ...
@@ -90,20 +91,18 @@ function ksr_route_relay()
90 90
 {
91 91
 	// enable additional event routes for forwarded requests
92 92
 	// - serial forking, RTP relaying handling, a.s.o.
93
-	local METHOD = KSR.pv.get("$rm");
94
-	if (METHOD=="INVITE" || METHOD=="BYE" || METHOD=="SUBSCRIBE"
95
-			|| METHOD=="UPDATE") {
93
+	if (KSR.is_method_in("IBSU")) {
96 94
 		if (KSR.tm.t_is_set("branch_route")<0) {
97 95
 			KSR.tm.t_on_branch("ksr_branch_manage");
98 96
 		}
99 97
 	}
100
-	if (METHOD=="INVITE" || METHOD=="SUBSCRIBE" || METHOD=="UPDATE") {
98
+	if (KSR.is_method_in("ISU")) {
101 99
 		if (KSR.tm.t_is_set("onreply_route")<0) {
102 100
 			KSR.tm.t_on_reply("ksr_onreply_manage");
103 101
 		}
104 102
 	}
105 103
 
106
-	if (METHOD=="INVITE") {
104
+	if (KSR.is_INVITE()) {
107 105
 		if (KSR.tm.t_is_set("failure_route")<0) {
108 106
 			KSR.tm.t_on_failure("ksr_failure_manage");
109 107
 		}
... ...
@@ -119,7 +118,7 @@ function ksr_route_relay()
119 119
 // Per SIP request initial checks
120 120
 function ksr_route_reqinit()
121 121
 {
122
-	if (!KSR.is_myself(KSR.pv.get("$si"))) {
122
+	if (!KSR.is_myself_srcip()) {
123 123
 		if (!KSR.pv.is_null("$sht(ipban=>$si)")) {
124 124
 			// ip is already blocked
125 125
 			KSR.dbg("request from blocked IP - " + KSR.pv.get("$rm")
... ...
@@ -135,7 +134,7 @@ function ksr_route_reqinit()
135 135
 			KSR.x.exit();
136 136
 		}
137 137
 	}
138
-	if (!KSR.pv.is_null("$ua")) {
138
+	if (KSR.corex.has_user_agent()>0) {
139 139
 		local UA = KSR.pv.get("$ua");
140 140
 		// if (sipscanregex.match(UA)) {
141 141
 		if (UA.find("friendly-scanner")!=null || UA.find("sipcli")!=null) {
... ...
@@ -149,9 +148,9 @@ function ksr_route_reqinit()
149 149
 		KSR.x.exit();
150 150
 	}
151 151
 
152
-	if (KSR.pv.get("$rm")=="OPTIONS"
153
-			&& KSR.is_myself(KSR.pv.get("$ru"))
154
-			&& KSR.pv.is_null("$rU")) {
152
+	if (KSR.is_OPTIONS()
153
+			&& KSR.is_myself_ruri()
154
+			&& KSR.corex.has_ruri_user() < 0) {
155 155
 		KSR.sl.sl_send_reply(200,"Keepalive");
156 156
 		KSR.x.exit();
157 157
 	}
... ...
@@ -169,25 +168,24 @@ function ksr_route_withindlg()
169 169
 {
170 170
 	if (KSR.siputils.has_totag()<0) { return; }
171 171
 
172
-	local METHOD = KSR.pv.get("$rm");
173 172
 	// sequential request withing a dialog should
174 173
 	// take the path determined by record-routing
175 174
 	if (KSR.rr.loose_route()>0) {
176 175
 		ksr_route_dlguri();
177
-		if (METHOD=="BYE") {
176
+		if (KSR.is_BYE()) {
178 177
 			KSR.setflag(FLAGS.FLT_ACC); // do accounting ...
179 178
 			KSR.setflag(FLAGS.FLT_ACCFAILED); // ... even if the transaction fails
180
-		} else if (METHOD=="ACK") {
179
+		} else if (KSR.is_ACK()) {
181 180
 			// ACK is forwarded statelessly
182 181
 			ksr_route_natmanage();
183
-		} else if (METHOD=="NOTIFY") {
182
+		} else if (KSR.is_NOTIFY()) {
184 183
 			// Add Record-Route for in-dialog NOTIFY as per RFC 6665.
185 184
 			KSR.rr.record_route();
186 185
 		}
187 186
 		ksr_route_relay();
188 187
 		KSR.x.exit();
189 188
 	}
190
-	if (METHOD=="ACK") {
189
+	if (KSR.is_ACK()) {
191 190
 		if (KSR.tm.t_check_trans() >0) {
192 191
 			// no loose-route, but stateful ACK;
193 192
 			// must be an ACK after a 487
... ...
@@ -206,7 +204,7 @@ function ksr_route_withindlg()
206 206
 // Handle SIP registrations
207 207
 function ksr_route_registrar()
208 208
 {
209
-	if (KSR.pv.get("$rm")!="REGISTER") { return; }
209
+	if (!KSR.is_REGISTER()) { return; }
210 210
 	if (KSR.isflagset(FLAGS.FLT_NATS)) {
211 211
 		KSR.setbflag(FLAGS.FLB_NATB);
212 212
 		// do SIP NAT pinging
... ...
@@ -234,7 +232,7 @@ function ksr_route_location()
234 234
 	}
235 235
 
236 236
 	// when routing via usrloc, log the missed calls also
237
-	if (KSR.pv.get("$rm")=="INVITE") {
237
+	if (KSR.is_INVITE()) {
238 238
 		KSR.setflag(FLAGS.FLT_ACCMISSED);
239 239
 	}
240 240
 
... ...
@@ -246,30 +244,29 @@ function ksr_route_location()
246 246
 // IP authorization and user uthentication
247 247
 function ksr_route_auth()
248 248
 {
249
-	local METHOD = KSR.pv.get("$rm");
250
-	if (METHOD!="REGISTER") {
249
+	if (!KSR.is_REGISTER()) {
251 250
 		if (KSR.permissions.allow_source_address(1)>0) {
252 251
 			// source IP allowed
253 252
 			return;
254 253
 		}
255 254
 	}
256 255
 
257
-	if (METHOD=="REGISTER" || KSR.is_myself(KSR.pv.get("$fu"))) {
256
+	if (KSR.is_REGISTER() || KSR.is_myself_furi()) {
258 257
 		// authenticate requests
259 258
 		if (KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0) {
260 259
 			KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0);
261 260
 			KSR.x.exit();
262 261
 		}
263 262
 		// user authenticated - remove auth header
264
-		if (METHOD!="REGISTER" && METHOD!="PUBLISH") {
263
+		if (!KSR.is_method_in("RP")) {
265 264
 			KSR.auth.consume_credentials();
266 265
 		}
267 266
 	}
268 267
 
269 268
 	// if caller is not local subscriber, then check if it calls
270 269
 	// a local destination, otherwise deny, not an open relay here
271
-	if ((! KSR.is_myself(KSR.pv.get("$fu"))
272
-			&& (! KSR.is_myself(KSR.pv.get("$ru"))))) {
270
+	if ((!KSR.is_myself_furi())
271
+			&& (!KSR.is_myself_ruri())) {
273 272
 		KSR.sl.sl_send_reply(403,"Not relaying");
274 273
 		KSR.x.exit();
275 274
 	}
... ...
@@ -282,7 +279,7 @@ function ksr_route_natdetect()
282 282
 {
283 283
 	KSR.force_rport();
284 284
 	if (KSR.nathelper.nat_uac_test(19)>0) {
285
-		if (KSR.pv.get("$rm")=="REGISTER") {
285
+		if (KSR.is_REGISTER()) {
286 286
 			KSR.nathelper.fix_nated_register();
287 287
 		} else if (KSR.siputils.is_first_hop()>0) {
288 288
 			KSR.nathelper.set_contact_alias();
... ...
@@ -335,7 +332,7 @@ function ksr_route_dlguri()
335 335
 // Routing to foreign domains
336 336
 function ksr_route_sipout()
337 337
 {
338
-	if (KSR.is_myself(KSR.pv.get("$ru"))) { return; }
338
+	if (KSR.is_myself_ruri()) { return; }
339 339
 
340 340
 	KSR.hdr.append_hf("P-Hint: outbound\r\n");
341 341
 	ksr_route_relay();