Skip to content

Commit 6ddb0ec

Browse files
InterLinked1Friendly Automation
authored and
Friendly Automation
committed
func_evalexten: Extension evaluation function.
This adds the EVAL_EXTEN function, which may be used to retrieve the variable-substituted data at any extension. ASTERISK-29486 Change-Id: Iad81019689674c9f4ac77d235f5d7234adbb1432
1 parent ce7846e commit 6ddb0ec

File tree

5 files changed

+219
-27
lines changed

5 files changed

+219
-27
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Subject: func_evalexten
2+
3+
This adds the EVAL_EXTEN function which may be
4+
used to evaluate data at dialplan extensions.

funcs/func_evalexten.c

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Asterisk -- An open source telephony toolkit.
3+
*
4+
* Copyright (C) 2021, Naveen Albert
5+
*
6+
* See http://www.asterisk.org for more information about
7+
* the Asterisk project. Please do not directly contact
8+
* any of the maintainers of this project for assistance;
9+
* the project provides a web site, mailing lists and IRC
10+
* channels for your use.
11+
*
12+
* This program is free software, distributed under the terms of
13+
* the GNU General Public License Version 2. See the LICENSE file
14+
* at the top of the source tree.
15+
*/
16+
17+
/*! \file
18+
*
19+
* \brief Dialplan extension evaluation function
20+
*
21+
* \author Naveen Albert <asterisk@phreaknet.org>
22+
*
23+
* \ingroup functions
24+
*/
25+
26+
/*** MODULEINFO
27+
<support_level>extended</support_level>
28+
***/
29+
30+
#include "asterisk.h"
31+
32+
#include "asterisk/module.h"
33+
#include "asterisk/channel.h"
34+
#include "asterisk/pbx.h"
35+
#include "asterisk/utils.h"
36+
#include "asterisk/app.h"
37+
38+
/*** DOCUMENTATION
39+
<function name="EVAL_EXTEN" language="en_US">
40+
<synopsis>
41+
Evaluates the contents of a dialplan extension and returns it as a string.
42+
</synopsis>
43+
<syntax>
44+
<parameter name="context" />
45+
<parameter name="extensions" />
46+
<parameter name="priority" required="true" />
47+
</syntax>
48+
<description>
49+
<para>The EVAL_EXTEN function looks up a dialplan entry by context,extension,priority,
50+
evaluates the contents of a Return statement to resolve any variable or function
51+
references, and returns the result as a string.</para>
52+
<para>You can use this function to create simple user-defined lookup tables or
53+
user-defined functions.</para>
54+
<example title="Custom dialplan functions">
55+
[call-types]
56+
exten => _1NNN,1,Return(internal)
57+
exten => _NXXNXXXXXX,1,Return(external)
58+
59+
[udf]
60+
exten => calleridlen,1,Return(${LEN(${CALLERID(num)})})
61+
62+
[default]
63+
exten => _X!,1,Verbose(Call type ${EVAL_EXTEN(call-types,${EXTEN},1)} - ${EVAL_EXTEN(udf,calleridlen,1)})
64+
</example>
65+
<para>Any variables in the evaluated data will be resolved in the context of
66+
that extension. For example, <literal>${EXTEN}</literal> would refer to the
67+
EVAL_EXTEN extension, not the extension in the context invoking the function.
68+
This behavior is similar to other applications, e.g. <literal>Gosub</literal>.</para>
69+
<example title="Choosing which prompt to use">
70+
same => n,Read(input,${EVAL_EXTEN(prompts,${CALLERID(num)},1)})
71+
72+
[prompts]
73+
exten => _X!,1,Return(default)
74+
exten => _20X,1,Return(welcome)
75+
exten => _2XX,1,Return(${DB(promptsettings/${EXTEN})})
76+
exten => _3XX,1,Return(${ODBC_MYFUNC(${EXTEN})})
77+
</example>
78+
<para>Extensions on which EVAL_EXTEN is invoked are not different from other
79+
extensions. However, the application at that extension is not executed.
80+
Only the application data is parsed and evaluated.</para>
81+
<para>A limitation of this function is that the application at the specified
82+
extension isn't actually executed, and thus unlike a Gosub, you can't pass
83+
arguments in the EVAL_EXTEN function.</para>
84+
</description>
85+
<see-also>
86+
<ref type="function">EVAL</ref>
87+
</see-also>
88+
</function>
89+
***/
90+
91+
static int eval_exten_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
92+
{
93+
char *exten, *pri, *context, *parse;
94+
int ipri;
95+
char tmpbuf[len];
96+
97+
if (ast_strlen_zero(data)) {
98+
ast_log(LOG_WARNING, "The EVAL_EXTEN function requires an extension\n");
99+
return -1;
100+
}
101+
102+
parse = ast_strdupa(data);
103+
/* Split context,exten,pri */
104+
context = strsep(&parse, ",");
105+
exten = strsep(&parse, ",");
106+
pri = strsep(&parse, ",");
107+
108+
if (pbx_parse_location(chan, &context, &exten, &pri, &ipri, NULL, NULL)) {
109+
return -1;
110+
}
111+
112+
if (ast_strlen_zero(exten) || ast_strlen_zero(context)) { /* only lock if we really need to */
113+
ast_channel_lock(chan);
114+
if (ast_strlen_zero(exten)) {
115+
exten = ast_strdupa(ast_channel_exten(chan));
116+
}
117+
if (ast_strlen_zero(context)) {
118+
context = ast_strdupa(ast_channel_context(chan));
119+
}
120+
ast_channel_unlock(chan);
121+
}
122+
123+
if (ast_get_extension_data(tmpbuf, len, chan, context, exten, ipri)) {
124+
return -1; /* EVAL_EXTEN failed */
125+
}
126+
127+
pbx_substitute_variables_helper_full_location(chan, (chan) ? ast_channel_varshead(chan) : NULL, tmpbuf, buf, len, NULL, context, exten, ipri);
128+
129+
return 0;
130+
}
131+
132+
static struct ast_custom_function eval_exten_function = {
133+
.name = "EVAL_EXTEN",
134+
.read = eval_exten_read,
135+
};
136+
137+
static int unload_module(void)
138+
{
139+
return ast_custom_function_unregister(&eval_exten_function);
140+
}
141+
142+
static int load_module(void)
143+
{
144+
return ast_custom_function_register(&eval_exten_function);
145+
}
146+
147+
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Extension evaluation function");

include/asterisk/pbx.h

+19
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,25 @@ int ast_explicit_goto(struct ast_channel *chan, const char *context, const char
15011501
*/
15021502
int ast_async_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority);
15031503

1504+
/*!
1505+
* \brief Parses a dialplan location into context, extension, priority
1506+
*
1507+
* \param chan Channel to execute on
1508+
* \param context Pointer to initial value for context.
1509+
* \param exten Pointer to initial value for exten.
1510+
* \param pri Pointer to initial value for pri
1511+
* \param ipri Pointer to integer value of priority
1512+
* \param mode Pointer to mode (or NULL if mode is not used)
1513+
* \param rest Pointer to buffer to capture rest of parsing (or NULL if not used)
1514+
*
1515+
* strsep should be used to initially populate context, exten, and pri prior
1516+
* to calling this function. All arguments are modified in place.
1517+
*
1518+
* \retval 0 success
1519+
* \retval non-zero failure
1520+
*/
1521+
int pbx_parse_location(struct ast_channel *chan, char **context, char **exten, char **pri, int *ipri, int *mode, char *rest);
1522+
15041523
struct ast_custom_function* ast_custom_function_find(const char *name);
15051524

15061525
/*!

main/pbx.c

+44-25
Original file line numberDiff line numberDiff line change
@@ -8826,6 +8826,47 @@ int ast_async_goto_if_exists(struct ast_channel *chan, const char * context, con
88268826
return __ast_goto_if_exists(chan, context, exten, priority, 1);
88278827
}
88288828

8829+
int pbx_parse_location(struct ast_channel *chan, char **contextp, char **extenp, char **prip, int *ipri, int *mode, char *rest)
8830+
{
8831+
char *context, *exten, *pri;
8832+
/* do the strsep before here, so we don't have to alloc and free */
8833+
if (!*extenp) {
8834+
/* Only a priority in this one */
8835+
*prip = *contextp;
8836+
*extenp = NULL;
8837+
*contextp = NULL;
8838+
} else if (!*prip) {
8839+
/* Only an extension and priority in this one */
8840+
*prip = *extenp;
8841+
*extenp = *contextp;
8842+
*contextp = NULL;
8843+
}
8844+
context = *contextp;
8845+
exten = *extenp;
8846+
pri = *prip;
8847+
if (mode) {
8848+
if (*pri == '+') {
8849+
*mode = 1;
8850+
pri++;
8851+
} else if (*pri == '-') {
8852+
*mode = -1;
8853+
pri++;
8854+
}
8855+
}
8856+
if ((rest && sscanf(pri, "%30d%1s", ipri, rest) != 1) || sscanf(pri, "%30d", ipri) != 1) {
8857+
*ipri = ast_findlabel_extension(chan, context ? context : ast_channel_context(chan),
8858+
exten ? exten : ast_channel_exten(chan), pri,
8859+
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
8860+
if (*ipri < 1) {
8861+
ast_log(LOG_WARNING, "Priority '%s' must be a number > 0, or valid label\n", pri);
8862+
return -1;
8863+
} else if (mode) {
8864+
*mode = 0;
8865+
}
8866+
}
8867+
return 0;
8868+
}
8869+
88298870
static int pbx_parseable_goto(struct ast_channel *chan, const char *goto_string, int async)
88308871
{
88318872
char *exten, *pri, *context;
@@ -8842,31 +8883,9 @@ static int pbx_parseable_goto(struct ast_channel *chan, const char *goto_string,
88428883
context = strsep(&stringp, ","); /* guaranteed non-null */
88438884
exten = strsep(&stringp, ",");
88448885
pri = strsep(&stringp, ",");
8845-
if (!exten) { /* Only a priority in this one */
8846-
pri = context;
8847-
exten = NULL;
8848-
context = NULL;
8849-
} else if (!pri) { /* Only an extension and priority in this one */
8850-
pri = exten;
8851-
exten = context;
8852-
context = NULL;
8853-
}
8854-
if (*pri == '+') {
8855-
mode = 1;
8856-
pri++;
8857-
} else if (*pri == '-') {
8858-
mode = -1;
8859-
pri++;
8860-
}
8861-
if (sscanf(pri, "%30d%1s", &ipri, rest) != 1) {
8862-
ipri = ast_findlabel_extension(chan, context ? context : ast_channel_context(chan),
8863-
exten ? exten : ast_channel_exten(chan), pri,
8864-
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
8865-
if (ipri < 1) {
8866-
ast_log(LOG_WARNING, "Priority '%s' must be a number > 0, or valid label\n", pri);
8867-
return -1;
8868-
} else
8869-
mode = 0;
8886+
8887+
if (pbx_parse_location(chan, &context, &exten, &pri, &ipri, &mode, rest)) {
8888+
return -1;
88708889
}
88718890
/* At this point we have a priority and maybe an extension and a context */
88728891

main/pbx_variables.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct
736736

737737
/* Substitute if necessary */
738738
if (needsub) {
739-
pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL);
739+
pbx_substitute_variables_helper_full_location(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL, context, exten, pri);
740740
vars = ltmp;
741741
} else {
742742
vars = var;
@@ -770,10 +770,13 @@ void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct
770770
/* For dialplan location, if we were told what to substitute explicitly, use that instead */
771771
if (exten && !strcmp(vars, "EXTEN")) {
772772
ast_copy_string(workspace, exten, VAR_BUF_SIZE);
773+
cp4 = workspace;
773774
} else if (context && !strcmp(vars, "CONTEXT")) {
774775
ast_copy_string(workspace, context, VAR_BUF_SIZE);
776+
cp4 = workspace;
775777
} else if (pri && !strcmp(vars, "PRIORITY")) {
776778
snprintf(workspace, VAR_BUF_SIZE, "%d", pri);
779+
cp4 = workspace;
777780
} else {
778781
pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp);
779782
}
@@ -829,7 +832,7 @@ void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct
829832

830833
/* Substitute if necessary */
831834
if (needsub) {
832-
pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL);
835+
pbx_substitute_variables_helper_full_location(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL, context, exten, pri);
833836
vars = ltmp;
834837
} else {
835838
vars = var;

0 commit comments

Comments
 (0)