diff --git a/CHANGES b/CHANGES
index 487bdfe21512c14592e4e02deca5750e43434b6d..caa63d558fc182eb9225f61b7ab02135b7b509b2 100644
--- a/CHANGES
+++ b/CHANGES
@@ -623,6 +623,9 @@ AGI Changes
 -----------
   * Added SPEECH commands for speech recognition. A complete listing can be found
      using agi show.
+  * If app_stack is loaded, GOSUB is a native AGI command that may be used to
+    invoke subroutines in the dialplan.  Note that calling EXEC with Gosub
+    does not behave as expected; the native command needs to be used, instead.
 
 Logger changes
 --------------
diff --git a/apps/app_dial.c b/apps/app_dial.c
index 105bce60d7eed358dba8811d7f008c9fcddab32c..205127014facd8307a292176a8b3508602cb3886 100644
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -1776,13 +1776,13 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 				ast_copy_string(peer->exten, "s", sizeof(peer->exten));
 				peer->priority = 0;
 
-				gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], '|');
+				gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], ',');
 				if (gosub_argstart) {
 					*gosub_argstart = 0;
-					asprintf(&gosub_args, "%s|s|1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1);
-					*gosub_argstart = '|';
+					asprintf(&gosub_args, "%s,s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1);
+					*gosub_argstart = ',';
 				} else {
-					asprintf(&gosub_args, "%s|s|1", opt_args[OPT_ARG_CALLEE_GOSUB]);
+					asprintf(&gosub_args, "%s,s,1", opt_args[OPT_ARG_CALLEE_GOSUB]);
 				}
 
 				if (gosub_args) {
diff --git a/apps/app_stack.c b/apps/app_stack.c
index 56f9465294e4d8bf50ec83bc9be11ae4fd7a6d95..7131a8d3cc9c1dcc88d9561013a79f9488551a95 100644
--- a/apps/app_stack.c
+++ b/apps/app_stack.c
@@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/manager.h"
 #include "asterisk/channel.h"
+#include "asterisk/agi.h"
 
 static const char *app_gosub = "Gosub";
 static const char *app_gosubif = "GosubIf";
@@ -61,7 +62,6 @@ static const char *pop_descrip =
 "  StackPop():\n"
 "Removes last label on the stack, discarding it.\n";
 
-
 static void gosub_free(void *data);
 
 static struct ast_datastore_info stack_info = {
@@ -234,7 +234,7 @@ static int gosub_exec(struct ast_channel *chan, void *data)
 	);
 
 	if (ast_strlen_zero(data)) {
-		ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[(arg1[|...][|argN])])\n", app_gosub, app_gosub);
+		ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub);
 		return -1;
 	}
 
@@ -394,8 +394,101 @@ static struct ast_custom_function local_function = {
 	.read = local_read,
 };
 
+static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, char **argv)
+{
+	int old_priority, priority;
+	char old_context[AST_MAX_CONTEXT], old_extension[AST_MAX_EXTENSION];
+	struct ast_app *theapp;
+	char *gosub_args;
+
+	if (argc < 4 || argc > 5) {
+		return RESULT_SHOWUSAGE;
+	}
+
+	ast_debug(1, "Gosub called with %d arguments: 0:%s 1:%s 2:%s 3:%s 4:%s\n", argc, argv[0], argv[1], argv[2], argv[3], argc == 5 ? argv[4] : "");
+
+	if (sscanf(argv[3], "%d", &priority) != 1 || priority < 1) {
+		/* Lookup the priority label */
+		if ((priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3], chan->cid.cid_num)) < 0) {
+			ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
+			ast_agi_fdprintf(chan, agi->fd, "200 result=-1 Gosub label not found\n");
+			return RESULT_FAILURE;
+		}
+	} else if (!ast_exists_extension(chan, argv[1], argv[2], priority, chan->cid.cid_num)) {
+		ast_agi_fdprintf(chan, agi->fd, "200 result=-1 Gosub label not found\n");
+		return RESULT_FAILURE;
+	}
+
+	/* Save previous location, since we're going to change it */
+	ast_copy_string(old_context, chan->context, sizeof(old_context));
+	ast_copy_string(old_extension, chan->exten, sizeof(old_extension));
+	old_priority = chan->priority;
+
+	if (!(theapp = pbx_findapp("Gosub"))) {
+		ast_log(LOG_ERROR, "Gosub() cannot be found in the list of loaded applications\n");
+		ast_agi_fdprintf(chan, agi->fd, "503 result=-2 Gosub is not loaded\n");
+		return RESULT_FAILURE;
+	}
+
+	/* Apparently, if you run ast_pbx_run on a channel that already has a pbx
+	 * structure, you need to add 1 to the priority to get it to go to the
+	 * right place.  But if it doesn't have a pbx structure, then leaving off
+	 * the 1 is the right thing to do.  See how this code differs when we
+	 * call a Gosub for the CALLEE channel in Dial or Queue.
+	 */
+	if (argc == 5) {
+		asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority + 1, argv[4]);
+	} else {
+		asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority + 1);
+	}
+
+	if (gosub_args) {
+		int res;
+
+		ast_debug(1, "Trying gosub with arguments '%s'\n", gosub_args);
+		ast_copy_string(chan->context, "app_stack_gosub_virtual_context", sizeof(chan->context));
+		ast_copy_string(chan->exten, "s", sizeof(chan->exten));
+		chan->priority = 0;
+
+		if ((res = pbx_exec(chan, theapp, gosub_args)) == 0) {
+			ast_agi_fdprintf(chan, agi->fd, "100 result=0 Trying...\n");
+			ast_pbx_run(chan);
+			ast_agi_fdprintf(chan, agi->fd, "200 result=0 Gosub complete\n");
+		} else {
+			ast_agi_fdprintf(chan, agi->fd, "200 result=%d Gosub failed\n", res);
+		}
+		ast_free(gosub_args);
+	} else {
+		ast_agi_fdprintf(chan, agi->fd, "503 result=-2 Memory allocation failure\n");
+		return RESULT_FAILURE;
+	}
+
+	/* Restore previous location */
+	ast_copy_string(chan->context, old_context, sizeof(chan->context));
+	ast_copy_string(chan->exten, old_extension, sizeof(chan->exten));
+	chan->priority = old_priority;
+
+	return RESULT_SUCCESS;
+}
+
+static char usage_gosub[] =
+" Usage: GOSUB <context> <extension> <priority> [<optional-argument>]\n"
+"   Cause the channel to execute the specified dialplan subroutine, returning\n"
+" to the dialplan with execution of a Return()\n";
+
+struct agi_command gosub_agi_command =
+	{ { "gosub", NULL }, handle_gosub, "Execute a dialplan subroutine", usage_gosub , 0 };
+
 static int unload_module(void)
 {
+	struct ast_context *con;
+
+	if ((con = ast_context_find("app_stack_gosub_virtual_context"))) {
+		ast_context_remove_extension2(con, "s", 1, NULL);
+		ast_context_destroy(con, "app_stack"); /* leave nothing behind */
+	}
+
+	ast_agi_unregister(ast_module_info->self, &gosub_agi_command);
 	ast_unregister_application(app_return);
 	ast_unregister_application(app_pop);
 	ast_unregister_application(app_gosubif);
@@ -407,10 +500,20 @@ static int unload_module(void)
 
 static int load_module(void)
 {
+	struct ast_context *con;
+	con = ast_context_find_or_create(NULL, NULL, "app_stack_gosub_virtual_context", "app_stack");
+	if (!con) {
+		ast_log(LOG_ERROR, "Virtual context 'app_stack_gosub_virtual_context' does not exist and unable to create\n");
+		return AST_MODULE_LOAD_DECLINE;
+	} else {
+		ast_add_extension2(con, 1, "s", 1, NULL, NULL, "KeepAlive", ast_strdup(""), ast_free_ptr, "app_stack");
+	}
+
 	ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
 	ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
 	ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
 	ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
+	ast_agi_register(ast_module_info->self, &gosub_agi_command);
 	ast_custom_function_register(&local_function);
 
 	return 0;
diff --git a/include/asterisk/agi.h b/include/asterisk/agi.h
index 5085f05df29b286a39660438f4378afcf8380210..7c1a0aa68d340e5143e1f1a1689670dfd43c7c7d 100644
--- a/include/asterisk/agi.h
+++ b/include/asterisk/agi.h
@@ -27,6 +27,8 @@
 extern "C" {
 #endif
 
+#include "asterisk/cli.h"
+
 typedef struct agi_state {
 	int fd;		        /*!< FD for general output */
 	int audio;	        /*!< FD for audio output */