Asterisk - The Open Source Telephony Project GIT-master-7e7a603
func_evalexten.c
Go to the documentation of this file.
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
91static 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 }
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
133 .name = "EVAL_EXTEN",
134 .read = eval_exten_read,
135};
136
137static int unload_module(void)
138{
140}
141
142static int load_module(void)
143{
145}
146
147AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Extension evaluation function");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
struct varshead * ast_channel_varshead(struct ast_channel *chan)
#define ast_channel_lock(chan)
Definition: channel.h:2922
const char * ast_channel_context(const struct ast_channel *chan)
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2923
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int eval_exten_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Extension evaluation function")
static struct ast_custom_function eval_exten_function
static int load_module(void)
static int unload_module(void)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
char * strsep(char **str, const char *delims)
#define LOG_WARNING
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_get_extension_data(char *buf, int bufsize, struct ast_channel *c, const char *context, const char *exten, int priority)
Fill a string buffer with the data at a dialplan extension.
Definition: pbx.c:8567
void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int cp2_size, size_t *used, const char *context, const char *exten, int pri)
Substitutes variables, similar to pbx_substitute_variables_helper_full, but allows passing the contex...
int pbx_parse_location(struct ast_channel *chan, char **context, char **exten, char **pri, int *ipri, int *mode, char *rest)
Parses a dialplan location into context, extension, priority.
Definition: pbx.c:8791
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Utility functions.