]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
func_channel: Add CHANNEL_EXISTS function.
authorNaveen Albert <asterisk@phreaknet.org>
Wed, 15 Sep 2021 18:21:13 +0000 (18:21 +0000)
committerN A <mail@interlinked.x10host.com>
Tue, 21 Sep 2021 23:20:35 +0000 (18:20 -0500)
Adds a function to check for the existence of a channel by
name or by UNIQUEID.

ASTERISK-29656 #close

Change-Id: Ib464e9eb6e13dc683a846286798fecff4fd943cb

doc/CHANGES-staging/func_channel.txt [new file with mode: 0644]
funcs/func_channel.c

diff --git a/doc/CHANGES-staging/func_channel.txt b/doc/CHANGES-staging/func_channel.txt
new file mode 100644 (file)
index 0000000..7f92c3e
--- /dev/null
@@ -0,0 +1,4 @@
+Subject: func_channel
+
+Adds the CHANNEL_EXISTS function to check for the existence
+of a channel by name or unique ID.
index d143fdcb06d77c0c0b6f1ee5b444bdb82c429017..312806d9668c14f5851ec967f5efdd216be7b8e0 100644 (file)
@@ -20,6 +20,7 @@
  *
  * \author Kevin P. Fleming <kpfleming@digium.com>
  * \author Ben Winslow
+ * \author Naveen Albert <asterisk@phreaknet.org>
  *
  * \ingroup functions
  */
                        will be space-delimited.</para>
                </description>
        </function>
+       <function name="CHANNEL_EXISTS" language="en_US">
+               <synopsis>
+                       Checks if the specified channel exists.
+               </synopsis>
+               <syntax>
+                       <parameter name="name_or_uid" required="true">
+                               <para>The name or unique ID of the channel to check.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Returns 1 if the channel <replaceable>name_or_uid</replaceable> exists, 0 if not.</para>
+               </description>
+       </function>
        <function name="MASTER_CHANNEL" language="en_US">
                <synopsis>
                        Gets or sets variables on the master channel
@@ -711,6 +725,28 @@ static struct ast_custom_function channels_function = {
        .read = func_channels_read,
 };
 
+static int func_chan_exists_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
+{
+       struct ast_channel *chan_found = NULL;
+
+       if (ast_strlen_zero(data)) {
+               ast_log(LOG_WARNING, "%s: Channel name or unique ID required\n", function);
+               return -1;
+       }
+
+       chan_found = ast_channel_get_by_name(data);
+       snprintf(buf, maxlen, "%d", (chan_found ? 1 : 0));
+       if (chan_found) {
+               ast_channel_unref(chan_found);
+       }
+       return 0;
+}
+
+static struct ast_custom_function chan_exists_function = {
+       .name = "CHANNEL_EXISTS",
+       .read = func_chan_exists_read,
+};
+
 static int func_mchan_read(struct ast_channel *chan, const char *function,
                             char *data, struct ast_str **buf, ssize_t len)
 {
@@ -761,6 +797,7 @@ static int unload_module(void)
 
        res |= ast_custom_function_unregister(&channel_function);
        res |= ast_custom_function_unregister(&channels_function);
+       res |= ast_custom_function_unregister(&chan_exists_function);
        res |= ast_custom_function_unregister(&mchan_function);
 
        return res;
@@ -772,6 +809,7 @@ static int load_module(void)
 
        res |= ast_custom_function_register(&channel_function);
        res |= ast_custom_function_register(&channels_function);
+       res |= ast_custom_function_register(&chan_exists_function);
        res |= ast_custom_function_register(&mchan_function);
 
        return res;