14ece0cfd526398d793be9357bda09c2da1940a0
[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                                         <enum name="buffers">
257                                                 <para>W/O Change the channel's buffer policy (for the current call only)</para>
258                                                 <para>This option takes two arguments:</para>
259                                                 <para>  Number of buffers,</para>
260                                                 <para>  Buffer policy being one of:</para>
261                                                 <para>      <literal>full</literal></para>
262                                                 <para>      <literal>immediate</literal></para>
263                                                 <para>      <literal>half</literal></para>
264                                         </enum>
265                                         <enum name="echocan_mode">
266                                                 <para>W/O Change the configuration of the active echo
267                                                 canceller on the channel (if any), for the current call
268                                                 only.</para>
269                                                 <para>Possible values are:</para>
270                                                 <para>  <literal>on</literal>   Normal mode (the echo canceller is actually reinitalized)</para>
271                                                 <para>  <literal>off</literal>  Disabled</para>
272                                                 <para>  <literal>fax</literal>  FAX/data mode (NLP disabled if possible, otherwise
273                                                         completely disabled)</para>
274                                                 <para>  <literal>voice</literal>        Voice mode (returns from FAX mode, reverting the changes that were made)</para>
275                                         </enum>
276                                 </enumlist>
277                                 <para><emphasis>chan_ooh323</emphasis> provides the following additional options:</para>
278                                 <enumlist>
279                                         <enum name="faxdetect">
280                                                 <para>Fax Detect [R/W]</para>
281                                                 <para>Returns 0 or 1</para>
282                                                 <para>Write yes or no</para>
283                                         </enum>
284                                         <enum name="t38support">
285                                                 <para>t38support [R/W]</para>
286                                                 <para>Returns 0 or 1</para>
287                                                 <para>Write yes or no</para>
288                                         </enum>
289                                         <enum name="h323id">
290                                                 <para>Returns h323id [R]</para>
291                                         </enum>
292                                 </enumlist>
293                         </parameter>
294                 </syntax>
295                 <description>
296                         <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
297                         be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
298                         requested that is not available on the current channel will return an empty string.</para>
299                 </description>
300         </function>
301  ***/
302
303 #define locked_copy_string(chan, dest, source, len) \
304         do { \
305                 ast_channel_lock(chan); \
306                 ast_copy_string(dest, source, len); \
307                 ast_channel_unlock(chan); \
308         } while (0)
309 #define locked_string_field_set(chan, field, source) \
310         do { \
311                 ast_channel_lock(chan); \
312                 ast_channel_##field##_set(chan, source); \
313                 ast_channel_unlock(chan); \
314         } while (0)
315
316 static const char * const transfercapability_table[0x20] = {
317         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
318         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
319         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
320         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
321
322 static int func_channel_read(struct ast_channel *chan, const char *function,
323                              char *data, char *buf, size_t len)
324 {
325         int ret = 0;
326         char tmp[512];
327         struct ast_format_cap *tmpcap;
328
329         if (!strcasecmp(data, "audionativeformat")) {
330                 if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_AUDIO))) {
331                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
332                         tmpcap = ast_format_cap_destroy(tmpcap);
333                 }
334         } else if (!strcasecmp(data, "videonativeformat")) {
335                 if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO))) {
336                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
337                         tmpcap = ast_format_cap_destroy(tmpcap);
338                 }
339         } else if (!strcasecmp(data, "audioreadformat")) {
340                 ast_copy_string(buf, ast_getformatname(ast_channel_readformat(chan)), len);
341         } else if (!strcasecmp(data, "audiowriteformat")) {
342                 ast_copy_string(buf, ast_getformatname(ast_channel_writeformat(chan)), len);
343 #ifdef CHANNEL_TRACE
344         } else if (!strcasecmp(data, "trace")) {
345                 ast_channel_lock(chan);
346                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
347                 ast_channel_unlock(chan);
348 #endif
349         } else if (!strcasecmp(data, "tonezone") && ast_channel_zone(chan))
350                 locked_copy_string(chan, buf, ast_channel_zone(chan)->country, len);
351         else if (!strcasecmp(data, "language"))
352                 locked_copy_string(chan, buf, ast_channel_language(chan), len);
353         else if (!strcasecmp(data, "musicclass"))
354                 locked_copy_string(chan, buf, ast_channel_musicclass(chan), len);
355         else if (!strcasecmp(data, "name")) {
356                 locked_copy_string(chan, buf, ast_channel_name(chan), len);
357         } else if (!strcasecmp(data, "parkinglot"))
358                 locked_copy_string(chan, buf, ast_channel_parkinglot(chan), len);
359         else if (!strcasecmp(data, "state"))
360                 locked_copy_string(chan, buf, ast_state2str(ast_channel_state(chan)), len);
361         else if (!strcasecmp(data, "channeltype"))
362                 locked_copy_string(chan, buf, ast_channel_tech(chan)->type, len);
363         else if (!strcasecmp(data, "accountcode"))
364                 locked_copy_string(chan, buf, ast_channel_accountcode(chan), len);
365         else if (!strcasecmp(data, "checkhangup")) {
366                 ast_channel_lock(chan);
367                 ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
368                 ast_channel_unlock(chan);
369         } else if (!strcasecmp(data, "peeraccount"))
370                 locked_copy_string(chan, buf, ast_channel_peeraccount(chan), len);
371         else if (!strcasecmp(data, "hangupsource"))
372                 locked_copy_string(chan, buf, ast_channel_hangupsource(chan), len);
373         else if (!strcasecmp(data, "appname") && ast_channel_appl(chan))
374                 locked_copy_string(chan, buf, ast_channel_appl(chan), len);
375         else if (!strcasecmp(data, "appdata") && ast_channel_data(chan))
376                 locked_copy_string(chan, buf, ast_channel_data(chan), len);
377         else if (!strcasecmp(data, "exten") && ast_channel_data(chan))
378                 locked_copy_string(chan, buf, ast_channel_exten(chan), len);
379         else if (!strcasecmp(data, "context") && ast_channel_data(chan))
380                 locked_copy_string(chan, buf, ast_channel_context(chan), len);
381         else if (!strcasecmp(data, "userfield") && ast_channel_data(chan))
382                 locked_copy_string(chan, buf, ast_channel_userfield(chan), len);
383         else if (!strcasecmp(data, "channame") && ast_channel_data(chan))
384                 locked_copy_string(chan, buf, ast_channel_name(chan), len);
385         else if (!strcasecmp(data, "linkedid")) {
386                 ast_channel_lock(chan);
387                 if (ast_strlen_zero(ast_channel_linkedid(chan))) {
388                         /* fall back on the channel's uniqueid if linkedid is unset */
389                         ast_copy_string(buf, ast_channel_uniqueid(chan), len);
390                 }
391                 else {
392                         ast_copy_string(buf, ast_channel_linkedid(chan), len);
393                 }
394                 ast_channel_unlock(chan);
395         } else if (!strcasecmp(data, "peer")) {
396                 struct ast_channel *p;
397                 ast_channel_lock(chan);
398                 p = ast_bridged_channel(chan);
399                 if (p || ast_channel_tech(chan) || ast_channel_cdr(chan)) /* dummy channel? if so, we hid the peer name in the language */
400                         ast_copy_string(buf, (p ? ast_channel_name(p) : ""), len);
401                 else {
402                         /* a dummy channel can still pass along bridged peer info via
403                            the BRIDGEPEER variable */
404                         const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
405                         if (!ast_strlen_zero(pname))
406                                 ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
407                         else
408                                 buf[0] = 0;
409                 }
410                 ast_channel_unlock(chan);
411         } else if (!strcasecmp(data, "uniqueid")) {
412                 locked_copy_string(chan, buf, ast_channel_uniqueid(chan), len);
413         } else if (!strcasecmp(data, "transfercapability")) {
414                 locked_copy_string(chan, buf, transfercapability_table[ast_channel_transfercapability(chan) & 0x1f], len);
415         } else if (!strcasecmp(data, "callgroup")) {
416                 char groupbuf[256];
417                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_callgroup(chan)), len);
418         } else if (!strcasecmp(data, "pickupgroup")) {
419                 char groupbuf[256];
420                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_pickupgroup(chan)), len);
421         } else if (!strcasecmp(data, "amaflags")) {
422                 char amabuf[256];
423                 snprintf(amabuf,sizeof(amabuf), "%d", ast_channel_amaflags(chan));
424                 locked_copy_string(chan, buf, amabuf, len);
425         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
426                 struct ast_datastore *ds;
427                 ast_channel_lock(chan);
428                 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
429                         struct ast_secure_call_store *encrypt = ds->data;
430                         if (!strcasecmp(data, "secure_bridge_signaling")) {
431                                 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
432                         } else if (!strcasecmp(data, "secure_bridge_media")) {
433                                 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
434                         }
435                 }
436                 ast_channel_unlock(chan);
437         } else if (!ast_channel_tech(chan) || !ast_channel_tech(chan)->func_channel_read || ast_channel_tech(chan)->func_channel_read(chan, function, data, buf, len)) {
438                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
439                 ret = -1;
440         }
441
442         return ret;
443 }
444
445 static int func_channel_write_real(struct ast_channel *chan, const char *function,
446                               char *data, const char *value)
447 {
448         int ret = 0;
449         signed char gainset;
450
451         if (!strcasecmp(data, "language"))
452                 locked_string_field_set(chan, language, value);
453         else if (!strcasecmp(data, "parkinglot"))
454                 locked_string_field_set(chan, parkinglot, value);
455         else if (!strcasecmp(data, "musicclass"))
456                 locked_string_field_set(chan, musicclass, value);
457         else if (!strcasecmp(data, "accountcode"))
458                 locked_string_field_set(chan, accountcode, value);
459         else if (!strcasecmp(data, "userfield"))
460                 locked_string_field_set(chan, userfield, value);
461         else if (!strcasecmp(data, "amaflags")) {
462                 ast_channel_lock(chan);
463                 if(isdigit(*value)) {
464                         int amaflags;
465                         sscanf(value, "%30d", &amaflags);
466                         ast_channel_amaflags_set(chan, amaflags);
467                 } else if (!strcasecmp(value,"OMIT")){
468                         ast_channel_amaflags_set(chan, 1);
469                 } else if (!strcasecmp(value,"BILLING")){
470                         ast_channel_amaflags_set(chan, 2);
471                 } else if (!strcasecmp(value,"DOCUMENTATION")){
472                         ast_channel_amaflags_set(chan, 3);
473                 }
474                 ast_channel_unlock(chan);
475         } else if (!strcasecmp(data, "peeraccount"))
476                 locked_string_field_set(chan, peeraccount, value);
477         else if (!strcasecmp(data, "hangupsource"))
478                 /* XXX - should we be forcing this here? */
479                 ast_set_hangupsource(chan, value, 0);
480 #ifdef CHANNEL_TRACE
481         else if (!strcasecmp(data, "trace")) {
482                 ast_channel_lock(chan);
483                 if (ast_true(value)) 
484                         ret = ast_channel_trace_enable(chan);
485                 else if (ast_false(value))
486                         ret = ast_channel_trace_disable(chan);
487                 else {
488                         ret = -1;
489                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).\n");
490                 }
491                 ast_channel_unlock(chan);
492         }
493 #endif
494         else if (!strcasecmp(data, "tonezone")) {
495                 struct ast_tone_zone *new_zone;
496                 if (!(new_zone = ast_get_indication_zone(value))) {
497                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
498                         ret = -1;       
499                 } else {
500                         ast_channel_lock(chan);
501                         if (ast_channel_zone(chan)) {
502                                 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
503                         }
504                         ast_channel_zone_set(chan, ast_tone_zone_ref(new_zone));
505                         ast_channel_unlock(chan);
506                         new_zone = ast_tone_zone_unref(new_zone);
507                 }
508         } else if (!strcasecmp(data, "callgroup")) {
509                 ast_channel_callgroup_set(chan, ast_get_group(value));
510         } else if (!strcasecmp(data, "pickupgroup")) {
511                 ast_channel_pickupgroup_set(chan, ast_get_group(value));
512         } else if (!strcasecmp(data, "txgain")) {
513                 sscanf(value, "%4hhd", &gainset);
514                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
515         } else if (!strcasecmp(data, "rxgain")) {
516                 sscanf(value, "%4hhd", &gainset);
517                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
518         } else if (!strcasecmp(data, "transfercapability")) {
519                 unsigned short i;
520                 for (i = 0; i < 0x20; i++) {
521                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
522                                 ast_channel_transfercapability_set(chan, i);
523                                 break;
524                         }
525                 }
526         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
527                 struct ast_datastore *ds;
528                 struct ast_secure_call_store *store;
529
530                 if (!chan || !value) {
531                         return -1;
532                 }
533
534                 ast_channel_lock(chan);
535                 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
536                         if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
537                                 ast_channel_unlock(chan);
538                                 return -1;
539                         }
540                         if (!(store = ast_calloc(1, sizeof(*store)))) {
541                                 ast_channel_unlock(chan);
542                                 ast_free(ds);
543                                 return -1;
544                         }
545                         ds->data = store;
546                         ast_channel_datastore_add(chan, ds);
547                 } else {
548                         store = ds->data;
549                 }
550                 ast_channel_unlock(chan);
551
552                 if (!strcasecmp(data, "secure_bridge_signaling")) {
553                         store->signaling = ast_true(value) ? 1 : 0;
554                 } else if (!strcasecmp(data, "secure_bridge_media")) {
555                         store->media = ast_true(value) ? 1 : 0;
556                 }
557         } else if (!ast_channel_tech(chan)->func_channel_write
558                  || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
559                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
560                                 data);
561                 ret = -1;
562         }
563
564         return ret;
565 }
566
567 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
568 {
569         int res;
570         ast_chan_write_info_t write_info = {
571                 .version = AST_CHAN_WRITE_INFO_T_VERSION,
572                 .write_fn = func_channel_write_real,
573                 .chan = chan,
574                 .function = function,
575                 .data = data,
576                 .value = value,
577         };
578
579         res = func_channel_write_real(chan, function, data, value);
580         ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
581
582         return res;
583 }
584
585 static struct ast_custom_function channel_function = {
586         .name = "CHANNEL",
587         .read = func_channel_read,
588         .write = func_channel_write,
589 };
590
591 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
592 {
593         struct ast_channel *c = NULL;
594         regex_t re;
595         int res;
596         size_t buflen = 0;
597         struct ast_channel_iterator *iter;
598
599         buf[0] = '\0';
600
601         if (!ast_strlen_zero(data)) {
602                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
603                         regerror(res, &re, buf, maxlen);
604                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
605                         return -1;
606                 }
607         }
608
609         if (!(iter = ast_channel_iterator_all_new())) {
610                 if (!ast_strlen_zero(data)) {
611                         regfree(&re);
612                 }
613                 return -1;
614         }
615
616         while ((c = ast_channel_iterator_next(iter))) {
617                 ast_channel_lock(c);
618                 if (ast_strlen_zero(data) || regexec(&re, ast_channel_name(c), 0, NULL, 0) == 0) {
619                         size_t namelen = strlen(ast_channel_name(c));
620                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
621                                 if (!ast_strlen_zero(buf)) {
622                                         strcat(buf, " ");
623                                         buflen++;
624                                 }
625                                 strcat(buf, ast_channel_name(c));
626                                 buflen += namelen;
627                         } else {
628                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
629                         }
630                 }
631                 ast_channel_unlock(c);
632                 c = ast_channel_unref(c);
633         }
634
635         ast_channel_iterator_destroy(iter);
636
637         if (!ast_strlen_zero(data)) {
638                 regfree(&re);
639         }
640
641         return 0;
642 }
643
644 static struct ast_custom_function channels_function = {
645         .name = "CHANNELS",
646         .read = func_channels_read,
647 };
648
649 static int func_mchan_read(struct ast_channel *chan, const char *function,
650                              char *data, struct ast_str **buf, ssize_t len)
651 {
652         struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
653         char *template = alloca(4 + strlen(data));
654         sprintf(template, "${%s}", data); /* SAFE */
655         ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
656         if (mchan) {
657                 ast_channel_unref(mchan);
658         }
659         return 0;
660 }
661
662 static int func_mchan_write(struct ast_channel *chan, const char *function,
663                               char *data, const char *value)
664 {
665         struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
666         pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
667         if (mchan) {
668                 ast_channel_unref(mchan);
669         }
670         return 0;
671 }
672
673 static struct ast_custom_function mchan_function = {
674         .name = "MASTER_CHANNEL",
675         .read2 = func_mchan_read,
676         .write = func_mchan_write,
677 };
678
679 static int unload_module(void)
680 {
681         int res = 0;
682
683         res |= ast_custom_function_unregister(&channel_function);
684         res |= ast_custom_function_unregister(&channels_function);
685         res |= ast_custom_function_unregister(&mchan_function);
686
687         return res;
688 }
689
690 static int load_module(void)
691 {
692         int res = 0;
693
694         res |= ast_custom_function_register(&channel_function);
695         res |= ast_custom_function_register(&channels_function);
696         res |= ast_custom_function_register(&mchan_function);
697
698         return res;
699 }
700
701 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");