Browse code

pv: pv_var_to_xavp

Victor Seva authored on 29/08/2015 07:07:27
Showing 6 changed files
... ...
@@ -391,6 +391,37 @@ sbranch_reset();
391 391
 				<programlisting format="linespecific">
392 392
 ...
393 393
 pv_xavp_print();
394
+...
395
+				</programlisting>
396
+			</example>
397
+		</section>
398
+		<section id="pv.f.pv_var_to_xavp">
399
+			<title>
400
+				<function moreinfo="none">pv_var_to_xavp(varname, xname)</function>
401
+			</title>
402
+			<para>
403
+				Copy script variables values to a xavp.
404
+			</para>
405
+			<para>
406
+				First parameter can be '*' in order to copy all script variables. Second parameter is the name of the destination xavp. If xavp already exists it will be reset first.
407
+			</para>
408
+			<para>
409
+			Function can be used from ANY_ROUTE.
410
+			</para>
411
+			<example>
412
+				<title><function>pv_var_to_xavp()</function> usage</title>
413
+				<programlisting format="linespecific">
414
+...
415
+$var("temp") = 3;
416
+$var("foo") = "foo indeed";
417
+pv_var_to_xavp("temp", "ok");
418
+...
419
+$xavp("ok[0]=>temp") now is 3
420
+...
421
+pv_var_to_xavp("*", "ok");
422
+...
423
+$xavp("ok[0]=>temp") now is 3
424
+$xavp("ok[0]=>foo") now is "foo indeed"
394 425
 ...
395 426
 				</programlisting>
396 427
 			</example>
... ...
@@ -494,6 +494,7 @@ static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
494 494
 static int w_sbranch_set_ruri(sip_msg_t *msg, char p1, char *p2);
495 495
 static int w_sbranch_append(sip_msg_t *msg, char p1, char *p2);
496 496
 static int w_sbranch_reset(sip_msg_t *msg, char p1, char *p2);
497
+static int w_var_to_xavp(sip_msg_t *msg, char *p1, char *p2);
497 498
 
498 499
 static int pv_init_rpc(void);
499 500
 
... ...
@@ -505,6 +506,8 @@ static cmd_export_t cmds[]={
505 506
 #ifdef WITH_XAVP
506 507
 	{"pv_xavp_print",  (cmd_function)pv_xavp_print,  0, 0, 0, 
507 508
 		ANY_ROUTE },
509
+	{"pv_var_to_xavp",  (cmd_function)w_var_to_xavp, 2, 0, 0,
510
+		ANY_ROUTE },
508 511
 #endif
509 512
 	{"is_int", (cmd_function)is_int, 1, fixup_pvar_null, fixup_free_pvar_null,
510 513
 		ANY_ROUTE},
... ...
@@ -693,6 +696,20 @@ static int is_int(struct sip_msg* msg, char* pvar, char* s2)
693 696
 	return -1;
694 697
 }
695 698
 
699
+static int w_var_to_xavp(sip_msg_t *msg, char *s1, char *s2)
700
+{
701
+	str xname, varname;
702
+
703
+	if(s1 == NULL || s2 == NULL) {
704
+		LM_ERR("wrong parameters\n");
705
+		return -1;
706
+	}
707
+
708
+	varname.len = strlen(s1); varname.s = s1;
709
+	xname.s = s2; xname.len = strlen(s2);
710
+	return pv_var_to_xavp(&varname, &xname);
711
+}
712
+
696 713
 /**
697 714
  *
698 715
  */
... ...
@@ -186,6 +186,10 @@ script_var_t* get_varnull_by_name(str *name)
186 186
 	return 0;
187 187
 }
188 188
 
189
+script_var_t* get_var_all(void) {
190
+	return script_vars;
191
+}
192
+
189 193
 void reset_vars(void)
190 194
 {
191 195
 	script_var_t *it;
... ...
@@ -51,6 +51,7 @@ script_var_t* add_var(str *name, int vtype);
51 51
 script_var_t* set_var_value(script_var_t *var, int_str *value, int flags);
52 52
 script_var_t* get_var_by_name(str *name);
53 53
 script_var_t* get_varnull_by_name(str *name);
54
+script_var_t* get_var_all(void);
54 55
 
55 56
 void reset_vars(void);
56 57
 void destroy_vars(void);
... ...
@@ -613,4 +613,65 @@ int xavp_params_explode(str *params, str *xname)
613 613
 
614 614
 	return 0;
615 615
 }
616
+
617
+int pv_var_to_xavp(str *varname, str *xname)
618
+{
619
+	script_var_t *it;
620
+	sr_xavp_t *xavp = NULL;
621
+	sr_xval_t xval;
622
+
623
+	LM_DBG("xname:%.*s varname:%.*s\n", xname->len, xname->s,
624
+		varname->len, varname->s);
625
+
626
+	// clean xavp
627
+	xavp_rm_by_name(xname, 1, NULL);
628
+
629
+	if(varname->len==1 && varname->s[0] == '*') {
630
+		for(it=get_var_all(); it; it=it->next) {
631
+			memset(&xval, 0, sizeof(sr_xval_t));
632
+			if(it->v.flags&VAR_VAL_INT)
633
+			{
634
+				xval.type = SR_XTYPE_INT;
635
+				xval.v.i = it->v.value.n;
636
+			} else {
637
+				if(it->v.value.s.len==0) continue;
638
+				xval.type = SR_XTYPE_STR;
639
+				xval.v.s.s = it->v.value.s.s;
640
+				xval.v.s.len = it->v.value.s.len;
641
+			}
642
+			xavp = xavp_add_xavp_value(xname, &it->name, &xval, NULL);
643
+			if(xavp==NULL) {
644
+				LM_ERR("can't copy [%.*s]\n", it->name.len, it->name.s);
645
+				goto error;
646
+			}
647
+		}
648
+	}
649
+	else {
650
+		it = get_var_by_name(varname);
651
+		if(it==NULL) {
652
+			LM_ERR("script var [%.*s] not found\n", varname->len, varname->s);
653
+			return -1;
654
+		}
655
+		memset(&xval, 0, sizeof(sr_xval_t));
656
+		if(it->v.flags&VAR_VAL_INT)
657
+		{
658
+			xval.type = SR_XTYPE_INT;
659
+			xval.v.i = it->v.value.n;
660
+		} else {
661
+			xval.type = SR_XTYPE_STR;
662
+			xval.v.s.s = it->v.value.s.s;
663
+			xval.v.s.len = it->v.value.s.len;
664
+		}
665
+		xavp = xavp_add_xavp_value(xname, &it->name, &xval, NULL);
666
+		if(xavp==NULL) {
667
+			LM_ERR("can't copy [%.*s]\n", it->name.len, it->name.s);
668
+			goto error;
669
+		}
670
+	}
671
+	return 1;
672
+
673
+error:
674
+	xavp_rm_by_name(xname, 1, NULL);
675
+	return -1;
676
+}
616 677
 #endif
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com) 
2
+ * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
3 3
  *
4 4
  * Permission to use, copy, modify, and distribute this software for any
5 5
  * purpose with or without fee is hereby granted, provided that the above
... ...
@@ -20,6 +20,7 @@
20 20
 #ifdef WITH_XAVP
21 21
 
22 22
 #include "../../pvar.h"
23
+#include "pv_svar.h"
23 24
 
24 25
 int pv_get_xavp(struct sip_msg *msg, pv_param_t *param,
25 26
 		pv_value_t *res);
... ...
@@ -31,5 +32,7 @@ int pv_xavp_print(struct sip_msg* msg, char* s1, char *s2);
31 32
 
32 33
 int xavp_params_explode(str *params, str *xname);
33 34
 
35
+int pv_var_to_xavp(str *varname, str *xname);
36
+
34 37
 #endif
35 38
 #endif