Fix func_config list entry allocation
[asterisk/asterisk.git] / funcs / func_dialplan.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
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 group functions check if a dialplan entry exists
20  *
21  * \author Gregory Nietsky AKA irroot <gregory@networksentry.co.za>
22  * \author Russell Bryant <russell@digium.com>
23  * 
24  * \ingroup functions
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/app.h"
39
40 /*** DOCUMENTATION
41         <function name="DIALPLAN_EXISTS" language="en_US">
42                 <synopsis>
43                         Checks the existence of a dialplan target.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="context" required="true" />
47                         <parameter name="extension" />
48                         <parameter name="priority" />
49                 </syntax>
50                 <description>
51                         <para>This function returns <literal>1</literal> if the target exits. Otherwise, it returns <literal>0</literal>.</para>
52                 </description>
53         </function>
54         <function name="VALID_EXTEN" language="en_US">
55                 <synopsis>
56                         Determine whether an extension exists or not.
57                 </synopsis>
58                 <syntax>
59                         <parameter name="context">
60                                 <para>Defaults to the current context</para>
61                         </parameter>
62                         <parameter name="extension" required="true" />
63                         <parameter name="priority">
64                                 <para>Priority defaults to <literal>1</literal>.</para>
65                         </parameter>
66                 </syntax>
67                 <description>
68                         <para>Returns a true value if the indicated <replaceable>context</replaceable>,
69                         <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
70                         <warning><para>This function has been deprecated in favor of the <literal>DIALPLAN_EXISTS()</literal> function</para></warning>
71                 </description>
72         </function>
73  ***/
74
75 static int isexten_function_read(struct ast_channel *chan, const char *cmd, char *data,
76         char *buf, size_t len)
77 {
78         char *parse;
79         AST_DECLARE_APP_ARGS(args,
80                 AST_APP_ARG(context);
81                 AST_APP_ARG(exten);
82                 AST_APP_ARG(priority);
83         );
84
85         strcpy(buf, "0");
86
87         if (ast_strlen_zero(data)) {
88                 ast_log(LOG_ERROR, "DIALPLAN_EXISTS() requires an argument\n");
89                 return -1;
90         }
91
92         parse = ast_strdupa(data);
93         AST_STANDARD_APP_ARGS(args, parse);
94
95         if (!ast_strlen_zero(args.priority)) {
96                 int priority_num;
97                 if (sscanf(args.priority, "%30d", &priority_num) == 1 && priority_num > 0) {
98                         int res;
99                         res = ast_exists_extension(chan, args.context, args.exten, priority_num, 
100                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
101                         if (res)
102                                 strcpy(buf, "1");
103                 } else {
104                         int res;
105                         res = ast_findlabel_extension(chan, args.context, args.exten, args.priority,
106                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
107                         if (res > 0)
108                                 strcpy(buf, "1");
109                 }
110         } else if (!ast_strlen_zero(args.exten)) {
111                 int res;
112                 res = ast_exists_extension(chan, args.context, args.exten, 1, 
113                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
114                 if (res)
115                         strcpy(buf, "1");
116         } else if (!ast_strlen_zero(args.context)) {
117                 if (ast_context_find(args.context))
118                         strcpy(buf, "1");
119         } else {
120                 ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
121                 return -1;
122         }
123
124         return 0;
125 }
126
127 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
128 {
129         int priority_int;
130         AST_DECLARE_APP_ARGS(args,
131                 AST_APP_ARG(context);
132                 AST_APP_ARG(extension);
133                 AST_APP_ARG(priority);
134         );
135
136         AST_STANDARD_APP_ARGS(args, parse);
137
138         if (ast_strlen_zero(args.context)) {
139                 args.context = ast_strdupa(ast_channel_context(chan));
140         }
141
142         if (ast_strlen_zero(args.extension)) {
143                 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
144                 return -1;
145         }
146
147         if (ast_strlen_zero(args.priority)) {
148                 priority_int = 1;
149         } else {
150                 priority_int = atoi(args.priority);
151         }
152
153         if (ast_exists_extension(chan, args.context, args.extension, priority_int,
154                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
155             ast_copy_string(buffer, "1", buflen);
156         } else {
157             ast_copy_string(buffer, "0", buflen);
158         }
159
160         return 0;
161 }
162
163 static struct ast_custom_function isexten_function = {
164         .name = "DIALPLAN_EXISTS",
165         .read = isexten_function_read,
166         .read_max = 2,
167 };
168
169 static struct ast_custom_function acf_isexten = {
170         .name = "VALID_EXTEN",
171         .read = acf_isexten_exec,
172 };
173
174 static int unload_module(void)
175 {
176         int res = ast_custom_function_unregister(&isexten_function);
177         res |= ast_custom_function_unregister(&acf_isexten);
178         return res;
179 }
180
181 static int load_module(void)
182 {
183         int res = ast_custom_function_register(&isexten_function);
184         res |= ast_custom_function_register(&acf_isexten);
185         return res;
186 }
187
188 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialplan Context/Extension/Priority Checking Functions",
189         .load = load_module,
190         .unload = unload_module,
191         .load_pri = AST_MODPRI_APP_DEPEND,
192         );