loader: Add dependency fields to module structures.
[asterisk/asterisk.git] / res / res_agi.c
index 7fe43f9..2d0dc27 100644 (file)
  *
  * \author Mark Spencer <markster@digium.com>
  *
- * \todo Convert the rest of the AGI commands over to XML documentation
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <depend>res_speech</depend>
+       <support_level>core</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk.h"
 
 #include <math.h>
 #include <signal.h>
@@ -57,11 +59,14 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/ast_version.h"
 #include "asterisk/speech.h"
 #include "asterisk/manager.h"
-#include "asterisk/features.h"
 #include "asterisk/term.h"
 #include "asterisk/xmldoc.h"
 #include "asterisk/srv.h"
 #include "asterisk/test.h"
+#include "asterisk/netsock2.h"
+#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_message_router.h"
+#include "asterisk/format_cache.h"
 
 #define AST_API_MODULE
 #include "asterisk/agi.h"
@@ -78,6 +83,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </description>
                <see-also>
                        <ref type="agi">hangup</ref>
+                       <ref type="application">AGI</ref>
                </see-also>
        </agi>
        <agi name="asyncagi break" language="en_US">
@@ -91,6 +97,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </description>
                <see-also>
                        <ref type="agi">hangup</ref>
+                       <ref type="application">AGI</ref>
                </see-also>
        </agi>
        <agi name="channel status" language="en_US">
@@ -131,6 +138,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                </enum>
                        </enumlist>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="control stream file" language="en_US">
                <synopsis>
@@ -143,20 +153,49 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <parameter name="escape_digits" required="true" />
                        <parameter name="skipms" />
                        <parameter name="ffchar">
-                               <para>Defaults to <literal>*</literal></para>
+                               <para>Defaults to <literal>#</literal></para>
                        </parameter>
                        <parameter name="rewchr">
-                               <para>Defaults to <literal>#</literal></para>
+                               <para>Defaults to <literal>*</literal></para>
                        </parameter>
                        <parameter name="pausechr" />
+                       <parameter name="offsetms">
+                               <para>Offset, in milliseconds, to start the audio playback</para>
+                       </parameter>
                </syntax>
                <description>
                        <para>Send the given file, allowing playback to be controlled by the given
                        digits, if any. Use double quotes for the digits if you wish none to be
-                       permitted. Returns <literal>0</literal> if playback completes without a digit
+                       permitted. If offsetms is provided then the audio will seek to offsetms
+                       before play starts. Returns <literal>0</literal> if playback completes without a digit
                        being pressed, or the ASCII numerical value of the digit if one was pressed,
-                       or <literal>-1</literal> on error or if the channel was disconnected.</para>
+                       or <literal>-1</literal> on error or if the channel was disconnected. Returns the
+                       position where playback was terminated as endpos.</para>
+
+                       <para>It sets the following channel variables upon completion:</para>
+                       <variablelist>
+                               <variable name="CPLAYBACKSTATUS">
+                                       <para>Contains the status of the attempt as a text string</para>
+                                       <value name="SUCCESS" />
+                                       <value name="USERSTOPPED" />
+                                       <value name="REMOTESTOPPED" />
+                                       <value name="ERROR" />
+                               </variable>
+                               <variable name="CPLAYBACKOFFSET">
+                                       <para>Contains the offset in ms into the file where playback
+                                       was at when it stopped. <literal>-1</literal> is end of file.</para>
+                               </variable>
+                               <variable name="CPLAYBACKSTOPKEY">
+                                       <para>If the playback is stopped by the user this variable contains
+                                       the key that was pressed.</para>
+                               </variable>
+                       </variablelist>
                </description>
+               <see-also>
+                       <ref type="agi">get option</ref>
+                       <ref type="agi">control stream file</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="database del" language="en_US">
                <synopsis>
@@ -172,6 +211,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Returns <literal>1</literal> if successful, <literal>0</literal>
                        otherwise.</para>
                </description>
+               <see-also>
+                       <ref type="agi">database get</ref>
+                       <ref type="agi">database put</ref>
+                       <ref type="agi">database deltree</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="database deltree" language="en_US">
                <synopsis>
@@ -186,6 +231,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        within a <replaceable>family</replaceable> in the Asterisk database.</para>
                        <para>Returns <literal>1</literal> if successful, <literal>0</literal> otherwise.</para>
                </description>
+               <see-also>
+                       <ref type="agi">database get</ref>
+                       <ref type="agi">database put</ref>
+                       <ref type="agi">database del</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="database get" language="en_US">
                <synopsis>
@@ -203,6 +254,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        in parenthesis.</para>
                        <para>Example return code: 200 result=1 (testvariable)</para>
                </description>
+               <see-also>
+                       <ref type="agi">database put</ref>
+                       <ref type="agi">database del</ref>
+                       <ref type="agi">database deltree</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="database put" language="en_US">
                <synopsis>
@@ -219,6 +276,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <replaceable>value</replaceable>.</para>
                        <para>Returns <literal>1</literal> if successful, <literal>0</literal> otherwise.</para>
                </description>
+               <see-also>
+                       <ref type="agi">database get</ref>
+                       <ref type="agi">database del</ref>
+                       <ref type="agi">database deltree</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="exec" language="en_US">
                <synopsis>
@@ -234,6 +297,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Returns whatever the <replaceable>application</replaceable> returns, or
                        <literal>-2</literal> on failure to find <replaceable>application</replaceable>.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="get data" language="en_US">
                <synopsis>
@@ -248,6 +314,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Stream the given <replaceable>file</replaceable>, and receive DTMF data.</para>
                        <para>Returns the digits received from the channel at the other end.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="get full variable" language="en_US">
                <synopsis>
@@ -264,6 +333,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        variables, unlike GET VARIABLE.</para>
                        <para>Example return code: 200 result=1 (testvariable)</para>
                </description>
+               <see-also>
+                       <ref type="agi">get variable</ref>
+                       <ref type="agi">set variable</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="get option" language="en_US">
                <synopsis>
@@ -279,6 +353,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </description>
                <see-also>
                        <ref type="agi">stream file</ref>
+                       <ref type="agi">control stream file</ref>
+                       <ref type="application">AGI</ref>
                </see-also>
        </agi>
        <agi name="get variable" language="en_US">
@@ -294,10 +370,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        the variable in parentheses.</para>
                        <para>Example return code: 200 result=1 (testvariable)</para>
                </description>
+               <see-also>
+                       <ref type="agi">get full variable</ref>
+                       <ref type="agi">set variable</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="hangup" language="en_US">
                <synopsis>
-                       Hangup the current channel.
+                       Hangup a channel.
                </synopsis>
                <syntax>
                        <parameter name="channelname" />
@@ -306,6 +387,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Hangs up the specified channel. If no channel name is given, hangs
                        up the current channel</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="noop" language="en_US">
                <synopsis>
@@ -315,6 +399,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Does nothing.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="receive char" language="en_US">
                <synopsis>
@@ -332,6 +419,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        if one is received, or <literal>0</literal> if the channel does not support
                        text reception. Returns <literal>-1</literal> only on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">receive text</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="receive text" language="en_US">
                <synopsis>
@@ -344,23 +435,49 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        </parameter>
                </syntax>
                <description>
-                       <para>Receives a string of text on a channel. Most channels 
+                       <para>Receives a string of text on a channel. Most channels
                        do not support the reception of text. Returns <literal>-1</literal> for failure
-                       or <literal>1</literal> for success, and the string in parenthesis.</para> 
+                       or <literal>1</literal> for success, and the string in parenthesis.</para>
                </description>
+               <see-also>
+                       <ref type="agi">receive char</ref>
+                       <ref type="agi">send text</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="record file" language="en_US">
                <synopsis>
                        Records to a given file.
                </synopsis>
                <syntax>
-                       <parameter name="filename" required="true" />
-                       <parameter name="format" required="true" />
-                       <parameter name="escape_digits" required="true" />
-                       <parameter name="timeout" required="true" />
-                       <parameter name="offset samples" />
-                       <parameter name="BEEP" />
-                       <parameter name="s=silence" />
+                       <parameter name="filename" required="true">
+                               <para>The destination filename of the recorded audio.</para>
+                       </parameter>
+                       <parameter name="format" required="true">
+                               <para>The audio format in which to save the resulting file.</para>
+                       </parameter>
+                       <parameter name="escape_digits" required="true">
+                               <para>The DTMF digits that will terminate the recording process.</para>
+                       </parameter>
+                       <parameter name="timeout" required="true">
+                               <para>The maximum recording time in milliseconds. Set to -1 for no
+                               limit.</para>
+                       </parameter>
+                       <parameter name="offset_samples">
+                               <para>Causes the recording to first seek to the specified offset before
+                               recording begins.</para>
+                       </parameter>
+                       <parameter name="beep">
+                               <para>Causes Asterisk to play a beep as recording begins. This argument
+                               can take any value.</para>
+                       </parameter>
+                       <parameter name="s=silence">
+                               <para>The number of seconds of silence that are permitted before the
+                               recording is terminated, regardless of the
+                               <replaceable>escape_digits</replaceable> or <replaceable>timeout</replaceable>
+                               arguments. If specified, this parameter must be preceded by
+                               <literal>s=</literal>.</para>
+                       </parameter>
                </syntax>
                <description>
                        <para>Record to a file until a given dtmf digit in the sequence is received.
@@ -368,11 +485,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        will be recorded. The <replaceable>timeout</replaceable> is the maximum record time in
                        milliseconds, or <literal>-1</literal> for no <replaceable>timeout</replaceable>.
                        <replaceable>offset samples</replaceable> is optional, and, if provided, will seek
-                       to the offset without exceeding the end of the file. <replaceable>silence</replaceable> is
+                       to the offset without exceeding the end of the
+                       file. <replaceable>beep</replaceable> can take any value, and causes Asterisk
+                       to play a beep to the channel that is about to be recorded. <replaceable>silence</replaceable> is
                        the number of seconds of silence allowed before the function returns despite the
                        lack of dtmf digits or reaching <replaceable>timeout</replaceable>. <replaceable>silence</replaceable>
                        value must be preceded by <literal>s=</literal> and is also optional.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say alpha" language="en_US">
                <synopsis>
@@ -388,6 +510,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        without a digit being pressed, or the ASCII numerical value of the digit if one
                        was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say digits" language="en_US">
                <synopsis>
@@ -403,6 +534,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        without a digit being pressed, or the ASCII numerical value of the digit if one
                        was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say number" language="en_US">
                <synopsis>
@@ -419,6 +559,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        completes without a digit being pressed, or the ASCII numerical value of
                        the digit if one was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say phonetic" language="en_US">
                <synopsis>
@@ -434,6 +583,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        playback completes without a digit pressed, the ASCII numerical value of the digit
                        if one was pressed, or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say date" language="en_US">
                <synopsis>
@@ -452,6 +610,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        completes without a digit being pressed, or the ASCII numerical value of the
                        digit if one was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say time" language="en_US">
                <synopsis>
@@ -470,6 +637,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        without a digit being pressed, or the ASCII numerical value of the digit if
                        one was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say datetime</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="say datetime" language="en_US">
                <synopsis>
@@ -497,6 +673,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        completes without a digit being pressed, or the ASCII numerical value of the
                        digit if one was pressed or <literal>-1</literal> on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">say alpha</ref>
+                       <ref type="agi">say digits</ref>
+                       <ref type="agi">say number</ref>
+                       <ref type="agi">say phonetic</ref>
+                       <ref type="agi">say date</ref>
+                       <ref type="agi">say time</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="send image" language="en_US">
                <synopsis>
@@ -511,6 +696,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        the channel does not support image transmission.  Returns <literal>-1</literal>
                        only on error/hangup. Image names should not include extensions.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="send text" language="en_US">
                <synopsis>
@@ -528,6 +716,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        channel does not support text transmission. Returns <literal>-1</literal> only
                        on error/hangup.</para>
                </description>
+               <see-also>
+                       <ref type="agi">receive text</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set autohangup" language="en_US">
                <synopsis>
@@ -541,6 +733,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        seconds in the future. Of course it can be hungup before then as well. Setting to
                        <literal>0</literal> will cause the autohangup feature to be disabled on this channel.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set callerid" language="en_US">
                <synopsis>
@@ -552,6 +747,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Changes the callerid of the current channel.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set context" language="en_US">
                <synopsis>
@@ -563,6 +761,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Sets the context for continuation upon exiting the application.</para>
                </description>
+               <see-also>
+                       <ref type="agi">set extension</ref>
+                       <ref type="agi">set priority</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set extension" language="en_US">
                <synopsis>
@@ -574,6 +777,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Changes the extension for continuation upon exiting the application.</para>
                </description>
+               <see-also>
+                       <ref type="agi">set context</ref>
+                       <ref type="agi">set priority</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set music" language="en_US">
                <synopsis>
@@ -595,9 +803,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Enables/Disables the music on hold generator. If <replaceable>class</replaceable>
                        is not specified, then the <literal>default</literal> music on hold class will be
-                       used.</para>
+                       used. This generator will be stopped automatically when playing a file.</para>
                        <para>Always returns <literal>0</literal>.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set priority" language="en_US">
                <synopsis>
@@ -610,6 +821,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Changes the priority for continuation upon exiting the application.
                        The priority must be a valid priority or label.</para>
                </description>
+               <see-also>
+                       <ref type="agi">set context</ref>
+                       <ref type="agi">set extension</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="set variable" language="en_US">
                <synopsis>
@@ -622,6 +838,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Sets a variable to the current channel.</para>
                </description>
+               <see-also>
+                       <ref type="agi">get variable</ref>
+                       <ref type="agi">get full variable</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="stream file" language="en_US">
                <synopsis>
@@ -645,10 +866,22 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Send the given file, allowing playback to be interrupted by the given
                        digits, if any. Returns <literal>0</literal> if playback completes without a digit
                        being pressed, or the ASCII numerical value of the digit if one was pressed,
-                       or <literal>-1</literal> on error or if the channel was disconnected.</para>
+                       or <literal>-1</literal> on error or if the channel was disconnected. If
+                       musiconhold is playing before calling stream file it will be automatically
+                       stopped and will not be restarted after completion.</para>
+                       <para>It sets the following channel variables upon completion:</para>
+                       <variablelist>
+                               <variable name="PLAYBACKSTATUS">
+                                       <para>The status of the playback attempt as a text string.</para>
+                                       <value name="SUCCESS"/>
+                                       <value name="FAILED"/>
+                               </variable>
+                       </variablelist>
                </description>
                <see-also>
                        <ref type="agi">control stream file</ref>
+                       <ref type="agi">get option</ref>
+                       <ref type="application">AGI</ref>
                </see-also>
        </agi>
        <agi name="tdd mode" language="en_US">
@@ -667,6 +900,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Enable/Disable TDD transmission/reception on a channel. Returns <literal>1</literal> if
                        successful, or <literal>0</literal> if channel is not TDD-capable.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="verbose" language="en_US">
                <synopsis>
@@ -681,6 +917,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        message system. <replaceable>level</replaceable> is the verbose level (1-4).
                        Always returns <literal>1</literal></para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="wait for digit" language="en_US">
                <synopsis>
@@ -688,22 +927,17 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </synopsis>
                <syntax>
                        <parameter name="timeout" required="true" />
-                       <parameter name="voicefile" required="false" />
-                       <parameter name="escape_chars" required="false" />
-                       <parameter name="maxdigits" required="false" />
-                       <parameter name="previously_die_on_chars" required="false" />
                </syntax>
                <description>
                        <para>Waits up to <replaceable>timeout</replaceable> milliseconds for channel to
                        receive a DTMF digit. Returns <literal>-1</literal> on channel failure, <literal>0</literal>
                        if no digit is received in the timeout, or the numerical value of the ascii of the digit if
                        one is received. Use <literal>-1</literal> for the <replaceable>timeout</replaceable> value if
-                       you desire the call to block indefinitely.
-
-                       If 'voicefile' is specified it is played as long 'previously die chars' (default '#') are
-                       not typed in or as long as any of 'escape chars' (default '1234567890*#ABCD') is pressed a
-                       'maxdigit' (default 1) times .</para>
+                       you desire the call to block indefinitely.</para>
                </description>
+               <see-also>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech create" language="en_US">
                <synopsis>
@@ -715,6 +949,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Create a speech object to be used by the other Speech AGI commands.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech set" language="en_US">
                <synopsis>
@@ -727,6 +971,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Set an engine-specific setting.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech destroy" language="en_US">
                <synopsis>
@@ -739,6 +993,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </description>
                <see-also>
                        <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
                </see-also>
        </agi>
        <agi name="speech load grammar" language="en_US">
@@ -752,6 +1013,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Loads the specified grammar as the specified name.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech unload grammar" language="en_US">
                <synopsis>
@@ -763,6 +1034,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Unloads the specified grammar.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech activate grammar" language="en_US">
                <synopsis>
@@ -774,6 +1055,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Activates the specified grammar on the speech object.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech deactivate grammar" language="en_US">
                <synopsis>
@@ -785,6 +1076,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Deactivates the specified grammar on the speech object.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech recognize</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <agi name="speech recognize" language="en_US">
                <synopsis>
@@ -799,14 +1100,27 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Plays back given <replaceable>prompt</replaceable> while listening for
                        speech and dtmf.</para>
                </description>
+               <see-also>
+                       <ref type="agi">speech create</ref>
+                       <ref type="agi">speech set</ref>
+                       <ref type="agi">speech destroy</ref>
+                       <ref type="agi">speech load grammar</ref>
+                       <ref type="agi">speech unload grammar</ref>
+                       <ref type="agi">speech activate grammar</ref>
+                       <ref type="agi">speech deactivate grammar</ref>
+                       <ref type="application">AGI</ref>
+               </see-also>
        </agi>
        <application name="AGI" language="en_US">
                <synopsis>
                        Executes an AGI compliant application.
                </synopsis>
                <syntax>
-                       <parameter name="command" required="true" />
+                       <parameter name="command" required="true">
+                               <para>How AGI should be invoked on the channel.</para>
+                       </parameter>
                        <parameter name="args">
+                               <para>Arguments to pass to the AGI script or server.</para>
                                <argument name="arg1" required="true" />
                                <argument name="arg2" multiple="yes" />
                        </parameter>
@@ -815,18 +1129,72 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>Executes an Asterisk Gateway Interface compliant
                        program on a channel. AGI allows Asterisk to launch external programs written
                        in any language to control a telephony channel, play audio, read DTMF digits,
-                       etc. by communicating with the AGI protocol on <emphasis>stdin</emphasis> and
-                       <emphasis>stdout</emphasis>. As of <literal>1.6.0</literal>, this channel will
+                       etc. by communicating with the AGI protocol.</para>
+                       <para>The following variants of AGI exist, and are chosen based on the value
+                       passed to <replaceable>command</replaceable>:</para>
+                       <enumlist>
+                               <enum name="AGI">
+                                       <para>The classic variant of AGI, this will launch the script
+                                       specified by <replaceable>command</replaceable> as a new process.
+                                       Communication with the script occurs on <literal>stdin</literal> and
+                                       <literal>stdout</literal>. If the full path to the script is not
+                                       provided, the <directory>astagidir</directory> specified in
+                                       <filename>asterisk.conf</filename> will be used.
+                                       </para>
+                               </enum>
+                               <enum name="FastAGI">
+                                       <para>Connect Asterisk to a FastAGI server using a TCP connection.
+                                       The URI to the FastAGI server should be given in the form
+                                       <literal>[scheme]://host.domain[:port][/script/name]</literal>,
+                                       where <replaceable>scheme</replaceable> is either <literal>agi</literal>
+                                       or <literal>hagi</literal>.</para>
+                                       <para>In the case of <literal>hagi</literal>, an SRV lookup will be
+                                       performed to try to connect to a list of FastAGI servers. The hostname in
+                                       the URI must be prefixed with <literal>_agi._tcp</literal>. prior to the DNS resolution. For
+                                       example, if you specify the URI <literal>hagi://agi.example.com/foo.agi</literal>
+                                       the DNS query would be for <literal>_agi._tcp.agi.example.com</literal>. You
+                                       will need to make sure this resolves correctly.</para>
+                               </enum>
+                               <enum name="AsyncAGI">
+                                       <para>Use AMI to control the channel in AGI. AGI commands can be invoked
+                                       using the <literal>AMI</literal> action, with a variety of AGI specific
+                                       events passed back over the AMI connection. AsyncAGI should be invoked
+                                       by passing <literal>agi:async</literal> to the <replaceable>command</replaceable>
+                                       parameter.</para>
+                               </enum>
+                       </enumlist>
+                       <note>
+                       <para>As of <literal>1.6.0</literal>, this channel will
                        not stop dialplan execution on hangup inside of this application. Dialplan
                        execution will continue normally, even upon hangup until the AGI application
                        signals a desire to stop (either by exiting or, in the case of a net script, by
-                       closing the connection). A locally executed AGI script will receive SIGHUP on
-                       hangup from the channel except when using DeadAGI. A fast AGI server will
-                       correspondingly receive a HANGUP inline with the command dialog. Both of theses
-                       signals may be disabled by setting the <variable>AGISIGHUP</variable> channel
-                       variable to <literal>no</literal> before executing the AGI application.</para>
-                       <para>Use the CLI command <literal>agi show commands</literal> to list available agi
-                       commands.</para>
+                       closing the connection).</para>
+                       <para>A locally executed AGI script will receive <literal>SIGHUP</literal> on
+                       hangup from the channel except when using <literal>DeadAGI</literal>
+                       (or when the channel is already hungup). A fast AGI server will
+                       correspondingly receive a <literal>HANGUP</literal> inline with the command dialog.
+                       Both of these signals may be disabled by setting the <variable>AGISIGHUP</variable>
+                       channel variable to <literal>no</literal> before executing the AGI application.
+                       Alternatively, if you would like the AGI application to exit immediately
+                       after a channel hangup is detected, set the <variable>AGIEXITONHANGUP</variable>
+                       variable to <literal>yes</literal>.</para>
+                       </note>
+                       <example title="AGI invocation examples">
+                               ; Start the AGI script /tmp/my-cool-script.sh, passing it the contents
+                               ; of the channel variable FOO
+                               same => n,AGI(/tmp/my-cool-script.sh,${FOO})
+
+                               ; Start the AGI script my-cool-script.sh located in the astagidir
+                               ; directory, specified in asterisk.conf
+                               same => n,AGI(my-cool-script.sh)
+
+                               ; Connect to the FastAGI server located at 127.0.0.1 and start the script
+                               ; awesome-script
+                               same => n,AGI(agi://127.0.0.1/awesome-script)
+
+                               ; Start AsyncAGI
+                               same => n,AGI(agi:async)
+                       </example>
                        <para>This application sets the following channel variable upon completion:</para>
                        <variablelist>
                                <variable name="AGISTATUS">
@@ -840,8 +1208,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        </variablelist>
                </description>
                <see-also>
+                       <ref type="manager">AGI</ref>
+                       <ref type="managerEvent">AsyncAGIStart</ref>
+                       <ref type="managerEvent">AsyncAGIEnd</ref>
                        <ref type="application">EAGI</ref>
                        <ref type="application">DeadAGI</ref>
+                       <ref type="filename">asterisk.conf</ref>
                </see-also>
        </application>
        <application name="EAGI" language="en_US">
@@ -854,8 +1226,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </syntax>
                <description>
                        <para>Using 'EAGI' provides enhanced AGI, with incoming audio available out of band
-                       on file descriptor 3.</para>
-                       <xi:include xpointer="xpointer(/docs/application[@name='AGI']/description/para)" />
+                       on file descriptor 3. In all other respects, it behaves in the same fashion as
+                       AGI. See the documentation for the <literal>AGI</literal> dialplan application for
+                       more information on invoking AGI on a channel.</para>
+                       <para>This application sets the following channel variable upon completion:</para>
                        <xi:include xpointer="xpointer(/docs/application[@name='AGI']/description/variablelist)" />
                </description>
                <see-also>
@@ -872,7 +1246,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <xi:include xpointer="xpointer(/docs/application[@name='AGI']/syntax/parameter[@name='args'])" />
                </syntax>
                <description>
-                       <xi:include xpointer="xpointer(/docs/application[@name='AGI']/description/para)" />
+                       <warning>
+                               <para>This application is deprecated and may be removed in a future version
+                               of Asterisk. Use the replacement application <literal>AGI</literal> instead
+                               of <literal>DeadAGI</literal>.
+                               </para>
+                       </warning>
+                       <para>Execute AGI on a 'dead' or hungup channel. See the documentation for the
+                       <literal>AGI</literal> dialplan application for more information on invoking
+                       AGI on a channel.</para>
+                       <para>This application sets the following channel variable upon completion:</para>
                        <xi:include xpointer="xpointer(/docs/application[@name='AGI']/description/variablelist)" />
                </description>
                <see-also>
@@ -900,7 +1283,100 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                <description>
                        <para>Add an AGI command to the execute queue of the channel in Async AGI.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">AsyncAGIStart</ref>
+                       <ref type="managerEvent">AsyncAGIExec</ref>
+                       <ref type="managerEvent">AsyncAGIEnd</ref>
+               </see-also>
        </manager>
+       <managerEvent language="en_US" name="AsyncAGIStart">
+               <managerEventInstance class="EVENT_FLAG_AGI">
+                       <synopsis>Raised when a channel starts AsyncAGI command processing.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <parameter name="Env">
+                                       <para>URL encoded string read from the AsyncAGI server.</para>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="managerEvent">AsyncAGIEnd</ref>
+                               <ref type="managerEvent">AsyncAGIExec</ref>
+                               <ref type="application">AGI</ref>
+                               <ref type="manager">AGI</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="AsyncAGIEnd">
+               <managerEventInstance class="EVENT_FLAG_AGI">
+                       <synopsis>Raised when a channel stops AsyncAGI command processing.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                       </syntax>
+                       <see-also>
+                               <ref type="managerEvent">AsyncAGIStart</ref>
+                               <ref type="managerEvent">AsyncAGIExec</ref>
+                               <ref type="application">AGI</ref>
+                               <ref type="manager">AGI</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="AsyncAGIExec">
+               <managerEventInstance class="EVENT_FLAG_AGI">
+                       <synopsis>Raised when AsyncAGI completes an AGI command.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <parameter name="CommandID" required="false">
+                                       <para>Optional command ID sent by the AsyncAGI server to identify the command.</para>
+                               </parameter>
+                               <parameter name="Result">
+                                       <para>URL encoded result string from the executed AGI command.</para>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="managerEvent">AsyncAGIStart</ref>
+                               <ref type="managerEvent">AsyncAGIEnd</ref>
+                               <ref type="application">AGI</ref>
+                               <ref type="manager">AGI</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="AGIExecStart">
+               <managerEventInstance class="EVENT_FLAG_AGI">
+                       <synopsis>Raised when a received AGI command starts processing.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <parameter name="Command">
+                                       <para>The AGI command as received from the external source.</para>
+                               </parameter>
+                               <parameter name="CommandId">
+                                       <para>Random identification number assigned to the execution of this command.</para>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="managerEvent">AGIExecEnd</ref>
+                               <ref type="application">AGI</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="AGIExecEnd">
+               <managerEventInstance class="EVENT_FLAG_AGI">
+                       <synopsis>Raised when a received AGI command completes processing.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='AGIExecStart']/managerEventInstance/syntax/parameter)" />
+                               <parameter name="ResultCode">
+                                       <para>The numeric result code from AGI</para>
+                               </parameter>
+                               <parameter name="Result">
+                                       <para>The text result reason from AGI</para>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="managerEvent">AGIExecStart</ref>
+                               <ref type="application">AGI</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
  ***/
 
 #define MAX_ARGS 128
@@ -924,6 +1400,9 @@ static int agidebug = 0;
 
 #define AGI_PORT 4573
 
+/*! Special return code for "asyncagi break" command. */
+#define ASYNC_AGI_BREAK        3
+
 enum agi_result {
        AGI_RESULT_FAILURE = -1,
        AGI_RESULT_SUCCESS,
@@ -933,6 +1412,66 @@ enum agi_result {
        AGI_RESULT_HANGUP,
 };
 
+static struct ast_manager_event_blob *agi_channel_to_ami(const char *type, struct stasis_message *message)
+{
+       struct ast_channel_blob *obj = stasis_message_data(message);
+       RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
+       RAII_VAR(struct ast_str *, event_string, NULL, ast_free);
+
+       channel_string = ast_manager_build_channel_state_string(obj->snapshot);
+       event_string = ast_manager_str_from_json_object(obj->blob, NULL);
+       if (!channel_string || !event_string) {
+               return NULL;
+       }
+
+       return ast_manager_event_blob_create(EVENT_FLAG_AGI, type,
+               "%s"
+               "%s",
+               ast_str_buffer(channel_string),
+               ast_str_buffer(event_string));
+}
+
+static struct ast_manager_event_blob *agi_exec_start_to_ami(struct stasis_message *message)
+{
+       return agi_channel_to_ami("AGIExecStart", message);
+}
+
+static struct ast_manager_event_blob *agi_exec_end_to_ami(struct stasis_message *message)
+{
+       return agi_channel_to_ami("AGIExecEnd", message);
+}
+
+static struct ast_manager_event_blob *agi_async_start_to_ami(struct stasis_message *message)
+{
+       return agi_channel_to_ami("AsyncAGIStart", message);
+}
+
+static struct ast_manager_event_blob *agi_async_exec_to_ami(struct stasis_message *message)
+{
+       return agi_channel_to_ami("AsyncAGIExec", message);
+}
+
+static struct ast_manager_event_blob *agi_async_end_to_ami(struct stasis_message *message)
+{
+       return agi_channel_to_ami("AsyncAGIEnd", message);
+}
+
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_exec_start_type,
+       .to_ami = agi_exec_start_to_ami,
+       );
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_exec_end_type,
+       .to_ami = agi_exec_end_to_ami,
+       );
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_start_type,
+       .to_ami = agi_async_start_to_ami,
+       );
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_exec_type,
+       .to_ami = agi_async_exec_to_ami,
+       );
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_end_type,
+       .to_ami = agi_async_end_to_ami,
+       );
+
 static agi_command *find_command(const char * const cmds[], int exact);
 
 AST_THREADSTORAGE(agi_buf);
@@ -958,7 +1497,7 @@ int AST_OPTIONAL_API_NAME(ast_agi_send)(int fd, struct ast_channel *chan, char *
 
        if (agidebug) {
                if (chan) {
-                       ast_verbose("<%s>AGI Tx >> %s", chan->name, ast_str_buffer(buf));
+                       ast_verbose("<%s>AGI Tx >> %s", ast_channel_name(chan), ast_str_buffer(buf));
                } else {
                        ast_verbose("AGI Tx >> %s", ast_str_buffer(buf));
                }
@@ -1001,24 +1540,34 @@ static const struct ast_datastore_info agi_commands_datastore_info = {
        .destroy = agi_destroy_commands_cb
 };
 
-static struct agi_cmd *get_agi_cmd(struct ast_channel *chan)
+/*!
+ * \brief Retrieve the list head to the requested channel's AGI datastore
+ * \param chan Channel datastore is requested for
+ * \param cmd Pointer to the struct pointer which will reference the head of the agi command list.
+ *
+ * \retval 0 if the datastore was valid and the list head was retrieved appropriately (even if it's
+ *           NULL and the list is empty)
+ * \retval -1 if the datastore could not be retrieved causing an error
+*/
+static int get_agi_cmd(struct ast_channel *chan, struct agi_cmd **cmd)
 {
        struct ast_datastore *store;
-       struct agi_cmd *cmd;
        AST_LIST_HEAD(, agi_cmd) *agi_commands;
 
        ast_channel_lock(chan);
        store = ast_channel_datastore_find(chan, &agi_commands_datastore_info, NULL);
        ast_channel_unlock(chan);
        if (!store) {
-               ast_log(LOG_ERROR, "Hu? datastore disappeared at Async AGI on Channel %s!\n", chan->name);
-               return NULL;
+               ast_log(LOG_ERROR, "Huh? Async AGI datastore disappeared on Channel %s!\n",
+                       ast_channel_name(chan));
+               *cmd = NULL;
+               return -1;
        }
        agi_commands = store->data;
        AST_LIST_LOCK(agi_commands);
-       cmd = AST_LIST_REMOVE_HEAD(agi_commands, entry);
+       *cmd = AST_LIST_REMOVE_HEAD(agi_commands, entry);
        AST_LIST_UNLOCK(agi_commands);
-       return cmd;
+       return 0;
 }
 
 /* channel is locked when calling this one either from the CLI or manager thread */
@@ -1030,7 +1579,7 @@ static int add_agi_cmd(struct ast_channel *chan, const char *cmd_buff, const cha
 
        store = ast_channel_datastore_find(chan, &agi_commands_datastore_info, NULL);
        if (!store) {
-               ast_log(LOG_WARNING, "Channel %s is not at Async AGI.\n", chan->name);
+               ast_log(LOG_WARNING, "Channel %s is not setup for Async AGI.\n", ast_channel_name(chan));
                return -1;
        }
        agi_commands = store->data;
@@ -1119,18 +1668,20 @@ static char *handle_cli_agi_add_cmd(struct ast_cli_entry *e, int cmd, struct ast
        }
 
        if (!(chan = ast_channel_get_by_name(a->argv[2]))) {
-               ast_log(LOG_WARNING, "Channel %s does not exists or cannot lock it\n", a->argv[2]);
+               ast_cli(a->fd, "Channel %s does not exist.\n", a->argv[2]);
                return CLI_FAILURE;
        }
 
+       ast_channel_lock(chan);
+
        if (add_agi_cmd(chan, a->argv[3], (a->argc > 4 ? a->argv[4] : ""))) {
-               ast_log(LOG_WARNING, "failed to add AGI command to queue of channel %s\n", chan->name);
+               ast_cli(a->fd, "Failed to add AGI command to queue of channel %s\n", ast_channel_name(chan));
                ast_channel_unlock(chan);
                chan = ast_channel_unref(chan);
                return CLI_FAILURE;
        }
 
-       ast_log(LOG_DEBUG, "Added AGI command to channel %s queue\n", chan->name);
+       ast_debug(1, "Added AGI command to channel %s queue\n", ast_channel_name(chan));
 
        ast_channel_unlock(chan);
        chan = ast_channel_unref(chan);
@@ -1163,7 +1714,7 @@ static int action_add_agi_cmd(struct mansession *s, const struct message *m)
        }
 
        if (!(chan = ast_channel_get_by_name(channel))) {
-               snprintf(buf, sizeof(buf), "Channel %s does not exists or cannot get its lock", channel);
+               snprintf(buf, sizeof(buf), "Channel %s does not exist.", channel);
                astman_send_error(s, m, buf);
                return 0;
        }
@@ -1171,7 +1722,7 @@ static int action_add_agi_cmd(struct mansession *s, const struct message *m)
        ast_channel_lock(chan);
 
        if (add_agi_cmd(chan, cmdbuff, cmdid)) {
-               snprintf(buf, sizeof(buf), "Failed to add AGI command to channel %s queue", chan->name);
+               snprintf(buf, sizeof(buf), "Failed to add AGI command to channel %s queue", ast_channel_name(chan));
                astman_send_error(s, m, buf);
                ast_channel_unlock(chan);
                chan = ast_channel_unref(chan);
@@ -1186,9 +1737,48 @@ static int action_add_agi_cmd(struct mansession *s, const struct message *m)
        return 0;
 }
 
-static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead);
+static enum agi_result agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead);
 static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced, int argc, char *argv[]);
-static enum agi_result launch_asyncagi(struct ast_channel *chan, char *argv[], int *efd)
+
+/*!
+ * \internal
+ * \brief Read and handle a channel frame for Async AGI.
+ *
+ * \param chan Channel to read a frame from.
+ *
+ * \retval AGI_RESULT_SUCCESS on success.
+ * \retval AGI_RESULT_HANGUP on hangup.
+ * \retval AGI_RESULT_FAILURE on error.
+ */
+static enum agi_result async_agi_read_frame(struct ast_channel *chan)
+{
+       struct ast_frame *f;
+
+       f = ast_read(chan);
+       if (!f) {
+               ast_debug(3, "No frame read on channel %s, going out ...\n", ast_channel_name(chan));
+               return AGI_RESULT_HANGUP;
+       }
+       if (f->frametype == AST_FRAME_CONTROL) {
+               /*
+                * Is there any other frame we should care about besides
+                * AST_CONTROL_HANGUP?
+                */
+               switch (f->subclass.integer) {
+               case AST_CONTROL_HANGUP:
+                       ast_debug(3, "Got HANGUP frame on channel %s, going out ...\n", ast_channel_name(chan));
+                       ast_frfree(f);
+                       return AGI_RESULT_HANGUP;
+               default:
+                       break;
+               }
+       }
+       ast_frfree(f);
+
+       return AGI_RESULT_SUCCESS;
+}
+
+static enum agi_result launch_asyncagi(struct ast_channel *chan, int argc, char *argv[], int *efd)
 {
 /* This buffer sizes might cause truncation if the AGI command writes more data
    than AGI_BUF_SIZE as result. But let's be serious, is there an AGI command
@@ -1209,14 +1799,17 @@ static enum agi_result launch_asyncagi(struct ast_channel *chan, char *argv[], i
  */
 #define AGI_BUF_SIZE 1024
 #define AMI_BUF_SIZE 2048
-       struct ast_frame *f;
+       enum agi_result cmd_status;
        struct agi_cmd *cmd;
-       int res, fds[2];
+       int res;
+       int fds[2];
+       int hungup;
        int timeout = 100;
        char agi_buffer[AGI_BUF_SIZE + 1];
        char ami_buffer[AMI_BUF_SIZE];
-       enum agi_result returnstatus = AGI_RESULT_SUCCESS_ASYNC;
+       enum agi_result returnstatus = AGI_RESULT_SUCCESS;
        AGI async_agi;
+       RAII_VAR(struct ast_json *, startblob, NULL, ast_json_unref);
 
        if (efd) {
                ast_log(LOG_WARNING, "Async AGI does not support Enhanced AGI yet\n");
@@ -1225,7 +1818,7 @@ static enum agi_result launch_asyncagi(struct ast_channel *chan, char *argv[], i
 
        /* add AsyncAGI datastore to the channel */
        if (add_to_agi(chan)) {
-               ast_log(LOG_ERROR, "failed to start Async AGI on channel %s\n", chan->name);
+               ast_log(LOG_ERROR, "Failed to start Async AGI on channel %s\n", ast_channel_name(chan));
                return AGI_RESULT_FAILURE;
        }
 
@@ -1233,10 +1826,12 @@ static enum agi_result launch_asyncagi(struct ast_channel *chan, char *argv[], i
           the AGI commands */
        res = pipe(fds);
        if (res) {
-               ast_log(LOG_ERROR, "failed to create Async AGI pipe\n");
-               /* intentionally do not remove datastore, added with
-                  add_to_agi(), from channel. It will be removed when
-                  the channel is hung up anyways */
+               ast_log(LOG_ERROR, "Failed to create Async AGI pipe\n");
+               /*
+                * Intentionally do not remove the datastore added with
+                * add_to_agi() the from channel.  It will be removed when the
+                * channel is hung up anyway.
+                */
                return AGI_RESULT_FAILURE;
        }
 
@@ -1250,117 +1845,215 @@ static enum agi_result launch_asyncagi(struct ast_channel *chan, char *argv[], i
 
        /* notify possible manager users of a new channel ready to
           receive commands */
-       setup_env(chan, "async", fds[1], 0, 0, NULL);
+       setup_env(chan, "async", fds[1], 0, argc, argv);
        /* read the environment */
        res = read(fds[0], agi_buffer, AGI_BUF_SIZE);
-       if (!res) {
-               ast_log(LOG_ERROR, "failed to read from Async AGI pipe on channel %s\n", chan->name);
+       if (res <= 0) {
+               ast_log(LOG_ERROR, "Failed to read from Async AGI pipe on channel %s: %s\n",
+                               ast_channel_name(chan), res < 0 ? strerror(errno) : "EOF");
                returnstatus = AGI_RESULT_FAILURE;
-               goto quit;
+               goto async_agi_abort;
        }
        agi_buffer[res] = '\0';
        /* encode it and send it thru the manager so whoever is going to take
           care of AGI commands on this channel can decide which AGI commands
           to execute based on the setup info */
-       ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, 1);
-       manager_event(EVENT_FLAG_AGI, "AsyncAGI", "SubEvent: Start\r\nChannel: %s\r\nEnv: %s\r\n", chan->name, ami_buffer);
-       while (1) {
-               /* bail out if we need to hangup */
-               if (ast_check_hangup(chan)) {
-                       ast_log(LOG_DEBUG, "ast_check_hangup returned true on chan %s\n", chan->name);
-                       break;
-               }
-               /* retrieve a command
-                  (commands are added via the manager or the cli threads) */
-               cmd = get_agi_cmd(chan);
-               if (cmd) {
-                       /* OK, we have a command, let's call the
-                          command handler. */
-                       res = agi_handle_command(chan, &async_agi, cmd->cmd_buffer, 0);
-                       if (res < 0) {
-                               free_agi_cmd(cmd);
+       ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, ast_uri_http);
+       startblob = ast_json_pack("{s: s}", "Env", ami_buffer);
+
+       ast_channel_publish_cached_blob(chan, agi_async_start_type(), startblob);
+
+       hungup = ast_check_hangup_locked(chan);
+
+       for (;;) {
+               /*
+                * Process as many commands as we can.  Commands are added via
+                * the manager or the cli threads.
+                */
+               while (!hungup) {
+                       RAII_VAR(struct ast_json *, execblob, NULL, ast_json_unref);
+                       res = get_agi_cmd(chan, &cmd);
+
+                       if (res) {
+                               returnstatus = AGI_RESULT_FAILURE;
+                               goto async_agi_done;
+                       } else if (!cmd) {
                                break;
                        }
-                       /* the command handler must have written to our fake
-                          AGI struct fd (the pipe), let's read the response */
+
+                       /* OK, we have a command, let's call the command handler. */
+                       cmd_status = agi_handle_command(chan, &async_agi, cmd->cmd_buffer, 0);
+
+                       /*
+                        * The command handler must have written to our fake AGI struct
+                        * fd (the pipe), let's read the response.
+                        */
                        res = read(fds[0], agi_buffer, AGI_BUF_SIZE);
-                       if (!res) {
-                               returnstatus = AGI_RESULT_FAILURE;
-                               ast_log(LOG_ERROR, "failed to read from AsyncAGI pipe on channel %s\n", chan->name);
+                       if (res <= 0) {
+                               ast_log(LOG_ERROR, "Failed to read from Async AGI pipe on channel %s: %s\n",
+                                       ast_channel_name(chan), res < 0 ? strerror(errno) : "EOF");
                                free_agi_cmd(cmd);
-                               break;
+                               returnstatus = AGI_RESULT_FAILURE;
+                               goto async_agi_done;
                        }
-                       /* we have a response, let's send the response thru the
-                          manager. Include the CommandID if it was specified
-                          when the command was added */
+                       /*
+                        * We have a response, let's send the response thru the manager.
+                        * Include the CommandID if it was specified when the command
+                        * was added.
+                        */
                        agi_buffer[res] = '\0';
-                       ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, 1);
-                       if (ast_strlen_zero(cmd->cmd_id))
-                               manager_event(EVENT_FLAG_AGI, "AsyncAGI", "SubEvent: Exec\r\nChannel: %s\r\nResult: %s\r\n", chan->name, ami_buffer);
-                       else
-                               manager_event(EVENT_FLAG_AGI, "AsyncAGI", "SubEvent: Exec\r\nChannel: %s\r\nCommandID: %s\r\nResult: %s\r\n", chan->name, cmd->cmd_id, ami_buffer);
+                       ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, ast_uri_http);
+
+                       execblob = ast_json_pack("{s: s}", "Result", ami_buffer);
+                       if (execblob && !ast_strlen_zero(cmd->cmd_id)) {
+                               ast_json_object_set(execblob, "CommandId", ast_json_string_create(cmd->cmd_id));
+                       }
+                       ast_channel_publish_cached_blob(chan, agi_async_exec_type(), execblob);
+
                        free_agi_cmd(cmd);
-               } else {
-                       /* no command so far, wait a bit for a frame to read */
-                       res = ast_waitfor(chan, timeout);
-                       if (res < 0) {
-                               ast_log(LOG_DEBUG, "ast_waitfor returned <= 0 on chan %s\n", chan->name);
+
+                       /*
+                        * Check the command status to determine if we should continue
+                        * executing more commands.
+                        */
+                       hungup = ast_check_hangup(chan);
+                       switch (cmd_status) {
+                       case AGI_RESULT_FAILURE:
+                               if (!hungup) {
+                                       /* The failure was not because of a hangup. */
+                                       returnstatus = AGI_RESULT_FAILURE;
+                                       goto async_agi_done;
+                               }
                                break;
-                       }
-                       if (res == 0)
-                               continue;
-                       f = ast_read(chan);
-                       if (!f) {
-                               ast_log(LOG_DEBUG, "No frame read on channel %s, going out ...\n", chan->name);
-                               returnstatus = AGI_RESULT_HANGUP;
+                       case AGI_RESULT_SUCCESS_ASYNC:
+                               /* Only the "asyncagi break" command does this. */
+                               returnstatus = AGI_RESULT_SUCCESS_ASYNC;
+                               goto async_agi_done;
+                       default:
                                break;
                        }
-                       /* is there any other frame we should care about
-                          besides AST_CONTROL_HANGUP? */
-                       if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HANGUP) {
-                               ast_log(LOG_DEBUG, "Got HANGUP frame on channel %s, going out ...\n", chan->name);
-                               ast_frfree(f);
+               }
+
+               if (!hungup) {
+                       /* Wait a bit for a frame to read or to poll for a new command. */
+                       res = ast_waitfor(chan, timeout);
+                       if (res < 0) {
+                               ast_debug(1, "ast_waitfor returned <= 0 on chan %s\n", ast_channel_name(chan));
+                               returnstatus = AGI_RESULT_FAILURE;
                                break;
                        }
-                       ast_frfree(f);
+               } else {
+                       /*
+                        * Read the channel control queue until it is dry so we can
+                        * quit.
+                        */
+                       res = 1;
+               }
+               if (0 < res) {
+                       do {
+                               cmd_status = async_agi_read_frame(chan);
+                               if (cmd_status != AGI_RESULT_SUCCESS) {
+                                       returnstatus = cmd_status;
+                                       goto async_agi_done;
+                               }
+                               hungup = ast_check_hangup(chan);
+                       } while (hungup);
+               } else {
+                       hungup = ast_check_hangup(chan);
+               }
+       }
+async_agi_done:
+
+       if (async_agi.speech) {
+               ast_speech_destroy(async_agi.speech);
+       }
+       /* notify manager users this channel cannot be controlled anymore by Async AGI */
+       ast_channel_publish_cached_blob(chan, agi_async_end_type(), NULL);
+
+async_agi_abort:
+       /* close the pipe */
+       close(fds[0]);
+       close(fds[1]);
+
+       /*
+        * Intentionally do not remove the datastore added with
+        * add_to_agi() the from channel.  There might be commands still
+        * in the queue or in-flight to us and AsyncAGI may get called
+        * again.  The datastore destructor will be called on channel
+        * destruction anyway.
+        */
+
+       if (returnstatus == AGI_RESULT_SUCCESS) {
+               returnstatus = AGI_RESULT_SUCCESS_ASYNC;
+       }
+       return returnstatus;
+
+#undef AGI_BUF_SIZE
+#undef AMI_BUF_SIZE
+}
+
+/*!
+ * \internal
+ * \brief Handle the connection that was started by launch_netscript.
+ *
+ * \param agiurl Url that we are trying to connect to.
+ * \param addr Address that host was resolved to.
+ * \param netsockfd File descriptor of socket.
+ *
+ * \retval 0 when connection is succesful.
+ * \retval 1 when there is an error.
+ */
+static int handle_connection(const char *agiurl, const struct ast_sockaddr addr, const int netsockfd)
+{
+       struct pollfd pfds[1];
+       int res, conresult;
+       socklen_t reslen;
+
+       reslen = sizeof(conresult);
+
+       pfds[0].fd = netsockfd;
+       pfds[0].events = POLLOUT;
+
+       while ((res = ast_poll(pfds, 1, MAX_AGI_CONNECT)) != 1) {
+               if (errno != EINTR) {
+                       if (!res) {
+                               ast_log(LOG_WARNING, "FastAGI connection to '%s' timed out after MAX_AGI_CONNECT (%d) milliseconds.\n",
+                                       agiurl, MAX_AGI_CONNECT);
+                       } else {
+                               ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
+                       }
+
+                       return 1;
                }
        }
 
-       if (async_agi.speech) {
-               ast_speech_destroy(async_agi.speech);
+       if (getsockopt(pfds[0].fd, SOL_SOCKET, SO_ERROR, &conresult, &reslen) < 0) {
+               ast_log(LOG_WARNING, "Connection to %s failed with error: %s\n",
+                       ast_sockaddr_stringify(&addr), strerror(errno));
+               return 1;
        }
-quit:
-       /* notify manager users this channel cannot be
-          controlled anymore by Async AGI */
-       manager_event(EVENT_FLAG_AGI, "AsyncAGI", "SubEvent: End\r\nChannel: %s\r\n", chan->name);
-
-       /* close the pipe */
-       close(fds[0]);
-       close(fds[1]);
 
-       /* intentionally don't get rid of the datastore. So commands can be
-          still in the queue in case AsyncAGI gets called again.
-          Datastore destructor will be called on channel destroy anyway  */
-
-       return returnstatus;
+       if (conresult) {
+               ast_log(LOG_WARNING, "Connecting to '%s' failed for url '%s': %s\n",
+                       ast_sockaddr_stringify(&addr), agiurl, strerror(conresult));
+               return 1;
+       }
 
-#undef AGI_BUF_SIZE
-#undef AMI_BUF_SIZE
+       return 0;
 }
 
 /* launch_netscript: The fastagi handler.
        FastAGI defaults to port 4573 */
 static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds)
 {
-       int s, flags, res, port = AGI_PORT;
-       struct pollfd pfds[1];
-       char *host, *c, *script;
-       struct sockaddr_in addr_in;
-       struct hostent *hp;
-       struct ast_hostent ahp;
+       int s = 0;
+       char *host, *script;
+       int num_addrs = 0, i = 0;
+       struct ast_sockaddr *addrs;
 
        /* agiurl is "agi://host.domain[:port][/script/name]" */
        host = ast_strdupa(agiurl + 6); /* Remove agi:// */
+
        /* Strip off any script name */
        if ((script = strchr(host, '/'))) {
                *script++ = '\0';
@@ -1368,50 +2061,46 @@ static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds)
                script = "";
        }
 
-       if ((c = strchr(host, ':'))) {
-               *c++ = '\0';
-               port = atoi(c);
-       }
-       if (!(hp = ast_gethostbyname(host, &ahp))) {
+       if (!(num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC))) {
                ast_log(LOG_WARNING, "Unable to locate host '%s'\n", host);
-               return -1;
-       }
-       if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-               ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
-               return -1;
-       }
-       if ((flags = fcntl(s, F_GETFL)) < 0) {
-               ast_log(LOG_WARNING, "Fcntl(F_GETFL) failed: %s\n", strerror(errno));
-               close(s);
-               return -1;
-       }
-       if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
-               ast_log(LOG_WARNING, "Fnctl(F_SETFL) failed: %s\n", strerror(errno));
-               close(s);
-               return -1;
-       }
-       memset(&addr_in, 0, sizeof(addr_in));
-       addr_in.sin_family = AF_INET;
-       addr_in.sin_port = htons(port);
-       memcpy(&addr_in.sin_addr, hp->h_addr, sizeof(addr_in.sin_addr));
-       if (connect(s, (struct sockaddr *)&addr_in, sizeof(addr_in)) && (errno != EINPROGRESS)) {
-               ast_log(LOG_WARNING, "Connect failed with unexpected error: %s\n", strerror(errno));
-               close(s);
                return AGI_RESULT_FAILURE;
        }
 
-       pfds[0].fd = s;
-       pfds[0].events = POLLOUT;
-       while ((res = ast_poll(pfds, 1, MAX_AGI_CONNECT)) != 1) {
-               if (errno != EINTR) {
-                       if (!res) {
-                               ast_log(LOG_WARNING, "FastAGI connection to '%s' timed out after MAX_AGI_CONNECT (%d) milliseconds.\n",
-                                       agiurl, MAX_AGI_CONNECT);
-                       } else
-                               ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
+       for (i = 0; i < num_addrs; i++) {
+               if (!ast_sockaddr_port(&addrs[i])) {
+                       ast_sockaddr_set_port(&addrs[i], AGI_PORT);
+               }
+
+               if ((s = socket(addrs[i].ss.ss_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+                       ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
+                       continue;
+               }
+
+               if (ast_fd_set_flags(s, O_NONBLOCK)) {
                        close(s);
-                       return AGI_RESULT_FAILURE;
+                       continue;
+               }
+
+               if (ast_connect(s, &addrs[i]) && errno == EINPROGRESS) {
+
+                       if (handle_connection(agiurl, addrs[i], s)) {
+                               close(s);
+                               continue;
+                       }
+
+               } else {
+                       ast_log(LOG_WARNING, "Connection to %s failed with unexpected error: %s\n",
+                       ast_sockaddr_stringify(&addrs[i]), strerror(errno));
                }
+
+               break;
+       }
+
+       ast_free(addrs);
+
+       if (i == num_addrs) {
+               ast_log(LOG_WARNING, "Couldn't connect to any host.  FastAGI failed.\n");
+               return AGI_RESULT_FAILURE;
        }
 
        if (ast_agi_send(s, NULL, "agi_network: yes\n") < 0) {
@@ -1424,8 +2113,9 @@ static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds)
 
        /* If we have a script parameter, relay it to the fastagi server */
        /* Script parameters take the form of: AGI(agi://my.example.com/?extension=${EXTEN}) */
-       if (!ast_strlen_zero(script))
+       if (!ast_strlen_zero(script)) {
                ast_agi_send(s, NULL, "agi_network_script: %s\n", script);
+       }
 
        ast_debug(4, "Wow, connected!\n");
        fds[0] = s;
@@ -1455,7 +2145,7 @@ static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds)
 static enum agi_result launch_ha_netscript(char *agiurl, char *argv[], int *fds)
 {
        char *host, *script;
-       enum agi_result result = AGI_RESULT_FAILURE;
+       enum agi_result result;
        struct srv_context *context = NULL;
        int srv_ret;
        char service[256];
@@ -1464,10 +2154,11 @@ static enum agi_result launch_ha_netscript(char *agiurl, char *argv[], int *fds)
        unsigned short srvport;
 
        /* format of agiurl is "hagi://host.domain[:port][/script/name]" */
-       if (!(host = ast_strdupa(agiurl + 7))) { /* Remove hagi:// */
+       if (strlen(agiurl) < 7) { /* Remove hagi:// */
                ast_log(LOG_WARNING, "An error occurred parsing the AGI URI: %s", agiurl);
                return AGI_RESULT_FAILURE;
        }
+       host = ast_strdupa(agiurl + 7);
 
        /* Strip off any script name */
        if ((script = strchr(host, '/'))) {
@@ -1489,19 +2180,23 @@ static enum agi_result launch_ha_netscript(char *agiurl, char *argv[], int *fds)
                if (result == AGI_RESULT_FAILURE || result == AGI_RESULT_NOTFOUND) {
                        ast_log(LOG_WARNING, "AGI request failed for host '%s' (%s:%d)\n", host, srvhost, srvport);
                } else {
-                       break;
+                       /* The script launched so we must cleanup the context. */
+                       ast_srv_cleanup(&context);
+                       return result;
                }
        }
+       /*
+        * The DNS SRV lookup failed or we ran out of servers to check.
+        * ast_srv_lookup() has already cleaned up the context for us.
+        */
        if (srv_ret < 0) {
                ast_log(LOG_WARNING, "SRV lookup failed for %s\n", agiurl);
-       } else {
-        ast_srv_cleanup(&context);
-    }
+       }
 
-       return result;
+       return AGI_RESULT_FAILURE;
 }
 
-static enum agi_result launch_script(struct ast_channel *chan, char *script, char *argv[], int *fds, int *efd, int *opid)
+static enum agi_result launch_script(struct ast_channel *chan, char *script, int argc, char *argv[], int *fds, int *efd, int *opid)
 {
        char tmp[256];
        int pid, toast[2], fromast[2], audio[2], res;
@@ -1514,7 +2209,7 @@ static enum agi_result launch_script(struct ast_channel *chan, char *script, cha
                return (efd == NULL) ? launch_ha_netscript(script, argv, fds) : AGI_RESULT_FAILURE;
        }
        if (!strncasecmp(script, "agi:async", sizeof("agi:async") - 1)) {
-               return launch_asyncagi(chan, argv, efd);
+               return launch_asyncagi(chan, argc, argv, efd);
        }
 
        if (script[0] != '/') {
@@ -1547,9 +2242,8 @@ static enum agi_result launch_script(struct ast_channel *chan, char *script, cha
                        close(toast[1]);
                        return AGI_RESULT_FAILURE;
                }
-               res = fcntl(audio[1], F_GETFL);
-               if (res > -1)
-                       res = fcntl(audio[1], F_SETFL, res | O_NONBLOCK);
+
+               res = ast_fd_set_flags(audio[1], O_NONBLOCK);
                if (res < 0) {
                        ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno));
                        close(fromast[0]);
@@ -1627,34 +2321,34 @@ static void setup_env(struct ast_channel *chan, char *request, int fd, int enhan
        /* Print initial environment, with agi_request always being the first
           thing */
        ast_agi_send(fd, chan, "agi_request: %s\n", request);
-       ast_agi_send(fd, chan, "agi_channel: %s\n", chan->name);
-       ast_agi_send(fd, chan, "agi_language: %s\n", chan->language);
-       ast_agi_send(fd, chan, "agi_type: %s\n", chan->tech->type);
-       ast_agi_send(fd, chan, "agi_uniqueid: %s\n", chan->uniqueid);
+       ast_agi_send(fd, chan, "agi_channel: %s\n", ast_channel_name(chan));
+       ast_agi_send(fd, chan, "agi_language: %s\n", ast_channel_language(chan));
+       ast_agi_send(fd, chan, "agi_type: %s\n", ast_channel_tech(chan)->type);
+       ast_agi_send(fd, chan, "agi_uniqueid: %s\n", ast_channel_uniqueid(chan));
        ast_agi_send(fd, chan, "agi_version: %s\n", ast_get_version());
 
        /* ANI/DNIS */
        ast_agi_send(fd, chan, "agi_callerid: %s\n",
-               S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, "unknown"));
+               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "unknown"));
        ast_agi_send(fd, chan, "agi_calleridname: %s\n",
-               S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, "unknown"));
+               S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "unknown"));
        ast_agi_send(fd, chan, "agi_callingpres: %d\n",
-               ast_party_id_presentation(&chan->caller.id));
-       ast_agi_send(fd, chan, "agi_callingani2: %d\n", chan->caller.ani2);
-       ast_agi_send(fd, chan, "agi_callington: %d\n", chan->caller.id.number.plan);
-       ast_agi_send(fd, chan, "agi_callingtns: %d\n", chan->dialed.transit_network_select);
-       ast_agi_send(fd, chan, "agi_dnid: %s\n", S_OR(chan->dialed.number.str, "unknown"));
+               ast_party_id_presentation(&ast_channel_caller(chan)->id));
+       ast_agi_send(fd, chan, "agi_callingani2: %d\n", ast_channel_caller(chan)->ani2);
+       ast_agi_send(fd, chan, "agi_callington: %d\n", ast_channel_caller(chan)->id.number.plan);
+       ast_agi_send(fd, chan, "agi_callingtns: %d\n", ast_channel_dialed(chan)->transit_network_select);
+       ast_agi_send(fd, chan, "agi_dnid: %s\n", S_OR(ast_channel_dialed(chan)->number.str, "unknown"));
        ast_agi_send(fd, chan, "agi_rdnis: %s\n",
-               S_COR(chan->redirecting.from.number.valid, chan->redirecting.from.number.str, "unknown"));
+               S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, "unknown"));
 
        /* Context information */
-       ast_agi_send(fd, chan, "agi_context: %s\n", chan->context);
-       ast_agi_send(fd, chan, "agi_extension: %s\n", chan->exten);
-       ast_agi_send(fd, chan, "agi_priority: %d\n", chan->priority);
+       ast_agi_send(fd, chan, "agi_context: %s\n", ast_channel_context(chan));
+       ast_agi_send(fd, chan, "agi_extension: %s\n", ast_channel_exten(chan));
+       ast_agi_send(fd, chan, "agi_priority: %d\n", ast_channel_priority(chan));
        ast_agi_send(fd, chan, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
 
        /* User information */
-       ast_agi_send(fd, chan, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
+       ast_agi_send(fd, chan, "agi_accountcode: %s\n", ast_channel_accountcode(chan) ? ast_channel_accountcode(chan) : "");
        ast_agi_send(fd, chan, "agi_threadid: %ld\n", (long)pthread_self());
 
        /* Send any parameters to the fastagi server that have been passed via the agi application */
@@ -1671,7 +2365,7 @@ static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, const cha
        int res = 0;
 
        /* Answer the channel */
-       if (chan->_state != AST_STATE_UP)
+       if (ast_channel_state(chan) != AST_STATE_UP)
                res = ast_answer(chan);
 
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -1681,90 +2375,19 @@ static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, const cha
 static int handle_asyncagi_break(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
        ast_agi_send(agi->fd, chan, "200 result=0\n");
-       return RESULT_FAILURE;
+       return ASYNC_AGI_BREAK;
 }
 
 static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-       int res;
-       int to;
-       char *valid_dtmf_digits = AST_DIGIT_ANY;
-       char *previously_die_on = "#";
-       char *digits = NULL;
-       char *escape_digits = NULL;
-       char *voicefile = NULL;
-       int maxdigits = 1;
+       int res, to;
 
-       if (argc < 4)
+       if (argc != 4)
                return RESULT_SHOWUSAGE;
        if (sscanf(argv[3], "%30d", &to) != 1)
                return RESULT_SHOWUSAGE;
-
-       /* Answer the chan */
-       if (chan->_state != AST_STATE_UP)
-               res = ast_answer(chan);
-
-       /* soundfile specified */
-       if (argc >= 5) {
-
-               /* escape characters defined */
-               if (argc >= 6)
-                       valid_dtmf_digits = (char *) argv[5];
-
-               /* maxdigits */
-               if (argc >= 7 && (sscanf(argv[6], "%d", &maxdigits) != 1))
-                       return RESULT_SHOWUSAGE;
-
-               /* escape before escape chars on */
-               if (argc >= 8)
-                       previously_die_on = (char *) argv[7];
-
-               voicefile = (char *) argv[4];
-               res = ast_streamfile(chan, voicefile, chan->language);
-               if (res < 0)
-                       return RESULT_FAILURE;
-
-               /* allocate space for the digits (2 chars per digit + \0 - <digit>|<digit>|...) */
-               digits = (char *)ast_malloc(maxdigits * 2 + 1);
-               ast_copy_string(digits, "", 1);
-
-               /* catenate the escape digits together with previously die digits */
-               escape_digits = (char *)ast_malloc(strlen(valid_dtmf_digits) + strlen(previously_die_on)+ 1);
-               ast_copy_string(escape_digits, valid_dtmf_digits, sizeof(valid_dtmf_digits));
-               strcat(escape_digits, previously_die_on);
-
-               if (chan->stream) {
-                       int dtmf_count = 0;
-                       do {
-                               char buf[3];
-                               res = ast_waitstream_full(chan, escape_digits, agi->audio, agi->ctrl);
-                               if (res > 0) {
-                                       if (strchr(previously_die_on, res) != NULL) {
-                                               /* previously die character found - end loop */
-                                               ast_log(LOG_DEBUG, "prev die digit %c pressed\n", res);
-                                               break;
-                                       } else {
-                                               /* chars in valid_dtmf_digits found */
-                                               ast_log(LOG_DEBUG, "dtmf turn=%d of %d | res=%d\n", dtmf_count, maxdigits, res);
-                                               sprintf(buf, "%c", res);
-                                               strcat(digits, buf);
-                                               dtmf_count++;
-                                       }
-                               }
-                       } while ( strchr(previously_die_on, res) == NULL && dtmf_count < maxdigits && chan->stream);
-                       ast_stopstream(chan);
-                       ast_agi_send(agi->fd, chan, "200 result=%s\n", digits);
-               } else {
-                       res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
-                       ast_agi_send(agi->fd, chan, "200 result=%c\n", res);
-               }
-       } else {
-               res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
-               ast_agi_send(agi->fd, chan, "200 result=%c\n", res);
-       }
-
-       ast_free(escape_digits);
-       ast_free(digits);
+       res = ast_waitfordigit_full(chan, to, NULL, agi->audio, agi->ctrl);
+       ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
        return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -1843,7 +2466,8 @@ static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, const ch
                x = 1;
        }
        res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0);
-       if (res != RESULT_SUCCESS) {
+       if (res) {
+               /* Set channel option failed */
                ast_agi_send(agi->fd, chan, "200 result=0\n");
        } else {
                ast_agi_send(agi->fd, chan, "200 result=1\n");
@@ -1871,8 +2495,11 @@ static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc
 {
        int res = 0, skipms = 3000;
        const char *fwd = "#", *rev = "*", *suspend = NULL, *stop = NULL;       /* Default values */
+       char stopkeybuf[2];
+       long offsetms = 0;
+       char offsetbuf[20];
 
-       if (argc < 5 || argc > 9) {
+       if (argc < 5 || argc > 10) {
                return RESULT_SHOWUSAGE;
        }
 
@@ -1896,66 +2523,98 @@ static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc
                suspend = argv[8];
        }
 
-       res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, suspend, NULL, skipms, NULL);
+       if (argc > 9 && (sscanf(argv[9], "%30ld", &offsetms) != 1)) {
+               return RESULT_SHOWUSAGE;
+       }
 
-       ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
+       res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, suspend, NULL, skipms, &offsetms);
+
+       /* If we stopped on one of our stop keys, return 0  */
+       if (res > 0 && stop && strchr(stop, res)) {
+               pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
+               snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
+               pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
+       } else if (res > 0 && res == AST_CONTROL_STREAM_STOP) {
+               pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED");
+               res = 0;
+       } else {
+               if (res < 0) {
+                       pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
+               } else {
+                       pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
+               }
+       }
+
+       snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
+       pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
+
+       ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", res, offsetms);
 
        return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
 static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-       int res, vres;
+       int res;
        struct ast_filestream *fs, *vfs;
        long sample_offset = 0, max_length;
        const char *edigits = "";
 
-       if (argc < 4 || argc > 5)
+       if (argc < 4 || argc > 5) {
                return RESULT_SHOWUSAGE;
+       }
 
-       if (argv[3])
+       if (argv[3]) {
                edigits = argv[3];
+       }
 
-       if ((argc > 4) && (sscanf(argv[4], "%30ld", &sample_offset) != 1))
+       if ((argc > 4) && (sscanf(argv[4], "%30ld", &sample_offset) != 1)) {
                return RESULT_SHOWUSAGE;
+       }
 
-       if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-               ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", 0, sample_offset);
-               return RESULT_SUCCESS;
+       if (!(fs = ast_openstream(chan, argv[2], ast_channel_language(chan)))) {
+               ast_agi_send(agi->fd, chan, "200 result=-1 endpos=%ld\n", sample_offset);
+               return RESULT_FAILURE;
        }
 
-       if ((vfs = ast_openvstream(chan, argv[2], chan->language)))
+       if ((vfs = ast_openvstream(chan, argv[2], ast_channel_language(chan)))) {
                ast_debug(1, "Ooh, found a video stream, too\n");
-
-       ast_verb(3, "Playing '%s' (escape_digits=%s) (sample_offset %ld)\n", argv[2], edigits, sample_offset);
+       }
+       ast_verb(3, "<%s> Playing '%s.%s' (escape_digits=%s) (sample_offset %ld) (language '%s')\n",
+               ast_channel_name(chan), argv[2], ast_format_get_name(ast_channel_writeformat(chan)),
+               edigits, sample_offset, S_OR(ast_channel_language(chan), "default"));
 
        ast_seekstream(fs, 0, SEEK_END);
        max_length = ast_tellstream(fs);
        ast_seekstream(fs, sample_offset, SEEK_SET);
        res = ast_applystream(chan, fs);
-       if (vfs)
-               vres = ast_applystream(chan, vfs);
+       if (vfs) {
+               ast_applystream(chan, vfs);
+       }
        ast_playstream(fs);
-       if (vfs)
+       if (vfs) {
                ast_playstream(vfs);
+       }
 
        res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
        /* this is to check for if ast_waitstream closed the stream, we probably are at
         * the end of the stream, return that amount, else check for the amount */
-       sample_offset = (chan->stream) ? ast_tellstream(fs) : max_length;
+       sample_offset = (ast_channel_stream(chan)) ? ast_tellstream(fs) : max_length;
        ast_stopstream(chan);
        if (res == 1) {
                /* Stop this command, don't print a result line, as there is a new command */
                return RESULT_SUCCESS;
        }
        ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", res, sample_offset);
+       pbx_builtin_setvar_helper(chan, "PLAYBACKSTATUS", res ? "FAILED" : "SUCCESS");
+
        return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
 /*! \brief get option - really similar to the handle_streamfile, but with a timeout */
 static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-       int res, vres;
+       int res;
        struct ast_filestream *fs, *vfs;
        long sample_offset = 0, max_length;
        int timeout = 0;
@@ -1969,18 +2628,18 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const
 
        if ( argc == 5 )
                timeout = atoi(argv[4]);
-       else if (chan->pbx->dtimeoutms) {
+       else if (ast_channel_pbx(chan)->dtimeoutms) {
                /* by default dtimeout is set to 5sec */
-               timeout = chan->pbx->dtimeoutms; /* in msec */
+               timeout = ast_channel_pbx(chan)->dtimeoutms; /* in msec */
        }
 
-       if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-               ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", 0, sample_offset);
+       if (!(fs = ast_openstream(chan, argv[2], ast_channel_language(chan)))) {
+               ast_agi_send(agi->fd, chan, "200 result=-1 endpos=%ld\n", sample_offset);
                ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
-               return RESULT_SUCCESS;
+               return RESULT_FAILURE;
        }
 
-       if ((vfs = ast_openvstream(chan, argv[2], chan->language)))
+       if ((vfs = ast_openvstream(chan, argv[2], ast_channel_language(chan))))
                ast_debug(1, "Ooh, found a video stream, too\n");
 
        ast_verb(3, "Playing '%s' (escape_digits=%s) (timeout %d)\n", argv[2], edigits, timeout);
@@ -1990,7 +2649,7 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const
        ast_seekstream(fs, sample_offset, SEEK_SET);
        res = ast_applystream(chan, fs);
        if (vfs)
-               vres = ast_applystream(chan, vfs);
+               ast_applystream(chan, vfs);
        ast_playstream(fs);
        if (vfs)
                ast_playstream(vfs);
@@ -1998,7 +2657,7 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const
        res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
        /* this is to check for if ast_waitstream closed the stream, we probably are at
         * the end of the stream, return that amount, else check for the amount */
-       sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
+       sample_offset = (ast_channel_stream(chan))?ast_tellstream(fs):max_length;
        ast_stopstream(chan);
        if (res == 1) {
                /* Stop this command, don't print a result line, as there is a new command */
@@ -2007,7 +2666,7 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const
 
        /* If the user didnt press a key, wait for digitTimeout*/
        if (res == 0 ) {
-               res = ast_waitfordigit_full(chan, timeout, agi->audio, agi->ctrl);
+               res = ast_waitfordigit_full(chan, timeout, NULL, agi->audio, agi->ctrl);
                /* Make sure the new result is in the escape digits of the GET OPTION */
                if ( !strchr(edigits,res) )
                        res=0;
@@ -2030,7 +2689,7 @@ static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, const
                return RESULT_SHOWUSAGE;
        if (sscanf(argv[2], "%30d", &num) != 1)
                return RESULT_SHOWUSAGE;
-       res = ast_say_number_full(chan, num, argv[3], chan->language, argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl);
+       res = ast_say_number_full(chan, num, argv[3], ast_channel_language(chan), argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl);
        if (res == 1)
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2046,7 +2705,7 @@ static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, const
        if (sscanf(argv[2], "%30d", &num) != 1)
                return RESULT_SHOWUSAGE;
 
-       res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
+       res = ast_say_digit_str_full(chan, argv[2], argv[3], ast_channel_language(chan), agi->audio, agi->ctrl);
        if (res == 1) /* New command */
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2056,11 +2715,37 @@ static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, const
 static int handle_sayalpha(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
        int res;
+       int sensitivity = AST_SAY_CASE_NONE;
 
-       if (argc != 4)
+       if (argc < 4 || argc > 5) {
                return RESULT_SHOWUSAGE;
+       }
 
-       res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
+       if (argc > 4) {
+               switch (argv[4][0]) {
+               case 'a':
+               case 'A':
+                       sensitivity = AST_SAY_CASE_ALL;
+                       break;
+               case 'l':
+               case 'L':
+                       sensitivity = AST_SAY_CASE_LOWER;
+                       break;
+               case 'n':
+               case 'N':
+                       sensitivity = AST_SAY_CASE_NONE;
+                       break;
+               case 'u':
+               case 'U':
+                       sensitivity = AST_SAY_CASE_UPPER;
+                       break;
+               case '\0':
+                       break;
+               default:
+                       return RESULT_SHOWUSAGE;
+               }
+       }
+       res = ast_say_character_str_full(chan, argv[2], argv[3], ast_channel_language(chan), sensitivity, agi->audio, agi->ctrl);
        if (res == 1) /* New command */
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2075,7 +2760,7 @@ static int handle_saydate(struct ast_channel *chan, AGI *agi, int argc, const ch
                return RESULT_SHOWUSAGE;
        if (sscanf(argv[2], "%30d", &num) != 1)
                return RESULT_SHOWUSAGE;
-       res = ast_say_date(chan, num, argv[3], chan->language);
+       res = ast_say_date(chan, num, argv[3], ast_channel_language(chan));
        if (res == 1)
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2090,7 +2775,7 @@ static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, const ch
                return RESULT_SHOWUSAGE;
        if (sscanf(argv[2], "%30d", &num) != 1)
                return RESULT_SHOWUSAGE;
-       res = ast_say_time(chan, num, argv[3], chan->language);
+       res = ast_say_time(chan, num, argv[3], ast_channel_language(chan));
        if (res == 1)
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2110,7 +2795,7 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, cons
                format = argv[4];
        } else {
                /* XXX this doesn't belong here, but in the 'say' module */
-               if (!strcasecmp(chan->language, "de")) {
+               if (!strcasecmp(ast_channel_language(chan), "de")) {
                        format = "A dBY HMS";
                } else {
                        format = "ABdY 'digits/at' IMp";
@@ -2123,7 +2808,7 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, cons
        if (ast_get_time_t(argv[2], &unixtime, 0, NULL))
                return RESULT_SHOWUSAGE;
 
-       res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone);
+       res = ast_say_date_with_format(chan, unixtime, argv[3], ast_channel_language(chan), format, zone);
        if (res == 1)
                return RESULT_SUCCESS;
 
@@ -2138,7 +2823,7 @@ static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, cons
        if (argc != 4)
                return RESULT_SHOWUSAGE;
 
-       res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
+       res = ast_say_phonetic_str_full(chan, argv[2], argv[3], ast_channel_language(chan), agi->audio, agi->ctrl);
        if (res == 1) /* New command */
                return RESULT_SUCCESS;
        ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
@@ -2177,7 +2862,7 @@ static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, const
 
        if (argc != 3)
                return RESULT_SHOWUSAGE;
-       ast_copy_string(chan->context, argv[2], sizeof(chan->context));
+       ast_channel_context_set(chan, argv[2]);
        ast_agi_send(agi->fd, chan, "200 result=0\n");
        return RESULT_SUCCESS;
 }
@@ -2186,7 +2871,7 @@ static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, con
 {
        if (argc != 3)
                return RESULT_SHOWUSAGE;
-       ast_copy_string(chan->exten, argv[2], sizeof(chan->exten));
+       ast_channel_exten_set(chan, argv[2]);
        ast_agi_send(agi->fd, chan, "200 result=0\n");
        return RESULT_SUCCESS;
 }
@@ -2199,8 +2884,8 @@ static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, cons
                return RESULT_SHOWUSAGE;
 
        if (sscanf(argv[2], "%30d", &pri) != 1) {
-               pri = ast_findlabel_extension(chan, chan->context, chan->exten, argv[2],
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL));
+               pri = ast_findlabel_extension(chan, ast_channel_context(chan), ast_channel_exten(chan), argv[2],
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
                if (pri < 1)
                        return RESULT_SHOWUSAGE;
        }
@@ -2225,7 +2910,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
        int silence = 0;                /* amount of silence to allow */
        int gotsilence = 0;             /* did we timeout for silence? */
        char *silencestr = NULL;
-       int rfmt = 0;
+       RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
 
        /* XXX EAGI FIXME XXX */
 
@@ -2255,28 +2940,30 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
        }
 
        if (silence > 0) {
-               rfmt = chan->readformat;
-               res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+               rfmt = ao2_bump(ast_channel_readformat(chan));
+               res = ast_set_read_format(chan, ast_format_slin);
                if (res < 0) {
                        ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
-                       return -1;
+                       ast_agi_send(agi->fd, chan, "200 result=%d\n", res);
+                       return RESULT_FAILURE;
                }
                sildet = ast_dsp_new();
                if (!sildet) {
                        ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
-                       return -1;
+                       ast_agi_send(agi->fd, chan, "200 result=-1\n");
+                       return RESULT_FAILURE;
                }
                ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
        }
-       
+
        /* backward compatibility, if no offset given, arg[6] would have been
         * caught below and taken to be a beep, else if it is a digit then it is a
         * offset */
        if ((argc >6) && (sscanf(argv[6], "%30ld", &sample_offset) != 1) && (!strchr(argv[6], '=')))
-               res = ast_streamfile(chan, "beep", chan->language);
+               res = ast_streamfile(chan, "beep", ast_channel_language(chan));
 
        if ((argc > 7) && (!strchr(argv[7], '=')))
-               res = ast_streamfile(chan, "beep", chan->language);
+               res = ast_streamfile(chan, "beep", ast_channel_language(chan));
 
        if (!res)
                res = ast_waitstream(chan, argv[4]);
@@ -2295,7 +2982,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
                /* Request a video update */
                ast_indicate(chan, AST_CONTROL_VIDUPDATE);
 
-               chan->stream = fs;
+               ast_channel_stream_set(chan, fs);
                ast_applystream(chan,fs);
                /* really should have checks */
                ast_seekstream(fs, sample_offset, SEEK_SET);
@@ -2313,8 +3000,8 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
                        }
                        f = ast_read(chan);
                        if (!f) {
-                               ast_agi_send(agi->fd, chan, "200 result=%d (hangup) endpos=%ld\n", -1, sample_offset);
                                ast_closestream(fs);
+                               ast_agi_send(agi->fd, chan, "200 result=%d (hangup) endpos=%ld\n", -1, sample_offset);
                                if (sildet)
                                        ast_dsp_free(sildet);
                                return RESULT_FAILURE;
@@ -2328,8 +3015,8 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
                                        ast_stream_rewind(fs, 200);
                                        ast_truncstream(fs);
                                        sample_offset = ast_tellstream(fs);
-                                       ast_agi_send(agi->fd, chan, "200 result=%d (dtmf) endpos=%ld\n", f->subclass.integer, sample_offset);
                                        ast_closestream(fs);
+                                       ast_agi_send(agi->fd, chan, "200 result=%d (dtmf) endpos=%ld\n", f->subclass.integer, sample_offset);
                                        ast_frfree(f);
                                        if (sildet)
                                                ast_dsp_free(sildet);
@@ -2373,14 +3060,14 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const
                        ast_truncstream(fs);
                        sample_offset = ast_tellstream(fs);
                }
-               ast_agi_send(agi->fd, chan, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
                ast_closestream(fs);
+               ast_agi_send(agi->fd, chan, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
        }
 
        if (silence > 0) {
                res = ast_set_read_format(chan, rfmt);
                if (res)
-                       ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
+                       ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan));
                ast_dsp_free(sildet);
        }
 
@@ -2402,7 +3089,9 @@ static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, const
                whentohangup.tv_sec = timeout;
                whentohangup.tv_usec = (timeout - whentohangup.tv_sec) * 1000000.0;
        }
+       ast_channel_lock(chan);
        ast_channel_setwhentohangup_tv(chan, whentohangup);
+       ast_channel_unlock(chan);
        ast_agi_send(agi->fd, chan, "200 result=0\n");
        return RESULT_SUCCESS;
 }
@@ -2446,32 +3135,14 @@ static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, const char
        ast_verb(3, "AGI Script Executing Application: (%s) Options: (%s)\n", argv[1], argc >= 3 ? argv[2] : "");
 
        if ((app_to_exec = pbx_findapp(argv[1]))) {
-               if(!strcasecmp(argv[1], PARK_APP_NAME)) {
-                       ast_masq_park_call(chan, NULL, 0, NULL);
-               }
-               if (!(workaround = ast_test_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS))) {
-                       ast_set_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS);
-               }
-               if (ast_compat_res_agi && argc >= 3 && !ast_strlen_zero(argv[2])) {
-                       char *compat = alloca(strlen(argv[2]) * 2 + 1), *cptr;
-                       const char *vptr;
-                       for (cptr = compat, vptr = argv[2]; *vptr; vptr++) {
-                               if (*vptr == ',') {
-                                       *cptr++ = '\\';
-                                       *cptr++ = ',';
-                               } else if (*vptr == '|') {
-                                       *cptr++ = ',';
-                               } else {
-                                       *cptr++ = *vptr;
-                               }
-                       }
-                       *cptr = '\0';
-                       res = pbx_exec(chan, app_to_exec, compat);
-               } else {
-                       res = pbx_exec(chan, app_to_exec, argc == 2 ? "" : argv[2]);
+               ast_channel_lock(chan);
+               if (!(workaround = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS))) {
+                       ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS);
                }
+               ast_channel_unlock(chan);
+               res = pbx_exec(chan, app_to_exec, argc == 2 ? "" : argv[2]);
                if (!workaround) {
-                       ast_clear_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS);
+                       ast_channel_clear_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS);
                }
        } else {
                ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
@@ -2506,16 +3177,18 @@ static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, cons
 
 static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-       struct ast_channel *c;
        if (argc == 2) {
                /* no argument: supply info on the current channel */
-               ast_agi_send(agi->fd, chan, "200 result=%d\n", chan->_state);
+               ast_agi_send(agi->fd, chan, "200 result=%u\n", ast_channel_state(chan));
                return RESULT_SUCCESS;
        } else if (argc == 3) {
+               RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+
                /* one argument: look for info on the specified channel */
-               if ((c = ast_channel_get_by_name(argv[2]))) {
-                       ast_agi_send(agi->fd, chan, "200 result=%d\n", c->_state);
-                       c = ast_channel_unref(c);
+               if ((msg = stasis_cache_get(ast_channel_cache_by_name(), ast_channel_snapshot_type(), argv[2]))) {
+                       struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
+
+                       ast_agi_send(agi->fd, chan, "200 result=%u\n", snapshot->state);
                        return RESULT_SUCCESS;
                }
                /* if we get this far no channel name matched the argument given */
@@ -2528,6 +3201,10 @@ static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, co
 
 static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
+       if (argc != 4) {
+               return RESULT_SHOWUSAGE;
+       }
+
        if (argv[3])
                pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
 
@@ -2538,7 +3215,7 @@ static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, cons
 static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
        char *ret;
-       char tempstr[1024];
+       char tempstr[1024] = "";
 
        if (argc != 3)
                return RESULT_SHOWUSAGE;
@@ -2602,7 +3279,7 @@ static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, const ch
        if (argv[2])
                sscanf(argv[2], "%30d", &level);
 
-       ast_verb(level, "%s: %s\n", chan->data, argv[1]);
+       ast_verb(level, "%s: %s\n", ast_channel_data(chan), argv[1]);
 
        ast_agi_send(agi->fd, chan, "200 result=1\n");
 
@@ -2632,7 +3309,7 @@ static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, const char
                        break;
                }
        } while (1);
-       
+
        if (res)
                ast_agi_send(agi->fd, chan, "200 result=0\n");
        else
@@ -2666,16 +3343,18 @@ static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, const char
 
 static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-       int res;
+       int num_deleted;
 
-       if ((argc < 3) || (argc > 4))
+       if ((argc < 3) || (argc > 4)) {
                return RESULT_SHOWUSAGE;
-       if (argc == 4)
-               res = ast_db_deltree(argv[2], argv[3]);
-       else
-               res = ast_db_deltree(argv[2], NULL);
+       }
+       if (argc == 4) {
+               num_deleted = ast_db_deltree(argv[2], argv[3]);
+       } else {
+               num_deleted = ast_db_deltree(argv[2], NULL);
+       }
 
-       ast_agi_send(agi->fd, chan, "200 result=%c\n", res ? '0' : '1');
+       ast_agi_send(agi->fd, chan, "200 result=%c\n", num_deleted > 0 ? '0' : '1');
        return RESULT_SUCCESS;
 }
 
@@ -2729,16 +3408,24 @@ static int handle_setmusic(struct ast_channel *chan, AGI *agi, int argc, const c
 
 static int handle_speechcreate(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
+       struct ast_format_cap *cap;
+
        /* If a structure already exists, return an error */
-        if (agi->speech) {
+       if (agi->speech) {
                ast_agi_send(agi->fd, chan, "200 result=0\n");
                return RESULT_SUCCESS;
        }
 
-       if ((agi->speech = ast_speech_new(argv[2], AST_FORMAT_SLINEAR)))
+       if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+               return RESULT_FAILURE;
+       }
+       ast_format_cap_append(cap, ast_format_slin, 0);
+       if ((agi->speech = ast_speech_new(argv[2], cap))) {
                ast_agi_send(agi->fd, chan, "200 result=1\n");
-       else
+       } else {
                ast_agi_send(agi->fd, chan, "200 result=0\n");
+       }
+       ao2_ref(cap, -1);
 
        return RESULT_SUCCESS;
 }
@@ -2870,7 +3557,7 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
        struct ast_speech *speech = agi->speech;
        const char *prompt;
        char dtmf = 0, tmp[4096] = "", *buf = tmp;
-       int timeout = 0, offset = 0, old_read_format = 0, res = 0, i = 0;
+       int timeout = 0, offset = 0, res = 0, i = 0;
        long current_offset = 0;
        const char *reason = NULL;
        struct ast_frame *fr = NULL;
@@ -2894,8 +3581,7 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
                offset = atoi(argv[4]);
 
        /* We want frames coming in signed linear */
-       old_read_format = chan->readformat;
-       if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
+       if (ast_set_read_format(chan, ast_format_slin)) {
                ast_agi_send(agi->fd, chan, "200 result=0\n");
                return RESULT_SUCCESS;
        }
@@ -2907,15 +3593,15 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
        }
 
        /* Start playing prompt */
-       speech_streamfile(chan, prompt, chan->language, offset);
+       speech_streamfile(chan, prompt, ast_channel_language(chan), offset);
 
        /* Go into loop reading in frames, passing to speech thingy, checking for hangup, all that jazz */
        while (ast_strlen_zero(reason)) {
                /* Run scheduled items */
-                ast_sched_runq(chan->sched);
+                ast_sched_runq(ast_channel_sched(chan));
 
                /* See maximum time of waiting */
-               if ((res = ast_sched_wait(chan->sched)) < 0)
+               if ((res = ast_sched_wait(ast_channel_sched(chan))) < 0)
                        res = 1000;
 
                /* Wait for frame */
@@ -2941,8 +3627,8 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
                ast_mutex_lock(&speech->lock);
 
                /* See if we need to quiet the audio stream playback */
-               if (ast_test_flag(speech, AST_SPEECH_QUIET) && chan->stream) {
-                       current_offset = ast_tellstream(chan->stream);
+               if (ast_test_flag(speech, AST_SPEECH_QUIET) && ast_channel_stream(chan)) {
+                       current_offset = ast_tellstream(ast_channel_stream(chan));
                        ast_stopstream(chan);
                        ast_clear_flag(speech, AST_SPEECH_QUIET);
                }
@@ -2951,7 +3637,7 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
                switch (speech->state) {
                case AST_SPEECH_STATE_READY:
                        /* If the stream is done, start timeout calculation */
-                       if ((timeout > 0) && start == 0 && ((!chan->stream) || (chan->streamid == -1 && chan->timingfunc == NULL))) {
+                       if ((timeout > 0) && start == 0 && ((!ast_channel_stream(chan)) || (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL))) {
                                ast_stopstream(chan);
                                time(&start);
                        }
@@ -2961,11 +3647,11 @@ static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc,
                        break;
                case AST_SPEECH_STATE_WAIT:
                        /* Cue waiting sound if not already playing */
-                       if ((!chan->stream) || (chan->streamid == -1 && chan->timingfunc == NULL)) {
+                       if ((!ast_channel_stream(chan)) || (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL)) {
                                ast_stopstream(chan);
                                /* If a processing sound exists, or is not none - play it */
                                if (!ast_strlen_zero(speech->processing_sound) && strcasecmp(speech->processing_sound, "none"))
-                                       speech_streamfile(chan, speech->processing_sound, chan->language, 0);
+                                       speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan), 0);
                        }
                        break;
                case AST_SPEECH_STATE_DONE:
@@ -3033,15 +3719,15 @@ static struct agi_command commands[] = {
        { { "noop", NULL }, handle_noop, NULL, NULL, 1 },
        { { "receive", "char", NULL }, handle_recvchar, NULL, NULL, 0 },
        { { "receive", "text", NULL }, handle_recvtext, NULL, NULL, 0 },
-       { { "record", "file", NULL }, handle_recordfile, NULL, NULL, 0 }, 
+       { { "record", "file", NULL }, handle_recordfile, NULL, NULL, 0 },
        { { "say", "alpha", NULL }, handle_sayalpha, NULL, NULL, 0},
        { { "say", "digits", NULL }, handle_saydigits, NULL, NULL, 0 },
        { { "say", "number", NULL }, handle_saynumber, NULL, NULL, 0 },
-       { { "say", "phonetic", NULL }, handle_sayphonetic, NULL, NULL, 0}, 
-       { { "say", "date", NULL }, handle_saydate, NULL, NULL, 0}, 
-       { { "say", "time", NULL }, handle_saytime, NULL, NULL, 0}, 
+       { { "say", "phonetic", NULL }, handle_sayphonetic, NULL, NULL, 0},
+       { { "say", "date", NULL }, handle_saydate, NULL, NULL, 0},
+       { { "say", "time", NULL }, handle_saytime, NULL, NULL, 0},
        { { "say", "datetime", NULL }, handle_saydatetime, NULL, NULL, 0},
-       { { "send", "image", NULL }, handle_sendimage, NULL, NULL, 0}, 
+       { { "send", "image", NULL }, handle_sendimage, NULL, NULL, 0},
        { { "send", "text", NULL }, handle_sendtext, NULL, NULL, 0},
        { { "set", "autohangup", NULL }, handle_autohangup, NULL, NULL, 0},
        { { "set", "callerid", NULL }, handle_setcallerid, NULL, NULL, 0},
@@ -3103,10 +3789,10 @@ int AST_OPTIONAL_API_NAME(ast_agi_register)(struct ast_module *mod, agi_command
                *((enum ast_doc_src *) &cmd->docsrc) = AST_STATIC_DOC;
                if (ast_strlen_zero(cmd->summary) && ast_strlen_zero(cmd->usage)) {
 #ifdef AST_XML_DOCS
-                       *((char **) &cmd->summary) = ast_xmldoc_build_synopsis("agi", fullcmd);
-                       *((char **) &cmd->usage) = ast_xmldoc_build_description("agi", fullcmd);
-                       *((char **) &cmd->syntax) = ast_xmldoc_build_syntax("agi", fullcmd);
-                       *((char **) &cmd->seealso) = ast_xmldoc_build_seealso("agi", fullcmd);
+                       *((char **) &cmd->summary) = ast_xmldoc_build_synopsis("agi", fullcmd, NULL);
+                       *((char **) &cmd->usage) = ast_xmldoc_build_description("agi", fullcmd, NULL);
+                       *((char **) &cmd->syntax) = ast_xmldoc_build_syntax("agi", fullcmd, NULL);
+                       *((char **) &cmd->seealso) = ast_xmldoc_build_seealso("agi", fullcmd, NULL);
                        *((enum ast_doc_src *) &cmd->docsrc) = AST_XML_DOC;
 #endif
 #ifndef HAVE_NULLSAFE_PRINTF
@@ -3171,10 +3857,9 @@ int AST_OPTIONAL_API_NAME(ast_agi_unregister)(struct ast_module *mod, agi_comman
        }
        AST_RWLIST_TRAVERSE_SAFE_END;
        AST_RWLIST_UNLOCK(&agi_commands);
-       if (unregistered)
+       if (unregistered) {
                ast_verb(2, "AGI Command '%s' unregistered\n",fullcmd);
-       else
-               ast_log(LOG_WARNING, "Unable to unregister command: '%s'!\n",fullcmd);
+       }
        return unregistered;
 }
 
@@ -3325,86 +4010,114 @@ normal:
        return 0;
 }
 
-static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead)
+static void publish_async_exec_end(struct ast_channel *chan, int command_id, const char *command, int result_code, const char *result)
+{
+       RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+       blob = ast_json_pack("{s: i, s: s, s: i, s: s}",
+                            "CommandId", command_id,
+                            "Command", command,
+                            "ResultCode", result_code,
+                            "Result", result);
+       ast_channel_publish_cached_blob(chan, agi_exec_end_type(), blob);
+}
+
+static enum agi_result agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead)
 {
-       const char *argv[MAX_ARGS];
-       int argc = MAX_ARGS, res;
+       const char *argv[MAX_ARGS] = {0};
+       int argc = MAX_ARGS;
+       int res;
        agi_command *c;
-       const char *ami_res = "Unknown Result";
        char *ami_cmd = ast_strdupa(buf);
-       int command_id = ast_random(), resultcode = 200;
+       const char *ami_res;
+       int command_id = ast_random();
+       int resultcode = 0;
+       RAII_VAR(struct ast_json *, startblob, NULL, ast_json_unref);
+
+       startblob = ast_json_pack("{s: i, s: s}",
+                            "CommandId", command_id,
+                            "Command", ami_cmd);
+       ast_channel_publish_cached_blob(chan, agi_exec_start_type(), startblob);
 
-       manager_event(EVENT_FLAG_AGI, "AGIExec",
-                       "SubEvent: Start\r\n"
-                       "Channel: %s\r\n"
-                       "CommandId: %d\r\n"
-                       "Command: %s\r\n", chan->name, command_id, ami_cmd);
        parse_args(buf, &argc, argv);
-       if ((c = find_command(argv, 0)) && (!dead || (dead && c->dead))) {
-               /* if this command wasnt registered by res_agi, be sure to usecount
-               the module we are using */
-               if (c->mod != ast_module_info->self)
-                       ast_module_ref(c->mod);
-               /* If the AGI command being executed is an actual application (using agi exec)
-               the app field will be updated in pbx_exec via handle_exec */
-               if (chan->cdr && !ast_check_hangup(chan) && strcasecmp(argv[0], "EXEC"))
-                       ast_cdr_setapp(chan->cdr, "AGI", buf);
+       c = find_command(argv, 0);
+       if (!c || !ast_module_running_ref(c->mod)) {
+               ami_res = "Invalid or unknown command";
+               resultcode = 510;
+
+               ast_agi_send(agi->fd, chan, "%d %s\n", resultcode, ami_res);
 
+               publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
+               return AGI_RESULT_SUCCESS;
+       }
+
+       if (!dead || (dead && c->dead)) {
                res = c->handler(chan, agi, argc, argv);
-               if (c->mod != ast_module_info->self)
-                       ast_module_unref(c->mod);
                switch (res) {
-               case RESULT_SHOWUSAGE: ami_res = "Usage"; resultcode = 520; break;
-               case RESULT_FAILURE: ami_res = "Failure"; resultcode = -1; break;
-               case RESULT_SUCCESS: ami_res = "Success"; resultcode = 200; break;
-               }
-               manager_event(EVENT_FLAG_AGI, "AGIExec",
-                               "SubEvent: End\r\n"
-                               "Channel: %s\r\n"
-                               "CommandId: %d\r\n"
-                               "Command: %s\r\n"
-                               "ResultCode: %d\r\n"
-                               "Result: %s\r\n", chan->name, command_id, ami_cmd, resultcode, ami_res);
-               switch(res) {
                case RESULT_SHOWUSAGE:
+                       ami_res = "Usage";
+                       resultcode = 520;
+
+                       publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
                        if (ast_strlen_zero(c->usage)) {
                                ast_agi_send(agi->fd, chan, "520 Invalid command syntax.  Proper usage not available.\n");
                        } else {
                                ast_agi_send(agi->fd, chan, "520-Invalid command syntax.  Proper usage follows:\n");
-                               ast_agi_send(agi->fd, chan, "%s", c->usage);
+                               ast_agi_send(agi->fd, chan, "%s\n", c->usage);
                                ast_agi_send(agi->fd, chan, "520 End of proper usage.\n");
                        }
+
                        break;
                case RESULT_FAILURE:
-                       /* They've already given the failure.  We've been hung up on so handle this
-                          appropriately */
-                       return -1;
+                       ami_res = "Failure";
+                       resultcode = -1;
+
+                       publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
+                       /* The RESULT_FAILURE code is usually because the channel hungup. */
+                       return AGI_RESULT_FAILURE;
+               case ASYNC_AGI_BREAK:
+                       ami_res = "Success";
+                       resultcode = 200;
+
+                       publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
+                       return AGI_RESULT_SUCCESS_ASYNC;
+               case RESULT_SUCCESS:
+                       ami_res = "Success";
+                       resultcode = 200;
+
+                       publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
+                       break;
+               default:
+                       ami_res = "Unknown Result";
+                       resultcode = 200;
+
+                       publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
+
+                       break;
                }
-       } else if ((c = find_command(argv, 0))) {
-               ast_agi_send(agi->fd, chan, "511 Command Not Permitted on a dead channel\n");
-               manager_event(EVENT_FLAG_AGI, "AGIExec",
-                               "SubEvent: End\r\n"
-                               "Channel: %s\r\n"
-                               "CommandId: %d\r\n"
-                               "Command: %s\r\n"
-                               "ResultCode: 511\r\n"
-                               "Result: Command not permitted on a dead channel\r\n", chan->name, command_id, ami_cmd);
        } else {
-               ast_agi_send(agi->fd, chan, "510 Invalid or unknown command\n");
-               manager_event(EVENT_FLAG_AGI, "AGIExec",
-                               "SubEvent: End\r\n"
-                               "Channel: %s\r\n"
-                               "CommandId: %d\r\n"
-                               "Command: %s\r\n"
-                               "ResultCode: 510\r\n"
-                               "Result: Invalid or unknown command\r\n", chan->name, command_id, ami_cmd);
+               ami_res = "Command Not Permitted on a dead channel or intercept routine";
+               resultcode = 511;
+
+               ast_agi_send(agi->fd, chan, "%d %s\n", resultcode, ami_res);
+
+               publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res);
        }
-       return 0;
+       ast_module_unref(c->mod);
+
+       return AGI_RESULT_SUCCESS;
 }
+
 static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int *status, int dead, int argc, char *argv[])
 {
        struct ast_channel *c;
-       int outfd, ms, needhup = 0;
+       int outfd;
+       int ms;
+       int needhup = 0;
        enum agi_result returnstatus = AGI_RESULT_SUCCESS;
        struct ast_frame *f;
        char buf[AGI_BUF_LEN];
@@ -3415,10 +4128,16 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
        int retry = AGI_NANDFS_RETRY;
        int send_sighup;
        const char *sighup_str;
-       
+       const char *exit_on_hangup_str;
+       int exit_on_hangup;
+       /*! Running in an interception routine is like DeadAGI mode.  No touchy the channel frames. */
+       int in_intercept = ast_channel_get_intercept_mode();
+
        ast_channel_lock(chan);
        sighup_str = pbx_builtin_getvar_helper(chan, "AGISIGHUP");
-       send_sighup = ast_strlen_zero(sighup_str) || !ast_false(sighup_str);
+       send_sighup = !ast_false(sighup_str);
+       exit_on_hangup_str = pbx_builtin_getvar_helper(chan, "AGIEXITONHANGUP");
+       exit_on_hangup = ast_true(exit_on_hangup_str);
        ast_channel_unlock(chan);
 
        if (!(readf = fdopen(agi->ctrl, "r"))) {
@@ -3428,7 +4147,7 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                close(agi->ctrl);
                return AGI_RESULT_FAILURE;
        }
-       
+
        setlinebuf(readf);
        setup_env(chan, request, agi->fd, (agi->audio > -1), argc, argv);
        for (;;) {
@@ -3439,21 +4158,35 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                                if (pid > -1) {
                                        kill(pid, SIGHUP);
                                } else if (agi->fast) {
-                                       send(agi->ctrl, "HANGUP\n", 7, 0);
+                                       ast_agi_send(agi->fd, chan, "HANGUP\n");
                                }
                        }
+                       if (exit_on_hangup) {
+                               break;
+                       }
                }
                ms = -1;
-               c = ast_waitfor_nandfds(&chan, dead ? 0 : 1, &agi->ctrl, 1, NULL, &outfd, &ms);
+               if (dead || in_intercept) {
+                       c = ast_waitfor_nandfds(&chan, 0, &agi->ctrl, 1, NULL, &outfd, &ms);
+               } else if (!ast_check_hangup(chan)) {
+                       c = ast_waitfor_nandfds(&chan, 1, &agi->ctrl, 1, NULL, &outfd, &ms);
+               } else {
+                       /*
+                        * Read the channel control queue until it is dry so we can
+                        * switch to dead mode.
+                        */
+                       c = chan;
+               }
                if (c) {
                        retry = AGI_NANDFS_RETRY;
                        /* Idle the channel until we get a command */
                        f = ast_read(c);
                        if (!f) {
-                               ast_debug(1, "%s hungup\n", chan->name);
-                               returnstatus = AGI_RESULT_HANGUP;
+                               ast_debug(1, "%s hungup\n", ast_channel_name(chan));
                                needhup = 1;
-                               continue;
+                               if (!returnstatus) {
+                                       returnstatus = AGI_RESULT_HANGUP;
+                               }
                        } else {
                                /* If it's voice, write it to the audio pipe */
                                if ((agi->audio > -1) && (f->frametype == AST_FRAME_VOICE)) {
@@ -3466,11 +4199,12 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                } else if (outfd > -1) {
                        size_t len = sizeof(buf);
                        size_t buflen = 0;
+                       enum agi_result cmd_status;
 
                        retry = AGI_NANDFS_RETRY;
                        buf[0] = '\0';
 
-                       while (buflen < (len - 1)) {
+                       while (len > 1) {
                                res = fgets(buf + buflen, len, readf);
                                if (feof(readf))
                                        break;
@@ -3481,17 +4215,14 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                                buflen = strlen(buf);
                                if (buflen && buf[buflen - 1] == '\n')
                                        break;
-                               len -= buflen;
+                               len = sizeof(buf) - buflen;
                                if (agidebug)
-                                       ast_verbose( "AGI Rx << temp buffer %s - errno %s\n", buf, strerror(errno));
+                                       ast_verbose("AGI Rx << temp buffer %s - errno %s\nNo \\n received, checking again.\n", buf, strerror(errno));
                        }
 
                        if (!buf[0]) {
                                /* Program terminated */
-                               if (returnstatus) {
-                                       returnstatus = -1;
-                               }
-                               ast_verb(3, "<%s>AGI Script %s completed, returning %d\n", chan->name, request, returnstatus);
+                               ast_verb(3, "<%s>AGI Script %s completed, returning %d\n", ast_channel_name(chan), request, returnstatus);
                                if (pid > 0)
                                        waitpid(pid, status, 0);
                                /* No need to kill the pid anymore, since they closed us */
@@ -3506,15 +4237,23 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                        }
 
                        /* get rid of trailing newline, if any */
-                       if (*buf && buf[strlen(buf) - 1] == '\n')
-                               buf[strlen(buf) - 1] = 0;
+                       buflen = strlen(buf);
+                       if (buflen && buf[buflen - 1] == '\n') {
+                               buf[buflen - 1] = '\0';
+                       }
+
                        if (agidebug)
-                               ast_verbose("<%s>AGI Rx << %s\n", chan->name, buf);
-                       returnstatus |= agi_handle_command(chan, agi, buf, dead);
-                       /* If the handle_command returns -1, we need to stop */
-                       if (returnstatus < 0) {
-                               needhup = 1;
-                               continue;
+                               ast_verbose("<%s>AGI Rx << %s\n", ast_channel_name(chan), buf);
+                       cmd_status = agi_handle_command(chan, agi, buf, dead || in_intercept);
+                       switch (cmd_status) {
+                       case AGI_RESULT_FAILURE:
+                               if (dead || in_intercept || !ast_check_hangup(chan)) {
+                                       /* The failure was not because of a hangup. */
+                                       returnstatus = AGI_RESULT_FAILURE;
+                               }
+                               break;
+                       default:
+                               break;
                        }
                } else {
                        if (--retry <= 0) {
@@ -3524,6 +4263,7 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                        }
                }
        }
+
        if (agi->speech) {
                ast_speech_destroy(agi->speech);
        }
@@ -3537,7 +4277,7 @@ static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi
                        }
                        waitpid(pid, status, WNOHANG);
                } else if (agi->fast) {
-                       send(agi->ctrl, "HANGUP\n", 7, 0);
+                       ast_agi_send(agi->fd, chan, "HANGUP\n");
                }
        }
        fclose(readf);
@@ -3693,9 +4433,6 @@ static int write_htmldump(const char *filename)
 
        AST_RWLIST_RDLOCK(&agi_commands);
        AST_RWLIST_TRAVERSE(&agi_commands, command, list) {
-#ifdef AST_XML_DOCS
-               char *stringptmp;
-#endif
                char *tempstr, *stringp;
 
                if (!command->cmda[0])  /* end ? */
@@ -3708,8 +4445,7 @@ static int write_htmldump(const char *filename)
                fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
                fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TH></TR>\n", fullcmd, command->summary);
 #ifdef AST_XML_DOCS
-               stringptmp = ast_xmldoc_printable(command->usage, 0);
-               stringp = ast_strdup(stringptmp);
+               stringp = ast_xmldoc_printable(command->usage, 0);
 #else
                stringp = ast_strdup(command->usage);
 #endif
@@ -3727,9 +4463,6 @@ static int write_htmldump(const char *filename)
                fprintf(htmlfile, "</TD></TR>\n");
                fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
                ast_free(stringp);
-#ifdef AST_XML_DOCS
-               ast_free(stringptmp);
-#endif
        }
        AST_RWLIST_UNLOCK(&agi_commands);
        fprintf(htmlfile, "</TABLE>\n</BODY>\n</HTML>\n");
@@ -3788,7 +4521,7 @@ static int agi_exec_full(struct ast_channel *chan, const char *data, int enhance
                        return -1;
        }
 #endif
-       res = launch_script(chan, args.argv[0], args.argv, fds, enhanced ? &efd : NULL, &pid);
+       res = launch_script(chan, args.argv[0], args.argc, args.argv, fds, enhanced ? &efd : NULL, &pid);
        /* Async AGI do not require run_agi(), so just proceed if normal AGI
           or Fast AGI are setup with success. */
        if (res == AGI_RESULT_SUCCESS || res == AGI_RESULT_SUCCESS_FAST) {
@@ -3838,23 +4571,43 @@ static int agi_exec(struct ast_channel *chan, const char *data)
 
 static int eagi_exec(struct ast_channel *chan, const char *data)
 {
-       int readformat, res;
+       int res;
+       struct ast_format *readformat;
+       struct ast_format *requested_format = NULL;
+       const char *requested_format_name;
 
        if (ast_check_hangup(chan)) {
                ast_log(LOG_ERROR, "EAGI cannot be run on a dead/hungup channel, please use AGI.\n");
                return 0;
        }
-       readformat = chan->readformat;
-       if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
-               ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
+
+       requested_format_name = pbx_builtin_getvar_helper(chan, "EAGI_AUDIO_FORMAT");
+       if (requested_format_name) {
+               requested_format = ast_format_cache_get(requested_format_name);
+               if (requested_format) {
+                       ast_verb(3, "<%s> Setting EAGI audio pipe format to %s\n",
+                                        ast_channel_name(chan), ast_format_get_name(requested_format));
+               } else {
+                       ast_log(LOG_ERROR, "Could not find requested format: %s\n", requested_format_name);
+               }
+       }
+
+       readformat = ao2_bump(ast_channel_readformat(chan));
+       if (ast_set_read_format(chan, requested_format ?: ast_format_slin)) {
+               ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", ast_channel_name(chan));
+               ao2_cleanup(requested_format);
+               ao2_cleanup(readformat);
                return -1;
        }
        res = agi_exec_full(chan, data, 1, 0);
        if (!res) {
                if (ast_set_read_format(chan, readformat)) {
-                       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(readformat));
+                       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", ast_channel_name(chan),
+                               ast_format_get_name(readformat));
                }
        }
+       ao2_cleanup(requested_format);
+       ao2_cleanup(readformat);
        return res;
 }
 
@@ -3913,34 +4666,56 @@ AST_TEST_DEFINE(test_agi_null_docs)
 
 static int unload_module(void)
 {
+       STASIS_MESSAGE_TYPE_CLEANUP(agi_exec_start_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(agi_exec_end_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(agi_async_start_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(agi_async_exec_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(agi_async_end_type);
+
        ast_cli_unregister_multiple(cli_agi, ARRAY_LEN(cli_agi));
-       /* we can safely ignore the result of ast_agi_unregister_multiple() here, since it cannot fail, as
-          we know that these commands were registered by this module and are still registered
-       */
-       (void) ast_agi_unregister_multiple(ast_module_info->self, commands, ARRAY_LEN(commands));
+       ast_agi_unregister_multiple(ast_module_info->self, commands, ARRAY_LEN(commands));
        ast_unregister_application(eapp);
        ast_unregister_application(deadapp);
        ast_manager_unregister("AGI");
+       ast_unregister_application(app);
        AST_TEST_UNREGISTER(test_agi_null_docs);
-       return ast_unregister_application(app);
+       return 0;
 }
 
 static int load_module(void)
 {
-       ast_cli_register_multiple(cli_agi, ARRAY_LEN(cli_agi));
-       /* we can safely ignore the result of ast_agi_register_multiple() here, since it cannot fail, as
-          no other commands have been registered yet
-       */
-       (void) ast_agi_register_multiple(ast_module_info->self, commands, ARRAY_LEN(commands));
-       ast_register_application_xml(deadapp, deadagi_exec);
-       ast_register_application_xml(eapp, eagi_exec);
-       ast_manager_register_xml("AGI", EVENT_FLAG_AGI, action_add_agi_cmd);
+       int err = 0;
+
+       err |= STASIS_MESSAGE_TYPE_INIT(agi_exec_start_type);
+       err |= STASIS_MESSAGE_TYPE_INIT(agi_exec_end_type);
+       err |= STASIS_MESSAGE_TYPE_INIT(agi_async_start_type);
+       err |= STASIS_MESSAGE_TYPE_INIT(agi_async_exec_type);
+       err |= STASIS_MESSAGE_TYPE_INIT(agi_async_end_type);
+
+       err |= ast_cli_register_multiple(cli_agi, ARRAY_LEN(cli_agi));
+       err |= ast_agi_register_multiple(ast_module_info->self, commands, ARRAY_LEN(commands));
+       err |= ast_register_application_xml(deadapp, deadagi_exec);
+       err |= ast_register_application_xml(eapp, eagi_exec);
+       err |= ast_manager_register_xml("AGI", EVENT_FLAG_AGI, action_add_agi_cmd);
+       err |= ast_register_application_xml(app, agi_exec);
+
        AST_TEST_REGISTER(test_agi_null_docs);
-       return ast_register_application_xml(app, agi_exec);
+
+       if (err) {
+               unload_module();
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       /* For Optional API. */
+       ast_module_shutdown_ref(AST_MODULE_SELF);
+
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk Gateway Interface (AGI)",
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_APP_DEPEND,
-               );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND,
+       .requires = "res_speech",
+);