Debian Codegraph @

funcs/func_channel.c

raw symbols SCIP
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 <[email protected]>
22 * \author Ben Winslow
23 * \author Naveen Albert <[email protected]>
24 *
25 * \ingroup functions
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include <regex.h>
35 #include <ctype.h>
36
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/bridge.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 #include "asterisk/bridge_basic.h"
47 #include "asterisk/bridge_after.h"
48 #include "asterisk/max_forwards.h"
49
50 /*** DOCUMENTATION
51 <function name="CHANNELS" language="en_US">
52 <since>
53 <version>1.6.1.0</version>
54 </since>
55 <synopsis>
56 Gets the list of channels, optionally filtering by a regular expression.
57 </synopsis>
58 <syntax>
59 <parameter name="regular_expression" />
60 </syntax>
61 <description>
62 <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
63 no argument is provided, all known channels are returned. The
64 <replaceable>regular_expression</replaceable> must correspond to
65 the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
66 will be space-delimited.</para>
67 </description>
68 </function>
69 <function name="CHANNEL_EXISTS" language="en_US">
70 <since>
71 <version>16.22.0</version>
72 <version>18.8.0</version>
73 </since>
74 <synopsis>
75 Checks if the specified channel exists.
76 </synopsis>
77 <syntax>
78 <parameter name="name_or_uid" required="true">
79 <para>The name or unique ID of the channel to check.</para>
80 </parameter>
81 </syntax>
82 <description>
83 <para>Returns 1 if the channel <replaceable>name_or_uid</replaceable> exists, 0 if not.</para>
84 </description>
85 </function>
86 <function name="MASTER_CHANNEL" language="en_US">
87 <since>
88 <version>1.8.0</version>
89 </since>
90 <synopsis>
91 Gets or sets variables on the master channel
92 </synopsis>
93 <description>
94 <para>Allows access to the oldest channel associated with the current
95 channel if it still exists. If the channel is the master channel or
96 the master channel no longer exists then access local channel variables
97 instead. In other words, the master channel is the channel identified by
98 the channel's linkedid.</para>
99 </description>
100 </function>
101 <function name="CHANNEL" language="en_US">
102 <since>
103 <version>1.2.0</version>
104 </since>
105 <synopsis>
106 Gets/sets various pieces of information about the channel.
107 </synopsis>
108 <syntax>
109 <parameter name="item" required="true">
110 <para>Standard items (provided by all channel technologies) are:</para>
111 <enumlist>
112 <enum name="amaflags">
113 <para>R/W the Automatic Message Accounting (AMA) flags on the channel.
114 When read from a channel, the integer value will always be returned.
115 When written to a channel, both the string format or integer value
116 is accepted.</para>
117 <enumlist>
118 <enum name="1"><para><literal>OMIT</literal></para></enum>
119 <enum name="2"><para><literal>BILLING</literal></para></enum>
120 <enum name="3"><para><literal>DOCUMENTATION</literal></para></enum>
121 </enumlist>
122 </enum>
123 <enum name="accountcode">
124 <para>R/W the channel's account code.</para>
125 </enum>
126 <enum name="adsicpe">
127 <para>R/W The channel's support for ADSI (Analog Display Services Interface) CPE.</para>
128 <enumlist>
129 <enum name="available" />
130 <enum name="offhookonly" />
131 <enum name="unavailable" />
132 <enum name="unknown" />
133 </enumlist>
134 </enum>
135 <enum name="audioreadformat">
136 <para>R/O format currently being read.</para>
137 </enum>
138 <enum name="audionativeformat">
139 <para>R/O format used natively for audio.</para>
140 </enum>
141 <enum name="audiowriteformat">
142 <para>R/O format currently being written.</para>
143 </enum>
144 <enum name="dtmf_features">
145 <para>R/W The channel's DTMF bridge features.
146 May include one or more of 'T' 'K' 'H' 'W' and 'X' in a similar manner to options
147 in the <literal>Dial</literal> application. When setting it, the features string
148 must be all upper case.</para>
149 </enum>
150 <enum name="callgroup">
151 <para>R/W numeric call pickup groups that this channel is a member.</para>
152 </enum>
153 <enum name="pickupgroup">
154 <para>R/W numeric call pickup groups this channel can pickup.</para>
155 </enum>
156 <enum name="namedcallgroup">
157 <para>R/W named call pickup groups that this channel is a member.</para>
158 </enum>
159 <enum name="namedpickupgroup">
160 <para>R/W named call pickup groups this channel can pickup.</para>
161 </enum>
162 <enum name="channeltype">
163 <para>R/O technology used for channel.</para>
164 </enum>
165 <enum name="checkhangup">
166 <para>R/O Whether the channel is hanging up (1/0)</para>
167 </enum>
168 <enum name="digitdetect">
169 <para>R/W Enable or disable DTMF detection on channel drivers that support it.</para>
170 <para>If set on a DAHDI channel, this will only disable DTMF detection, not pulse dialing detection.
171 To disable pulse dialing, use the <literal>dialmode</literal> option.</para>
172 <para>On DAHDI channels, this will disable DSP if it is not needed for anything else.
173 This will prevent DTMF detection regardless of the <literal>dialmode</literal> setting.</para>
174 </enum>
175 <enum name="faxdetect">
176 <para>R/W Enable or disable fax detection on channel drivers that support it.</para>
177 </enum>
178 <enum name="after_bridge_goto">
179 <para>R/W the parseable goto string indicating where the channel is
180 expected to return to in the PBX after exiting the next bridge it joins
181 on the condition that it doesn't hang up. The parseable goto string uses
182 the same syntax as the <literal>Goto</literal> application.</para>
183 </enum>
184 <enum name="hangup_handler_pop">
185 <para>W/O Replace the most recently added hangup handler
186 with a new hangup handler on the channel if supplied. The
187 assigned string is passed to the Gosub application when
188 the channel is hung up. Any optionally omitted context
189 and exten are supplied by the channel pushing the handler
190 before it is pushed.</para>
191 </enum>
192 <enum name="hangup_handler_push">
193 <para>W/O Push a hangup handler onto the channel hangup
194 handler stack. The assigned string is passed to the
195 Gosub application when the channel is hung up. Any
196 optionally omitted context and exten are supplied by the
197 channel pushing the handler before it is pushed.</para>
198 </enum>
199 <enum name="hangup_handler_wipe">
200 <para>W/O Wipe the entire hangup handler stack and replace
201 with a new hangup handler on the channel if supplied. The
202 assigned string is passed to the Gosub application when
203 the channel is hung up. Any optionally omitted context
204 and exten are supplied by the channel pushing the handler
205 before it is pushed.</para>
206 </enum>
207 <enum name="onhold">
208 <para>R/O Whether or not the channel is onhold. (1/0)</para>
209 </enum>
210 <enum name="language">
211 <para>R/W language for sounds played.</para>
212 </enum>
213 <enum name="musicclass">
214 <para>R/W class (from musiconhold.conf) for hold music.</para>
215 </enum>
216 <enum name="name">
217 <para>The name of the channel</para>
218 </enum>
219 <enum name="parkinglot">
220 <para>R/W parkinglot for parking.</para>
221 </enum>
222 <enum name="relaxdtmf">
223 <para>W/O Enable or disable relaxed DTMF detection for channel drivers that support it,
224 overriding any setting previously defaulted by the channel driver.</para>
225 </enum>
226 <enum name="rxgain">
227 <para>R/W set rxgain level on channel drivers that support it.</para>
228 </enum>
229 <enum name="secure_bridge_signaling">
230 <para>Whether or not channels bridged to this channel require secure signaling (1/0)</para>
231 </enum>
232 <enum name="secure_bridge_media">
233 <para>Whether or not channels bridged to this channel require secure media (1/0)</para>
234 </enum>
235 <enum name="state">
236 <para>R/O state of the channel</para>
237 </enum>
238 <enum name="tdd">
239 <para>R/W Enable or disable TDD mode on channel drivers that support it.</para>
240 <para>When reading this option, 1 indicates TDD mode enabled, 0 indicates TDD mode disabled,
241 and <literal>mate</literal> indicates TDD mate mode.</para>
242 </enum>
243 <enum name="tonezone">
244 <para>R/W zone for indications played</para>
245 </enum>
246 <enum name="transfercapability">
247 <para>R/W ISDN Transfer Capability, one of:</para>
248 <enumlist>
249 <enum name="SPEECH" />
250 <enum name="DIGITAL" />
251 <enum name="RESTRICTED_DIGITAL" />
252 <enum name="3K1AUDIO" />
253 <enum name="DIGITAL_W_TONES" />
254 <enum name="VIDEO" />
255 </enumlist>
256 </enum>
257 <enum name="txgain">
258 <para>R/W set txgain level on channel drivers that support it.</para>
259 </enum>
260 <enum name="videonativeformat">
261 <para>R/O format used natively for video</para>
262 </enum>
263 <enum name="hangupsource">
264 <para>R/W returns the channel responsible for hangup.</para>
265 </enum>
266 <enum name="appname">
267 <para>R/O returns the internal application name.</para>
268 </enum>
269 <enum name="appdata">
270 <para>R/O returns the application data if available.</para>
271 </enum>
272 <enum name="exten">
273 <para>R/O returns the extension for an outbound channel.</para>
274 </enum>
275 <enum name="context">
276 <para>R/O returns the context for an outbound channel.</para>
277 </enum>
278 <enum name="lastexten">
279 <para>R/O returns the last unique extension for an outbound channel.</para>
280 </enum>
281 <enum name="lastcontext">
282 <para>R/O returns the last unique context for an outbound channel.</para>
283 </enum>
284 <enum name="channame">
285 <para>R/O returns the channel name for an outbound channel.</para>
286 </enum>
287 <enum name="uniqueid">
288 <para>R/O returns the channel uniqueid.</para>
289 </enum>
290 <enum name="linkedid">
291 <para>R/O returns the linkedid if available, otherwise returns the uniqueid.</para>
292 </enum>
293 <enum name="tenantid">
294 <para>R/W The channel tenantid.</para>
295 </enum>
296 <enum name="max_forwards">
297 <para>R/W The maximum number of forwards allowed.</para>
298 </enum>
299 <enum name="callid">
300 <para>R/O Call identifier log tag associated with the channel
301 e.g., <literal>[C-00000000]</literal>.</para>
302 </enum>
303 </enumlist>
304 <xi:include xpointer="xpointer(/docs/info[@name='CHANNEL'])" />
305 </parameter>
306 </syntax>
307 <description>
308 <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
309 be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
310 requested that is not available on the current channel will return an empty string.</para>
311 <example title="Standard CHANNEL item examples">
312 ; Push a hangup handler subroutine existing at dialplan
313 ; location default,s,1 onto the current channel
314 same => n,Set(CHANNEL(hangup_handler_push)=default,s,1)
315
316 ; Set the current tonezone to Germany (de)
317 same => n,Set(CHANNEL(tonezone)=de)
318
319 ; Set the allowed maximum number of forwarding attempts
320 same => n,Set(CHANNEL(max_forwards)=10)
321
322 ; If this channel is ejected from its next bridge, and if
323 ; the channel is not hung up, begin executing dialplan at
324 ; location default,after-bridge,1
325 same => n,Set(CHANNEL(after_bridge_goto)=default,after-bridge,1)
326
327 ; Log the current state of the channel
328 same => n,Log(NOTICE, This channel is: ${CHANNEL(state)})
329 </example>
330 <xi:include xpointer="xpointer(/docs/info[@name='CHANNEL_EXAMPLES'])" />
331 <para>The following channel variables are available as special built-in
332 dialplan channel variables. These variables cannot be set or modified
333 and are read-only.</para>
334 <variablelist>
335 <variable name="CALLINGPRES">
336 <para>Caller ID presentation for incoming calls (PRI channels)</para>
337 </variable>
338 <variable name="CALLINGANI2">
339 <para>Caller ANI2 (PRI channels)</para>
340 </variable>
341 <variable name="CALLINGTON">
342 <para>Caller Type of Number (PRI channels)</para>
343 </variable>
344 <variable name="CALLINGTNS">
345 <para>Transit Network Selector (PRI channels)</para>
346 </variable>
347 <variable name="EXTEN">
348 <para>Current extension</para>
349 </variable>
350 <variable name="CONTEXT">
351 <para>Current context</para>
352 </variable>
353 <variable name="PRIORITY">
354 <para>Current priority</para>
355 </variable>
356 <variable name="CHANNEL">
357 <para>Current channel name</para>
358 </variable>
359 <variable name="UNIQUEID">
360 <para>Current call unique identifier</para>
361 </variable>
362 <variable name="HANGUPCAUSE">
363 <para>Asterisk cause of hangup (inbound/outbound)</para>
364 </variable>
365 </variablelist>
366 </description>
367 </function>
368 ***/
369
370 #define locked_copy_string(chan, dest, source, len) \
371 do { \
372 ast_channel_lock(chan); \
373 ast_copy_string(dest, source, len); \
374 ast_channel_unlock(chan); \
375 } while (0)
376 #define locked_string_field_set(chan, field, source) \
377 do { \
378 ast_channel_lock(chan); \
379 ast_channel_##field##_set(chan, source); \
380 ast_channel_unlock(chan); \
381 } while (0)
382
383 static const char * const transfercapability_table[0x20] = {
384 "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
385 "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
386 "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
387 "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
388
389 static int func_channel_read(struct ast_channel *chan, const char *function,
390 char *data, char *buf, size_t len)
391 {
392 int ret = 0;
393 struct ast_format_cap *tmpcap;
394
395 if (!chan) {
396 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
397 return -1;
398 }
399
400 if (!strcasecmp(data, "audionativeformat")) {
401 tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
402 if (tmpcap) {
403 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
404
405 ast_channel_lock(chan);
406 ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_AUDIO);
407 ast_channel_unlock(chan);
408 ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
409 ao2_ref(tmpcap, -1);
410 }
411 } else if (!strcasecmp(data, "videonativeformat")) {
412 tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
413 if (tmpcap) {
414 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
415
416 ast_channel_lock(chan);
417 ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO);
418 ast_channel_unlock(chan);
419 ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
420 ao2_ref(tmpcap, -1);
421 }
422 } else if (!strcasecmp(data, "audioreadformat")) {
423 locked_copy_string(chan, buf, ast_format_get_name(ast_channel_readformat(chan)), len);
424 } else if (!strcasecmp(data, "audiowriteformat")) {
425 locked_copy_string(chan, buf, ast_format_get_name(ast_channel_writeformat(chan)), len);
426 } else if (!strcasecmp(data, "tonezone") && ast_channel_zone(chan)) {
427 locked_copy_string(chan, buf, ast_channel_zone(chan)->country, len);
428 } else if (!strcasecmp(data, "adsicpe")) {
429 int adsi;
430 ast_channel_lock(chan);
431 adsi = ast_channel_adsicpe(chan);
432 ast_channel_unlock(chan);
433 switch (adsi) {
434 case AST_ADSI_AVAILABLE:
435 ast_copy_string(buf, "available", len);
436 break;
437 case AST_ADSI_UNAVAILABLE:
438 ast_copy_string(buf, "unavailable", len);
439 break;
440 case AST_ADSI_OFFHOOKONLY:
441 ast_copy_string(buf, "offhookonly", len);
442 break;
443 default:
444 ast_copy_string(buf, "unknown", len);
445 break;
446 }
447 } else if (!strcasecmp(data, "dtmf_features")) {
448 if (ast_bridge_features_ds_get_string(chan, buf, len)) {
449 buf[0] = '\0';
450 }
451 } else if (!strcasecmp(data, "language"))
452 locked_copy_string(chan, buf, ast_channel_language(chan), len);
453 else if (!strcasecmp(data, "musicclass"))
454 locked_copy_string(chan, buf, ast_channel_musicclass(chan), len);
455 else if (!strcasecmp(data, "name")) {
456 locked_copy_string(chan, buf, ast_channel_name(chan), len);
457 } else if (!strcasecmp(data, "parkinglot"))
458 locked_copy_string(chan, buf, ast_channel_parkinglot(chan), len);
459 else if (!strcasecmp(data, "state"))
460 locked_copy_string(chan, buf, ast_state2str(ast_channel_state(chan)), len);
461 else if (!strcasecmp(data, "onhold")) {
462 locked_copy_string(chan, buf,
463 ast_channel_hold_state(chan) == AST_CONTROL_HOLD ? "1" : "0", len);
464 } else if (!strcasecmp(data, "channeltype"))
465 locked_copy_string(chan, buf, ast_channel_tech(chan)->type, len);
466 else if (!strcasecmp(data, "accountcode"))
467 locked_copy_string(chan, buf, ast_channel_accountcode(chan), len);
468 else if (!strcasecmp(data, "checkhangup")) {
469 locked_copy_string(chan, buf, ast_check_hangup(chan) ? "1" : "0", len);
470 } else if (!strcasecmp(data, "peeraccount"))
471 locked_copy_string(chan, buf, ast_channel_peeraccount(chan), len);
472 else if (!strcasecmp(data, "hangupsource"))
473 locked_copy_string(chan, buf, ast_channel_hangupsource(chan), len);
474 else if (!strcasecmp(data, "appname") && ast_channel_appl(chan))
475 locked_copy_string(chan, buf, ast_channel_appl(chan), len);
476 else if (!strcasecmp(data, "appdata") && ast_channel_data(chan))
477 locked_copy_string(chan, buf, ast_channel_data(chan), len);
478 else if (!strcasecmp(data, "exten"))
479 locked_copy_string(chan, buf, ast_channel_exten(chan), len);
480 else if (!strcasecmp(data, "context"))
481 locked_copy_string(chan, buf, ast_channel_context(chan), len);
482 else if (!strcasecmp(data, "lastexten"))
483 locked_copy_string(chan, buf, ast_channel_lastexten(chan), len);
484 else if (!strcasecmp(data, "lastcontext"))
485 locked_copy_string(chan, buf, ast_channel_lastcontext(chan), len);
486 else if (!strcasecmp(data, "userfield"))
487 locked_copy_string(chan, buf, ast_channel_userfield(chan), len);
488 else if (!strcasecmp(data, "channame"))
489 locked_copy_string(chan, buf, ast_channel_name(chan), len);
490 else if (!strcasecmp(data, "linkedid")) {
491 ast_channel_lock(chan);
492 if (ast_strlen_zero(ast_channel_linkedid(chan))) {
493 /* fall back on the channel's uniqueid if linkedid is unset */
494 ast_copy_string(buf, ast_channel_uniqueid(chan), len);
495 }
496 else {
497 ast_copy_string(buf, ast_channel_linkedid(chan), len);
498 }
499 ast_channel_unlock(chan);
500 } else if (!strcasecmp(data, "peer")) {
501 struct ast_channel *peer;
502
503 peer = ast_channel_bridge_peer(chan);
504 if (peer) {
505 /* Only real channels could have a bridge peer this way. */
506 ast_channel_lock(peer);
507 ast_copy_string(buf, ast_channel_name(peer), len);
508 ast_channel_unlock(peer);
509 ast_channel_unref(peer);
510 } else {
511 buf[0] = '\0';
512 ast_channel_lock(chan);
513 if (!ast_channel_tech(chan)) {
514 const char *pname;
515
516 /*
517 * A dummy channel can still pass along bridged peer info
518 * via the BRIDGEPEER variable.
519 *
520 * A horrible kludge, but... how else?
521 */
522 pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
523 if (!ast_strlen_zero(pname)) {
524 ast_copy_string(buf, pname, len);
525 }
526 }
527 ast_channel_unlock(chan);
528 }
529 } else if (!strcasecmp(data, "uniqueid")) {
530 locked_copy_string(chan, buf, ast_channel_uniqueid(chan), len);
531 } else if (!strcasecmp(data, "transfercapability")) {
532 locked_copy_string(chan, buf, transfercapability_table[ast_channel_transfercapability(chan) & 0x1f], len);
533 } else if (!strcasecmp(data, "callgroup")) {
534 char groupbuf[256];
535
536 locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_callgroup(chan)), len);
537 } else if (!strcasecmp(data, "pickupgroup")) {
538 char groupbuf[256];
539
540 locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_pickupgroup(chan)), len);
541 } else if (!strcasecmp(data, "namedcallgroup")) {
542 struct ast_str *tmp_str = ast_str_alloca(1024);
543
544 locked_copy_string(chan, buf, ast_print_namedgroups(&tmp_str, ast_channel_named_callgroups(chan)), len);
545 } else if (!strcasecmp(data, "namedpickupgroup")) {
546 struct ast_str *tmp_str = ast_str_alloca(1024);
547
548 locked_copy_string(chan, buf, ast_print_namedgroups(&tmp_str, ast_channel_named_pickupgroups(chan)), len);
549 } else if (!strcasecmp(data, "after_bridge_goto")) {
550 ast_bridge_read_after_goto(chan, buf, len);
551 } else if (!strcasecmp(data, "amaflags")) {
552 ast_channel_lock(chan);
553 snprintf(buf, len, "%u", ast_channel_amaflags(chan));
554 ast_channel_unlock(chan);
555 } else if (!strncasecmp(data, "secure_bridge_", 14)) {
556 struct ast_datastore *ds;
557
558 buf[0] = '\0';
559 ast_channel_lock(chan);
560 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
561 struct ast_secure_call_store *encrypt = ds->data;
562
563 if (!strcasecmp(data, "secure_bridge_signaling")) {
564 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
565 } else if (!strcasecmp(data, "secure_bridge_media")) {
566 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
567 }
568 }
569 ast_channel_unlock(chan);
570 } else if (!strcasecmp(data, "max_forwards")) {
571 ast_channel_lock(chan);
572 snprintf(buf, len, "%d", ast_max_forwards_get(chan));
573 ast_channel_unlock(chan);
574 } else if (!strcasecmp(data, "callid")) {
575 ast_callid callid;
576
577 buf[0] = '\0';
578 ast_channel_lock(chan);
579 callid = ast_channel_callid(chan);
580 if (callid) {
581 ast_callid_strnprint(buf, len, callid);
582 }
583 ast_channel_unlock(chan);
584 } else if (!strcasecmp(data, "tdd")) {
585 char status;
586 int status_size = (int) sizeof(status);
587 ret = ast_channel_queryoption(chan, AST_OPTION_TDD, &status, &status_size, 0);
588 if (!ret) {
589 ast_copy_string(buf, status == 2 ? "mate" : status ? "1" : "0", len);
590 }
591 } else if (!strcasecmp(data, "digitdetect")) {
592 char status;
593 int status_size = (int) sizeof(status);
594 ret = ast_channel_queryoption(chan, AST_OPTION_DIGIT_DETECT, &status, &status_size, 0);
595 if (!ret) {
596 ast_copy_string(buf, status ? "1" : "0", len);
597 }
598 } else if (!strcasecmp(data, "faxdetect")) {
599 char status;
600 int status_size = (int) sizeof(status);
601 ret = ast_channel_queryoption(chan, AST_OPTION_FAX_DETECT, &status, &status_size, 0);
602 if (!ret) {
603 ast_copy_string(buf, status ? "1" : "0", len);
604 }
605 } else if (!strcasecmp(data, "device_name")) {
606 ret = ast_channel_get_device_name(chan, buf, len);
607 } else if (!strcasecmp(data, "tenantid")) {
608 locked_copy_string(chan, buf, ast_channel_tenantid(chan), len);
609 } 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)) {
610 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
611 ret = -1;
612 }
613
614 return ret;
615 }
616
617 static int func_channel_write_real(struct ast_channel *chan, const char *function,
618 char *data, const char *value)
619 {
620 int ret = 0;
621 signed char gainset;
622
623 if (!strcasecmp(data, "language"))
624 locked_string_field_set(chan, language, value);
625 else if (!strcasecmp(data, "parkinglot"))
626 locked_string_field_set(chan, parkinglot, value);
627 else if (!strcasecmp(data, "musicclass"))
628 locked_string_field_set(chan, musicclass, value);
629 else if (!strcasecmp(data, "accountcode"))
630 locked_string_field_set(chan, accountcode, value);
631 else if (!strcasecmp(data, "userfield"))
632 locked_string_field_set(chan, userfield, value);
633 else if (!strcasecmp(data, "after_bridge_goto")) {
634 if (ast_strlen_zero(value)) {
635 ast_bridge_discard_after_goto(chan);
636 } else {
637 ast_bridge_set_after_go_on(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), value);
638 }
639 } else if (!strcasecmp(data, "amaflags")) {
640 int amaflags;
641
642 if (isdigit(*value)) {
643 if (sscanf(value, "%30d", &amaflags) != 1) {
644 amaflags = AST_AMA_NONE;
645 }
646 } else {
647 amaflags = ast_channel_string2amaflag(value);
648 }
649 ast_channel_lock(chan);
650 ast_channel_amaflags_set(chan, amaflags);
651 ast_channel_unlock(chan);
652 } else if (!strcasecmp(data, "peeraccount"))
653 locked_string_field_set(chan, peeraccount, value);
654 else if (!strcasecmp(data, "hangupsource"))
655 /* XXX - should we be forcing this here? */
656 ast_set_hangupsource(chan, value, 0);
657 else if (!strcasecmp(data, "tonezone")) {
658 struct ast_tone_zone *new_zone;
659 if (!(new_zone = ast_get_indication_zone(value))) {
660 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
661 ret = -1;
662 } else {
663 ast_channel_lock(chan);
664 if (ast_channel_zone(chan)) {
665 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
666 }
667 ast_channel_zone_set(chan, ast_tone_zone_ref(new_zone));
668 ast_channel_unlock(chan);
669 new_zone = ast_tone_zone_unref(new_zone);
670 }
671 } else if (!strcasecmp(data, "adsicpe")) {
672 int adsi;
673 if (!strcasecmp(value, "unknown")) {
674 adsi = AST_ADSI_UNKNOWN;
675 } else if (!strcasecmp(value, "available")) {
676 adsi = AST_ADSI_AVAILABLE;
677 } else if (!strcasecmp(value, "unavailable")) {
678 adsi = AST_ADSI_UNAVAILABLE;
679 } else if (!strcasecmp(value, "offhookonly")) {
680 adsi = AST_ADSI_OFFHOOKONLY;
681 } else {
682 ast_log(LOG_ERROR, "Unknown ADSI availability '%s'\n", value);
683 return -1;
684 }
685 ast_channel_lock(chan);
686 ast_channel_adsicpe_set(chan, adsi);
687 ast_channel_unlock(chan);
688 } else if (!strcasecmp(data, "dtmf_features")) {
689 ret = ast_bridge_features_ds_set_string(chan, value);
690 } else if (!strcasecmp(data, "callgroup")) {
691 ast_channel_lock(chan);
692 ast_channel_callgroup_set(chan, ast_get_group(value));
693 ast_channel_unlock(chan);
694 } else if (!strcasecmp(data, "pickupgroup")) {
695 ast_channel_lock(chan);
696 ast_channel_pickupgroup_set(chan, ast_get_group(value));
697 ast_channel_unlock(chan);
698 } else if (!strcasecmp(data, "namedcallgroup")) {
699 struct ast_namedgroups *groups = ast_get_namedgroups(value);
700
701 ast_channel_lock(chan);
702 ast_channel_named_callgroups_set(chan, groups);
703 ast_channel_unlock(chan);
704 ast_unref_namedgroups(groups);
705 } else if (!strcasecmp(data, "namedpickupgroup")) {
706 struct ast_namedgroups *groups = ast_get_namedgroups(value);
707
708 ast_channel_lock(chan);
709 ast_channel_named_pickupgroups_set(chan, groups);
710 ast_channel_unlock(chan);
711 ast_unref_namedgroups(groups);
712 } else if (!strcasecmp(data, "tdd")) {
713 char enabled;
714 if (!strcasecmp(value, "mate")) {
715 enabled = 2;
716 } else {
717 enabled = ast_true(value) ? 1 : 0;
718 }
719 ast_channel_setoption(chan, AST_OPTION_TDD, &enabled, sizeof(enabled), 0);
720 } else if (!strcasecmp(data, "relaxdtmf")) {
721 char enabled = ast_true(value) ? 1 : 0;
722 ast_channel_setoption(chan, AST_OPTION_RELAXDTMF, &enabled, sizeof(enabled), 0);
723 } else if (!strcasecmp(data, "txgain")) {
724 sscanf(value, "%4hhd", &gainset);
725 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
726 } else if (!strcasecmp(data, "rxgain")) {
727 sscanf(value, "%4hhd", &gainset);
728 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
729 } else if (!strcasecmp(data, "digitdetect")) {
730 char enabled = ast_true(value) ? 1 : 0;
731 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &enabled, sizeof(enabled), 0);
732 } else if (!strcasecmp(data, "faxdetect")) {
733 char enabled = ast_true(value) ? 1 : 0;
734 ast_channel_setoption(chan, AST_OPTION_FAX_DETECT, &enabled, sizeof(enabled), 0);
735 } else if (!strcasecmp(data, "transfercapability")) {
736 unsigned short i;
737
738 ast_channel_lock(chan);
739 for (i = 0; i < 0x20; i++) {
740 if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
741 ast_channel_transfercapability_set(chan, i);
742 break;
743 }
744 }
745 ast_channel_unlock(chan);
746 } else if (!strcasecmp(data, "hangup_handler_pop")) {
747 /* Pop one hangup handler before pushing the new handler. */
748 ast_pbx_hangup_handler_pop(chan);
749 ast_pbx_hangup_handler_push(chan, value);
750 } else if (!strcasecmp(data, "hangup_handler_push")) {
751 ast_pbx_hangup_handler_push(chan, value);
752 } else if (!strcasecmp(data, "hangup_handler_wipe")) {
753 /* Pop all hangup handlers before pushing the new handler. */
754 while (ast_pbx_hangup_handler_pop(chan)) {
755 }
756 ast_pbx_hangup_handler_push(chan, value);
757 } else if (!strncasecmp(data, "secure_bridge_", 14)) {
758 struct ast_datastore *ds;
759 struct ast_secure_call_store *store;
760
761 if (!chan || !value) {
762 return -1;
763 }
764
765 ast_channel_lock(chan);
766 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
767 if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
768 ast_channel_unlock(chan);
769 return -1;
770 }
771 if (!(store = ast_calloc(1, sizeof(*store)))) {
772 ast_channel_unlock(chan);
773 ast_free(ds);
774 return -1;
775 }
776 ds->data = store;
777 ast_channel_datastore_add(chan, ds);
778 } else {
779 store = ds->data;
780 }
781
782 if (!strcasecmp(data, "secure_bridge_signaling")) {
783 store->signaling = ast_true(value) ? 1 : 0;
784 } else if (!strcasecmp(data, "secure_bridge_media")) {
785 store->media = ast_true(value) ? 1 : 0;
786 }
787 ast_channel_unlock(chan);
788 } else if (!strcasecmp(data, "max_forwards")) {
789 int max_forwards;
790 if (sscanf(value, "%d", &max_forwards) != 1) {
791 ast_log(LOG_WARNING, "Unable to set max forwards to '%s'\n", value);
792 ret = -1;
793 } else {
794 ast_channel_lock(chan);
795 ret = ast_max_forwards_set(chan, max_forwards);
796 ast_channel_unlock(chan);
797 }
798 } else if (!strcasecmp(data, "tenantid")) {
799 ast_channel_tenantid_set(chan, value);
800 } else if (!ast_channel_tech(chan)->func_channel_write
801 || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
802 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
803 data);
804 ret = -1;
805 }
806
807 return ret;
808 }
809
810 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
811 {
812 int res;
813 ast_chan_write_info_t write_info = {
814 .version = AST_CHAN_WRITE_INFO_T_VERSION,
815 .write_fn = func_channel_write_real,
816 .chan = chan,
817 .function = function,
818 .data = data,
819 .value = value,
820 };
821
822 if (!chan) {
823 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
824 return -1;
825 }
826
827 res = func_channel_write_real(chan, function, data, value);
828 ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
829
830 return res;
831 }
832
833 static struct ast_custom_function channel_function = {
834 .name = "CHANNEL",
835 .read = func_channel_read,
836 .write = func_channel_write,
837 };
838
839 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
840 {
841 struct ast_channel *c = NULL;
842 regex_t re;
843 int res;
844 size_t buflen = 0;
845 struct ast_channel_iterator *iter;
846
847 buf[0] = '\0';
848
849 if (!ast_strlen_zero(data)) {
850 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
851 regerror(res, &re, buf, maxlen);
852 ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
853 return -1;
854 }
855 }
856
857 if (!(iter = ast_channel_iterator_all_new())) {
858 if (!ast_strlen_zero(data)) {
859 regfree(&re);
860 }
861 return -1;
862 }
863
864 while ((c = ast_channel_iterator_next(iter))) {
865 ast_channel_lock(c);
866 if (ast_strlen_zero(data) || regexec(&re, ast_channel_name(c), 0, NULL, 0) == 0) {
867 size_t namelen = strlen(ast_channel_name(c));
868 if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
869 if (!ast_strlen_zero(buf)) {
870 strcat(buf, " ");
871 buflen++;
872 }
873 strcat(buf, ast_channel_name(c));
874 buflen += namelen;
875 } else {
876 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
877 }
878 }
879 ast_channel_unlock(c);
880 c = ast_channel_unref(c);
881 }
882
883 ast_channel_iterator_destroy(iter);
884
885 if (!ast_strlen_zero(data)) {
886 regfree(&re);
887 }
888
889 return 0;
890 }
891
892 static struct ast_custom_function channels_function = {
893 .name = "CHANNELS",
894 .read = func_channels_read,
895 };
896
897 static int func_chan_exists_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
898 {
899 struct ast_channel *chan_found = NULL;
900
901 if (ast_strlen_zero(data)) {
902 ast_log(LOG_WARNING, "%s: Channel name or unique ID required\n", function);
903 return -1;
904 }
905
906 chan_found = ast_channel_get_by_name(data);
907 snprintf(buf, maxlen, "%d", (chan_found ? 1 : 0));
908 if (chan_found) {
909 ast_channel_unref(chan_found);
910 }
911 return 0;
912 }
913
914 static struct ast_custom_function chan_exists_function = {
915 .name = "CHANNEL_EXISTS",
916 .read = func_chan_exists_read,
917 };
918
919 static int func_mchan_read(struct ast_channel *chan, const char *function,
920 char *data, struct ast_str **buf, ssize_t len)
921 {
922 struct ast_channel *mchan;
923 char *template = ast_alloca(4 + strlen(data));
924
925 if (!chan) {
926 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
927 return -1;
928 }
929
930 mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
931 sprintf(template, "${%s}", data); /* SAFE */
932 ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
933 if (mchan) {
934 ast_channel_unref(mchan);
935 }
936 return 0;
937 }
938
939 static int func_mchan_write(struct ast_channel *chan, const char *function,
940 char *data, const char *value)
941 {
942 struct ast_channel *mchan;
943
944 if (!chan) {
945 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
946 return -1;
947 }
948
949 mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
950 pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
951 if (mchan) {
952 ast_channel_unref(mchan);
953 }
954 return 0;
955 }
956
957 static struct ast_custom_function mchan_function = {
958 .name = "MASTER_CHANNEL",
959 .read2 = func_mchan_read,
960 .write = func_mchan_write,
961 };
962
963 static int unload_module(void)
964 {
965 int res = 0;
966
967 res |= ast_custom_function_unregister(&channel_function);
968 res |= ast_custom_function_unregister(&channels_function);
969 res |= ast_custom_function_unregister(&chan_exists_function);
970 res |= ast_custom_function_unregister(&mchan_function);
971
972 return res;
973 }
974
975 static int load_module(void)
976 {
977 int res = 0;
978
979 res |= ast_custom_function_register(&channel_function);
980 res |= ast_custom_function_register(&channels_function);
981 res |= ast_custom_function_register(&chan_exists_function);
982 res |= ast_custom_function_register(&mchan_function);
983
984 return res;
985 }
986
987 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");