Asterisk media architecture conversion - no more format bitfields
[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>R/O Whether the channel is hanging up (1/0)</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         char tmp[512];
269         struct ast_format_cap *tmpcap;
270
271         if (!strcasecmp(data, "audionativeformat")) {
272                 if ((tmpcap = ast_format_cap_get_type(chan->nativeformats, AST_FORMAT_TYPE_AUDIO))) {
273                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
274                         tmpcap = ast_format_cap_destroy(tmpcap);
275                 }
276         } else if (!strcasecmp(data, "videonativeformat")) {
277                 if ((tmpcap = ast_format_cap_get_type(chan->nativeformats, AST_FORMAT_TYPE_VIDEO))) {
278                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
279                         tmpcap = ast_format_cap_destroy(tmpcap);
280                 }
281         } else if (!strcasecmp(data, "audioreadformat")) {
282                 ast_copy_string(buf, ast_getformatname(&chan->readformat), len);
283         } else if (!strcasecmp(data, "audiowriteformat")) {
284                 ast_copy_string(buf, ast_getformatname(&chan->writeformat), len);
285 #ifdef CHANNEL_TRACE
286         } else if (!strcasecmp(data, "trace")) {
287                 ast_channel_lock(chan);
288                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
289                 ast_channel_unlock(chan);
290 #endif
291         } else if (!strcasecmp(data, "tonezone") && chan->zone)
292                 locked_copy_string(chan, buf, chan->zone->country, len);
293         else if (!strcasecmp(data, "language"))
294                 locked_copy_string(chan, buf, chan->language, len);
295         else if (!strcasecmp(data, "musicclass"))
296                 locked_copy_string(chan, buf, chan->musicclass, len);
297         else if (!strcasecmp(data, "name")) {
298                 locked_copy_string(chan, buf, chan->name, len);
299         } else if (!strcasecmp(data, "parkinglot"))
300                 locked_copy_string(chan, buf, chan->parkinglot, len);
301         else if (!strcasecmp(data, "state"))
302                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
303         else if (!strcasecmp(data, "channeltype"))
304                 locked_copy_string(chan, buf, chan->tech->type, len);
305         else if (!strcasecmp(data, "accountcode"))
306                 locked_copy_string(chan, buf, chan->accountcode, len);
307         else if (!strcasecmp(data, "checkhangup")) {
308                 ast_channel_lock(chan);
309                 ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
310                 ast_channel_unlock(chan);
311         } else if (!strcasecmp(data, "peeraccount"))
312                 locked_copy_string(chan, buf, chan->peeraccount, len);
313         else if (!strcasecmp(data, "hangupsource"))
314                 locked_copy_string(chan, buf, chan->hangupsource, len);
315         else if (!strcasecmp(data, "appname") && chan->appl)
316                 locked_copy_string(chan, buf, chan->appl, len);
317         else if (!strcasecmp(data, "appdata") && chan->data)
318                 locked_copy_string(chan, buf, chan->data, len);
319         else if (!strcasecmp(data, "exten") && chan->data)
320                 locked_copy_string(chan, buf, chan->exten, len);
321         else if (!strcasecmp(data, "context") && chan->data)
322                 locked_copy_string(chan, buf, chan->context, len);
323         else if (!strcasecmp(data, "userfield") && chan->data)
324                 locked_copy_string(chan, buf, chan->userfield, len);
325         else if (!strcasecmp(data, "channame") && chan->data)
326                 locked_copy_string(chan, buf, chan->name, len);
327         else if (!strcasecmp(data, "linkedid")) {
328                 ast_channel_lock(chan);
329                 if (ast_strlen_zero(chan->linkedid)) {
330                         /* fall back on the channel's uniqueid if linkedid is unset */
331                         ast_copy_string(buf, chan->uniqueid, len);
332                 }
333                 else {
334                         ast_copy_string(buf, chan->linkedid, len);
335                 }
336                 ast_channel_unlock(chan);
337         } else if (!strcasecmp(data, "peer")) {
338                 struct ast_channel *p;
339                 ast_channel_lock(chan);
340                 p = ast_bridged_channel(chan);
341                 if (p || chan->tech || chan->cdr) /* dummy channel? if so, we hid the peer name in the language */
342                         ast_copy_string(buf, (p ? p->name : ""), len);
343                 else {
344                         /* a dummy channel can still pass along bridged peer info via
345                            the BRIDGEPEER variable */
346                         const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
347                         if (!ast_strlen_zero(pname))
348                                 ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
349                         else
350                                 buf[0] = 0;
351                 }
352                 ast_channel_unlock(chan);
353         } else if (!strcasecmp(data, "uniqueid")) {
354                 locked_copy_string(chan, buf, chan->uniqueid, len);
355         } else if (!strcasecmp(data, "transfercapability")) {
356                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
357         } else if (!strcasecmp(data, "callgroup")) {
358                 char groupbuf[256];
359                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
360         } else if (!strcasecmp(data, "amaflags")) {
361                 char amabuf[256];
362                 snprintf(amabuf,sizeof(amabuf), "%d", chan->amaflags);
363                 locked_copy_string(chan, buf, amabuf, len);
364         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
365                 struct ast_datastore *ds;
366                 ast_channel_lock(chan);
367                 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
368                         struct ast_secure_call_store *encrypt = ds->data;
369                         if (!strcasecmp(data, "secure_bridge_signaling")) {
370                                 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
371                         } else if (!strcasecmp(data, "secure_bridge_media")) {
372                                 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
373                         }
374                 }
375                 ast_channel_unlock(chan);
376         } else if (!chan->tech || !chan->tech->func_channel_read || chan->tech->func_channel_read(chan, function, data, buf, len)) {
377                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
378                 ret = -1;
379         }
380
381         return ret;
382 }
383
384 static int func_channel_write_real(struct ast_channel *chan, const char *function,
385                               char *data, const char *value)
386 {
387         int ret = 0;
388         signed char gainset;
389
390         if (!strcasecmp(data, "language"))
391                 locked_string_field_set(chan, language, value);
392         else if (!strcasecmp(data, "parkinglot"))
393                 locked_string_field_set(chan, parkinglot, value);
394         else if (!strcasecmp(data, "musicclass"))
395                 locked_string_field_set(chan, musicclass, value);
396         else if (!strcasecmp(data, "accountcode"))
397                 locked_string_field_set(chan, accountcode, value);
398         else if (!strcasecmp(data, "userfield"))
399                 locked_string_field_set(chan, userfield, value);
400         else if (!strcasecmp(data, "amaflags")) {
401                 ast_channel_lock(chan);
402                 if(isdigit(*value)) {
403                         sscanf(value, "%30d", &chan->amaflags);
404                 } else if (!strcasecmp(value,"OMIT")){
405                         chan->amaflags = 1;
406                 } else if (!strcasecmp(value,"BILLING")){
407                         chan->amaflags = 2;
408                 } else if (!strcasecmp(value,"DOCUMENTATION")){
409                         chan->amaflags = 3;
410                 }
411                 ast_channel_unlock(chan);
412         } else if (!strcasecmp(data, "peeraccount"))
413                 locked_string_field_set(chan, peeraccount, value);
414         else if (!strcasecmp(data, "hangupsource"))
415                 /* XXX - should we be forcing this here? */
416                 ast_set_hangupsource(chan, value, 0);
417 #ifdef CHANNEL_TRACE
418         else if (!strcasecmp(data, "trace")) {
419                 ast_channel_lock(chan);
420                 if (ast_true(value)) 
421                         ret = ast_channel_trace_enable(chan);
422                 else if (ast_false(value))
423                         ret = ast_channel_trace_disable(chan);
424                 else {
425                         ret = -1;
426                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
427                 }
428                 ast_channel_unlock(chan);
429         }
430 #endif
431         else if (!strcasecmp(data, "tonezone")) {
432                 struct ast_tone_zone *new_zone;
433                 if (!(new_zone = ast_get_indication_zone(value))) {
434                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
435                         ret = -1;       
436                 } else {
437                         ast_channel_lock(chan);
438                         if (chan->zone) {
439                                 chan->zone = ast_tone_zone_unref(chan->zone);
440                         }
441                         chan->zone = ast_tone_zone_ref(new_zone);
442                         ast_channel_unlock(chan);
443                         new_zone = ast_tone_zone_unref(new_zone);
444                 }
445         } else if (!strcasecmp(data, "callgroup"))
446                 chan->callgroup = ast_get_group(value);
447         else if (!strcasecmp(data, "txgain")) {
448                 sscanf(value, "%4hhd", &gainset);
449                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
450         } else if (!strcasecmp(data, "rxgain")) {
451                 sscanf(value, "%4hhd", &gainset);
452                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
453         } else if (!strcasecmp(data, "transfercapability")) {
454                 unsigned short i;
455                 for (i = 0; i < 0x20; i++) {
456                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
457                                 chan->transfercapability = i;
458                                 break;
459                         }
460                 }
461         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
462                 struct ast_datastore *ds;
463                 struct ast_secure_call_store *store;
464
465                 if (!chan || !value) {
466                         return -1;
467                 }
468
469                 ast_channel_lock(chan);
470                 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
471                         if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
472                                 ast_channel_unlock(chan);
473                                 return -1;
474                         }
475                         if (!(store = ast_calloc(1, sizeof(*store)))) {
476                                 ast_channel_unlock(chan);
477                                 ast_free(ds);
478                                 return -1;
479                         }
480                         ds->data = store;
481                         ast_channel_datastore_add(chan, ds);
482                 } else {
483                         store = ds->data;
484                 }
485                 ast_channel_unlock(chan);
486
487                 if (!strcasecmp(data, "secure_bridge_signaling")) {
488                         store->signaling = ast_true(value) ? 1 : 0;
489                 } else if (!strcasecmp(data, "secure_bridge_media")) {
490                         store->media = ast_true(value) ? 1 : 0;
491                 }
492         } else if (!chan->tech->func_channel_write
493                  || chan->tech->func_channel_write(chan, function, data, value)) {
494                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
495                                 data);
496                 ret = -1;
497         }
498
499         return ret;
500 }
501
502 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
503 {
504         int res;
505         ast_chan_write_info_t write_info = {
506                 .version = AST_CHAN_WRITE_INFO_T_VERSION,
507                 .write_fn = func_channel_write_real,
508                 .chan = chan,
509                 .function = function,
510                 .data = data,
511                 .value = value,
512         };
513
514         res = func_channel_write_real(chan, function, data, value);
515         ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
516
517         return res;
518 }
519
520 static struct ast_custom_function channel_function = {
521         .name = "CHANNEL",
522         .read = func_channel_read,
523         .write = func_channel_write,
524 };
525
526 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
527 {
528         struct ast_channel *c = NULL;
529         regex_t re;
530         int res;
531         size_t buflen = 0;
532         struct ast_channel_iterator *iter;
533
534         buf[0] = '\0';
535
536         if (!ast_strlen_zero(data)) {
537                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
538                         regerror(res, &re, buf, maxlen);
539                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
540                         return -1;
541                 }
542         }
543
544         if (!(iter = ast_channel_iterator_all_new())) {
545                 if (!ast_strlen_zero(data)) {
546                         regfree(&re);
547                 }
548                 return -1;
549         }
550
551         while ((c = ast_channel_iterator_next(iter))) {
552                 ast_channel_lock(c);
553                 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
554                         size_t namelen = strlen(c->name);
555                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
556                                 if (!ast_strlen_zero(buf)) {
557                                         strcat(buf, " ");
558                                         buflen++;
559                                 }
560                                 strcat(buf, c->name);
561                                 buflen += namelen;
562                         } else {
563                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
564                         }
565                 }
566                 ast_channel_unlock(c);
567                 c = ast_channel_unref(c);
568         }
569
570         ast_channel_iterator_destroy(iter);
571
572         if (!ast_strlen_zero(data)) {
573                 regfree(&re);
574         }
575
576         return 0;
577 }
578
579 static struct ast_custom_function channels_function = {
580         .name = "CHANNELS",
581         .read = func_channels_read,
582 };
583
584 static int func_mchan_read(struct ast_channel *chan, const char *function,
585                              char *data, struct ast_str **buf, ssize_t len)
586 {
587         struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
588         char *template = alloca(4 + strlen(data));
589         sprintf(template, "${%s}", data); /* SAFE */
590         ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
591         if (mchan) {
592                 ast_channel_unref(mchan);
593         }
594         return 0;
595 }
596
597 static int func_mchan_write(struct ast_channel *chan, const char *function,
598                               char *data, const char *value)
599 {
600         struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
601         pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
602         if (mchan) {
603                 ast_channel_unref(mchan);
604         }
605         return 0;
606 }
607
608 static struct ast_custom_function mchan_function = {
609         .name = "MASTER_CHANNEL",
610         .read2 = func_mchan_read,
611         .write = func_mchan_write,
612 };
613
614 static int unload_module(void)
615 {
616         int res = 0;
617
618         res |= ast_custom_function_unregister(&channel_function);
619         res |= ast_custom_function_unregister(&channels_function);
620         res |= ast_custom_function_unregister(&mchan_function);
621
622         return res;
623 }
624
625 static int load_module(void)
626 {
627         int res = 0;
628
629         res |= ast_custom_function_register(&channel_function);
630         res |= ast_custom_function_register(&channels_function);
631         res |= ast_custom_function_register(&mchan_function);
632
633         return res;
634 }
635
636 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");