Make evaluation of channel variables consistently case-sensitive.
[asterisk/asterisk.git] / funcs / func_enum.c
index fbe51de..4b5fb9f 100644 (file)
  * \ingroup functions
  */
 
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -43,10 +47,112 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/lock.h"
 #include "asterisk/file.h"
-#include "asterisk/options.h"
 #include "asterisk/enum.h"
 #include "asterisk/app.h"
 
+/*** DOCUMENTATION
+       <function name="ENUMQUERY" language="en_US">
+               <synopsis>
+                       Initiate an ENUM query.
+               </synopsis>
+               <syntax>
+                       <parameter name="number" required="true" />
+                       <parameter name="method-type">
+                               <para>If no <replaceable>method-type</replaceable> is given, the default will be
+                               <literal>sip</literal>.</para>
+                       </parameter>
+                       <parameter name="zone-suffix">
+                               <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
+                               <literal>e164.arpa</literal></para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This will do a ENUM lookup of the given phone number.</para>
+               </description>
+       </function>
+       <function name="ENUMRESULT" language="en_US">
+               <synopsis>
+                       Retrieve results from a ENUMQUERY.
+               </synopsis>
+               <syntax>
+                       <parameter name="id" required="true">
+                               <para>The identifier returned by the ENUMQUERY function.</para>
+                       </parameter>
+                       <parameter name="resultnum" required="true">
+                               <para>The number of the result that you want to retrieve.</para>
+                               <para>Results start at <literal>1</literal>. If this argument is specified
+                               as <literal>getnum</literal>, then it will return the total number of results 
+                               that are available or -1 on error.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This function will retrieve results from a previous use
+                       of the ENUMQUERY function.</para>
+               </description>
+       </function>     
+       <function name="ENUMLOOKUP" language="en_US">
+               <synopsis>
+                       General or specific querying of NAPTR records for ENUM or ENUM-like DNS pointers.
+               </synopsis>
+               <syntax>
+                       <parameter name="number" required="true" />
+                       <parameter name="method-type">
+                               <para>If no <replaceable>method-type</replaceable> is given, the default will be
+                                <literal>sip</literal>.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="c">
+                                               <para>Returns an integer count of the number of NAPTRs of a certain RR type.</para>
+                                               <para>Combination of <literal>c</literal> and Method-type of <literal>ALL</literal> will
+                                               return a count of all NAPTRs for the record or -1 on error.</para>
+                                       </option>
+                                       <option name="u">
+                                               <para>Returns the full URI and does not strip off the URI-scheme.</para>
+                                       </option>
+                                       <option name="s">
+                                               <para>Triggers ISN specific rewriting.</para>
+                                       </option>
+                                       <option name="i">
+                                               <para>Looks for branches into an Infrastructure ENUM tree.</para>
+                                       </option>
+                                       <option name="d">
+                                               <para>for a direct DNS lookup without any flipping of digits.</para>
+                                       </option>
+                               </optionlist>   
+                       </parameter>
+                       <parameter name="record#">
+                               <para>If no <replaceable>record#</replaceable> is given, 
+                               defaults to <literal>1</literal>.</para>
+                       </parameter>
+                       <parameter name="zone-suffix">
+                               <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
+                               <literal>e164.arpa</literal></para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>For more information see <filename>doc/AST.pdf</filename>.</para>
+               </description>
+       </function>
+       <function name="TXTCIDNAME" language="en_US">
+               <synopsis>
+                       TXTCIDNAME looks up a caller name via DNS.
+               </synopsis>
+               <syntax>
+                       <parameter name="number" required="true" />
+                       <parameter name="zone-suffix">
+                               <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
+                               <literal>e164.arpa</literal></para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This function looks up the given phone number in DNS to retrieve
+                       the caller id name.  The result will either be blank or be the value
+                       found in the TXT record in DNS.</para>
+               </description>
+       </function>
+ ***/
+
 static char *synopsis = "Syntax: ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])\n";
 
 static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
@@ -59,7 +165,6 @@ static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
                AST_APP_ARG(record);
                AST_APP_ARG(zone);
        );
-       int res = 0;
        char tech[80];
        char dest[256] = "", tmp[2] = "", num[AST_MAX_EXTENSION] = "";
        char *s, *p;
@@ -68,49 +173,53 @@ static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
        buf[0] = '\0';
 
        if (ast_strlen_zero(data)) {
-               ast_log(LOG_WARNING, synopsis);
+               ast_log(LOG_WARNING, "%s", synopsis);
                return -1;
        }
 
        AST_STANDARD_APP_ARGS(args, data);
 
        if (args.argc < 1) {
-               ast_log(LOG_WARNING, synopsis);
+               ast_log(LOG_WARNING, "%s", synopsis);
                return -1;
        }
 
-       ast_copy_string(tech, args.tech ? args.tech : "sip", sizeof(tech));
+       if (args.tech && !ast_strlen_zero(args.tech)) {
+               ast_copy_string(tech,args.tech, sizeof(tech));
+       } else {
+               ast_copy_string(tech,"sip",sizeof(tech));
+       }
 
-       if (!args.zone)
+       if (!args.zone) {
                args.zone = "e164.arpa";
-
-       if (!args.options)
+       }
+       if (!args.options) {
                args.options = "";
-
-       if (args.record)
-               record = atoi(args.record);
+       }
+       if (args.record) {
+               record = atoi(args.record) ? atoi(args.record) : record;
+       }
 
        /* strip any '-' signs from number */
        for (s = p = args.number; *s; s++) {
                if (*s != '-') {
                        snprintf(tmp, sizeof(tmp), "%c", *s);
-                       strncat(num, tmp, sizeof(num));
+                       strncat(num, tmp, sizeof(num) - strlen(num) - 1);
                }
 
        }
-
-       res = ast_get_enum(chan, num, dest, sizeof(dest), tech, sizeof(tech), args.zone, args.options, 1, NULL);
+       ast_get_enum(chan, num, dest, sizeof(dest), tech, sizeof(tech), args.zone, args.options, record, NULL);
 
        p = strchr(dest, ':');
-       if (p && strcasecmp(tech, "ALL"))
+       if (p && strcasecmp(tech, "ALL") && !strchr(args.options, 'u')) {
                ast_copy_string(buf, p + 1, len);
-       else
+       } else {
                ast_copy_string(buf, dest, len);
-
+       }
        return 0;
 }
 
-unsigned int enum_datastore_id;
+static unsigned int enum_datastore_id;
 
 struct enum_result_datastore {
        struct enum_context *context;
@@ -137,7 +246,7 @@ static void erds_destroy_cb(void *data)
        erds_destroy(erds);
 }
 
-const struct ast_datastore_info enum_result_datastore_info = {
+static const struct ast_datastore_info enum_result_datastore_info = {
        .type = "ENUMQUERY",
        .destroy = erds_destroy_cb,
 }; 
@@ -186,7 +295,7 @@ static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data
 
        snprintf(buf, len, "%u", erds->id);
 
-       if (!(datastore = ast_channel_datastore_alloc(&enum_result_datastore_info, buf))) {
+       if (!(datastore = ast_datastore_alloc(&enum_result_datastore_info, buf))) {
                ast_free(erds->context);
                ast_free(erds);
                goto finish;
@@ -254,12 +363,12 @@ static int enum_result_read(struct ast_channel *chan, const char *cmd, char *dat
        erds = datastore->data;
 
        if (!strcasecmp(args.resultnum, "getnum")) {
-               snprintf(buf, len, "%u", erds->context->naptr_rrs_count);
+               snprintf(buf, len, "%d", erds->context->naptr_rrs_count);
                res = 0;
                goto finish;
        }
 
-       if (sscanf(args.resultnum, "%u", &num) != 1) {
+       if (sscanf(args.resultnum, "%30u", &num) != 1) {
                ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to ENUMRESULT!\n", args.resultnum);
                goto finish;
        }
@@ -292,76 +401,52 @@ finish:
 
 static struct ast_custom_function enum_query_function = {
        .name = "ENUMQUERY",
-       .synopsis = "Initiate an ENUM query",
-       .syntax = "ENUMQUERY(number[,Method-type[,zone-suffix]])",
-       .desc = "This will do a ENUM lookup of the given phone number.\n"
-       "If no method-tpye is given, the default will be sip. If no\n"
-       "zone-suffix is given, the default will be \"e164.arpa\".\n"
-       "The result of this function will be a numeric ID that can\n"
-       "be used to retrieve the results using the ENUMRESULT function.\n",
        .read = enum_query_read,
 };
 
 static struct ast_custom_function enum_result_function = {
        .name = "ENUMRESULT",
-       .synopsis = "Retrieve results from a ENUMQUERY",
-       .syntax = "ENUMRESULT(id,resultnum)",
-       .desc = "This function will retrieve results from a previous use\n"
-       "of the ENUMQUERY function.\n"
-       "  id - This argument is the identifier returned by the ENUMQUERY function.\n"
-       "  resultnum - This is the number of the result that you want to retrieve.\n"
-       "       Results start at 1.  If this argument is specified as \"getnum\",\n"
-       "       then it will return the total number of results that are available.\n",
        .read = enum_result_read,
 };
 
 static struct ast_custom_function enum_function = {
        .name = "ENUMLOOKUP",
-       .synopsis =
-               "General or specific querying of NAPTR records for ENUM or ENUM-like DNS pointers",
-       .syntax =
-               "ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])",
-       .desc =
-               "Option 'c' returns an integer count of the number of NAPTRs of a certain RR type.\n"
-               "Combination of 'c' and Method-type of 'ALL' will return a count of all NAPTRs for the record.\n"
-               "Defaults are: Method-type=sip, no options, record=1, zone-suffix=e164.arpa\n\n"
-               "For more information, see doc/asterisk.pdf",
        .read = function_enum,
 };
 
 static int function_txtcidname(struct ast_channel *chan, const char *cmd,
                               char *data, char *buf, size_t len)
 {
-       int res;
-       char tech[80];
-       char txt[256] = "";
-       char dest[80];
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(number);
+               AST_APP_ARG(zone);
+       );
 
        buf[0] = '\0';
 
-
        if (ast_strlen_zero(data)) {
-               ast_log(LOG_WARNING, "TXTCIDNAME requires an argument (number)\n");
+               ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
+               return -1;
+       }
+
+       AST_STANDARD_APP_ARGS(args, data);
+
+       if (args.argc < 1) {
+               ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
                return -1;
        }
 
-       res = ast_get_txt(chan, data, dest, sizeof(dest), tech, sizeof(tech), txt,
-                         sizeof(txt));
+       if (!args.zone) {
+               args.zone = "e164.arpa";
+       }
 
-       if (!ast_strlen_zero(txt))
-               ast_copy_string(buf, txt, len);
+       ast_get_txt(chan, args.number, buf, len, args.zone);
 
        return 0;
 }
 
 static struct ast_custom_function txtcidname_function = {
        .name = "TXTCIDNAME",
-       .synopsis = "TXTCIDNAME looks up a caller name via DNS",
-       .syntax = "TXTCIDNAME(<number>)",
-       .desc =
-               "This function looks up the given phone number in DNS to retrieve\n"
-               "the caller id name.  The result will either be blank or be the value\n"
-               "found in the TXT record in DNS.\n",
        .read = function_txtcidname,
 };