// File: /workspace/github_c_makefile_star2_O0/liuhaotian[P]xmppsh/xmppsh.c
// Function: message_handler
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
	xmpp_stanza_t *reply, *body, *text;
	char *intext;
	xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
	
	if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
	if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1;
	
	intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
	
	xmpp_debug(conn->ctx, "xmpp", "Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext);

	/*	handle unauthorized message	*/
	if (strncmp(master, xmpp_stanza_get_attribute(stanza, "from"), strlen(master)) != 0)
	{
		xmpp_debug(conn->ctx, "xmpp", "But only Master %s is allowed!\n", master);
		return 1;
	}

	if (strncmp(intext, "exit", 4) == 0)
	{
		//kill(-pid, SIGINT);
		//write(pipeo2i[1], "exit", 4);
		//write(pipeo2i[1], "\n", 1);
		ctx->loop_status = XMPP_LOOP_QUIT;
		xmpp_debug(conn->ctx, "xmpp", "exiting");
		return 1;
	}


	
	reply = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(reply, "message");
	xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat");
	xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from"));
	
	body = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(body, "body");
	
	/*	declare the variable	*/
	
	char* templine;
	char temptext[BUFFSIZE];
	char replytext[BUFFSIZE];
	memset(temptext, 0, BUFFSIZE);
	memset(replytext, 0, BUFFSIZE);
	
	/*	send the commmand to bash	*/
	write(pipeo2i[1], intext, strlen(intext));
	write(pipeo2i[1], "\n", 1);

	/*	get the result	*/
	sleep(1);
	read(pipei2o[0], temptext, BUFFSIZE);

	/*	if nothing return	*/
	if(*temptext == 0){
		
		/*	test if finish	*/
		write(pipeo2i[1], "echo $?", 7);
		write(pipeo2i[1], "\n", 1);
		//sleep(1);
		read(pipei2o[0], temptext, BUFFSIZE);

		/*	completed, no further action to take	*/
		if (*temptext == '0')
		{
			strcpy(temptext, "\n");
		}
		/*	get the previous result	*/
		else if (*temptext != 0)
		{
			/*	clear the test '0' return	*/
			templine = strrchr(temptext,'0');
			memset(templine, 0, strlen(templine));
		}
		/*	not finish yet, use waiting mode	*/
		/*	future support signal control	*/
		else
		{
			while(*temptext == 0)
			{
				sleep(1);
				read(pipei2o[0], temptext, BUFFSIZE);

			}
			/*	back to nonblock mode again	*/
			templine = strrchr(temptext,'0');
			if (templine == NULL)
			{
				xmpp_debug(conn->ctx, "xmpp", "BASH: unsuccessfully execute cmd: %s, return: %s\n", intext, temptext);
				sprintf(temptext, "\"%s\" command not found", intext);
			}
			else
				memset(templine, 0, strlen(templine));
		}


	}

	/*	format the output	*/
	templine = strtok (temptext,"\n");
	while(templine != NULL){

		strcat(replytext, templine);
		strcat(replytext, "\n");

		templine = strtok (NULL, "\n");
		
		if (templine == NULL)
			break;
	}

	text = xmpp_stanza_new(ctx);
	xmpp_stanza_set_text(text, replytext);
	xmpp_stanza_add_child(body, text);
	xmpp_stanza_add_child(reply, body);
	
	if(strlen(replytext) > 0)xmpp_send(conn, reply);
	xmpp_stanza_release(reply);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0a0, %rsp           # imm = 0xA0A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1183a(%rip), %rsi     # 0x14044
callq 0x12b40
cmpq $0x0, %rax
jne 0x2821
movl $0x1, -0x4(%rbp)
jmp 0x2cfd
movq -0x18(%rbp), %rdi
leaq 0x11bf4(%rip), %rsi     # 0x14420
callq 0x12e50
movq %rax, %rdi
leaq 0x123ea(%rip), %rsi     # 0x14c25
callq 0x22e0
cmpl $0x0, %eax
jne 0x2851
movl $0x1, -0x4(%rbp)
jmp 0x2cfd
movq -0x18(%rbp), %rdi
leaq 0x117e8(%rip), %rsi     # 0x14044
callq 0x12b40
movq %rax, %rdi
callq 0x12c60
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa060(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x11ab4(%rip), %rsi     # 0x1433b
callq 0x12e50
movq -0xa060(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %r8
leaq 0x117a8(%rip), %rsi     # 0x14049
leaq 0x117a6(%rip), %rdx     # 0x1404e
movb $0x0, %al
callq 0x6a80
movq -0x18(%rbp), %rdi
leaq 0x11a81(%rip), %rsi     # 0x1433b
callq 0x12e50
movq %rax, -0xa058(%rbp)
leaq 0x169b3(%rip), %rdi     # 0x19280
callq 0x2180
movq -0xa058(%rbp), %rsi
movq %rax, %rdx
leaq 0x1699d(%rip), %rdi     # 0x19280
callq 0x2100
cmpl $0x0, %eax
je 0x291d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x1174d(%rip), %rsi     # 0x14049
leaq 0x11769(%rip), %rdx     # 0x1406c
leaq 0x16976(%rip), %rcx     # 0x19280
movb $0x0, %al
callq 0x6a80
movl $0x1, -0x4(%rbp)
jmp 0x2cfd
movq -0x40(%rbp), %rdi
leaq 0x11764(%rip), %rsi     # 0x1408c
movl $0x4, %edx
callq 0x2100
cmpl $0x0, %eax
jne 0x296b
movq -0x48(%rbp), %rax
movl $0x2, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x116f8(%rip), %rsi     # 0x14049
leaq 0x11739(%rip), %rdx     # 0x14091
movb $0x0, %al
callq 0x6a80
movl $0x1, -0x4(%rbp)
jmp 0x2cfd
movq -0x48(%rbp), %rdi
callq 0x11cb0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x11716(%rip), %rsi     # 0x14099
callq 0x12550
movq -0x28(%rbp), %rax
movq %rax, -0xa068(%rbp)
movq -0x18(%rbp), %rdi
callq 0x12ae0
cmpq $0x0, %rax
je 0x29b4
movq -0x18(%rbp), %rdi
callq 0x12ae0
movq %rax, -0xa070(%rbp)
jmp 0x29c4
leaq 0x116e6(%rip), %rax     # 0x140a1
movq %rax, -0xa070(%rbp)
jmp 0x29c4
movq -0xa068(%rbp), %rdi
movq -0xa070(%rbp), %rsi
callq 0x12e20
movq -0x28(%rbp), %rax
movq %rax, -0xa088(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1194e(%rip), %rsi     # 0x1433b
callq 0x12e50
movq -0xa088(%rbp), %rdi
movq %rax, %rdx
leaq 0x1162d(%rip), %rsi     # 0x14030
callq 0x12760
movq -0x48(%rbp), %rdi
callq 0x11cb0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x11624(%rip), %rsi     # 0x14044
callq 0x12550
leaq -0x5050(%rbp), %rdi
xorl %esi, %esi
movl $0x5000, %edx           # imm = 0x5000
callq 0x2140
leaq -0xa050(%rbp), %rdi
xorl %esi, %esi
movl $0x5000, %edx           # imm = 0x5000
callq 0x2140
movl 0x1685b(%rip), %eax     # 0x192ac
movl %eax, -0xa07c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xa078(%rbp)
movq -0x40(%rbp), %rdi
callq 0x2180
movl -0xa07c(%rbp), %edi
movq -0xa078(%rbp), %rsi
movq %rax, %rdx
callq 0x23a0
movl 0x16826(%rip), %edi     # 0x192ac
leaq 0x115fd(%rip), %rsi     # 0x1408a
movl $0x1, %edx
callq 0x23a0
movl $0x1, %edi
callq 0x23e0
movl 0x16809(%rip), %edi     # 0x192b0
leaq -0x5050(%rbp), %rsi
movl $0x5000, %edx           # imm = 0x5000
callq 0x22c0
movsbl -0x5050(%rbp), %eax
cmpl $0x0, %eax
jne 0x2c37
movl 0x167de(%rip), %edi     # 0x192ac
leaq 0x115d1(%rip), %rsi     # 0x140a6
movl $0x7, %edx
callq 0x23a0
movl 0x167c7(%rip), %edi     # 0x192ac
leaq 0x1159e(%rip), %rsi     # 0x1408a
movl $0x1, %edx
callq 0x23a0
movl 0x167b4(%rip), %edi     # 0x192b0
leaq -0x5050(%rbp), %rsi
movl $0x5000, %edx           # imm = 0x5000
callq 0x22c0
movsbl -0x5050(%rbp), %eax
cmpl $0x30, %eax
jne 0x2b31
leaq -0x5050(%rbp), %rdi
leaq 0x11563(%rip), %rsi     # 0x1408a
callq 0x2190
jmp 0x2c35
movsbl -0x5050(%rbp), %eax
cmpl $0x0, %eax
je 0x2b7c
leaq -0x5050(%rbp), %rdi
movl $0x30, %esi
callq 0x2330
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xa090(%rbp)
movq -0x50(%rbp), %rdi
callq 0x2180
movq -0xa090(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x2140
jmp 0x2c33
jmp 0x2b7e
movsbl -0x5050(%rbp), %eax
cmpl $0x0, %eax
jne 0x2bad
movl $0x1, %edi
callq 0x23e0
movl 0x16716(%rip), %edi     # 0x192b0
leaq -0x5050(%rbp), %rsi
movl $0x5000, %edx           # imm = 0x5000
callq 0x22c0
jmp 0x2b7e
leaq -0x5050(%rbp), %rdi
movl $0x30, %esi
callq 0x2330
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2c0c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rcx
leaq -0x5050(%rbp), %r8
leaq 0x11466(%rip), %rsi     # 0x14049
leaq 0x114c4(%rip), %rdx     # 0x140ae
movb $0x0, %al
callq 0x6a80
leaq -0x5050(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x114dd(%rip), %rsi     # 0x140e0
movb $0x0, %al
callq 0x23b0
jmp 0x2c31
movq -0x50(%rbp), %rax
movq %rax, -0xa098(%rbp)
movq -0x50(%rbp), %rdi
callq 0x2180
movq -0xa098(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x2140
jmp 0x2c33
jmp 0x2c35
jmp 0x2c37
leaq -0x5050(%rbp), %rdi
leaq 0x11445(%rip), %rsi     # 0x1408a
callq 0x2130
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x2c97
leaq -0xa050(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x2280
leaq -0xa050(%rbp), %rdi
leaq 0x11417(%rip), %rsi     # 0x1408a
callq 0x2280
xorl %eax, %eax
movl %eax, %edi
leaq 0x11407(%rip), %rsi     # 0x1408a
callq 0x2130
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2c95
jmp 0x2c97
jmp 0x2c4e
movq -0x48(%rbp), %rdi
callq 0x11cb0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq -0xa050(%rbp), %rsi
callq 0x128e0
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x12850
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x12850
leaq -0xa050(%rbp), %rdi
callq 0x2180
cmpq $0x0, %rax
jbe 0x2ced
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6290
movq -0x28(%rbp), %rdi
callq 0x11f80
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0a0, %rsp           # imm = 0xA0A0
popq %rbp
retq
nopl (%rax)