Skip to content

Commit de6ecd5

Browse files
InterLinked1Friendly Automation
authored andcommitted
func_channel: Add CHANNEL_EXISTS function.
Adds a function to check for the existence of a channel by name or by UNIQUEID. ASTERISK-29656 #close Change-Id: Ib464e9eb6e13dc683a846286798fecff4fd943cb
1 parent 5abf499 commit de6ecd5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

doc/CHANGES-staging/func_channel.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Subject: func_channel
2+
3+
Adds the CHANNEL_EXISTS function to check for the existence
4+
of a channel by name or unique ID.

funcs/func_channel.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* \author Kevin P. Fleming <kpfleming@digium.com>
2222
* \author Ben Winslow
23+
* \author Naveen Albert <asterisk@phreaknet.org>
2324
*
2425
* \ingroup functions
2526
*/
@@ -62,6 +63,19 @@
6263
will be space-delimited.</para>
6364
</description>
6465
</function>
66+
<function name="CHANNEL_EXISTS" language="en_US">
67+
<synopsis>
68+
Checks if the specified channel exists.
69+
</synopsis>
70+
<syntax>
71+
<parameter name="name_or_uid" required="true">
72+
<para>The name or unique ID of the channel to check.</para>
73+
</parameter>
74+
</syntax>
75+
<description>
76+
<para>Returns 1 if the channel <replaceable>name_or_uid</replaceable> exists, 0 if not.</para>
77+
</description>
78+
</function>
6579
<function name="MASTER_CHANNEL" language="en_US">
6680
<synopsis>
6781
Gets or sets variables on the master channel
@@ -711,6 +725,28 @@ static struct ast_custom_function channels_function = {
711725
.read = func_channels_read,
712726
};
713727

728+
static int func_chan_exists_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
729+
{
730+
struct ast_channel *chan_found = NULL;
731+
732+
if (ast_strlen_zero(data)) {
733+
ast_log(LOG_WARNING, "%s: Channel name or unique ID required\n", function);
734+
return -1;
735+
}
736+
737+
chan_found = ast_channel_get_by_name(data);
738+
snprintf(buf, maxlen, "%d", (chan_found ? 1 : 0));
739+
if (chan_found) {
740+
ast_channel_unref(chan_found);
741+
}
742+
return 0;
743+
}
744+
745+
static struct ast_custom_function chan_exists_function = {
746+
.name = "CHANNEL_EXISTS",
747+
.read = func_chan_exists_read,
748+
};
749+
714750
static int func_mchan_read(struct ast_channel *chan, const char *function,
715751
char *data, struct ast_str **buf, ssize_t len)
716752
{
@@ -761,6 +797,7 @@ static int unload_module(void)
761797

762798
res |= ast_custom_function_unregister(&channel_function);
763799
res |= ast_custom_function_unregister(&channels_function);
800+
res |= ast_custom_function_unregister(&chan_exists_function);
764801
res |= ast_custom_function_unregister(&mchan_function);
765802

766803
return res;
@@ -772,6 +809,7 @@ static int load_module(void)
772809

773810
res |= ast_custom_function_register(&channel_function);
774811
res |= ast_custom_function_register(&channels_function);
812+
res |= ast_custom_function_register(&chan_exists_function);
775813
res |= ast_custom_function_register(&mchan_function);
776814

777815
return res;

0 commit comments

Comments
 (0)