Browse code

@ruri.rn_user is introduced.

- The select call returns the value of "rn" parameter if exists, otherwise the user name of the URI. Visual separators are removed from
the user name if needed, and they are always removed from the "rn" parameter value.
- Nested function call, it works with any URI.

Miklos Tirpak authored on 13/05/2009 14:59:39
Showing 2 changed files
... ...
@@ -717,6 +717,74 @@ int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
717 717
 	RETURN0_res(uri.user);
718 718
 }
719 719
 
720
+/* search for a parameter with "name"
721
+ * Return value:
722
+ *	0: not found
723
+ *	1: found
724
+ *	-1: error
725
+ *
726
+ * val is set to the value of the parameter.
727
+ */
728
+static inline int search_param(str *params, char *name, int name_len,
729
+				str *val)
730
+{
731
+	param_hooks_t h;
732
+	param_t *p, *list;
733
+
734
+	if (params->s == NULL)
735
+		return 0;
736
+
737
+	if (parse_params(params, CLASS_ANY, &h, &list) < 0)
738
+		return -1;
739
+	for (p = list; p; p=p->next) {
740
+		if ((p->name.len == name_len)
741
+			&& (strncasecmp(p->name.s, name, name_len) == 0)
742
+		) {
743
+			*val=p->body;
744
+			free_params(list);
745
+			return 1;
746
+		}
747
+	}
748
+	free_params(list);
749
+	return 0;
750
+}
751
+
752
+/* Return the value of the "rn" parameter if exists, otherwise the user name.
753
+ * The user name is normalized if needed, i.e. visual separators are removed,
754
+ * the "rn" param is always normalized. */
755
+int select_uri_rn_user(str* res, select_t* s, struct sip_msg* msg)
756
+{
757
+	int	ret;
758
+	str	val;
759
+
760
+	if (parse_uri(res->s, res->len, &uri)<0)
761
+		return -1;
762
+
763
+	/* search for the "rn" parameter */
764
+	if ((ret = search_param(&uri.params, "rn", 2, &val)) != 0)
765
+		goto done;
766
+
767
+	if (uri.sip_params.s != uri.params.s) {
768
+		/* check also the original sip: URI parameters */
769
+		if ((ret = search_param(&uri.sip_params, "rn", 2, &val)) != 0)
770
+			goto done;
771
+	}
772
+
773
+	if ((uri.flags & URI_USER_NORMALIZE) == 0)
774
+		RETURN0_res(uri.user);
775
+	/* else normalize the user name */
776
+	val = uri.user;
777
+done:
778
+	if (ret < 0)
779
+		return -1; /* error */
780
+
781
+	if (!(res->s=get_static_buffer(val.len)))
782
+		return -1;
783
+	if ((res->len=normalize_tel_user(res->s, &val))==0)
784
+		return 1;
785
+	return 0;
786
+}
787
+
720 788
 int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
721 789
 {
722 790
 	if (parse_uri(res->s, res->len, &uri)<0)
... ...
@@ -832,8 +900,6 @@ int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
832 900
 
833 901
 int select_any_params(str* res, select_t* s, struct sip_msg* msg)
834 902
 {
835
-	param_hooks_t h;
836
-	param_t *p, *list=NULL;
837 903
 	str* wanted;
838 904
 	int i;
839 905
 
... ...
@@ -851,20 +917,13 @@ int select_any_params(str* res, select_t* s, struct sip_msg* msg)
851 917
 	wanted=&s->params[s->param_offset[select_level]+1].v.s;
852 918
 	
853 919
 	if (!res->len) return -1;
854
-	if (parse_params(res, CLASS_ANY, &h, &list)<0) return -1;
855
-	
856
-	for (p = list; p; p=p->next) {
857
-		if ((p->name.len==wanted->len) && 
858
-			 !strncasecmp(p->name.s, wanted->s,wanted->len)) {
859
-			*res=p->body;
860
-			free_params(list);
861
-			return (res->len ? 0 : 1);
862
-		}
863
-	}
864
-	free_params(list);
865 920
 
866
-	DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
867
-	return -1;
921
+	if (search_param(res, wanted->s, wanted->len, res) <= 0) {
922
+		DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
923
+		return -1;
924
+	} else {
925
+		return (res->len) ? 0 : 1;
926
+	}
868 927
 }
869 928
 
870 929
 int select_event(str* res, select_t* s, struct sip_msg* msg)
... ...
@@ -153,6 +153,7 @@ SELECT_F(select_any_params)
153 153
 SELECT_F(select_any_uri)
154 154
 SELECT_F(select_uri_type)
155 155
 SELECT_F(select_uri_user)
156
+SELECT_F(select_uri_rn_user)
156 157
 SELECT_F(select_uri_pwd)
157 158
 SELECT_F(select_uri_host)
158 159
 SELECT_F(select_uri_port)
... ...
@@ -278,6 +279,7 @@ static select_row_t select_core[] = {
278 279
 	{ select_rr_uri, SEL_PARAM_INT, STR_NULL, select_any_uri, NESTED},
279 280
 	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("type"), select_uri_type, 0},
280 281
 	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("user"), select_uri_user, 0},
282
+	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("rn_user"), select_uri_rn_user, 0},
281 283
 	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("pwd"), select_uri_pwd, 0},
282 284
 	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("host"), select_uri_host, 0},
283 285
 	{ select_any_uri, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_uri_port, 0},