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