Browse code

- add an API to the textops module, closes #2018652 - patch contributed from Ardjan Zwartjes, a dot zwartjes at telecats dot nl

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4660 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 11/08/2008 06:46:44
Showing 7 changed files
... ...
@@ -67,6 +67,12 @@ Daniel-Constantin Mierla
67 67
 
68 68
         1.4. Known Limitations
69 69
 
70
+   2. Developer Guide
71
+
72
+        2.1. Functions
73
+
74
+              2.1.1. load_textops(*import_structure)
75
+
70 76
    List of Examples
71 77
 
72 78
    1.1. search usage
... ...
@@ -668,3 +674,15 @@ if(is_privacy("id"))
668 674
    Search functions are applied to the original request, i.e.,
669 675
    they ignore all changes resulting from message processing in
670 676
    Kamailio script.
677
+
678
+Chapter 2. Developer Guide
679
+
680
+2.1. Functions
681
+
682
+2.1.1.  load_textops(*import_structure)
683
+
684
+   For programmatic use only--import the Textops API.
685
+
686
+   Meaning of the parameters is as follows:
687
+     * import_structure - Pointer to the import structure - see
688
+       "struct textops_binds" in modules/textops/api.h
671 689
new file mode 100644
... ...
@@ -0,0 +1,114 @@
1
+/* Copyright (C) 2008 Telecats BV
2
+ *
3
+ * This file is part of openser, a free SIP server.
4
+ *
5
+ * openser is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public License
7
+ * as published by the Free Software Foundation; either version 2
8
+ * of the License, or (at your option) any later version.
9
+ *
10
+ * openser is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
+ *
19
+ *
20
+ * History:
21
+ * ---------
22
+ *  2008-07-14 first version (Ardjan Zwartjes)
23
+ */
24
+
25
+
26
+#include "api.h"
27
+#include "textops.h"
28
+#include "../../mod_fix.h"
29
+#include "../../mem/mem.h"
30
+
31
+
32
+/*
33
+ * User friendly wrapper around add_hf_helper, to be called from 
34
+ * other modules.
35
+ */
36
+int append_hf_api(struct sip_msg *msg, str* str_hf){
37
+	return add_hf_helper(msg, str_hf, NULL, NULL, 0, NULL);
38
+}
39
+
40
+/*
41
+ * User friendly wrapper around remove_hf_f, to be called from 
42
+ * other modules.
43
+ */
44
+int remove_hf_api(struct sip_msg *msg, str* str_hf){
45
+	return remove_hf_f(msg, (char*)str_hf,NULL);
46
+}
47
+
48
+
49
+/*
50
+ * User friendly wrapper to call search_append from other
51
+ * modules
52
+ */
53
+
54
+int search_append_api(struct sip_msg *msg, str *regex, str *data_str){
55
+	int retval;
56
+	
57
+	char *data=pkg_malloc(data_str->len+1);
58
+	memcpy(data,data_str->s,data_str->len);
59
+	memset(data+data_str->len,0,1);
60
+	
61
+	void **param=pkg_malloc(sizeof(void*));
62
+	*param=pkg_malloc(regex->len+1);
63
+	memcpy(*param,regex->s,regex->len);
64
+	memset(*param+regex->len,0,1);
65
+	
66
+	fixup_regexp_none(param,1);
67
+	
68
+	retval=search_append_f(msg, *param, data);
69
+	
70
+	fixup_free_regexp_none(param,1);
71
+
72
+	pkg_free(param);
73
+	pkg_free(data);
74
+	
75
+	return retval;
76
+	
77
+}
78
+
79
+/*
80
+ * User friendly wrapper to call search from other modules.
81
+ */
82
+int search_api(struct sip_msg *msg, str *regex){
83
+	int retval;
84
+
85
+	void **param=pkg_malloc(sizeof(void*));
86
+	
87
+	*param=pkg_malloc(regex->len+1);
88
+	memcpy(*param,regex->s,regex->len);
89
+	memset(*param+regex->len,0,1);
90
+	
91
+	fixup_regexp_none(param,1);
92
+	
93
+	retval=search_f(msg, *param, NULL);
94
+	
95
+	fixup_free_regexp_none(param,1);
96
+	pkg_free(param);
97
+	
98
+	return retval;
99
+	
100
+}
101
+/*
102
+ * Function to load the textops api.
103
+ */
104
+int load_textops(struct textops_binds *tob){
105
+	if(tob==NULL){
106
+		LM_WARN("textops_binds: Cannot load textops API into a NULL pointer\n");
107
+		return -1;
108
+	}
109
+	tob->append_hf=append_hf_api;
110
+	tob->remove_hf=remove_hf_api;
111
+	tob->search_append=search_append_api;
112
+	tob->search=search_api;
113
+	return 0;
114
+}
0 115
new file mode 100644
... ...
@@ -0,0 +1,67 @@
1
+/* Copyright (C) 2008 Telecats BV
2
+ *
3
+ * This file is part of openser, a free SIP server.
4
+ *
5
+ * openser is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public License
7
+ * as published by the Free Software Foundation; either version 2
8
+ * of the License, or (at your option) any later version.
9
+ *
10
+ * openser is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
+ *
19
+ *
20
+ * History:
21
+ * ---------
22
+ *  2008-07-14 first version (Ardjan Zwartjes)
23
+ */
24
+
25
+
26
+#ifndef TEXTOPS_API_H_
27
+#define TEXTOPS_API_H_
28
+#include "../../str.h"
29
+#include "../../sr_module.h"
30
+
31
+
32
+typedef int (*append_hf_t)(struct sip_msg*, str*);
33
+typedef int (*remove_hf_t)(struct sip_msg*, str*);
34
+typedef int (*search_append_t)(struct sip_msg*, str*, str*);
35
+typedef int (*search_t)(struct sip_msg*, str*);
36
+
37
+/*
38
+ * Struct with the textops api.
39
+ */
40
+struct textops_binds{
41
+	append_hf_t	append_hf; // Append a header to the message.
42
+	remove_hf_t	remove_hf; // Remove a header with the specified name from the message.
43
+	search_append_t search_append; // Append a str after a match of the specified regex.
44
+	search_t search; // Check if the regex matches a part of the message.
45
+};
46
+
47
+typedef int (*load_textops_f)(struct textops_binds*);
48
+
49
+/*
50
+ * function exported by module - it will load the other functions
51
+ */
52
+int load_textops(struct textops_binds*);
53
+
54
+/*
55
+ * Function to be called direclty from other modules to load
56
+ * the textops API.
57
+ */
58
+inline static int load_textops_api(struct textops_binds *tob){
59
+	load_textops_f load_textops_exports;
60
+	if(!(load_textops_exports=(load_textops_f)find_export("load_textops",0,0))){
61
+		LM_ERR("Failed to import load_textops\n");
62
+		return -1;
63
+	}
64
+	return load_textops_exports(tob);
65
+}
66
+
67
+#endif /*TEXT_OPS_API_H_*/
... ...
@@ -4,6 +4,7 @@
4 4
 
5 5
 
6 6
 <!ENTITY admin SYSTEM "textops_admin.xml">
7
+<!ENTITY devel SYSTEM "textops_devel.xml">
7 8
 <!ENTITY faq SYSTEM "../../../doc/module_faq.xml">
8 9
 
9 10
 <!-- Include general documentation entities -->
... ...
@@ -48,6 +49,7 @@
48 49
 	<toc></toc>
49 50
 	
50 51
 	&admin;
52
+	&devel;
51 53
 	&faq;
52 54
 	
53 55
 </book>
54 56
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+<!-- Module Developer's Guide -->
2
+
3
+<chapter>
4
+	
5
+	<title>&develguide;</title>
6
+		<section>
7
+		<title>Functions</title>
8
+		<section>
9
+			<title>
10
+			<function moreinfo="none">load_textops(*import_structure)</function>
11
+			</title>
12
+			<para>
13
+			For programmatic use only--import the  <acronym>Textops</acronym> API.
14
+			</para>
15
+			<para>Meaning of the parameters is as follows:</para>
16
+			<itemizedlist>
17
+			<listitem>
18
+				<para><emphasis>import_structure</emphasis> - Pointer to 
19
+				the import structure - see <quote>struct textops_binds</quote> in 
20
+				modules/textops/api.h
21
+				</para>
22
+			</listitem>
23
+			</itemizedlist>
24
+		</section>
25
+		</section>
26
+</chapter>
... ...
@@ -44,6 +44,7 @@
44 44
  *  2005-07-05  is_method("name") to check method using id (ramona)
45 45
  *  2006-03-17  applied patch from Marc Haisenko <haisenko@comdasys.com> 
46 46
  *              for adding has_body() function (bogdan)
47
+ *  2008-07-14  Moved some function declarations to a separate file (Ardjan Zwartjes) 
47 48
  *
48 49
  */
49 50
 
... ...
@@ -72,6 +73,9 @@
72 73
 #include <time.h>
73 74
 #include <sys/time.h>
74 75
 
76
+#include "textops.h"
77
+#include "api.h"
78
+
75 79
 MODULE_VERSION
76 80
 
77 81
 
... ...
@@ -85,7 +89,6 @@ MODULE_VERSION
85 89
 #define MAX_TIME 64
86 90
 
87 91
 
88
-static int search_f(struct sip_msg*, char*, char*);
89 92
 static int search_body_f(struct sip_msg*, char*, char*);
90 93
 static int replace_f(struct sip_msg*, char*, char*);
91 94
 static int replace_body_f(struct sip_msg*, char*, char*);
... ...
@@ -97,9 +100,7 @@ static int subst_uri_f(struct sip_msg*, char*, char*);
97 100
 static int subst_user_f(struct sip_msg*, char*, char*);
98 101
 static int subst_body_f(struct sip_msg*, char*, char*);
99 102
 static int filter_body_f(struct sip_msg*, char*, char*);
100
-static int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
101 103
 static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
102
-static int search_append_f(struct sip_msg*, char*, char*);
103 104
 static int search_append_body_f(struct sip_msg*, char*, char*);
104 105
 static int append_to_reply_f(struct sip_msg* msg, char* key, char* str);
105 106
 static int append_hf_1(struct sip_msg* msg, char* str1, char* str2);
... ...
@@ -237,7 +238,7 @@ static char *get_header(struct sip_msg *msg)
237 238
 
238 239
 
239 240
 
240
-static int search_f(struct sip_msg* msg, char* key, char* str2)
241
+int search_f(struct sip_msg* msg, char* key, char* str2)
241 242
 {
242 243
 	/*we registered only 1 param, so we ignore str2*/
243 244
 	regmatch_t pmatch;
... ...
@@ -269,7 +270,7 @@ static int search_body_f(struct sip_msg* msg, char* key, char* str2)
269 270
 }
270 271
 
271 272
 
272
-static int search_append_f(struct sip_msg* msg, char* key, char* str2)
273
+int search_append_f(struct sip_msg* msg, char* key, char* str2)
273 274
 {
274 275
 	struct lump* l;
275 276
 	regmatch_t pmatch;
... ...
@@ -857,7 +858,7 @@ static int filter_body_f(struct sip_msg* msg, char* _content_type,
857 858
 }
858 859
 
859 860
 
860
-static int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
861
+int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
861 862
 {
862 863
 	struct hdr_field *hf;
863 864
 	struct lump* l;
... ...
@@ -1007,7 +1008,7 @@ static int append_to_reply_f(struct sip_msg* msg, char* key, char* str0)
1007 1008
 
1008 1009
 /* add str1 to end of header or str1.r-uri.str2 */
1009 1010
 
1010
-static int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
1011
+int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
1011 1012
 		gparam_p hfval, int mode, gparam_p hfanc)
1012 1013
 {
1013 1014
 	struct lump* anchor;
1014 1015
new file mode 100644
... ...
@@ -0,0 +1,35 @@
1
+/* Copyright (C) 2008 Telecats BV
2
+ *
3
+ * This file is part of openser, a free SIP server.
4
+ *
5
+ * openser is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public License
7
+ * as published by the Free Software Foundation; either version 2
8
+ * of the License, or (at your option) any later version.
9
+ *
10
+ * openser is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
+ *
19
+ *
20
+ * History:
21
+ * ---------
22
+ *  2008-07-14 first version, function definitions copied from textops.c (Ardjan Zwartjes)
23
+ */
24
+
25
+#ifndef TEXTOPS_H_
26
+#define TEXTOPS_H_
27
+#include "../../mod_fix.h"
28
+
29
+int search_f(struct sip_msg*, char*, char*);
30
+int search_append_f(struct sip_msg*, char*, char*);
31
+int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
32
+int add_hf_helper(struct sip_msg* msg, str *str1, str *str2, gparam_p hfval, int mode, gparam_p hfanc);
33
+
34
+
35
+#endif /*TEXTOPS_H_*/