Merge "BuildSystem: Really do not pass unknown-warning options to the compiler."
[asterisk/asterisk.git] / funcs / func_channel.c
index f4aa763..e290fa0 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <regex.h>
 #include <ctype.h>
 
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
-#include "asterisk/bridging.h"
+#include "asterisk/bridge.h"
 #include "asterisk/pbx.h"
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
 #include "asterisk/indications.h"
 #include "asterisk/stringfields.h"
 #include "asterisk/global_datastores.h"
-#include "asterisk/bridging_basic.h"
-#include "asterisk/bridging_after.h"
+#include "asterisk/bridge_basic.h"
+#include "asterisk/bridge_after.h"
+#include "asterisk/max_forwards.h"
 
 /*** DOCUMENTATION
        <function name="CHANNELS" language="en_US">
@@ -68,8 +67,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        Gets or sets variables on the master channel
                </synopsis>
                <description>
-                       <para>Allows access to the channel which created the current channel, if any.  If the channel is already
-                       a master channel, then accesses local channel variables.</para>
+                       <para>Allows access to the oldest channel associated with the current
+                       channel if it still exists.  If the channel is the master channel or
+                       the master channel no longer exists then access local channel variables
+                       instead.  In other words, the master channel is the channel identified by
+                       the channel's linkedid.</para>
                </description>
        </function>
        <function name="CHANNEL" language="en_US">
@@ -156,6 +158,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                                and exten are supplied by the channel pushing the handler
                                                before it is pushed.</para>
                                        </enum>
+                                       <enum name="onhold">
+                                               <para>R/O Whether or not the channel is onhold. (1/0)</para>
+                                       </enum>
                                        <enum name="language">
                                                <para>R/W language for sounds played.</para>
                                        </enum>
@@ -172,13 +177,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                                <para>R/W set rxgain level on channel drivers that support it.</para>
                                        </enum>
                                        <enum name="secure_bridge_signaling">
-                                               <para>Whether or not channels bridged to this channel require secure signaling</para>
+                                               <para>Whether or not channels bridged to this channel require secure signaling (1/0)</para>
                                        </enum>
                                        <enum name="secure_bridge_media">
-                                               <para>Whether or not channels bridged to this channel require secure media</para>
+                                               <para>Whether or not channels bridged to this channel require secure media (1/0)</para>
                                        </enum>
                                        <enum name="state">
-                                               <para>R/O state for channel</para>
+                                               <para>R/O state of the channel</para>
                                        </enum>
                                        <enum name="tonezone">
                                                <para>R/W zone for indications played</para>
@@ -204,193 +209,65 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                                <para>R/W whether or not context tracing is enabled, only available
                                                <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
                                        </enum>
-                               </enumlist>
-                               <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
-                               <enumlist>
-                                       <enum name="peerip">
-                                               <para>R/O Get the IP address of the peer.</para>
-                                       </enum>
-                                       <enum name="recvip">
-                                               <para>R/O Get the source IP address of the peer.</para>
-                                       </enum>
-                                       <enum name="from">
-                                               <para>R/O Get the URI from the From: header.</para>
-                                       </enum>
-                                       <enum name="uri">
-                                               <para>R/O Get the URI from the Contact: header.</para>
-                                       </enum>
-                                       <enum name="useragent">
-                                               <para>R/O Get the useragent.</para>
-                                       </enum>
-                                       <enum name="peername">
-                                               <para>R/O Get the name of the peer.</para>
-                                       </enum>
-                                       <enum name="t38passthrough">
-                                               <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
-                                               otherwise <literal>0</literal></para>
-                                       </enum>
-                                       <enum name="rtpqos">
-                                               <para>R/O Get QOS information about the RTP stream</para>
-                                               <para>    This option takes two additional arguments:</para>
-                                               <para>    Argument 1:</para>
-                                               <para>     <literal>audio</literal>             Get data about the audio stream</para>
-                                               <para>     <literal>video</literal>             Get data about the video stream</para>
-                                               <para>     <literal>text</literal>              Get data about the text stream</para>
-                                               <para>    Argument 2:</para>
-                                               <para>     <literal>local_ssrc</literal>        Local SSRC (stream ID)</para>
-                                               <para>     <literal>local_lostpackets</literal> Local lost packets</para>
-                                               <para>     <literal>local_jitter</literal>      Local calculated jitter</para>
-                                               <para>     <literal>local_maxjitter</literal>   Local calculated jitter (maximum)</para>
-                                               <para>     <literal>local_minjitter</literal>   Local calculated jitter (minimum)</para>
-                                               <para>     <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
-                                               <para>     <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
-                                               <para>     <literal>local_count</literal>       Number of received packets</para>
-                                               <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
-                                               <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
-                                               <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
-                                               <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
-                                               <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
-                                               <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
-                                               <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
-                                               <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
-                                               <para>     <literal>rtt</literal>               Round trip time</para>
-                                               <para>     <literal>maxrtt</literal>            Round trip time (maximum)</para>
-                                               <para>     <literal>minrtt</literal>            Round trip time (minimum)</para>
-                                               <para>     <literal>normdevrtt</literal>        Round trip time (normal deviation)</para>
-                                               <para>     <literal>stdevrtt</literal>          Round trip time (standard deviation)</para>
-                                               <para>     <literal>all</literal>               All statistics (in a form suited to logging,
-                                               but not for parsing)</para>
-                                       </enum>
-                                       <enum name="rtpdest">
-                                               <para>R/O Get remote RTP destination information.</para>
-                                               <para>   This option takes one additional argument:</para>
-                                               <para>    Argument 1:</para>
-                                               <para>     <literal>audio</literal>             Get audio destination</para>
-                                               <para>     <literal>video</literal>             Get video destination</para>
-                                               <para>     <literal>text</literal>              Get text destination</para>
-                                               <para>   Defaults to <literal>audio</literal> if unspecified.</para>
-                                       </enum>
-                                       <enum name="rtpsource">
-                                               <para>R/O Get source RTP destination information.</para>
-                                               <para>   This option takes one additional argument:</para>
-                                               <para>    Argument 1:</para>
-                                               <para>     <literal>audio</literal>             Get audio destination</para>
-                                               <para>     <literal>video</literal>             Get video destination</para>
-                                               <para>     <literal>text</literal>              Get text destination</para>
-                                               <para>   Defaults to <literal>audio</literal> if unspecified.</para>
+                                       <enum name="hangupsource">
+                                               <para>R/W returns the channel responsible for hangup.</para>
                                        </enum>
-                               </enumlist>
-                               <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
-                               <enumlist>
-                                       <enum name="osptoken">
-                                               <para>R/O Get the peer's osptoken.</para>
+                                       <enum name="appname">
+                                               <para>R/O returns the internal application name.</para>
                                        </enum>
-                                       <enum name="peerip">
-                                               <para>R/O Get the peer's ip address.</para>
+                                       <enum name="appdata">
+                                               <para>R/O returns the application data if available.</para>
                                        </enum>
-                                       <enum name="peername">
-                                               <para>R/O Get the peer's username.</para>
+                                       <enum name="exten">
+                                               <para>R/O returns the extension for an outbound channel.</para>
                                        </enum>
-                                       <enum name="secure_signaling">
-                                               <para>R/O Get the if the IAX channel is secured.</para>
+                                       <enum name="context">
+                                               <para>R/O returns the context for an outbound channel.</para>
                                        </enum>
-                                       <enum name="secure_media">
-                                               <para>R/O Get the if the IAX channel is secured.</para>
+                                       <enum name="channame">
+                                               <para>R/O returns the channel name for an outbound channel.</para>
                                        </enum>
-                               </enumlist>
-                               <para><emphasis>chan_dahdi</emphasis> provides the following additional options:</para>
-                               <enumlist>
-                                       <enum name="dahdi_channel">
-                                               <para>R/O DAHDI channel related to this channel.</para>
+                                       <enum name="uniqueid">
+                                               <para>R/O returns the channel uniqueid.</para>
                                        </enum>
-                                       <enum name="dahdi_span">
-                                               <para>R/O DAHDI span related to this channel.</para>
+                                       <enum name="linkedid">
+                                               <para>R/O returns the linkedid if available, otherwise returns the uniqueid.</para>
                                        </enum>
-                                       <enum name="dahdi_type">
-                                               <para>R/O DAHDI channel type, one of:</para>
-                                               <enumlist>
-                                                       <enum name="analog" />
-                                                       <enum name="mfc/r2" />
-                                                       <enum name="pri" />
-                                                       <enum name="pseudo" />
-                                                       <enum name="ss7" />
-                                               </enumlist>
+                                       <enum name="max_forwards">
+                                               <para>R/W The maximum number of forwards allowed.</para>
                                        </enum>
-                                       <enum name="keypad_digits">
-                                               <para>R/O PRI Keypad digits that came in with the SETUP message.</para>
-                                       </enum>
-                                       <enum name="reversecharge">
-                                               <para>R/O PRI Reverse Charging Indication, one of:</para>
-                                               <enumlist>
-                                                       <enum name="-1"> <para>None</para></enum>
-                                                       <enum name=" 1"> <para>Reverse Charging Requested</para></enum>
-                                               </enumlist>
-                                       </enum>
-                                       <enum name="no_media_path">
-                                               <para>R/O PRI Nonzero if the channel has no B channel.
-                                               The channel is either on hold or a call waiting call.</para>
-                                       </enum>
-                                       <enum name="buffers">
-                                               <para>W/O Change the channel's buffer policy (for the current call only)</para>
-                                               <para>This option takes two arguments:</para>
-                                               <para>  Number of buffers,</para>
-                                               <para>  Buffer policy being one of:</para>
-                                               <para>      <literal>full</literal></para>
-                                               <para>      <literal>immediate</literal></para>
-                                               <para>      <literal>half</literal></para>
-                                       </enum>
-                                       <enum name="echocan_mode">
-                                               <para>W/O Change the configuration of the active echo
-                                               canceller on the channel (if any), for the current call
-                                               only.</para>
-                                               <para>Possible values are:</para>
-                                               <para>  <literal>on</literal>   Normal mode (the echo canceller is actually reinitalized)</para>
-                                               <para>  <literal>off</literal>  Disabled</para>
-                                               <para>  <literal>fax</literal>  FAX/data mode (NLP disabled if possible, otherwise
-                                                       completely disabled)</para>
-                                               <para>  <literal>voice</literal>        Voice mode (returns from FAX mode, reverting the changes that were made)</para>
-                                       </enum>
-                               </enumlist>
-                               <para><emphasis>chan_ooh323</emphasis> provides the following additional options:</para>
-                               <enumlist>
-                                       <enum name="faxdetect">
-                                               <para>R/W Fax Detect</para>
-                                               <para>Returns 0 or 1</para>
-                                               <para>Write yes or no</para>
-                                       </enum>
-                                       <enum name="t38support">
-                                               <para>R/W t38support</para>
-                                               <para>Returns 0 or 1</para>
-                                               <para>Write yes or no</para>
-                                       </enum>
-                                       <enum name="h323id_url">
-                                               <para>R/0 Returns caller URL</para>
-                                       </enum>
-                                       <enum name="caller_h323id">
-                                               <para>R/0 Returns caller h323id</para>
-                                       </enum>
-                                       <enum name="caller_dialeddigits">
-                                               <para>R/0 Returns caller dialed digits</para>
-                                       </enum>
-                                       <enum name="caller_email">
-                                               <para>R/0 Returns caller email</para>
-                                       </enum>
-                                       <enum name="callee_email">
-                                               <para>R/0 Returns callee email</para>
-                                       </enum>
-                                       <enum name="callee_dialeddigits">
-                                               <para>R/0 Returns callee dialed digits</para>
-                                       </enum>
-                                       <enum name="caller_url">
-                                               <para>R/0 Returns caller URL</para>
+                                       <enum name="callid">
+                                               <para>R/O Call identifier log tag associated with the channel
+                                               e.g., <literal>[C-00000000]</literal>.</para>
                                        </enum>
                                </enumlist>
+                               <xi:include xpointer="xpointer(/docs/info[@name='CHANNEL'])" />
                        </parameter>
                </syntax>
                <description>
                        <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
                        be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
                        requested that is not available on the current channel will return an empty string.</para>
+                       <example title="Standard CHANNEL item examples">
+                               ; Push a hangup handler subroutine existing at dialplan
+                               ; location default,s,1 onto the current channel
+                               same => n,Set(CHANNEL(hangup_handler_push)=default,s,1)
+
+                               ; Set the current tonezone to Germany (de)
+                               same => n,Set(CHANNEL(tonezone)=de)
+
+                               ; Set the allowed maximum number of forwarding attempts
+                               same => n,Set(CHANNEL(max_forwards)=10)
+
+                               ; If this channel is ejected from its next bridge, and if
+                               ; the channel is not hung up, begin executing dialplan at
+                               ; location default,after-bridge,1
+                               same => n,Set(CHANNEL(after_bridge_goto)=default,after-bridge,1)
+
+                               ; Log the current state of the channel
+                               same => n,Log(NOTICE, This channel is: ${CHANNEL(state)})
+                       </example>
+                       <xi:include xpointer="xpointer(/docs/info[@name='CHANNEL_EXAMPLES'])" />
                </description>
        </function>
  ***/
@@ -420,29 +297,40 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
        int ret = 0;
        struct ast_format_cap *tmpcap;
 
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
+               return -1;
+       }
+
        if (!strcasecmp(data, "audionativeformat")) {
-               char tmp[512];
+               tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+               if (tmpcap) {
+                       struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
 
-               if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_AUDIO))) {
-                       ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
-                       tmpcap = ast_format_cap_destroy(tmpcap);
+                       ast_channel_lock(chan);
+                       ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_AUDIO);
+                       ast_channel_unlock(chan);
+                       ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
+                       ao2_ref(tmpcap, -1);
                }
        } else if (!strcasecmp(data, "videonativeformat")) {
-               char tmp[512];
+               tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+               if (tmpcap) {
+                       struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
 
-               if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO))) {
-                       ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
-                       tmpcap = ast_format_cap_destroy(tmpcap);
+                       ast_channel_lock(chan);
+                       ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO);
+                       ast_channel_unlock(chan);
+                       ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
+                       ao2_ref(tmpcap, -1);
                }
        } else if (!strcasecmp(data, "audioreadformat")) {
-               ast_copy_string(buf, ast_getformatname(ast_channel_readformat(chan)), len);
+               locked_copy_string(chan, buf, ast_format_get_name(ast_channel_readformat(chan)), len);
        } else if (!strcasecmp(data, "audiowriteformat")) {
-               ast_copy_string(buf, ast_getformatname(ast_channel_writeformat(chan)), len);
+               locked_copy_string(chan, buf, ast_format_get_name(ast_channel_writeformat(chan)), len);
 #ifdef CHANNEL_TRACE
        } else if (!strcasecmp(data, "trace")) {
-               ast_channel_lock(chan);
-               ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
-               ast_channel_unlock(chan);
+               locked_copy_string(chan, buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
 #endif
        } else if (!strcasecmp(data, "tonezone") && ast_channel_zone(chan)) {
                locked_copy_string(chan, buf, ast_channel_zone(chan)->country, len);
@@ -460,14 +348,15 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                locked_copy_string(chan, buf, ast_channel_parkinglot(chan), len);
        else if (!strcasecmp(data, "state"))
                locked_copy_string(chan, buf, ast_state2str(ast_channel_state(chan)), len);
-       else if (!strcasecmp(data, "channeltype"))
+       else if (!strcasecmp(data, "onhold")) {
+               locked_copy_string(chan, buf,
+                       ast_channel_hold_state(chan) == AST_CONTROL_HOLD ? "1" : "0", len);
+       } else if (!strcasecmp(data, "channeltype"))
                locked_copy_string(chan, buf, ast_channel_tech(chan)->type, len);
        else if (!strcasecmp(data, "accountcode"))
                locked_copy_string(chan, buf, ast_channel_accountcode(chan), len);
        else if (!strcasecmp(data, "checkhangup")) {
-               ast_channel_lock(chan);
-               ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
-               ast_channel_unlock(chan);
+               locked_copy_string(chan, buf, ast_check_hangup(chan) ? "1" : "0", len);
        } else if (!strcasecmp(data, "peeraccount"))
                locked_copy_string(chan, buf, ast_channel_peeraccount(chan), len);
        else if (!strcasecmp(data, "hangupsource"))
@@ -495,22 +384,34 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                }
                ast_channel_unlock(chan);
        } else if (!strcasecmp(data, "peer")) {
-               struct ast_channel *p;
-
-               ast_channel_lock(chan);
-               p = ast_bridged_channel(chan);
-               if (p || ast_channel_tech(chan)) /* dummy channel? if so, we hid the peer name in the language */
-                       ast_copy_string(buf, (p ? ast_channel_name(p) : ""), len);
-               else {
-                       /* a dummy channel can still pass along bridged peer info via
-                           the BRIDGEPEER variable */
-                       const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
-                       if (!ast_strlen_zero(pname))
-                               ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
-                       else
-                               buf[0] = 0;
+               struct ast_channel *peer;
+
+               peer = ast_channel_bridge_peer(chan);
+               if (peer) {
+                       /* Only real channels could have a bridge peer this way. */
+                       ast_channel_lock(peer);
+                       ast_copy_string(buf, ast_channel_name(peer), len);
+                       ast_channel_unlock(peer);
+                       ast_channel_unref(peer);
+               } else {
+                       buf[0] = '\0';
+                       ast_channel_lock(chan);
+                       if (!ast_channel_tech(chan)) {
+                               const char *pname;
+
+                               /*
+                                * A dummy channel can still pass along bridged peer info
+                                * via the BRIDGEPEER variable.
+                                *
+                                * A horrible kludge, but... how else?
+                                */
+                               pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
+                               if (!ast_strlen_zero(pname)) {
+                                       ast_copy_string(buf, pname, len);
+                               }
+                       }
+                       ast_channel_unlock(chan);
                }
-               ast_channel_unlock(chan);
        } else if (!strcasecmp(data, "uniqueid")) {
                locked_copy_string(chan, buf, ast_channel_uniqueid(chan), len);
        } else if (!strcasecmp(data, "transfercapability")) {
@@ -535,14 +436,16 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                ast_bridge_read_after_goto(chan, buf, len);
        } else if (!strcasecmp(data, "amaflags")) {
                ast_channel_lock(chan);
-               snprintf(buf, len, "%d", ast_channel_amaflags(chan));
+               snprintf(buf, len, "%u", ast_channel_amaflags(chan));
                ast_channel_unlock(chan);
        } else if (!strncasecmp(data, "secure_bridge_", 14)) {
                struct ast_datastore *ds;
 
+               buf[0] = '\0';
                ast_channel_lock(chan);
                if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
                        struct ast_secure_call_store *encrypt = ds->data;
+
                        if (!strcasecmp(data, "secure_bridge_signaling")) {
                                snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
                        } else if (!strcasecmp(data, "secure_bridge_media")) {
@@ -550,6 +453,20 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                        }
                }
                ast_channel_unlock(chan);
+       } else if (!strcasecmp(data, "max_forwards")) {
+               ast_channel_lock(chan);
+               snprintf(buf, len, "%d", ast_max_forwards_get(chan));
+               ast_channel_unlock(chan);
+       } else if (!strcasecmp(data, "callid")) {
+               ast_callid callid;
+
+               buf[0] = '\0';
+               ast_channel_lock(chan);
+               callid = ast_channel_callid(chan);
+               if (callid) {
+                       ast_callid_strnprint(buf, len, callid);
+               }
+               ast_channel_unlock(chan);
        } 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)) {
                ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
                ret = -1;
@@ -581,18 +498,17 @@ static int func_channel_write_real(struct ast_channel *chan, const char *functio
                        ast_bridge_set_after_go_on(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), value);
                }
        } else if (!strcasecmp(data, "amaflags")) {
-               ast_channel_lock(chan);
+               int amaflags;
+
                if (isdigit(*value)) {
-                       int amaflags;
-                       sscanf(value, "%30d", &amaflags);
-                       ast_channel_amaflags_set(chan, amaflags);
-               } else if (!strcasecmp(value,"OMIT")){
-                       ast_channel_amaflags_set(chan, 1);
-               } else if (!strcasecmp(value,"BILLING")){
-                       ast_channel_amaflags_set(chan, 2);
-               } else if (!strcasecmp(value,"DOCUMENTATION")){
-                       ast_channel_amaflags_set(chan, 3);
+                       if (sscanf(value, "%30d", &amaflags) != 1) {
+                               amaflags = AST_AMA_NONE;
+                       }
+               } else {
+                       amaflags = ast_channel_string2amaflag(value);
                }
+               ast_channel_lock(chan);
+               ast_channel_amaflags_set(chan, amaflags);
                ast_channel_unlock(chan);
        } else if (!strcasecmp(data, "peeraccount"))
                locked_string_field_set(chan, peeraccount, value);
@@ -710,6 +626,16 @@ static int func_channel_write_real(struct ast_channel *chan, const char *functio
                        store->media = ast_true(value) ? 1 : 0;
                }
                ast_channel_unlock(chan);
+       } else if (!strcasecmp(data, "max_forwards")) {
+               int max_forwards;
+               if (sscanf(value, "%d", &max_forwards) != 1) {
+                       ast_log(LOG_WARNING, "Unable to set max forwards to '%s'\n", value);
+                       ret = -1;
+               } else {
+                       ast_channel_lock(chan);
+                       ret = ast_max_forwards_set(chan, max_forwards);
+                       ast_channel_unlock(chan);
+               }
        } else if (!ast_channel_tech(chan)->func_channel_write
                 || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
                ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
@@ -732,6 +658,11 @@ static int func_channel_write(struct ast_channel *chan, const char *function, ch
                .value = value,
        };
 
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
+               return -1;
+       }
+
        res = func_channel_write_real(chan, function, data, value);
        ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
 
@@ -805,8 +736,15 @@ static struct ast_custom_function channels_function = {
 static int func_mchan_read(struct ast_channel *chan, const char *function,
                             char *data, struct ast_str **buf, ssize_t len)
 {
-       struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
+       struct ast_channel *mchan;
        char *template = ast_alloca(4 + strlen(data));
+
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
+               return -1;
+       }
+
+       mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
        sprintf(template, "${%s}", data); /* SAFE */
        ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
        if (mchan) {
@@ -818,7 +756,14 @@ static int func_mchan_read(struct ast_channel *chan, const char *function,
 static int func_mchan_write(struct ast_channel *chan, const char *function,
                              char *data, const char *value)
 {
-       struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
+       struct ast_channel *mchan;
+
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
+               return -1;
+       }
+
+       mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
        pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
        if (mchan) {
                ast_channel_unref(mchan);