]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
variables: Add additional variable dialplan functions.
authorJoshua C. Colp <jcolp@sangoma.com>
Thu, 31 Aug 2023 10:31:20 +0000 (07:31 -0300)
committerAsterisk Development Team <asteriskteam@digium.com>
Wed, 21 Feb 2024 13:35:03 +0000 (13:35 +0000)
Using the Set dialplan application does not actually
delete channel or global variables. Instead the
variables are set to an empty value.

This change adds two dialplan functions,
GLOBAL_DELETE and DELETE which can be used to
delete global and channel variables instead
of just setting them to empty.

There is also no ability within the dialplan to
determine if a global or channel variable has
actually been set or not.

This change also adds two dialplan functions,
GLOBAL_EXISTS and VARIABLE_EXISTS which can be
used to determine if a global or channel variable
has been set or not.

Resolves: #289

UserNote: Four new dialplan functions have been added.
GLOBAL_DELETE and DELETE have been added which allows
the deletion of global and channel variables.
GLOBAL_EXISTS and VARIABLE_EXISTS have been added
which checks whether a global or channel variable has
been set.

(cherry picked from commit 405a08aaed7b50aea70ac55c6288ad6e6acf09f1)

funcs/func_global.c
funcs/func_logic.c

index 4b5e0d659c452245288b28e1cb6bc3d4349d0c00..74f58663b41e1fc03595be32dc253a14a48b5bb5 100644 (file)
                        <para>Set or get the value of a global variable specified in <replaceable>varname</replaceable></para>
                </description>
        </function>
+       <function name="GLOBAL_DELETE" language="en_US">
+               <synopsis>
+                       Deletes a specified global variable.
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true">
+                               <para>Global variable name</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Delete the global variable specified in <replaceable>varname</replaceable>.
+                       Will succeed if the global variable exists or not.</para>
+               </description>
+               <see-also>
+                       <ref type="function">GLOBAL</ref>
+                       <ref type="function">DELETE</ref>
+               </see-also>
+       </function>
+       <function name="GLOBAL_EXISTS" language="en_US">
+               <synopsis>
+                       Check if a global variable exists or not.
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true">
+                               <para>Global variable name</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Returns <literal>1</literal> if global variable exists or <literal>0</literal> otherwise.</para>
+               </description>
+               <see-also>
+                       <ref type="function">VARIABLE_EXISTS</ref>
+               </see-also>
+       </function>
        <function name="SHARED" language="en_US">
                <synopsis>
                        Gets or sets the shared variable specified.
@@ -145,6 +179,33 @@ static struct ast_custom_function global_function = {
        .write = global_write,
 };
 
+static int global_delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+       pbx_builtin_setvar_helper(NULL, data, NULL);
+
+       return 0;
+}
+
+static struct ast_custom_function global_delete_function = {
+       .name = "GLOBAL_DELETE",
+       .write = global_delete_write,
+};
+
+static int global_exists_read(struct ast_channel *chan, const char *cmd, char *data,
+                 char *buf, size_t len)
+{
+       const char *var = pbx_builtin_getvar_helper(NULL, data);
+
+       strcpy(buf, var ? "1" : "0");
+
+       return 0;
+}
+
+static struct ast_custom_function global_exists_function = {
+       .name = "GLOBAL_EXISTS",
+       .read = global_exists_read,
+};
+
 static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
        struct ast_datastore *varstore;
@@ -313,6 +374,8 @@ static int unload_module(void)
        int res = 0;
 
        res |= ast_custom_function_unregister(&global_function);
+       res |= ast_custom_function_unregister(&global_delete_function);
+       res |= ast_custom_function_unregister(&global_exists_function);
        res |= ast_custom_function_unregister(&shared_function);
 
        return res;
@@ -323,6 +386,8 @@ static int load_module(void)
        int res = 0;
 
        res |= ast_custom_function_register(&global_function);
+       res |= ast_custom_function_register(&global_delete_function);
+       res |= ast_custom_function_register(&global_exists_function);
        res |= ast_custom_function_register(&shared_function);
 
        return res;
index d2677493334a305e3c8670c6a3809e76ef93100f..9aa908be92580814cfd0157c76805a1a13afc9f0 100644 (file)
                <description>
                </description>
        </function>
+       <function name="DELETE" language="en_US">
+               <synopsis>
+                       Deletes a specified channel variable.
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true">
+                               <para>Channel variable name</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Delete the channel variable specified in <replaceable>varname</replaceable>.
+                       Will succeed if the channel variable exists or not.</para>
+               </description>
+               <see-also>
+                       <ref type="function">GLOBAL_DELETE</ref>
+               </see-also>
+       </function>
+       <function name="VARIABLE_EXISTS" language="en_US">
+               <synopsis>
+                       Check if a dialplan variable exists or not.
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true">
+                               <para>Channel variable name</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Returns <literal>1</literal> if channel variable exists or <literal>0</literal> otherwise.</para>
+               </description>
+               <see-also>
+                       <ref type="function">GLOBAL_EXISTS</ref>
+               </see-also>
+       </function>
  ***/
 
 static int isnull(struct ast_channel *chan, const char *cmd, char *data,
@@ -274,6 +307,23 @@ static int import_read2(struct ast_channel *chan, const char *cmd, char *data, s
        return import_helper(chan, cmd, data, NULL, str, len);
 }
 
+static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+       pbx_builtin_setvar_helper(chan, data, NULL);
+
+       return 0;
+}
+
+static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data,
+                 char *buf, size_t len)
+{
+       const char *var = pbx_builtin_getvar_helper(chan, data);
+
+       strcpy(buf, var ? "1" : "0");
+
+       return 0;
+}
+
 static struct ast_custom_function isnull_function = {
        .name = "ISNULL",
        .read = isnull,
@@ -308,6 +358,16 @@ static struct ast_custom_function import_function = {
        .read2 = import_read2,
 };
 
+static struct ast_custom_function delete_function = {
+       .name = "DELETE",
+       .write = delete_write,
+};
+
+static struct ast_custom_function variable_exists_function = {
+       .name = "VARIABLE_EXISTS",
+       .read = variable_exists_read,
+};
+
 static int unload_module(void)
 {
        int res = 0;
@@ -318,6 +378,8 @@ static int unload_module(void)
        res |= ast_custom_function_unregister(&if_function);
        res |= ast_custom_function_unregister(&if_time_function);
        res |= ast_custom_function_unregister(&import_function);
+       res |= ast_custom_function_unregister(&delete_function);
+       res |= ast_custom_function_unregister(&variable_exists_function);
 
        return res;
 }
@@ -332,6 +394,8 @@ static int load_module(void)
        res |= ast_custom_function_register(&if_function);
        res |= ast_custom_function_register(&if_time_function);
        res |= ast_custom_function_register(&import_function);
+       res |= ast_custom_function_register(&delete_function);
+       res |= ast_custom_function_register(&variable_exists_function);
 
        return res;
 }