func_dialplan.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Dialplan group functions check if a dialplan entry exists
00020  *
00021  * \author Gregory Nietsky AKA irroot <gregory@networksentry.co.za>
00022  * \author Russell Bryant <russell@digium.com>
00023  * 
00024  * \ingroup functions
00025  */
00026 
00027 /*** MODULEINFO
00028    <support_level>core</support_level>
00029  ***/
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/app.h"
00039 
00040 /*** DOCUMENTATION
00041    <function name="DIALPLAN_EXISTS" language="en_US">
00042       <synopsis>
00043          Checks the existence of a dialplan target.
00044       </synopsis>
00045       <syntax>
00046          <parameter name="context" required="true" />
00047          <parameter name="extension" />
00048          <parameter name="priority" />
00049       </syntax>
00050       <description>
00051          <para>This function returns <literal>1</literal> if the target exits. Otherwise, it returns <literal>0</literal>.</para>
00052       </description>
00053    </function>
00054    <function name="VALID_EXTEN" language="en_US">
00055       <synopsis>
00056          Determine whether an extension exists or not.
00057       </synopsis>
00058       <syntax>
00059          <parameter name="context">
00060             <para>Defaults to the current context</para>
00061          </parameter>
00062          <parameter name="extension" required="true" />
00063          <parameter name="priority">
00064             <para>Priority defaults to <literal>1</literal>.</para>
00065          </parameter>
00066       </syntax>
00067       <description>
00068          <para>Returns a true value if the indicated <replaceable>context</replaceable>,
00069          <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
00070          <warning><para>This function has been deprecated in favor of the <literal>DIALPLAN_EXISTS()</literal> function</para></warning>
00071       </description>
00072    </function>
00073  ***/
00074 
00075 static int isexten_function_read(struct ast_channel *chan, const char *cmd, char *data,
00076    char *buf, size_t len)
00077 {
00078    char *parse;
00079    AST_DECLARE_APP_ARGS(args,
00080       AST_APP_ARG(context);
00081       AST_APP_ARG(exten);
00082       AST_APP_ARG(priority);
00083    );
00084 
00085    strcpy(buf, "0");
00086 
00087    if (ast_strlen_zero(data)) {
00088       ast_log(LOG_ERROR, "DIALPLAN_EXISTS() requires an argument\n");
00089       return -1;
00090    }
00091 
00092    parse = ast_strdupa(data);
00093    AST_STANDARD_APP_ARGS(args, parse);
00094 
00095    if (!ast_strlen_zero(args.priority)) {
00096       int priority_num;
00097       if (sscanf(args.priority, "%30d", &priority_num) == 1 && priority_num > 0) {
00098          int res;
00099          res = ast_exists_extension(chan, args.context, args.exten, priority_num,
00100             !chan ? NULL :
00101             S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
00102          if (res)
00103             strcpy(buf, "1");
00104       } else {
00105          int res;
00106          res = ast_findlabel_extension(chan, args.context, args.exten, args.priority,
00107             !chan ? NULL :
00108             S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
00109          if (res > 0)
00110             strcpy(buf, "1");
00111       }
00112    } else if (!ast_strlen_zero(args.exten)) {
00113       int res;
00114       res = ast_exists_extension(chan, args.context, args.exten, 1,
00115          !chan ? NULL :
00116          S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
00117       if (res)
00118          strcpy(buf, "1");
00119    } else if (!ast_strlen_zero(args.context)) {
00120       if (ast_context_find(args.context))
00121          strcpy(buf, "1");
00122    } else {
00123       ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
00124       return -1;
00125    }
00126 
00127    return 0;
00128 }
00129 
00130 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
00131 {
00132    int priority_int;
00133    AST_DECLARE_APP_ARGS(args,
00134       AST_APP_ARG(context);
00135       AST_APP_ARG(extension);
00136       AST_APP_ARG(priority);
00137    );
00138 
00139    if (!chan) {
00140       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00141       return -1;
00142    }
00143 
00144    AST_STANDARD_APP_ARGS(args, parse);
00145 
00146    if (ast_strlen_zero(args.context)) {
00147       args.context = ast_strdupa(ast_channel_context(chan));
00148    }
00149 
00150    if (ast_strlen_zero(args.extension)) {
00151       ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
00152       return -1;
00153    }
00154 
00155    if (ast_strlen_zero(args.priority)) {
00156       priority_int = 1;
00157    } else {
00158       priority_int = atoi(args.priority);
00159    }
00160 
00161    if (ast_exists_extension(chan, args.context, args.extension, priority_int,
00162       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00163        ast_copy_string(buffer, "1", buflen);
00164    } else {
00165        ast_copy_string(buffer, "0", buflen);
00166    }
00167 
00168    return 0;
00169 }
00170 
00171 static struct ast_custom_function isexten_function = {
00172    .name = "DIALPLAN_EXISTS",
00173    .read = isexten_function_read,
00174    .read_max = 2,
00175 };
00176 
00177 static struct ast_custom_function acf_isexten = {
00178    .name = "VALID_EXTEN",
00179    .read = acf_isexten_exec,
00180 };
00181 
00182 static int unload_module(void)
00183 {
00184    int res = ast_custom_function_unregister(&isexten_function);
00185    res |= ast_custom_function_unregister(&acf_isexten);
00186    return res;
00187 }
00188 
00189 static int load_module(void)
00190 {
00191    int res = ast_custom_function_register(&isexten_function);
00192    res |= ast_custom_function_register(&acf_isexten);
00193    return res;
00194 }
00195 
00196 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialplan Context/Extension/Priority Checking Functions",
00197    .support_level = AST_MODULE_SUPPORT_CORE,
00198    .load = load_module,
00199    .unload = unload_module,
00200    .load_pri = AST_MODPRI_APP_DEPEND,
00201    );

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6