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 */
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+
714750static 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