0f3c0f823b947219cbea173ed790a031212e4ba5
[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                                 !chan ? NULL :
101                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
102                         if (res)
103                                 strcpy(buf, "1");
104                 } else {
105                         int res;
106                         res = ast_findlabel_extension(chan, args.context, args.exten, args.priority,
107                                 !chan ? NULL :
108                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
109                         if (res > 0)
110                                 strcpy(buf, "1");
111                 }
112         } else if (!ast_strlen_zero(args.exten)) {
113                 int res;
114                 res = ast_exists_extension(chan, args.context, args.exten, 1,
115                         !chan ? NULL :
116                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
117                 if (res)
118                         strcpy(buf, "1");
119         } else if (!ast_strlen_zero(args.context)) {
120                 if (ast_context_find(args.context))
121                         strcpy(buf, "1");
122         } else {
123                 ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
124                 return -1;
125         }
126
127         return 0;
128 }
129
130 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
131 {
132         int priority_int;
133         AST_DECLARE_APP_ARGS(args,
134                 AST_APP_ARG(context);
135                 AST_APP_ARG(extension);
136                 AST_APP_ARG(priority);
137         );
138
139         if (!chan) {
140                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
141                 return -1;
142         }
143
144         AST_STANDARD_APP_ARGS(args, parse);
145
146         if (ast_strlen_zero(args.context)) {
147                 args.context = ast_strdupa(ast_channel_context(chan));
148         }
149
150         if (ast_strlen_zero(args.extension)) {
151                 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
152                 return -1;
153         }
154
155         if (ast_strlen_zero(args.priority)) {
156                 priority_int = 1;
157         } else {
158                 priority_int = atoi(args.priority);
159         }
160
161         if (ast_exists_extension(chan, args.context, args.extension, priority_int,
162                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
163             ast_copy_string(buffer, "1", buflen);
164         } else {
165             ast_copy_string(buffer, "0", buflen);
166         }
167
168         return 0;
169 }
170
171 static struct ast_custom_function isexten_function = {
172         .name = "DIALPLAN_EXISTS",
173         .read = isexten_function_read,
174         .read_max = 2,
175 };
176
177 static struct ast_custom_function acf_isexten = {
178         .name = "VALID_EXTEN",
179         .read = acf_isexten_exec,
180 };
181
182 static int unload_module(void)
183 {
184         int res = ast_custom_function_unregister(&isexten_function);
185         res |= ast_custom_function_unregister(&acf_isexten);
186         return res;
187 }
188
189 static int load_module(void)
190 {
191         int res = ast_custom_function_register(&isexten_function);
192         res |= ast_custom_function_register(&acf_isexten);
193         return res;
194 }
195
196 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialplan Context/Extension/Priority Checking Functions",
197         .support_level = AST_MODULE_SUPPORT_CORE,
198         .load = load_module,
199         .unload = unload_module,
200         .load_pri = AST_MODPRI_APP_DEPEND,
201         );