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