// File: /workspace/github_c_makefile_star2_O1/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 0xa7c1(%rip), %rsi      # 0xd044
movq %r13, %rdi
callq 0xbd67
testq %rax, %rax
je 0x2c1d
leaq 0xab84(%rip), %rsi      # 0xd41f
movq %r13, %rdi
callq 0xbef9
leaq 0xb396(%rip), %rsi      # 0xdc40
movq %rax, %rdi
callq 0x22e0
testl %eax, %eax
je 0x2c1d
leaq 0xa783(%rip), %rsi      # 0xd044
movq %r13, %rdi
callq 0xbd67
movq %rax, %rdi
callq 0xbe0c
movq %rax, %r12
movq 0x8(%rbx), %r15
leaq 0xaa5b(%rip), %rbp      # 0xd33a
movq %r13, %rdi
movq %rbp, %rsi
callq 0xbef9
leaq 0xa758(%rip), %rsi      # 0xd049
leaq 0xa756(%rip), %rdx      # 0xd04e
movq %r15, %rdi
movq %rax, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x5546
movq %r13, %rdi
movq %rbp, %rsi
callq 0xbef9
movq %rax, %r15
leaq 0xf973(%rip), %rbp      # 0x12290
movq %rbp, %rdi
callq 0x2180
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2100
testl %eax, %eax
je 0x295c
movq 0x8(%rbx), %rdi
leaq 0xa707(%rip), %rsi      # 0xd049
leaq 0xa723(%rip), %rdx      # 0xd06c
leaq 0xf940(%rip), %rcx      # 0x12290
xorl %eax, %eax
callq 0x5546
jmp 0x2c1d
leaq 0xa729(%rip), %rsi      # 0xd08c
movl $0x4, %edx
movq %r12, %rdi
callq 0x2100
testl %eax, %eax
je 0x29a8
movq %r14, %rdi
callq 0xb5a0
movq %rax, %r15
leaq 0xa713(%rip), %rsi      # 0xd099
movq %rax, %rdi
callq 0xbac6
movq %r13, %rdi
callq 0xbd49
testq %rax, %rax
je 0x29ce
movq %r13, %rdi
callq 0xbd49
movq %rax, %rsi
jmp 0x29d5
movl $0x2, 0x10(%r14)
movq 0x8(%rbx), %rdi
leaq 0xa68e(%rip), %rsi      # 0xd049
leaq 0xa6cf(%rip), %rdx      # 0xd091
xorl %eax, %eax
callq 0x5546
jmp 0x2c1d
leaq 0xa6cc(%rip), %rsi      # 0xd0a1
movq %r15, %rdi
callq 0xbeea
leaq 0xa956(%rip), %rsi      # 0xd33a
movq %r13, %rdi
callq 0xbef9
leaq 0xa63d(%rip), %rsi      # 0xd030
movq %r15, %rdi
movq %rax, %rdx
callq 0xbbb0
movq %r14, %rdi
callq 0xb5a0
leaq 0xa637(%rip), %rsi      # 0xd044
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0xbac6
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 0xf873(%rip), %r13d     # 0x122bc
movq %r12, %rdi
callq 0x2180
movl %r13d, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x23b0
movl 0xf857(%rip), %edi      # 0x122bc
leaq 0xa61e(%rip), %rsi      # 0xd08a
movl $0x1, %edx
callq 0x23b0
movl $0x1, %edi
callq 0x2400
movl 0xf83a(%rip), %edi      # 0x122c0
movl $0x5000, %edx           # imm = 0x5000
movq %rbp, %rsi
callq 0x22c0
cmpb $0x0, (%rbp)
jne 0x2b79
movl 0xf819(%rip), %edi      # 0x122bc
leaq 0xa5fc(%rip), %rsi      # 0xd0a6
movl $0x7, %edx
callq 0x23b0
movl 0xf802(%rip), %edi      # 0x122bc
leaq 0xa5c9(%rip), %rsi      # 0xd08a
movl $0x1, %edx
callq 0x23b0
movl 0xf7ef(%rip), %edi      # 0x122c0
leaq 0x10(%rsp), %r13
movl $0x5000, %edx           # imm = 0x5000
movq %r13, %rsi
callq 0x22c0
movzbl (%r13), %eax
cmpl $0x30, %eax
je 0x2b49
testl %eax, %eax
jne 0x2b52
cmpb $0x0, 0x10(%rsp)
jne 0x2b21
leaq 0x10(%rsp), %rbp
movl $0x1, %edi
callq 0x2400
movl 0xf7b3(%rip), %edi      # 0x122c0
movl $0x5000, %edx           # imm = 0x5000
movq %rbp, %rsi
callq 0x22c0
cmpb $0x0, 0x10(%rsp)
je 0x2afd
leaq 0x10(%rsp), %rdi
movl $0x30, %esi
callq 0x2330
testq %rax, %rax
je 0x2c34
movq %rax, %rbp
movq %rax, %rdi
callq 0x2180
movq %rbp, %rdi
jmp 0x2b6f
movw $0xa, 0x10(%rsp)
jmp 0x2b79
leaq 0x10(%rsp), %rdi
movl $0x30, %esi
callq 0x2330
movq %rax, %r12
movq %rax, %rdi
callq 0x2180
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2140
leaq 0xa50a(%rip), %rsi      # 0xd08a
leaq 0x10(%rsp), %rdi
callq 0x2130
testq %rax, %rax
je 0x2bca
leaq 0x5010(%rsp), %r12
leaq 0xa4ec(%rip), %rbp      # 0xd08a
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 0x2b9e
movq %r14, %rdi
callq 0xb5a0
movq %rax, %r14
leaq 0x5010(%rsp), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xbc58
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xbc2c
movq %r15, %rdi
movq %r13, %rsi
callq 0xbc2c
cmpb $0x0, (%r12)
je 0x2c15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5174
movq %r15, %rdi
callq 0xb73c
movl $0x1, %eax
addq $0xa018, %rsp           # imm = 0xA018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0xa40a(%rip), %rsi      # 0xd049
leaq 0xa468(%rip), %rdx      # 0xd0ae
leaq 0x10(%rsp), %r13
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x5546
leaq 0xa481(%rip), %rsi      # 0xd0e0
movq %r13, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x23c0
jmp 0x2b79