--- apps/app_queue.c.org	2006-11-02 12:05:28.000000000 +0100
+++ apps/app_queue.c	2006-11-02 14:42:33.000000000 +0100
@@ -343,6 +343,7 @@
 	int servicelevel;               /*!< seconds setting for servicelevel*/
 	int callscompletedinsl;         /*!< Number of calls answered with servicelevel*/
 	char monfmt[8];                 /*!< Format to use when recording calls */
+	char connectmacro[80];			/*!< Macro to execute on agent-answering-call */
 	char sound_next[80];            /*!< Sound file: "Your call is now first in line" (def. queue-youarenext) */
 	char sound_thereare[80];        /*!< Sound file: "There are currently" (def. queue-thereare) */
 	char sound_calls[80];           /*!< Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting)*/
@@ -610,6 +611,7 @@
 	q->announceholdtime = 0;
 	q->roundingseconds = 0; /* Default - don't announce seconds */
 	q->servicelevel = 0;
+	q->connectmacro[0] = '\0'; /* Default - connectmacro is empty string */
 	q->moh[0] = '\0';
 	q->announce[0] = '\0';
 	q->context[0] = '\0';
@@ -736,6 +738,8 @@
 {
 	if (!strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
 		ast_copy_string(q->moh, val, sizeof(q->moh));
+	} else if (!strcasecmp(param, "connectmacro")) {
+		ast_copy_string(q->connectmacro, val, sizeof(q->connectmacro));
 	} else if (!strcasecmp(param, "announce")) {
 		ast_copy_string(q->announce, val, sizeof(q->announce));
 	} else if (!strcasecmp(param, "context")) {
@@ -2152,6 +2156,9 @@
 
 static int try_calling(struct queue_ent *qe, const char *options, char *announceoverride, const char *url, int *go_on)
 {
+	char macroname[256];
+	int using_macro=0;
+	void *app=NULL;
 	struct member *cur;
 	struct localuser *outgoing=NULL, *tmp = NULL;
 	int to;
@@ -2204,6 +2211,8 @@
 		case 'n':
 			if ((now - qe->start >= qe->parent->timeout))
 				*go_on = 1;
 			break;
+		case 'M':
+			using_macro++;
+ 			break;
 		}
 
@@ -2215,6 +2224,8 @@
 		ast_log(LOG_DEBUG, "%s is trying to call a queue member.\n", 
 							qe->chan->name);
 	ast_copy_string(queuename, qe->parent->name, sizeof(queuename));
+	ast_copy_string(macroname, qe->parent->connectmacro, sizeof(macroname));
+
 	cur = qe->parent->members;
 	if (!ast_strlen_zero(qe->announce))
 		announce = qe->announce;
@@ -2298,6 +2309,24 @@
 		/* Ah ha!  Someone answered within the desired timeframe.  Of course after this
 		   we will always return with -1 so that it is hung up properly after the 
 		   conversation.  */
+
+		/* Firstly, check if the Queue App was started with M option to run a macro */
+		if(using_macro) {
+			if(!strlen(macroname)) {
+				ast_log(LOG_WARNING, "Please define \"connectmacro\" for queue %s in queues.conf\n", 
+						queuename);
+			} else {
+				/* run the macro */
+				app = pbx_findapp("Macro");
+				if(app) {
+
+					pbx_builtin_setvar_helper(qe->chan, "ANSWERER", peer->name);
+					ast_log(LOG_NOTICE, "running macro %s in channel %s\n", macroname, qe->chan->name);
+					res = pbx_exec(qe->chan, app, macroname, 1);
+				}
+			}
+		}
+
 		qe->handled++;
 		if (!strcmp(qe->chan->type,"Zap"))
 			ast_channel_setoption(qe->chan, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);

