Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 2005-2006, Digium, Inc.
* Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
* Portions Copyright (C) 2005, Anthony Minessale II
* 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.
*/
*
* \brief String manipulation dialplan functions
*
* \author Tilghman Lesher
* \author Anothony Minessale II
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <ctype.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Kevin P. Fleming
committed
#include "asterisk/localtime.h"
Tilghman Lesher
committed
AST_THREADSTORAGE(result_buf);
Tilghman Lesher
committed
AST_THREADSTORAGE(tmp_buf);
Tilghman Lesher
committed
/*** DOCUMENTATION
<function name="FIELDQTY" language="en_US">
<synopsis>
Count the fields with an arbitrary delimiter
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delim" required="true" />
</syntax>
<description>
<para>The delimiter may be specified as a special or extended ASCII character, by encoding it. The characters
<literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
carriage return, and tab characters, respectively. Also, octal and hexadecimal specifications are recognized
by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively. For example, if you wanted
to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
<para>Example: If ${example} contains <literal>ex-amp-le</literal>, then ${FIELDQTY(example,-)} returns 3.</para>
Tilghman Lesher
committed
<function name="LISTFILTER" language="en_US">
<synopsis>Remove an item from a list, by name.</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delim" required="true" default="," />
<parameter name="value" required="true" />
</syntax>
<description>
<para>Remove <replaceable>value</replaceable> from the list contained in the <replaceable>varname</replaceable>
variable, where the list delimiter is specified by the <replaceable>delim</replaceable> parameter. This is
very useful for removing a single channel name from a list of channels, for example.</para>
</description>
</function>
<function name="FILTER" language="en_US">
<synopsis>
Filter the string to include only the allowed characters
</synopsis>
<syntax>
<parameter name="allowed-chars" required="true" />
<parameter name="string" required="true" />
</syntax>
<description>
<para>Permits all characters listed in <replaceable>allowed-chars</replaceable>,
filtering all others outs. In addition to literally listing the characters,
you may also use ranges of characters (delimited by a <literal>-</literal></para>
<para>Hexadecimal characters started with a <literal>\x</literal>(i.e. \x20)</para>
<para>Octal characters started with a <literal>\0</literal> (i.e. \040)</para>
<para>Also <literal>\t</literal>,<literal>\n</literal> and <literal>\r</literal> are recognized.</para>
<note><para>If you want the <literal>-</literal> character it needs to be prefixed with a
<literal>\</literal></para></note>
</description>
</function>
Tilghman Lesher
committed
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
<function name="REPLACE" language="en_US">
<synopsis>
Replace a set of characters in a given string with another character.
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="find-chars" required="true" />
<parameter name="replace-char" required="false" />
</syntax>
<description>
<para>Iterates through a string replacing all the <replaceable>find-chars</replaceable> with
<replaceable>replace-char</replaceable>. <replaceable>replace-char</replaceable> may be either
empty or contain one character. If empty, all <replaceable>find-chars</replaceable> will be
deleted from the output.</para>
<note><para>The replacement only occurs in the output. The original variable is not
altered.</para></note>
</description>
</function>
<function name="PASSTHRU" language="en_US">
<synopsis>
Pass the given argument back as a value.
</synopsis>
<syntax>
<parameter name="string" required="false" />
</syntax>
<description>
<para>Literally returns the given <replaceable>string</replaceable>. The intent is to permit
other dialplan functions which take a variable name as an argument to be able to take a literal
string, instead.</para>
</description>
</function>
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
<function name="REGEX" language="en_US">
<synopsis>
Check string against a regular expression.
</synopsis>
<syntax argsep=" ">
<parameter name=""regular expression"" required="true" />
<parameter name="string" required="true" />
</syntax>
<description>
<para>Return <literal>1</literal> on regular expression match or <literal>0</literal> otherwise</para>
<para>Please note that the space following the double quotes separating the
regex from the data is optional and if present, is skipped. If a space is
desired at the beginning of the data, then put two spaces there; the second
will not be skipped.</para>
</description>
</function>
<application name="ClearHash" language="en_US">
<synopsis>
Clear the keys from a specified hashname.
</synopsis>
<syntax>
<parameter name="hashname" required="true" />
</syntax>
<description>
<para>Clears all keys out of the specified <replaceable>hashname</replaceable>.</para>
</description>
</application>
<function name="HASH" language="en_US">
<synopsis>
Implementation of a dialplan associative array
</synopsis>
<syntax>
<parameter name="hashname" required="true" />
<parameter name="hashkey" />
</syntax>
<description>
<para>In two arguments mode, gets and sets values to corresponding keys within
a named associative array. The single-argument mode will only work when assigned
to from a function defined by func_odbc</para>
</description>
</function>
<function name="HASHKEYS" language="en_US">
<synopsis>
Retrieve the keys of the HASH() function.
</synopsis>
<syntax>
<parameter name="hashname" required="true" />
</syntax>
<description>
<para>Returns a comma-delimited list of the current keys of the associative array
defined by the HASH() function. Note that if you iterate over the keys of
the result, adding keys during iteration will cause the result of the HASHKEYS()
function to change.</para>
</description>
</function>
<function name="KEYPADHASH" language="en_US">
<synopsis>
Hash the letters in string into equivalent keypad numbers.
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${KEYPADHASH(Les)} returns "537"</para>
</description>
</function>
<function name="ARRAY" language="en_US">
<synopsis>
Allows setting multiple variables at once.
</synopsis>
<syntax>
<parameter name="var1" required="true" />
<parameter name="var2" required="false" multiple="true" />
<parameter name="varN" required="false" />
</syntax>
<description>
<para>The comma-delimited list passed as a value to which the function is set will
be interpreted as a set of values to which the comma-delimited list of
variable names in the argument should be set.</para>
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
<para>Example: Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2</para>
</description>
</function>
<function name="STRPTIME" language="en_US">
<synopsis>
Returns the epoch of the arbitrary date/time string structured as described by the format.
</synopsis>
<syntax>
<parameter name="datetime" required="true" />
<parameter name="timezone" required="true" />
<parameter name="format" required="true" />
</syntax>
<description>
<para>This is useful for converting a date into <literal>EPOCH</literal> time,
possibly to pass to an application like SayUnixTime or to calculate the difference
between the two date strings</para>
<para>Example: ${STRPTIME(2006-03-01 07:30:35,America/Chicago,%Y-%m-%d %H:%M:%S)} returns 1141219835</para>
</description>
</function>
<function name="STRFTIME" language="en_US">
<synopsis>
Returns the current date/time in the specified format.
</synopsis>
<syntax>
<parameter name="epoch" />
<parameter name="timezone" />
<parameter name="format" />
</syntax>
<description>
<para>STRFTIME supports all of the same formats as the underlying C function
<emphasis>strftime(3)</emphasis>.
It also supports the following format: <literal>%[n]q</literal> - fractions of a second,
with leading zeros.</para>
<para>Example: <literal>%3q</literal> will give milliseconds and <literal>%1q</literal>
will give tenths of a second. The default is set at milliseconds (n=3).
The common case is to use it in combination with %S, as in <literal>%S.%3q</literal>.</para>
</description>
<see-also>
<ref type="manpage">strftime(3)</ref>
</see-also>
</function>
<function name="EVAL" language="en_US">
<synopsis>
Evaluate stored variables
</synopsis>
<syntax>
<parameter name="variable" required="true" />
</syntax>
<description>
<para>Using EVAL basically causes a string to be evaluated twice.
When a variable or expression is in the dialplan, it will be
evaluated at runtime. However, if the results of the evaluation
is in fact another variable or expression, using EVAL will have it
evaluated a second time.</para>
<para>Example: If the <variable>MYVAR</variable> contains
<variable>OTHERVAR</variable>, then the result of ${EVAL(
<variable>MYVAR</variable>)} in the dialplan will be the
contents of <variable>OTHERVAR</variable>. Normally just
putting <variable>MYVAR</variable> in the dialplan the result
would be <variable>OTHERVAR</variable>.</para>
</description>
</function>
<function name="TOUPPER" language="en_US">
<synopsis>
Convert string to all uppercase letters.
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${TOUPPER(Example)} returns "EXAMPLE"</para>
</description>
</function>
<function name="TOLOWER" language="en_US">
<synopsis>
Convert string to all lowercase letters.
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${TOLOWER(Example)} returns "example"</para>
</description>
</function>
<function name="LEN" language="en_US">
<synopsis>
Return the length of the string given.
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${LEN(example)} returns 7</para>
</description>
</function>
<function name="QUOTE" language="en_US">
<synopsis>
Quotes a given string, escaping embedded quotes as necessary
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${QUOTE(ab"c"de)} will return "abcde"</para>
</description>
</function>
<function name="CSV_QUOTE" language="en_US">
<synopsis>
Quotes a given string for use in a CSV file, escaping embedded quotes as necessary
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Example: ${CSV_QUOTE("a,b" 123)} will return """a,b"" 123"</para>
</description>
</function>
<function name="SHIFT" language="en_US">
<synopsis>
Removes and returns the first item off of a variable containing delimited text
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delimiter" required="false" default="," />
</syntax>
<description>
<para>Example:</para>
<para>exten => s,1,Set(array=one,two,three)</para>
<para>exten => s,n,While($["${SET(var=${SHIFT(array)})}" != ""])</para>
<para>exten => s,n,NoOp(var is ${var})</para>
<para>exten => s,n,EndWhile</para>
<para>This would iterate over each value in array, left to right, and
would result in NoOp(var is one), NoOp(var is two), and
NoOp(var is three) being executed.
</para>
</description>
</function>
<function name="POP" language="en_US">
<synopsis>
Removes and returns the last item off of a variable containing delimited text
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delimiter" required="false" default="," />
</syntax>
<description>
<para>Example:</para>
<para>exten => s,1,Set(array=one,two,three)</para>
<para>exten => s,n,While($["${SET(var=${POP(array)})}" != ""])</para>
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
<para>exten => s,n,NoOp(var is ${var})</para>
<para>exten => s,n,EndWhile</para>
<para>This would iterate over each value in array, right to left, and
would result in NoOp(var is three), NoOp(var is two), and
NoOp(var is one) being executed.
</para>
</description>
</function>
<function name="PUSH" language="en_US">
<synopsis>
Appends one or more values to the end of a variable containing delimited text
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delimiter" required="false" default="," />
</syntax>
<description>
<para>Example: Set(PUSH(array)=one,two,three) would append one,
two, and three to the end of the values stored in the variable
"array".
</para>
</description>
</function>
<function name="UNSHIFT" language="en_US">
<synopsis>
Inserts one or more values to the beginning of a variable containing delimited text
</synopsis>
<syntax>
<parameter name="varname" required="true" />
<parameter name="delimiter" required="false" default="," />
</syntax>
<description>
<para>Example: Set(UNSHIFT(array)=one,two,three) would insert one,
two, and three before the values stored in the variable
"array".
</para>
</description>
</function>
static int function_fieldqty_helper(struct ast_channel *chan, const char *cmd,
char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
Tilghman Lesher
committed
struct ast_str *str = ast_str_thread_get(&result_buf, 16);
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(delim);
);
Tilghman Lesher
committed
char delim[2] = "";
size_t delim_used;
AST_STANDARD_APP_ARGS(args, parse);
if (args.delim) {
Tilghman Lesher
committed
ast_get_encoded_char(args.delim, delim, &delim_used);
varsubst = alloca(strlen(args.varname) + 4);
sprintf(varsubst, "${%s}", args.varname);
ast_str_substitute_variables(&str, 0, chan, varsubst);
if (ast_str_strlen(str) == 0) {
} else {
char *varval = ast_str_buffer(str);
while (strsep(&varval, delim)) {
if (sbuf) {
ast_str_set(sbuf, len, "%d", fieldcount);
} else {
snprintf(buf, len, "%d", fieldcount);
}
static int function_fieldqty(struct ast_channel *chan, const char *cmd,
char *parse, char *buf, size_t len)
{
return function_fieldqty_helper(chan, cmd, parse, buf, NULL, len);
}
static int function_fieldqty_str(struct ast_channel *chan, const char *cmd,
char *parse, struct ast_str **buf, ssize_t len)
{
return function_fieldqty_helper(chan, cmd, parse, NULL, buf, len);
}
static struct ast_custom_function fieldqty_function = {
.name = "FIELDQTY",
.read = function_fieldqty,
static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **bufstr, ssize_t len)
Tilghman Lesher
committed
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(listname);
AST_APP_ARG(delimiter);
AST_APP_ARG(fieldvalue);
);
Tilghman Lesher
committed
const char *ptr;
struct ast_str *orig_list = ast_str_thread_get(&tmp_buf, 16);
Tilghman Lesher
committed
const char *begin, *cur, *next;
int dlen, flen, first = 1;
struct ast_str *result, **result_ptr = &result;
Tilghman Lesher
committed
char *delim, *varsubst;
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(args, parse);
Tilghman Lesher
committed
if (!(result = ast_str_thread_get(&result_buf, 16))) {
return -1;
}
} else {
/* Place the result directly into the output buffer */
result_ptr = bufstr;
}
Tilghman Lesher
committed
if (args.argc < 3) {
ast_log(LOG_ERROR, "Usage: LISTFILTER(<listname>,<delimiter>,<fieldvalue>)\n");
return -1;
}
Tilghman Lesher
committed
varsubst = alloca(strlen(args.listname) + 4);
sprintf(varsubst, "${%s}", args.listname);
Tilghman Lesher
committed
/* If we don't lock the channel, the variable could disappear out from underneath us. */
if (chan) {
ast_channel_lock(chan);
}
Tilghman Lesher
committed
ast_str_substitute_variables(&orig_list, 0, chan, varsubst);
if (ast_str_strlen(orig_list)) {
Tilghman Lesher
committed
ast_log(LOG_ERROR, "List variable '%s' not found\n", args.listname);
if (chan) {
ast_channel_unlock(chan);
}
return -1;
}
/* If the string isn't there, just copy out the string and be done with it. */
Tilghman Lesher
committed
if (!(ptr = strstr(ast_str_buffer(orig_list), args.fieldvalue))) {
Tilghman Lesher
committed
ast_copy_string(buf, ast_str_buffer(orig_list), len);
Tilghman Lesher
committed
ast_str_set(result_ptr, len, "%s", ast_str_buffer(orig_list));
Tilghman Lesher
committed
if (chan) {
ast_channel_unlock(chan);
}
return 0;
}
dlen = strlen(args.delimiter);
delim = alloca(dlen + 1);
ast_get_encoded_str(args.delimiter, delim, dlen + 1);
if ((dlen = strlen(delim)) == 0) {
delim = ",";
dlen = 1;
}
flen = strlen(args.fieldvalue);
ast_str_reset(result);
/* Enough space for any result */
Tilghman Lesher
committed
ast_str_make_space(result_ptr, len ? len : ast_str_strlen(orig_list) + 1);
Tilghman Lesher
committed
Tilghman Lesher
committed
begin = ast_str_buffer(orig_list);
Tilghman Lesher
committed
next = strstr(begin, delim);
do {
/* Find next boundary */
if (next) {
cur = next;
next = strstr(cur + dlen, delim);
} else {
cur = strchr(begin + dlen, '\0');
}
if (flen == cur - begin && !strncmp(begin, args.fieldvalue, flen)) {
/* Skip field */
begin += flen + dlen;
} else {
/* Copy field to output */
if (!first) {
ast_str_append(result_ptr, len, "%s", delim);
Tilghman Lesher
committed
}
ast_str_append_substr(result_ptr, len, begin, cur - begin + 1);
first = 0;
Tilghman Lesher
committed
begin = cur + dlen;
}
} while (*cur != '\0');
if (chan) {
ast_channel_unlock(chan);
}
if (buf) {
ast_copy_string(buf, ast_str_buffer(result), len);
}
Tilghman Lesher
committed
return 0;
}
static int listfilter_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
{
return listfilter(chan, cmd, parse, buf, NULL, len);
}
static int listfilter_read2(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
{
return listfilter(chan, cmd, parse, NULL, buf, len);
}
Tilghman Lesher
committed
static struct ast_custom_function listfilter_function = {
.name = "LISTFILTER",
.read = listfilter_read,
.read2 = listfilter_read2,
Tilghman Lesher
committed
};
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(allowed);
AST_APP_ARG(string);
Tilghman Lesher
committed
char *outbuf = buf, ac;
char allowed[256] = "";
size_t allowedlen = 0;
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(args, parse);
Tilghman Lesher
committed
ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
}
Tilghman Lesher
committed
/* Expand ranges */
for (; *(args.allowed) && allowedlen < sizeof(allowed); ) {
Tilghman Lesher
committed
char c1 = 0, c2 = 0;
size_t consumed = 0;
Tilghman Lesher
committed
if (ast_get_encoded_char(args.allowed, &c1, &consumed))
Tilghman Lesher
committed
return -1;
args.allowed += consumed;
if (*(args.allowed) == '-') {
Tilghman Lesher
committed
if (ast_get_encoded_char(args.allowed + 1, &c2, &consumed))
Tilghman Lesher
committed
c2 = -1;
args.allowed += consumed + 1;
/*!\note
* Looks a little strange, until you realize that we can overflow
* the size of a char.
*/
for (ac = c1; ac != c2 && allowedlen < sizeof(allowed) - 1; ac++)
allowed[allowedlen++] = ac;
allowed[allowedlen++] = ac;
ast_debug(4, "c1=%d, c2=%d\n", c1, c2);
/* Decrement before the loop increment */
(args.allowed)--;
} else
allowed[allowedlen++] = c1;
}
ast_debug(1, "Allowed: %s\n", allowed);
for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
Tilghman Lesher
committed
if (strchr(allowed, *(args.string)))
}
*outbuf = '\0';
}
static struct ast_custom_function filter_function = {
.name = "FILTER",
.read = filter,
};
Tilghman Lesher
committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(varname);
AST_APP_ARG(find);
AST_APP_ARG(replace);
);
char *strptr, *varsubst;
struct ast_str *str = ast_str_thread_get(&result_buf, 16);
char find[256]; /* Only 256 characters possible */
char replace[2] = "";
size_t unused;
AST_STANDARD_APP_ARGS(args, data);
if (!str) {
return -1;
}
if (args.argc < 2) {
ast_log(LOG_ERROR, "Usage: %s(<varname>,<search-chars>[,<replace-char>])\n", cmd);
return -1;
}
/* Decode escapes */
ast_get_encoded_str(args.find, find, sizeof(find));
ast_get_encoded_char(args.replace, replace, &unused);
if (ast_strlen_zero(find) || ast_strlen_zero(args.varname)) {
ast_log(LOG_ERROR, "The characters to search for and the variable name must not be empty.\n");
return -1;
}
varsubst = alloca(strlen(args.varname) + 4);
sprintf(varsubst, "${%s}", args.varname);
ast_str_substitute_variables(&str, 0, chan, varsubst);
if (!ast_str_strlen(str)) {
/* Blank, nothing to replace */
return -1;
}
ast_debug(3, "String to search: (%s)\n", ast_str_buffer(str));
ast_debug(3, "Characters to find: (%s)\n", find);
ast_debug(3, "Character to replace with: (%s)\n", replace);
for (strptr = ast_str_buffer(str); *strptr; strptr++) {
/* buf is already a mutable buffer, so we construct the result
* directly there */
if (strchr(find, *strptr)) {
if (ast_strlen_zero(replace)) {
/* Remove character */
strcpy(strptr, strptr + 1); /* SAFE */
strptr--;
} else {
/* Replace character */
*strptr = *replace;
}
}
}
ast_str_set(buf, len, "%s", ast_str_buffer(str));
return 0;
}
static struct ast_custom_function replace_function = {
.name = "REPLACE",
.read2 = replace,
};
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(null);
AST_APP_ARG(reg);
AST_APP_ARG(str);
int errcode;
regex_t regexbuf;
AST_NONSTANDARD_APP_ARGS(args, parse, '"');
Tilghman Lesher
committed
if (args.argc != 3) {
ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
return -1;
}
if ((*args.str == ' ') || (*args.str == '\t'))
args.str++;
Tilghman Lesher
committed
ast_debug(1, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
if ((errcode = regcomp(®exbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
regerror(errcode, ®exbuf, buf, len);
ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
return -1;
Russell Bryant
committed
}
Russell Bryant
committed
strcpy(buf, regexec(®exbuf, args.str, 0, NULL, 0) ? "0" : "1");
Russell Bryant
committed
regfree(®exbuf);
static struct ast_custom_function regex_function = {
.read = regex,
#define HASH_PREFIX "~HASH~%s~"
#define HASH_FORMAT HASH_PREFIX "%s~"
static char *app_clearhash = "ClearHash";
/* This function probably should migrate to main/pbx.c, as pbx_builtin_clearvar_prefix() */
static void clearvar_prefix(struct ast_channel *chan, const char *prefix)
{
struct ast_var_t *var;
int len = strlen(prefix);
AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->varshead, var, entries) {
if (strncasecmp(prefix, ast_var_name(var), len) == 0) {
AST_LIST_REMOVE_CURRENT(entries);
Tilghman Lesher
committed
ast_free(var);
}
}
AST_LIST_TRAVERSE_SAFE_END
}
static int exec_clearhash(struct ast_channel *chan, const char *data)
{
char prefix[80];
snprintf(prefix, sizeof(prefix), HASH_PREFIX, data ? (char *)data : "null");
clearvar_prefix(chan, prefix);
return 0;
}
static int array(struct ast_channel *chan, const char *cmd, char *var,
AST_DECLARE_APP_ARGS(arg1,
);
AST_DECLARE_APP_ARGS(arg2,
char *origvar = "", *value2, varname[256];
int i, ishash = 0;
if (!strcmp(cmd, "HASH")) {
const char *var2 = pbx_builtin_getvar_helper(chan, "~ODBCFIELDS~");
origvar = var;
if (var2)
var = ast_strdupa(var2);
else {
if (chan)
ast_autoservice_stop(chan);
return -1;
ishash = 1;
}
/* The functions this will generally be used with are SORT and ODBC_*, which
* both return comma-delimited lists. However, if somebody uses literal lists,
* their commas will be translated to vertical bars by the load, and I don't
* want them to be surprised by the result. Hence, we prefer commas as the
* delimiter, but we'll fall back to vertical bars if commas aren't found.
*/
ast_debug(1, "array (%s=%s)\n", var, value2);
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(arg1, var);
Tilghman Lesher
committed
AST_STANDARD_APP_ARGS(arg2, value2);
for (i = 0; i < arg1.argc; i++) {
ast_debug(1, "array set value (%s=%s)\n", arg1.var[i],
arg2.val[i]);
if (i < arg2.argc) {
if (ishash) {
snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
pbx_builtin_setvar_helper(chan, varname, arg2.val[i]);
} else {
pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
}
} else {
/* We could unset the variable, by passing a NULL, but due to
* pushvar semantics, that could create some undesired behavior. */
if (ishash) {
snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
pbx_builtin_setvar_helper(chan, varname, "");
} else {
pbx_builtin_setvar_helper(chan, arg1.var[i], "");
}
static int hashkeys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct ast_var_t *newvar;
struct ast_str *prefix = ast_str_alloca(80);
ast_str_set(&prefix, -1, HASH_PREFIX, data);
memset(buf, 0, len);
AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
/* Copy everything after the prefix */
strncat(buf, ast_var_name(newvar) + ast_str_strlen(prefix), len - strlen(buf) - 1);
/* Trim the trailing ~ */
buf[strlen(buf) - 1] = ',';
}
}
/* Trim the trailing comma */
buf[strlen(buf) - 1] = '\0';
return 0;
}
static int hashkeys_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
struct ast_var_t *newvar;
struct ast_str *prefix = ast_str_alloca(80);
char *tmp;
ast_str_set(&prefix, -1, HASH_PREFIX, data);
AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
/* Copy everything after the prefix */
ast_str_append(buf, len, "%s", ast_var_name(newvar) + ast_str_strlen(prefix));
/* Trim the trailing ~ */
tmp = ast_str_buffer(*buf);
tmp[ast_str_strlen(*buf) - 1] = ',';
}
}
/* Trim the trailing comma */
tmp = ast_str_buffer(*buf);
tmp[ast_str_strlen(*buf) - 1] = '\0';
return 0;
}
static int hash_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
{
char varname[256];
AST_DECLARE_APP_ARGS(arg,
AST_APP_ARG(hashname);
AST_APP_ARG(hashkey);
);
Tilghman Lesher
committed
if (!strchr(var, ',')) {
/* Single argument version */
return array(chan, "HASH", var, value);
}
AST_STANDARD_APP_ARGS(arg, var);
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
pbx_builtin_setvar_helper(chan, varname, value);
return 0;
}
static int hash_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
{
char varname[256];
const char *varvalue;
AST_DECLARE_APP_ARGS(arg,
AST_APP_ARG(hashname);
AST_APP_ARG(hashkey);
);
AST_STANDARD_APP_ARGS(arg, data);
if (arg.argc == 2) {
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
varvalue = pbx_builtin_getvar_helper(chan, varname);
if (varvalue)
ast_copy_string(buf, varvalue, len);
else
*buf = '\0';
} else if (arg.argc == 1) {
char colnames[4096];
int i;
AST_DECLARE_APP_ARGS(arg2,
AST_APP_ARG(col)[100];
);
/* Get column names, in no particular order */
hashkeys_read(chan, "HASHKEYS", arg.hashname, colnames, sizeof(colnames));
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
Mark Michelson
committed
AST_STANDARD_APP_ARGS(arg2, colnames);
*buf = '\0';
/* Now get the corresponding column values, in exactly the same order */
for (i = 0; i < arg2.argc; i++) {
snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg2.col[i]);
varvalue = pbx_builtin_getvar_helper(chan, varname);
strncat(buf, varvalue, len - strlen(buf) - 1);
strncat(buf, ",", len - strlen(buf) - 1);
}
/* Strip trailing comma */
buf[strlen(buf) - 1] = '\0';
}
return 0;
}
static struct ast_custom_function hash_function = {
.name = "HASH",
.write = hash_write,
.read = hash_read,
};
static struct ast_custom_function hashkeys_function = {
.name = "HASHKEYS",
.read = hashkeys_read,
};
static struct ast_custom_function array_function = {
.write = array,
static int quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *bufptr = buf, *dataptr = data;
if (len < 3){ /* at least two for quotes and one for binary zero */
ast_log(LOG_ERROR, "Not enough buffer");
return -1;
}
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "No argument specified!\n");
ast_copy_string(buf, "\"\"", len);
return 0;
}
for (; bufptr < buf + len - 3; dataptr++) {
if (*dataptr == '\\') {
*bufptr++ = '\\';
*bufptr++ = '\\';