Browse code

configuration values can be retrieved in the script via selects: @cfg_get.<group_name>.<var_name>

Miklos Tirpak authored on 11/01/2008 16:38:52
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,105 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2008 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ *
27
+ * History
28
+ * -------
29
+ *  2008-01-10	Initial version (Miklos)
30
+ */
31
+
32
+#include "../select.h"
33
+#include "../ut.h"
34
+#include "cfg_struct.h"
35
+#include "cfg_select.h"
36
+
37
+int select_cfg_var(str *res, select_t *s, struct sip_msg *msg)
38
+{
39
+	cfg_group_t	*group;
40
+	cfg_mapping_t	*var;
41
+	void		*p;
42
+	int		i;
43
+
44
+	if (msg == NULL) {
45
+		/* fixup call */
46
+
47
+		/* two parameters are mandatory, group name and variable name */
48
+		if (s->n != 3) {
49
+			LOG(L_ERR, "ERROR: select_cfg_var(): two parameters are expected\n");
50
+			return -1;
51
+		}
52
+
53
+		if ((s->params[1].type != SEL_PARAM_STR)
54
+		|| (s->params[2].type != SEL_PARAM_STR)) {
55
+			LOG(L_ERR, "ERROR: select_cfg_var(): string parameters are expected\n");
56
+			return -1;
57
+		}
58
+
59
+		/* look-up the group and the variable */
60
+		if (cfg_lookup_var(&s->params[1].v.s, &s->params[2].v.s, &group, &var)) {
61
+			LOG(L_ERR, "ERROR: select_cfg_var(): unknown variable\n");
62
+			return -1;
63
+		}
64
+
65
+		if (var->def->on_change_cb) {
66
+			/* fixup function is defined -- safer to return an error
67
+			than an incorrect value */
68
+			LOG(L_ERR, "ERROR: select_cfg_var(): variable cannot be retrieved\n");
69
+			return -1;
70
+		}
71
+
72
+		s->params[1].type = SEL_PARAM_PTR;
73
+		s->params[1].v.p = (void *)group;
74
+
75
+		s->params[2].type = SEL_PARAM_PTR;
76
+		s->params[2].v.p = (void *)var;
77
+		return 1;
78
+	}
79
+
80
+	group = (cfg_group_t *)s->params[1].v.p;
81
+	var = (cfg_mapping_t *)s->params[2].v.p;
82
+
83
+	/* use the module's handle to access the variable, so the variables
84
+	are read from private memory */
85
+	p = *(group->handle) + var->offset;
86
+
87
+	switch (CFG_VAR_TYPE(var)) {
88
+	case CFG_VAR_INT:
89
+		memcpy(&i, p, sizeof(int));
90
+		/* WARNING: int2str uses a static buffer */
91
+		res->s = int2str(i, &res->len);
92
+		break;
93
+
94
+	case CFG_VAR_STRING:
95
+		memcpy(&res->s, p, sizeof(char *));
96
+		res->len = strlen(res->s);
97
+		break;
98
+
99
+	case CFG_VAR_STR:
100
+		memcpy(res, p, sizeof(str));
101
+		break;
102
+
103
+	}
104
+	return 0;
105
+}
0 106
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2008 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ *
27
+ * History
28
+ * -------
29
+ *  2008-01-10	Initial version (Miklos)
30
+ */
31
+
32
+#ifndef _CFG_SELECT_H
33
+#define _CFG_SELECT_H
34
+
35
+int select_cfg_var(str *res, select_t *s, struct sip_msg *msg);
36
+
37
+#endif /* _CFG_SELECT_H */
... ...
@@ -208,6 +208,8 @@ SELECT_F(select_date)
208 208
 SELECT_F(select_identity)
209 209
 SELECT_F(select_identity_info)
210 210
 
211
+SELECT_F(select_cfg_var)
212
+
211 213
 static select_row_t select_core[] = {
212 214
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0}, /* not the same as request.uri because it is involved by new_uri */
213 215
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("request_uri"), select_ruri, 0},
... ...
@@ -395,6 +397,8 @@ static select_row_t select_core[] = {
395 397
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity"), select_identity, 0},
396 398
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity_info"), select_identity_info, 0},
397 399
 
400
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_get"), select_cfg_var, CONSUME_ALL | FIXUP_CALL },
401
+
398 402
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
399 403
 };
400 404