Allow built in variables to be used with dynamic weights.
authorLeif Madsen <leif@leifmadsen.com>
Mon, 7 Nov 2011 21:58:14 +0000 (21:58 +0000)
committerLeif Madsen <leif@leifmadsen.com>
Mon, 7 Nov 2011 21:58:14 +0000 (21:58 +0000)
You can now use the built in variables , , and
within a dynamic weight. For example, this could be useful when you want
to pass requested lookup number to the SHELL() function which could be
used to execute a script to dynamically set the weight of the result.

(Closes issue ASTERISK-13657)
Reported by: Joel Vandal
Tested by: Leif Madsen, Russell Bryant
Patches:
     asterisk-1.6-dundi-varhead.patch uploaded by Joel Vandal (License #5374)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@343693 65c4cc65-6c06-0410-ace0-fbb531ad65f3

CHANGES
configs/dundi.conf.sample
pbx/pbx_dundi.c

diff --git a/CHANGES b/CHANGES
index 49e5843..04de88c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -50,6 +50,11 @@ FAX changes
  * FAXOPT(faxdetect) will enable a generic fax detect framehook for dialplan
    control of faxdetect.
 
  * FAXOPT(faxdetect) will enable a generic fax detect framehook for dialplan
    control of faxdetect.
 
+DUNDi changes
+-------------
+ * Allow the built in variables ${NUMBER}, ${IPADDR} and ${SECRET} to be
+   used within the dynamic weight attribute when specifying a mapping.
+
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 1.8 to Asterisk 10 -------------------
 ------------------------------------------------------------------------------
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 1.8 to Asterisk 10 -------------------
 ------------------------------------------------------------------------------
index 4733733..70f97d4 100644 (file)
@@ -143,6 +143,10 @@ autokill=yes
 ;    in a response shouuld be.
 ;testmap3 => context3,${SHELL(echo 123)},IAX2,guest@peer3/${NUMBER}
 ;
 ;    in a response shouuld be.
 ;testmap3 => context3,${SHELL(echo 123)},IAX2,guest@peer3/${NUMBER}
 ;
+; The built in variables ${SECRET}, ${IPADDR} and ${NUMBER} can also be
+; passed to the weight. For example, you could pass the ${NUMBER} value
+; to your SHELL() script and use that to dynamically return a weight.
+;
 ; Note than when using a global variable or dialplan function to set the
 ; weight for a mapping, that response caching should be disabled if you
 ; plan for these values to change frequently at all.  If the results are
 ; Note than when using a global variable or dialplan function to set the
 ; weight for a mapping, that response caching should be disabled if you
 ; plan for these values to change frequently at all.  If the results are
index f2f5569..7eafaa5 100644 (file)
@@ -545,13 +545,18 @@ struct dundi_query_state {
        char fluffy[0];
 };
 
        char fluffy[0];
 };
 
-static int get_mapping_weight(struct dundi_mapping *map)
+static int get_mapping_weight(struct dundi_mapping *map, struct varshead *headp)
 {
        char buf[32];
 
        buf[0] = 0;
        if (map->weightstr) {
 {
        char buf[32];
 
        buf[0] = 0;
        if (map->weightstr) {
-               pbx_substitute_variables_helper(NULL, map->weightstr, buf, sizeof(buf) - 1);
+               if (headp) {
+                       pbx_substitute_variables_varshead(headp, map->weightstr, buf, sizeof(buf) - 1);
+               } else {                
+                       pbx_substitute_variables_helper(NULL, map->weightstr, buf, sizeof(buf) - 1);
+               }
+
                if (sscanf(buf, "%30d", &map->_weight) != 1)
                        map->_weight = MAX_WEIGHT;
        }
                if (sscanf(buf, "%30d", &map->_weight) != 1)
                        map->_weight = MAX_WEIGHT;
        }
@@ -587,7 +592,6 @@ static int dundi_lookup_local(struct dundi_result *dr, struct dundi_mapping *map
                        ast_set_flag(&flags, map->options & 0xffff);
                        ast_copy_flags(dr + anscnt, &flags, AST_FLAGS_ALL);
                        dr[anscnt].techint = map->tech;
                        ast_set_flag(&flags, map->options & 0xffff);
                        ast_copy_flags(dr + anscnt, &flags, AST_FLAGS_ALL);
                        dr[anscnt].techint = map->tech;
-                       dr[anscnt].weight = get_mapping_weight(map);
                        dr[anscnt].expiration = dundi_cache_time;
                        ast_copy_string(dr[anscnt].tech, tech2str(map->tech), sizeof(dr[anscnt].tech));
                        dr[anscnt].eid = *us_eid;
                        dr[anscnt].expiration = dundi_cache_time;
                        ast_copy_string(dr[anscnt].tech, tech2str(map->tech), sizeof(dr[anscnt].tech));
                        dr[anscnt].eid = *us_eid;
@@ -603,10 +607,13 @@ static int dundi_lookup_local(struct dundi_result *dr, struct dundi_mapping *map
                                newvariable = ast_var_assign("IPADDR", ipaddr);
                                AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
                                pbx_substitute_variables_varshead(&headp, map->dest, dr[anscnt].dest, sizeof(dr[anscnt].dest));
                                newvariable = ast_var_assign("IPADDR", ipaddr);
                                AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
                                pbx_substitute_variables_varshead(&headp, map->dest, dr[anscnt].dest, sizeof(dr[anscnt].dest));
+                               dr[anscnt].weight = get_mapping_weight(map, &headp);
                                while ((newvariable = AST_LIST_REMOVE_HEAD(&headp, entries)))
                                        ast_var_delete(newvariable);
                                while ((newvariable = AST_LIST_REMOVE_HEAD(&headp, entries)))
                                        ast_var_delete(newvariable);
-                       } else
+                       } else {
                                dr[anscnt].dest[0] = '\0';
                                dr[anscnt].dest[0] = '\0';
+                               dr[anscnt].weight = get_mapping_weight(map, NULL);
+                       }
                        anscnt++;
                } else {
                        /* No answers...  Find the fewest number of digits from the
                        anscnt++;
                } else {
                        /* No answers...  Find the fewest number of digits from the
@@ -2836,7 +2843,7 @@ static char *dundi_show_mappings(struct ast_cli_entry *e, int cmd, struct ast_cl
        AST_LIST_LOCK(&peers);
        ast_cli(a->fd, FORMAT2, "DUNDi Cntxt", "Weight", "Local Cntxt", "Options", "Tech", "Destination");
        AST_LIST_TRAVERSE(&mappings, map, list) {
        AST_LIST_LOCK(&peers);
        ast_cli(a->fd, FORMAT2, "DUNDi Cntxt", "Weight", "Local Cntxt", "Options", "Tech", "Destination");
        AST_LIST_TRAVERSE(&mappings, map, list) {
-               snprintf(weight, sizeof(weight), "%d", get_mapping_weight(map));
+               snprintf(weight, sizeof(weight), "%d", get_mapping_weight(map, NULL));
                ast_cli(a->fd, FORMAT, map->dcontext, weight,
                        ast_strlen_zero(map->lcontext) ? "<none>" : map->lcontext,
                        dundi_flags2str(fs, sizeof(fs), map->options), tech2str(map->tech), map->dest);
                ast_cli(a->fd, FORMAT, map->dcontext, weight,
                        ast_strlen_zero(map->lcontext) ? "<none>" : map->lcontext,
                        dundi_flags2str(fs, sizeof(fs), map->options), tech2str(map->tech), map->dest);