// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa018, %rsp           # imm = 0xA018
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x98a5(%rip), %rsi      # 0xc044
movq %r13, %rdi
callq 0xb81c
testq %rax, %rax
je 0x2b27
leaq 0x9c68(%rip), %rsi      # 0xc41f
movq %r13, %rdi
callq 0xb9a4
leaq 0xa47a(%rip), %rsi      # 0xcc40
movq %rax, %rdi
callq 0x22e0
testl %eax, %eax
je 0x2b27
leaq 0x9867(%rip), %rsi      # 0xc044
movq %r13, %rdi
callq 0xb81c
movq %rax, %rdi
callq 0xb8ae
movq %rax, %r12
movq 0x8(%rbx), %r15
leaq 0x9b3f(%rip), %rbp      # 0xc33a
movq %r13, %rdi
movq %rbp, %rsi
callq 0xb9a4
leaq 0x983c(%rip), %rsi      # 0xc049
leaq 0x983a(%rip), %rdx      # 0xc04e
movq %r15, %rdi
movq %rax, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x544e
movq %r13, %rdi
movq %rbp, %rsi
callq 0xb9a4
movq %rax, %r15
leaq 0xea57(%rip), %rbp      # 0x11290
movq %rbp, %rdi
callq 0x2180
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2100
testl %eax, %eax
je 0x2878
movq 0x8(%rbx), %rdi
leaq 0x97eb(%rip), %rsi      # 0xc049
leaq 0x9807(%rip), %rdx      # 0xc06c
leaq 0xea24(%rip), %rcx      # 0x11290
xorl %eax, %eax
callq 0x544e
jmp 0x2b27
leaq 0x980d(%rip), %rsi      # 0xc08c
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2100
testl %eax, %eax
je 0x28c2
movq %r14, %rdi
callq 0xb0b4
movq %rax, %r15
leaq 0x97f9(%rip), %rsi      # 0xc099
movq %rax, %rdi
callq 0xb57d
movq %r13, %rdi
callq 0xb7fe
testq %rax, %rax
je 0x28e8
movq %r13, %rdi
callq 0xb7fe
movq %rax, %rsi
jmp 0x28ef
movl $0x2, 0x10(%r14)
movq 0x8(%rbx), %rdi
leaq 0x9774(%rip), %rsi      # 0xc049
leaq 0x97b5(%rip), %rdx      # 0xc091
xorl %eax, %eax
callq 0x544e
jmp 0x2b27
leaq 0x97b2(%rip), %rsi      # 0xc0a1
movq %r15, %rdi
callq 0xb995
leaq 0x9a3c(%rip), %rsi      # 0xc33a
movq %r13, %rdi
callq 0xb9a4
leaq 0x9723(%rip), %rsi      # 0xc030
movq %r15, %rdi
movq %rax, %rdx
callq 0xb666
movq %r14, %rdi
callq 0xb0b4
leaq 0x971d(%rip), %rsi      # 0xc044
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0xb57d
leaq 0x10(%rsp), %rbp
movl $0x5000, %edx           # imm = 0x5000
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x5010(%rsp), %rdi
movl $0x5000, %edx           # imm = 0x5000
xorl %esi, %esi
callq 0x2140
movl 0xe959(%rip), %r13d     # 0x112bc
movq %r12, %rdi
callq 0x2180
movl %r13d, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x23b0
movl 0xe93d(%rip), %edi      # 0x112bc
leaq 0x9704(%rip), %rsi      # 0xc08a
pushq $0x1
popq %rdx
callq 0x23b0
pushq $0x1
popq %rdi
callq 0x2400
movl 0xe924(%rip), %edi      # 0x112c0
movl $0x5000, %edx           # imm = 0x5000
movq %rbp, %rsi
callq 0x22c0
cmpb $0x0, (%rbp)
jne 0x2a83
movl 0xe903(%rip), %edi      # 0x112bc
leaq 0x96e6(%rip), %rsi      # 0xc0a6
pushq $0x7
popq %rdx
callq 0x23b0
movl 0xe8ee(%rip), %edi      # 0x112bc
leaq 0x96b5(%rip), %rsi      # 0xc08a
pushq $0x1
popq %rdx
callq 0x23b0
movl 0xe8dd(%rip), %edi      # 0x112c0
leaq 0x10(%rsp), %r13
movl $0x5000, %edx           # imm = 0x5000
movq %r13, %rsi
callq 0x22c0
movzbl (%r13), %eax
cmpl $0x30, %eax
je 0x2a55
testl %eax, %eax
jne 0x2a5e
pushq $0x1
popq %rbp
leaq 0x10(%rsp), %r13
testb %al, %al
jne 0x2a2f
movl %ebp, %edi
callq 0x2400
movl 0xe8a4(%rip), %edi      # 0x112c0
movl $0x5000, %edx           # imm = 0x5000
movq %r13, %rsi
callq 0x22c0
movb 0x10(%rsp), %al
jmp 0x2a0b
leaq 0x10(%rsp), %rdi
pushq $0x30
popq %rsi
callq 0x2330
testq %rax, %rax
je 0x2b3c
movq %rax, %r13
movq %rax, %rdi
callq 0x2180
movq %r13, %rdi
jmp 0x2a79
movw $0xa, 0x10(%rsp)
jmp 0x2a83
leaq 0x10(%rsp), %rdi
pushq $0x30
popq %rsi
callq 0x2330
movq %rax, %r12
movq %rax, %rdi
callq 0x2180
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2140
leaq 0x9600(%rip), %rsi      # 0xc08a
leaq 0x10(%rsp), %rdi
callq 0x2130
testq %rax, %rax
je 0x2ad4
leaq 0x5010(%rsp), %r12
leaq 0x95e2(%rip), %rbp      # 0xc08a
movq %r12, %rdi
movq %rax, %rsi
callq 0x2280
movq %r12, %rdi
callq 0x2180
movw $0xa, 0x5010(%rsp,%rax)
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2130
testq %rax, %rax
jne 0x2aa8
movq %r14, %rdi
callq 0xb0b4
movq %rax, %r14
leaq 0x5010(%rsp), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xb711
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xb6e5
movq %r15, %rdi
movq %r13, %rsi
callq 0xb6e5
cmpb $0x0, (%r12)
je 0x2b1f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5084
movq %r15, %rdi
callq 0xb220
pushq $0x1
popq %rax
addq $0xa018, %rsp           # imm = 0xA018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x9502(%rip), %rsi      # 0xc049
leaq 0x9560(%rip), %rdx      # 0xc0ae
leaq 0x10(%rsp), %r13
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x544e
leaq 0x9579(%rip), %rsi      # 0xc0e0
movq %r13, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x23c0
jmp 0x2a83