1416efc3995e2cd79215000785a44861b43c7b3e
[asterisk/asterisk.git] / funcs / func_channel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Channel info dialplan functions
20  *
21  * \author Kevin P. Fleming <kpfleming@digium.com>
22  * \author Ben Winslow
23  * 
24  * \ingroup functions
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include <regex.h>
32 #include <ctype.h>
33
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/stringfields.h"
41 #include "asterisk/global_datastores.h"
42
43 /*** DOCUMENTATION
44         <function name="CHANNELS" language="en_US">
45                 <synopsis>
46                         Gets the list of channels, optionally filtering by a regular expression.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="regular_expression" />
50                 </syntax>
51                 <description>
52                         <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
53                         no argument is provided, all known channels are returned. The
54                         <replaceable>regular_expression</replaceable> must correspond to
55                         the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
56                         will be space-delimited.</para>
57                 </description>
58         </function>
59         <function name="MASTER_CHANNEL" language="en_US">
60                 <synopsis>
61                         Gets or sets variables on the master channel
62                 </synopsis>
63                 <description>
64                         <para>Allows access to the channel which created the current channel, if any.  If the channel is already
65                         a master channel, then accesses local channel variables.</para>
66                 </description>
67         </function>
68         <function name="CHANNEL" language="en_US">
69                 <synopsis>
70                         Gets/sets various pieces of information about the channel.
71                 </synopsis>
72                 <syntax>
73                         <parameter name="item" required="true">
74                                 <para>Standard items (provided by all channel technologies) are:</para>
75                                 <enumlist>
76                                         <enum name="audioreadformat">
77                                                 <para>R/O format currently being read.</para>
78                                         </enum>
79                                         <enum name="audionativeformat">
80                                                 <para>R/O format used natively for audio.</para>
81                                         </enum>
82                                         <enum name="audiowriteformat">
83                                                 <para>R/O format currently being written.</para>
84                                         </enum>
85                                         <enum name="callgroup">
86                                                 <para>R/W call groups for call pickup.</para>
87                                         </enum>
88                                         <enum name="channeltype">
89                                                 <para>R/O technology used for channel.</para>
90                                         </enum>
91                                         <enum name="checkhangup">
92                                                 <para>1/0 whether the channel is hanging up</para>
93                                         </enum>
94                                         <enum name="language">
95                                                 <para>R/W language for sounds played.</para>
96                                         </enum>
97                                         <enum name="musicclass">
98                                                 <para>R/W class (from musiconhold.conf) for hold music.</para>
99                                         </enum>
100                                         <enum name="name">
101                                                 <para>The name of the channel</para>
102                                         </enum>
103                                         <enum name="parkinglot">
104                                                 <para>R/W parkinglot for parking.</para>
105                                         </enum>
106                                         <enum name="rxgain">
107                                                 <para>R/W set rxgain level on channel drivers that support it.</para>
108                                         </enum>
109                                         <enum name="secure_bridge_signaling">
110                                                 <para>Whether or not channels bridged to this channel require secure signaling</para>
111                                         </enum>
112                                         <enum name="secure_bridge_media">
113                                                 <para>Whether or not channels bridged to this channel require secure media</para>
114                                         </enum>
115                                         <enum name="state">
116                                                 <para>R/O state for channel</para>
117                                         </enum>
118                                         <enum name="tonezone">
119                                                 <para>R/W zone for indications played</para>
120                                         </enum>
121                                         <enum name="transfercapability">
122                                                 <para>R/W ISDN Transfer Capability, one of:</para>
123                                                 <enumlist>
124                                                         <enum name="SPEECH" />
125                                                         <enum name="DIGITAL" />
126                                                         <enum name="RESTRICTED_DIGITAL" />
127                                                         <enum name="3K1AUDIO" />
128                                                         <enum name="DIGITAL_W_TONES" />
129                                                         <enum name="VIDEO" />
130                                                 </enumlist>
131                                         </enum>
132                                         <enum name="txgain">
133                                                 <para>R/W set txgain level on channel drivers that support it.</para>
134                                         </enum>
135                                         <enum name="videonativeformat">
136                                                 <para>R/O format used natively for video</para>
137                                         </enum>
138                                         <enum name="trace">
139                                                 <para>R/W whether or not context tracing is enabled, only available
140                                                 <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
141                                         </enum>
142                                 </enumlist>
143                                 <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
144                                 <enumlist>
145                                         <enum name="peerip">
146                                                 <para>R/O Get the IP address of the peer.</para>
147                                         </enum>
148                                         <enum name="recvip">
149                                                 <para>R/O Get the source IP address of the peer.</para>
150                                         </enum>
151                                         <enum name="from">
152                                                 <para>R/O Get the URI from the From: header.</para>
153                                         </enum>
154                                         <enum name="uri">
155                                                 <para>R/O Get the URI from the Contact: header.</para>
156                                         </enum>
157                                         <enum name="useragent">
158                                                 <para>R/O Get the useragent.</para>
159                                         </enum>
160                                         <enum name="peername">
161                                                 <para>R/O Get the name of the peer.</para>
162                                         </enum>
163                                         <enum name="t38passthrough">
164                                                 <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
165                                                 otherwise <literal>0</literal></para>
166                                         </enum>
167                                         <enum name="rtpqos">
168                                                 <para>R/O Get QOS information about the RTP stream</para>
169                                                 <para>    This option takes two additional arguments:</para>
170                                                 <para>    Argument 1:</para>
171                                                 <para>     <literal>audio</literal>             Get data about the audio stream</para>
172                                                 <para>     <literal>video</literal>             Get data about the video stream</para>
173                                                 <para>     <literal>text</literal>              Get data about the text stream</para>
174                                                 <para>    Argument 2:</para>
175                                                 <para>     <literal>local_ssrc</literal>        Local SSRC (stream ID)</para>
176                                                 <para>     <literal>local_lostpackets</literal> Local lost packets</para>
177                                                 <para>     <literal>local_jitter</literal>      Local calculated jitter</para>
178                                                 <para>     <literal>local_maxjitter</literal>   Local calculated jitter (maximum)</para>
179                                                 <para>     <literal>local_minjitter</literal>   Local calculated jitter (minimum)</para>
180                                                 <para>     <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
181                                                 <para>     <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
182                                                 <para>     <literal>local_count</literal>       Number of received packets</para>
183                                                 <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
184                                                 <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
185                                                 <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
186                                                 <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
187                                                 <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
188                                                 <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
189                                                 <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
190                                                 <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
191                                                 <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
192                                                 <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
193                                                 <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
194                                                 <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
195                                                 <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
196                                                 <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
197                                                 <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
198                                                 <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
199                                                 <para>     <literal>rtt</literal>               Round trip time</para>
200                                                 <para>     <literal>maxrtt</literal>            Round trip time (maximum)</para>
201                                                 <para>     <literal>minrtt</literal>            Round trip time (minimum)</para>
202                                                 <para>     <literal>normdevrtt</literal>        Round trip time (normal deviation)</para>
203                                                 <para>     <literal>stdevrtt</literal>          Round trip time (standard deviation)</para>
204                                                 <para>     <literal>all</literal>               All statistics (in a form suited to logging,
205                                                 but not for parsing)</para>
206                                         </enum>
207                                         <enum name="rtpdest">
208                                                 <para>R/O Get remote RTP destination information.</para>
209                                                 <para>   This option takes one additional argument:</para>
210                                                 <para>    Argument 1:</para>
211                                                 <para>     <literal>audio</literal>             Get audio destination</para>
212                                                 <para>     <literal>video</literal>             Get video destination</para>
213                                                 <para>     <literal>text</literal>              Get text destination</para>
214                                         </enum>
215                                 </enumlist>
216                                 <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
217                                 <enumlist>
218                                         <enum name="peerip">
219                                                 <para>R/O Get the peer's ip address.</para>
220                                         </enum>
221                                         <enum name="peername">
222                                                 <para>R/O Get the peer's username.</para>
223                                         </enum>
224                                 </enumlist>
225                                 <para><emphasis>chan_dahdi</emphasis> provides the following additional options:</para>
226                                 <enumlist>
227                                         <enum name="reversecharge">
228                                                 <para>R/O Reverse Charging Indication, one of:</para>
229                                                 <enumlist>
230                                                         <enum name="-1 - None" />
231                                                         <enum name="1 - Reverse Charging Requested" />
232                                                 </enumlist>
233                                         </enum>
234                                 </enumlist>
235                         </parameter>
236                 </syntax>
237                 <description>
238                         <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
239                         be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
240                         requested that is not available on the current channel will return an empty string.</para>
241                 </description>
242         </function>
243  ***/
244
245 #define locked_copy_string(chan, dest, source, len) \
246         do { \
247                 ast_channel_lock(chan); \
248                 ast_copy_string(dest, source, len); \
249                 ast_channel_unlock(chan); \
250         } while (0)
251 #define locked_string_field_set(chan, field, source) \
252         do { \
253                 ast_channel_lock(chan); \
254                 ast_string_field_set(chan, field, source); \
255                 ast_channel_unlock(chan); \
256         } while (0)
257
258 static const char * const transfercapability_table[0x20] = {
259         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
260         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
261         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
262         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
263
264 static int func_channel_read(struct ast_channel *chan, const char *function,
265                              char *data, char *buf, size_t len)
266 {
267         int ret = 0;
268
269         if (!strcasecmp(data, "audionativeformat"))
270                 /* use the _multiple version when chan->nativeformats holds multiple formats */
271                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
272                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
273         else if (!strcasecmp(data, "videonativeformat"))
274                 /* use the _multiple version when chan->nativeformats holds multiple formats */
275                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
276                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
277         else if (!strcasecmp(data, "audioreadformat"))
278                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
279         else if (!strcasecmp(data, "audiowriteformat"))
280                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
281 #ifdef CHANNEL_TRACE
282         else if (!strcasecmp(data, "trace")) {
283                 ast_channel_lock(chan);
284                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
285                 ast_channel_unlock(chan);
286         }
287 #endif
288         else if (!strcasecmp(data, "tonezone") && chan->zone)
289                 locked_copy_string(chan, buf, chan->zone->country, len);
290         else if (!strcasecmp(data, "language"))
291                 locked_copy_string(chan, buf, chan->language, len);
292         else if (!strcasecmp(data, "musicclass"))
293                 locked_copy_string(chan, buf, chan->musicclass, len);
294         else if (!strcasecmp(data, "name")) {
295                 locked_copy_string(chan, buf, chan->name, len);
296         } else if (!strcasecmp(data, "parkinglot"))
297                 locked_copy_string(chan, buf, chan->parkinglot, len);
298         else if (!strcasecmp(data, "state"))
299                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
300         else if (!strcasecmp(data, "channeltype"))
301                 locked_copy_string(chan, buf, chan->tech->type, len);
302         else if (!strcasecmp(data, "accountcode"))
303                 locked_copy_string(chan, buf, chan->accountcode, len);
304         else if (!strcasecmp(data, "checkhangup")) {
305                 ast_channel_lock(chan);
306                 ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
307                 ast_channel_unlock(chan);
308         } else if (!strcasecmp(data, "peeraccount"))
309                 locked_copy_string(chan, buf, chan->peeraccount, len);
310         else if (!strcasecmp(data, "hangupsource"))
311                 locked_copy_string(chan, buf, chan->hangupsource, len);
312         else if (!strcasecmp(data, "appname") && chan->appl)
313                 locked_copy_string(chan, buf, chan->appl, len);
314         else if (!strcasecmp(data, "appdata") && chan->data)
315                 locked_copy_string(chan, buf, chan->data, len);
316         else if (!strcasecmp(data, "exten") && chan->data)
317                 locked_copy_string(chan, buf, chan->exten, len);
318         else if (!strcasecmp(data, "context") && chan->data)
319                 locked_copy_string(chan, buf, chan->context, len);
320         else if (!strcasecmp(data, "userfield") && chan->data)
321                 locked_copy_string(chan, buf, chan->userfield, len);
322         else if (!strcasecmp(data, "channame") && chan->data)
323                 locked_copy_string(chan, buf, chan->name, len);
324         else if (!strcasecmp(data, "linkedid")) {
325                 ast_channel_lock(chan);
326                 if (ast_strlen_zero(chan->linkedid)) {
327                         /* fall back on the channel's uniqueid if linkedid is unset */
328                         ast_copy_string(buf, chan->uniqueid, len);
329                 }
330                 else {
331                         ast_copy_string(buf, chan->linkedid, len);
332                 }
333                 ast_channel_unlock(chan);
334         } else if (!strcasecmp(data, "peer")) {
335                 struct ast_channel *p;
336                 ast_channel_lock(chan);
337                 p = ast_bridged_channel(chan);
338                 if (p || chan->tech || chan->cdr) /* dummy channel? if so, we hid the peer name in the language */
339                         ast_copy_string(buf, (p ? p->name : ""), len);
340                 else {
341                         /* a dummy channel can still pass along bridged peer info via
342                            the BRIDGEPEER variable */
343                         const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
344                         if (!ast_strlen_zero(pname))
345                                 ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
346                         else
347                                 buf[0] = 0;
348                 }
349                 ast_channel_unlock(chan);
350         } else if (!strcasecmp(data, "uniqueid")) {
351                 locked_copy_string(chan, buf, chan->uniqueid, len);
352         } else if (!strcasecmp(data, "transfercapability"))
353                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
354         else if (!strcasecmp(data, "callgroup")) {
355                 char groupbuf[256];
356                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
357         } else if (!strcasecmp(data, "amaflags")) {
358                 char amabuf[256];
359                 snprintf(amabuf,sizeof(amabuf), "%d", chan->amaflags);
360                 locked_copy_string(chan, buf, amabuf, len);
361         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
362                 struct ast_datastore *ds;
363                 ast_channel_lock(chan);
364                 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
365                         struct ast_secure_call_store *encrypt = ds->data;
366                         if (!strcasecmp(data, "secure_bridge_signaling")) {
367                                 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
368                         } else if (!strcasecmp(data, "secure_bridge_media")) {
369                                 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
370                         }
371                 }
372                 ast_channel_unlock(chan);
373         } else if (!chan->tech || !chan->tech->func_channel_read || chan->tech->func_channel_read(chan, function, data, buf, len)) {
374                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
375                 ret = -1;
376         }
377
378         return ret;
379 }
380
381 static int func_channel_write(struct ast_channel *chan, const char *function,
382                               char *data, const char *value)
383 {
384         int ret = 0;
385         signed char gainset;
386
387         if (!strcasecmp(data, "language"))
388                 locked_string_field_set(chan, language, value);
389         else if (!strcasecmp(data, "parkinglot"))
390                 locked_string_field_set(chan, parkinglot, value);
391         else if (!strcasecmp(data, "musicclass"))
392                 locked_string_field_set(chan, musicclass, value);
393         else if (!strcasecmp(data, "accountcode"))
394                 locked_string_field_set(chan, accountcode, value);
395         else if (!strcasecmp(data, "userfield"))
396                 locked_string_field_set(chan, userfield, value);
397         else if (!strcasecmp(data, "amaflags")) {
398                 ast_channel_lock(chan);
399                 if(isdigit(*value)) {
400                         sscanf(value, "%30d", &chan->amaflags);
401                 } else if (!strcasecmp(value,"OMIT")){
402                         chan->amaflags = 1;
403                 } else if (!strcasecmp(value,"BILLING")){
404                         chan->amaflags = 2;
405                 } else if (!strcasecmp(value,"DOCUMENTATION")){
406                         chan->amaflags = 3;
407                 }
408                 ast_channel_unlock(chan);
409         } else if (!strcasecmp(data, "peeraccount"))
410                 locked_string_field_set(chan, peeraccount, value);
411         else if (!strcasecmp(data, "hangupsource"))
412                 /* XXX - should we be forcing this here? */
413                 ast_set_hangupsource(chan, value, 0);
414 #ifdef CHANNEL_TRACE
415         else if (!strcasecmp(data, "trace")) {
416                 ast_channel_lock(chan);
417                 if (ast_true(value)) 
418                         ret = ast_channel_trace_enable(chan);
419                 else if (ast_false(value))
420                         ret = ast_channel_trace_disable(chan);
421                 else {
422                         ret = -1;
423                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
424                 }
425                 ast_channel_unlock(chan);
426         }
427 #endif
428         else if (!strcasecmp(data, "tonezone")) {
429                 struct ast_tone_zone *new_zone;
430                 if (!(new_zone = ast_get_indication_zone(value))) {
431                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
432                         ret = -1;       
433                 } else {
434                         ast_channel_lock(chan);
435                         if (chan->zone) {
436                                 chan->zone = ast_tone_zone_unref(chan->zone);
437                         }
438                         chan->zone = ast_tone_zone_ref(new_zone);
439                         ast_channel_unlock(chan);
440                         new_zone = ast_tone_zone_unref(new_zone);
441                 }
442         } else if (!strcasecmp(data, "callgroup"))
443                 chan->callgroup = ast_get_group(value);
444         else if (!strcasecmp(data, "txgain")) {
445                 sscanf(value, "%4hhd", &gainset);
446                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
447         } else if (!strcasecmp(data, "rxgain")) {
448                 sscanf(value, "%4hhd", &gainset);
449                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
450         } else if (!strcasecmp(data, "transfercapability")) {
451                 unsigned short i;
452                 for (i = 0; i < 0x20; i++) {
453                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
454                                 chan->transfercapability = i;
455                                 break;
456                         }
457                 }
458         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
459                 struct ast_datastore *ds;
460                 struct ast_secure_call_store *store;
461
462                 if (!chan || !value) {
463                         return -1;
464                 }
465
466                 ast_channel_lock(chan);
467                 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
468                         if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
469                                 ast_channel_unlock(chan);
470                                 return -1;
471                         }
472                         if (!(store = ast_calloc(1, sizeof(*store)))) {
473                                 ast_channel_unlock(chan);
474                                 ast_free(ds);
475                                 return -1;
476                         }
477                         ds->data = store;
478                         ast_channel_datastore_add(chan, ds);
479                 } else {
480                         store = ds->data;
481                 }
482                 ast_channel_unlock(chan);
483
484                 if (!strcasecmp(data, "secure_bridge_signaling")) {
485                         store->signaling = ast_true(value) ? 1 : 0;
486                 } else if (!strcasecmp(data, "secure_bridge_media")) {
487                         store->media = ast_true(value) ? 1 : 0;
488                 }
489         } else if (!chan->tech->func_channel_write
490                  || chan->tech->func_channel_write(chan, function, data, value)) {
491                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
492                                 data);
493                 ret = -1;
494         }
495
496         return ret;
497 }
498
499 static struct ast_custom_function channel_function = {
500         .name = "CHANNEL",
501         .read = func_channel_read,
502         .write = func_channel_write,
503 };
504
505 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
506 {
507         struct ast_channel *c = NULL;
508         regex_t re;
509         int res;
510         size_t buflen = 0;
511         struct ast_channel_iterator *iter;
512
513         buf[0] = '\0';
514
515         if (!ast_strlen_zero(data)) {
516                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
517                         regerror(res, &re, buf, maxlen);
518                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
519                         return -1;
520                 }
521         }
522
523         if (!(iter = ast_channel_iterator_all_new())) {
524                 if (!ast_strlen_zero(data)) {
525                         regfree(&re);
526                 }
527                 return -1;
528         }
529
530         while ((c = ast_channel_iterator_next(iter))) {
531                 ast_channel_lock(c);
532                 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
533                         size_t namelen = strlen(c->name);
534                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
535                                 if (!ast_strlen_zero(buf)) {
536                                         strcat(buf, " ");
537                                         buflen++;
538                                 }
539                                 strcat(buf, c->name);
540                                 buflen += namelen;
541                         } else {
542                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
543                         }
544                 }
545                 ast_channel_unlock(c);
546                 c = ast_channel_unref(c);
547         }
548
549         ast_channel_iterator_destroy(iter);
550
551         if (!ast_strlen_zero(data)) {
552                 regfree(&re);
553         }
554
555         return 0;
556 }
557
558 static struct ast_custom_function channels_function = {
559         .name = "CHANNELS",
560         .read = func_channels_read,
561 };
562
563 static int func_mchan_read(struct ast_channel *chan, const char *function,
564                              char *data, struct ast_str **buf, ssize_t len)
565 {
566         struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
567         char *template = alloca(4 + strlen(data));
568         sprintf(template, "${%s}", data); /* SAFE */
569         ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
570         if (mchan) {
571                 ast_channel_unref(mchan);
572         }
573         return 0;
574 }
575
576 static int func_mchan_write(struct ast_channel *chan, const char *function,
577                               char *data, const char *value)
578 {
579         struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
580         pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
581         if (mchan) {
582                 ast_channel_unref(mchan);
583         }
584         return 0;
585 }
586
587 static struct ast_custom_function mchan_function = {
588         .name = "MASTER_CHANNEL",
589         .read2 = func_mchan_read,
590         .write = func_mchan_write,
591 };
592
593 static int unload_module(void)
594 {
595         int res = 0;
596
597         res |= ast_custom_function_unregister(&channel_function);
598         res |= ast_custom_function_unregister(&channels_function);
599         res |= ast_custom_function_unregister(&mchan_function);
600
601         return res;
602 }
603
604 static int load_module(void)
605 {
606         int res = 0;
607
608         res |= ast_custom_function_register(&channel_function);
609         res |= ast_custom_function_register(&channels_function);
610         res |= ast_custom_function_register(&mchan_function);
611
612         return res;
613 }
614
615 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");