Browse code

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

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