-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunc_global.c
415 lines (357 loc) · 11 KB
/
func_global.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2006, Tilghman Lesher
*
* Tilghman Lesher <func_global__200605@the-tilghman.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Global variable dialplan functions
*
* \author Tilghman Lesher <func_global__200605@the-tilghman.com>
*
* \ingroup functions
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/stasis_channels.h"
/*** DOCUMENTATION
<function name="GLOBAL" language="en_US">
<since>
<version>1.4.0</version>
</since>
<synopsis>
Gets or sets the global variable specified.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Global variable name</para>
</parameter>
</syntax>
<description>
<para>Set or get the value of a global variable specified in <replaceable>varname</replaceable></para>
</description>
</function>
<function name="GLOBAL_DELETE" language="en_US">
<since>
<version>18.21.0</version>
<version>20.6.0</version>
<version>21.1.0</version>
</since>
<synopsis>
Deletes a specified global variable.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Global variable name</para>
</parameter>
</syntax>
<description>
<para>Delete the global variable specified in <replaceable>varname</replaceable>.
Will succeed if the global variable exists or not.</para>
</description>
<see-also>
<ref type="function">GLOBAL</ref>
<ref type="function">DELETE</ref>
</see-also>
</function>
<function name="GLOBAL_EXISTS" language="en_US">
<since>
<version>18.21.0</version>
<version>20.6.0</version>
<version>21.1.0</version>
</since>
<synopsis>
Check if a global variable exists or not.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Global variable name</para>
</parameter>
</syntax>
<description>
<para>Returns <literal>1</literal> if global variable exists or <literal>0</literal> otherwise.</para>
</description>
<see-also>
<ref type="function">VARIABLE_EXISTS</ref>
</see-also>
</function>
<function name="SHARED" language="en_US">
<since>
<version>1.6.0</version>
</since>
<synopsis>
Gets or sets the shared variable specified.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Variable name</para>
</parameter>
<parameter name="channel">
<para>If not specified will default to current channel. It is the complete
channel name: <literal>SIP/12-abcd1234</literal> or the prefix only <literal>SIP/12</literal>.</para>
</parameter>
</syntax>
<description>
<para>Implements a shared variable area, in which you may share variables between
channels.</para>
<para>The variables used in this space are separate from the general namespace of
the channel and thus <variable>SHARED(foo)</variable> and <variable>foo</variable>
represent two completely different variables, despite sharing the same name.</para>
<para>Finally, realize that there is an inherent race between channels operating
at the same time, fiddling with each others' internal variables, which is why
this special variable namespace exists; it is to remind you that variables in
the SHARED namespace may change at any time, without warning. You should
therefore take special care to ensure that when using the SHARED namespace,
you retrieve the variable and store it in a regular channel variable before
using it in a set of calculations (or you might be surprised by the result).</para>
</description>
</function>
<managerEvent language="en_US" name="VarSet">
<managerEventInstance class="EVENT_FLAG_DIALPLAN">
<since>
<version>12.0.0</version>
</since>
<synopsis>Raised when a variable is shared between channels.</synopsis>
<syntax>
<channel_snapshot/>
<parameter name="Variable">
<para>The SHARED variable being set.</para>
<note><para>The variable name will always be enclosed with
<literal>SHARED()</literal></para></note>
</parameter>
<parameter name="Value">
<para>The new value of the variable.</para>
</parameter>
</syntax>
<see-also>
<ref type="function">SHARED</ref>
</see-also>
</managerEventInstance>
</managerEvent>
***/
static void shared_variable_free(void *data);
static const struct ast_datastore_info shared_variable_info = {
.type = "SHARED_VARIABLES",
.destroy = shared_variable_free,
};
static void shared_variable_free(void *data)
{
struct varshead *varshead = data;
struct ast_var_t *var;
while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
ast_var_delete(var);
}
ast_free(varshead);
}
static int global_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
const char *var = pbx_builtin_getvar_helper(NULL, data);
*buf = '\0';
if (var)
ast_copy_string(buf, var, len);
return 0;
}
static int global_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
pbx_builtin_setvar_helper(NULL, data, value);
return 0;
}
static struct ast_custom_function global_function = {
.name = "GLOBAL",
.read = global_read,
.write = global_write,
};
static int global_delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
pbx_builtin_setvar_helper(NULL, data, NULL);
return 0;
}
static struct ast_custom_function global_delete_function = {
.name = "GLOBAL_DELETE",
.write = global_delete_write,
};
static int global_exists_read(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t len)
{
const char *var = pbx_builtin_getvar_helper(NULL, data);
strcpy(buf, var ? "1" : "0");
return 0;
}
static struct ast_custom_function global_exists_function = {
.name = "GLOBAL_EXISTS",
.read = global_exists_read,
};
static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct ast_datastore *varstore;
struct varshead *varshead;
struct ast_var_t *var;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(chan);
);
struct ast_channel *c_ref = NULL;
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
return -1;
}
AST_STANDARD_APP_ARGS(args, data);
if (!ast_strlen_zero(args.chan)) {
char *prefix = ast_alloca(strlen(args.chan) + 2);
sprintf(prefix, "%s-", args.chan);
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
return -1;
}
chan = c_ref;
} else if (!chan) {
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
return -1;
}
ast_channel_lock(chan);
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
varshead = varstore->data;
*buf = '\0';
/* Protected by the channel lock */
AST_LIST_TRAVERSE(varshead, var, entries) {
if (!strcmp(args.var, ast_var_name(var))) {
ast_copy_string(buf, ast_var_value(var), len);
break;
}
}
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return 0;
}
static int shared_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
struct ast_datastore *varstore;
struct varshead *varshead;
struct ast_var_t *var;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(var);
AST_APP_ARG(chan);
);
struct ast_channel *c_ref = NULL;
int len;
RAII_VAR(char *, shared_buffer, NULL, ast_free);
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
return -1;
}
AST_STANDARD_APP_ARGS(args, data);
if (!ast_strlen_zero(args.chan)) {
char *prefix = ast_alloca(strlen(args.chan) + 2);
sprintf(prefix, "%s-", args.chan);
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
return -1;
}
chan = c_ref;
} else if (!chan) {
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
return -1;
}
len = 9 + strlen(args.var); /* SHARED() + var */
shared_buffer = ast_malloc(len);
if (!shared_buffer) {
if (c_ref) {
ast_channel_unref(c_ref);
}
return -1;
}
ast_channel_lock(chan);
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
ast_datastore_free(varstore);
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
varstore->data = varshead;
ast_channel_datastore_add(chan, varstore);
}
varshead = varstore->data;
/* Protected by the channel lock */
AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) {
/* If there's a previous value, remove it */
if (!strcmp(args.var, ast_var_name(var))) {
AST_LIST_REMOVE_CURRENT(entries);
ast_var_delete(var);
break;
}
}
AST_LIST_TRAVERSE_SAFE_END;
if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
AST_LIST_INSERT_HEAD(varshead, var, entries);
sprintf(shared_buffer, "SHARED(%s)", args.var);
ast_channel_publish_varset(chan, shared_buffer, value);
}
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return 0;
}
static struct ast_custom_function shared_function = {
.name = "SHARED",
.read = shared_read,
.write = shared_write,
};
static int unload_module(void)
{
int res = 0;
res |= ast_custom_function_unregister(&global_function);
res |= ast_custom_function_unregister(&global_delete_function);
res |= ast_custom_function_unregister(&global_exists_function);
res |= ast_custom_function_unregister(&shared_function);
return res;
}
static int load_module(void)
{
int res = 0;
res |= ast_custom_function_register(&global_function);
res |= ast_custom_function_register(&global_delete_function);
res |= ast_custom_function_register(&global_exists_function);
res |= ast_custom_function_register(&shared_function);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Variable dialplan functions");