Index: channels/chan_agent.c
===================================================================
--- channels/chan_agent.c	(revision 8106)
+++ channels/chan_agent.c	(working copy)
@@ -245,7 +245,7 @@
 
 static struct ast_channel *agent_request(const char *type, int format, void *data, int *cause);
 static int agent_devicestate(void *data);
-static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long logintime, char *uniqueid, char *logcommand);
+static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long logintime, char *uniqueid, char *logcommand, char *channame);
 static int agent_digit(struct ast_channel *ast, char digit);
 static int agent_call(struct ast_channel *ast, char *dest, int timeout);
 static int agent_hangup(struct ast_channel *ast);
@@ -507,9 +507,9 @@
 				status = pbx_builtin_getvar_helper(p->chan, "CHANLOCALSTATUS");
 				if (autologoffunavail && status && !strcasecmp(status, "CHANUNAVAIL")) {
 					long logintime = time(NULL) - p->loginstart;
-					p->loginstart = 0;
+					p->loginstart = -1;
 					ast_log(LOG_NOTICE, "Agent read: '%s' is not available now, auto logoff\n", p->name);
-					agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Chanunavail");
+					agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Chanunavail", NULL);
 				}
 				ast_hangup(p->chan);
 				if (p->wrapuptime && p->acknowledged)
@@ -798,9 +798,9 @@
 				status = pbx_builtin_getvar_helper(p->chan, "CHANLOCALSTATUS");
 				if (autologoffunavail && status && !strcasecmp(status, "CHANUNAVAIL")) {
 					long logintime = time(NULL) - p->loginstart;
-					p->loginstart = 0;
+					p->loginstart = -1;
 					ast_log(LOG_NOTICE, "Agent hangup: '%s' is not available now, auto logoff\n", p->name);
-					agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Chanunavail");
+					agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Chanunavail", NULL);
 				}
 				/* Recognize the hangup and pass it along immediately */
 				ast_hangup(p->chan);
@@ -809,9 +809,9 @@
 			ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
 			if (howlong  && p->autologoff && (howlong > p->autologoff)) {
 				long logintime = time(NULL) - p->loginstart;
-				p->loginstart = 0;
+				p->loginstart = -1;
 				ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
-				agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Autologoff");
+				agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Autologoff", NULL);
 			}
 		} else if (p->dead) {
 			ast_mutex_lock(&p->chan->lock);
@@ -1502,7 +1502,7 @@
 	return 0;
 }
 
-static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long logintime, char *uniqueid, char *logcommand)
+static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long logintime, char *uniqueid, char *logcommand, char *channame)
 {
 	char *tmp = NULL;
 	char agent[AST_MAX_AGENT];
@@ -1514,26 +1514,35 @@
 	}
 	snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
 
-	if (!ast_strlen_zero(uniqueid)) {
-		manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
-				"Agent: %s\r\n"
-				"Reason: %s\r\n"
-				"Loginchan: %s\r\n"
-				"Logintime: %ld\r\n"
-				"Uniqueid: %s\r\n", 
-				p->agent, tmp, loginchan, logintime, uniqueid);
-				ast_queue_log("NONE", uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
+	if (!ast_strlen_zero(loginchan)) {
+		if (!ast_strlen_zero(uniqueid)) {
+			manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
+					"Agent: %s\r\n"
+					"Reason: %s\r\n"
+					"Loginchan: %s\r\n"
+					"Logintime: %ld\r\n"
+					"Uniqueid: %s\r\n", 
+					p->agent, tmp, loginchan, logintime, uniqueid);
+					ast_queue_log("NONE", uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
+		} else {
+			manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
+					"Agent: %s\r\n"
+					"Reason: %s\r\n"
+					"Loginchan: %s\r\n"
+					"Logintime: %ld\r\n",
+					p->agent, tmp, loginchan, logintime);
+					ast_queue_log("NONE", "NONE", agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
+		}
 	} else {
-		manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
-				"Agent: %s\r\n"
-				"Reason: %s\r\n"
-				"Loginchan: %s\r\n"
-				"Logintime: %ld\r\n",
-				p->agent, tmp, loginchan, logintime);
-				ast_queue_log("NONE", "NONE", agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
+					manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
+						      "Agent: %s\r\n"
+						      "Logintime: %ld\r\n"
+						      "Uniqueid: %s\r\n",
+						      p->agent, logintime, uniqueid);
+					ast_queue_log("NONE", uniqueid, agent, "AGENTLOGOFF", "%s|%ld", channame, logintime);
+
 	}
 
-
 	set_agentbycallerid(p->logincallerid, NULL);
 	p->loginchan[0] ='\0';
 	p->logincallerid[0] = '\0';
@@ -1561,8 +1570,8 @@
 			}
 			ret = 0; /* found an agent => return 0 */
 			logintime = time(NULL) - p->loginstart;
-			p->loginstart = 0;
-			agent_logoff_maintenance(p, p->loginchan, logintime, NULL, "CommandLogoff");
+			p->loginstart = -1;
+			agent_logoff_maintenance(p, p->loginchan, logintime, NULL, "CommandLogoff", NULL);
 			break;
 		}
 	}
@@ -1984,7 +1993,7 @@
 					if (callbackmode && !res) {
 						/* Just say goodbye and be done with it */
 						if (!ast_strlen_zero(p->loginchan)) {
-							if (p->loginstart == 0)
+							if (p->loginstart <= 0)
 								time(&p->loginstart);
 							manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogin",
 								      "Agent: %s\r\n"
@@ -1999,9 +2008,9 @@
 								dump_agents();
 						} else {
 							logintime = time(NULL) - p->loginstart;
-							p->loginstart = 0;
+							p->loginstart = -1;
 
-							agent_logoff_maintenance(p, last_loginchan, logintime, chan->uniqueid, NULL);
+							agent_logoff_maintenance(p, last_loginchan, logintime, chan->uniqueid, NULL, "");
 							if (option_verbose > 1)
 								ast_verbose(VERBOSE_PREFIX_2 "Callback Agent '%s' logged out\n", p->agent);
 						}
@@ -2016,7 +2025,7 @@
 							ast_copy_string(p->moh, chan->musicclass, sizeof(p->moh));
 #endif								
 						ast_moh_start(chan, p->moh);
-						if (p->loginstart == 0)
+						if (p->loginstart <= 0)
 							time(&p->loginstart);
 						manager_event(EVENT_FLAG_AGENT, "Agentlogin",
 							      "Agent: %s\r\n"
@@ -2092,15 +2101,13 @@
 						if (p->chan == chan)
 							p->chan = NULL;
 						p->acknowledged = 0;
-						logintime = time(NULL) - p->loginstart;
-						p->loginstart = 0;
+						if (p->loginstart != -1) {
+							logintime = time(NULL) - p->loginstart;
+							p->loginstart = 0;
+						}
 						ast_mutex_unlock(&p->lock);
-						manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
-							      "Agent: %s\r\n"
-							      "Logintime: %ld\r\n"
-							      "Uniqueid: %s\r\n",
-							      p->agent, logintime, chan->uniqueid);
-						ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGOFF", "%s|%ld", chan->name, logintime);
+						if (!p->loginstart)
+							agent_logoff_maintenance(p, NULL, logintime, chan->uniqueid, NULL, chan->name);
 						if (option_verbose > 1)
 							ast_verbose(VERBOSE_PREFIX_2 "Agent '%s' logged out\n", p->agent);
 						/* If there is no owner, go ahead and kill it now */
@@ -2263,7 +2270,7 @@
 		else
 			p->ackcall = 0;
 
-		if (p->loginstart == 0)
+		if (p->loginstart <= 0)
 			time(&p->loginstart);
 		manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogin",
 			      "Agent: %s\r\n"
