[sr-dev] git:jh/mtree: modules/utils: new script function in_list(subject, list, separator)
Juha Heinanen
jh at tutpro.com
Thu Sep 1 08:59:24 CEST 2011
Module: sip-router
Branch: jh/mtree
Commit: e2f8a96bf5221456113852970f210e37beb39fc8
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=e2f8a96bf5221456113852970f210e37beb39fc8
Author: Juha Heinanen <jh at tutpro.com>
Committer: Juha Heinanen <jh at tutpro.com>
Date: Thu Sep 1 09:58:35 2011 +0300
modules/utils: new script function in_list(subject, list, separator)
---
modules/utils/README | 40 +++++++++++++++++-----
modules/utils/doc/utils.xml | 2 +-
modules/utils/doc/utils_admin.xml | 23 +++++++++++++
modules/utils/functions.c | 65 ++++++++++++++++++++++++++++++++++++-
modules/utils/functions.h | 8 ++++-
modules/utils/utils.c | 38 ++++++++++++++++++++-
6 files changed, 162 insertions(+), 14 deletions(-)
diff --git a/modules/utils/README b/modules/utils/README
index aaf7d35..b09dd5b 100644
--- a/modules/utils/README
+++ b/modules/utils/README
@@ -4,7 +4,7 @@ Juha Heinanen
TutPro Inc.
- Copyright © 2008-2009 Juha Heinanen
+ Copyright © 2008-2011 Juha Heinanen
__________________________________________________________________
Table of Contents
@@ -29,6 +29,7 @@ Juha Heinanen
4.1. http_query(url, result)
4.2. xcap_auth_status(watcher_uri, presentity_uri)
4.3. is_int(pvar)
+ 4.4. in_list(subject, list, separator)
5. MI Commands
@@ -48,10 +49,11 @@ Juha Heinanen
1.5. http_query() usage
1.6. xcap_auth_status() usage
1.7. is_int() usage
- 1.8. forward_list usage
- 1.9. forward_switch usage
- 1.10. forward_filter usage
- 1.11. forward_proxy usage
+ 1.8. in_list() usage
+ 1.9. forward_list usage
+ 1.10. forward_switch usage
+ 1.11. forward_filter usage
+ 1.12. forward_proxy usage
Chapter 1. Admin Guide
@@ -75,6 +77,7 @@ Chapter 1. Admin Guide
4.1. http_query(url, result)
4.2. xcap_auth_status(watcher_uri, presentity_uri)
4.3. is_int(pvar)
+ 4.4. in_list(subject, list, separator)
5. MI Commands
@@ -177,6 +180,7 @@ modparam("utils", "xcap_table", "pres_xcap")
4.1. http_query(url, result)
4.2. xcap_auth_status(watcher_uri, presentity_uri)
4.3. is_int(pvar)
+ 4.4. in_list(subject, list, separator)
4.1. http_query(url, result)
@@ -243,6 +247,24 @@ if (is_int("$var(foo)")) {
}
...
+4.4. in_list(subject, list, separator)
+
+ Function checks if subject string is found in list string where list
+ items are separated by separator string. Subject and list strings may
+ contain pseudo variables. Separator string needs to be one character
+ long. Returns 1 if subject is found and -1 otherwise.
+
+ Function can be used from all kinds of routes.
+
+ Example 1.8. in_list() usage
+...
+$var(subject) = "fi";
+$var(list) = "dk,fi,no,se";
+if (in_list("$var(subject)", "$var(list)", ",") {
+ xlog("L_INFO", "subject is found in list\n");
+}
+...
+
5. MI Commands
5.1. forward_list
@@ -256,7 +278,7 @@ if (is_int("$var(foo)")) {
No parameters.
- Example 1.8. forward_list usage
+ Example 1.9. forward_list usage
...
kamctl fifo forward_list
id switch filter proxy
@@ -269,7 +291,7 @@ id switch filter proxy
The syntax of this configuration string is described in 1.6.
(switch_setting_list).
- Example 1.9. forward_switch usage
+ Example 1.10. forward_switch usage
...
kamctl fifo sp_forward_switch 0=on
...
@@ -288,7 +310,7 @@ kamctl fifo sp_forward_switch 0=on
The syntax of this configuration string is described in 1.6.
(filter_setting_list).
- Example 1.10. forward_filter usage
+ Example 1.11. forward_filter usage
...
kamctl fifo sp_forward_filter 0=REGISTER:INVITE
...
@@ -301,7 +323,7 @@ kamctl fifo sp_forward_filter 0=REGISTER:INVITE
switch). The syntax of this configuration string is described in 1.6.
(proxy_setting_list).
- Example 1.11. forward_proxy usage
+ Example 1.12. forward_proxy usage
...
kamctl fifo sp_forward_proxy 0=host-c.domain-c:5060
...
diff --git a/modules/utils/doc/utils.xml b/modules/utils/doc/utils.xml
index dfdb290..86cf93d 100644
--- a/modules/utils/doc/utils.xml
+++ b/modules/utils/doc/utils.xml
@@ -23,7 +23,7 @@
</author>
</authorgroup>
<copyright>
- <year>2008-2009</year>
+ <year>2008-2011</year>
<holder>Juha Heinanen</holder>
</copyright>
</bookinfo>
diff --git a/modules/utils/doc/utils_admin.xml b/modules/utils/doc/utils_admin.xml
index 23527d0..bc88cfc 100644
--- a/modules/utils/doc/utils_admin.xml
+++ b/modules/utils/doc/utils_admin.xml
@@ -251,6 +251,29 @@ if (is_int("$var(foo)")) {
</programlisting>
</example>
</section>
+ <section>
+ <title>
+ <function moreinfo="none">in_list(subject, list, separator)</function>
+ </title>
+ <para>
+ Function checks if subject string is found in list string where list items are separated by separator string. Subject and list strings may contain pseudo variables. Separator string needs to be one character long. Returns 1 if subject is found and -1 otherwise.
+ </para>
+ <para>
+ Function can be used from all kinds of routes.
+ </para>
+ <example>
+ <title><function>in_list()</function> usage</title>
+ <programlisting format="linespecific">
+...
+$var(subject) = "fi";
+$var(list) = "dk,fi,no,se";
+if (in_list("$var(subject)", "$var(list)", ",") {
+ xlog("L_INFO", "subject is found in list\n");
+}
+...
+ </programlisting>
+ </example>
+ </section>
</section>
<section>
diff --git a/modules/utils/functions.c b/modules/utils/functions.c
index 878073a..bdd4491 100644
--- a/modules/utils/functions.c
+++ b/modules/utils/functions.c
@@ -1,7 +1,7 @@
/*
* script functions of utils module
*
- * Copyright (C) 2008 Juha Heinanen
+ * Copyright (C) 2008-2011 Juha Heinanen
*
* This file is part of Kamailio, a free SIP server.
*
@@ -157,3 +157,66 @@ int is_int(struct sip_msg* _m, char* _pvar, char* _s2)
return -1;
}
+
+
+/*
+ * Checks if subject is found in list
+ */
+int in_list(struct sip_msg* _m, char* _subject, char* _list, char* _sep)
+{
+ str subject, list;
+ int sep;
+ char *at, *past, *s;
+
+ if (fixup_get_svalue(_m, (gparam_p)_subject, &subject) != 0) {
+ LM_ERR("cannot get subject value\n");
+ return -1;
+ } else {
+ if (subject.len == 0) {
+ LM_ERR("subject cannot be empty string\n");
+ return -1;
+ }
+ }
+
+ if (fixup_get_svalue(_m, (gparam_p)_list, &list) != 0) {
+ LM_ERR("cannot get list value\n");
+ return -1;
+ } else {
+ if (list.len == 0) return -1;
+ }
+
+ if (strlen(_sep) != 1) {
+ LM_ERR("separator needs to be a one charter string\n");
+ return -1;
+ } else {
+ sep = _sep[0];
+ if (sep == 0) {
+ LM_INFO("separator cannot be NUL\n");
+ return -1;
+ }
+ }
+
+ at = list.s;
+ past = list.s + list.len;
+
+ while (at < past) {
+ s = index(at, sep);
+ if (s == NULL) {
+ if ((subject.len == (past - at)) &&
+ strncmp(at, subject.s, subject.len) == 0) {
+ return 1;
+ } else {
+ return -1;
+ }
+ } else {
+ if ((subject.len == (s - at)) &&
+ strncmp(at, subject.s, subject.len) == 0) {
+ return 1;
+ } else {
+ at = s + 1;
+ }
+ }
+ }
+
+ return -1;
+}
diff --git a/modules/utils/functions.h b/modules/utils/functions.h
index df34211..a7bbdf3 100644
--- a/modules/utils/functions.h
+++ b/modules/utils/functions.h
@@ -1,7 +1,7 @@
/*
* headers of script functions of utils module
*
- * Copyright (C) 2008 Juha Heinanen
+ * Copyright (C) 2008-2011Juha Heinanen
*
* This file is part of Kamailio, a free SIP server.
*
@@ -47,4 +47,10 @@ int http_query(struct sip_msg* _m, char* _page, char* _params, char* _dst);
*/
int is_int(struct sip_msg* _m, char* _pvar, char* _s2);
+
+/*
+ * Checks if subject is found in list
+ */
+int in_list(struct sip_msg* _m, char* _subject, char* _list, char* _sep);
+
#endif /* UTILS_FUNCTIONS_H */
diff --git a/modules/utils/utils.c b/modules/utils/utils.c
index b5fa5b1..88ea37f 100644
--- a/modules/utils/utils.c
+++ b/modules/utils/utils.c
@@ -1,7 +1,7 @@
/*
* utils Module
*
- * Copyright (C) 2008 Juha Heinanen
+ * Copyright (C) 2008-2011 Juha Heinanen
* Copyright (C) 2009 1&1 Internet AG
*
* This file is part of Kamailio, a free SIP server.
@@ -57,7 +57,7 @@
MODULE_VERSION
-#define XCAP_TABLE_VERSION 3
+#define XCAP_TABLE_VERSION 4
/* Module parameter variables */
int http_query_timeout = 4;
@@ -90,6 +90,8 @@ static void destroy(void);
/* Fixup functions to be defined later */
static int fixup_http_query(void** param, int param_no);
static int fixup_free_http_query(void** param, int param_no);
+static int fixup_in_list(void** param, int param_no);
+static int fixup_free_in_list(void** param, int param_no);
/* forward function */
int utils_forward(struct sip_msg *msg, int id, int proto);
@@ -103,6 +105,8 @@ static cmd_export_t cmds[] = {
fixup_free_pvar_pvar, REQUEST_ROUTE},
{"is_int", (cmd_function)is_int, 1, fixup_pvar_null, fixup_free_pvar_null,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+ {"in_list", (cmd_function)in_list, 3, fixup_in_list, fixup_free_in_list,
+ REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
{0, 0, 0, 0, 0, 0}
};
@@ -361,6 +365,36 @@ static int fixup_free_http_query(void** param, int param_no)
return -1;
}
+/*
+ * Fix in_list params: subject and list (strings that may contain pvars),
+ * separator (string)
+ */
+static int fixup_in_list(void** param, int param_no)
+{
+ if ((param_no == 1) || (param_no == 2)) return fixup_spve_null(param, 1);
+
+ if (param_no == 3) return 0;
+
+ LM_ERR("invalid parameter number <%d>\n", param_no);
+ return -1;
+}
+
+/*
+ * Free http_query params.
+ */
+static int fixup_free_in_list(void** param, int param_no)
+{
+ if ((param_no == 1) || (param_no == 2)) {
+ LM_WARN("free function has not been defined for spve\n");
+ return 0;
+ }
+
+ if (param_no == 3) return 0;
+
+ LM_ERR("invalid parameter number <%d>\n", param_no);
+ return -1;
+}
+
/*!
* \brief checks precondition, switch, filter and forwards msg if necessary
More information about the sr-dev
mailing list