/* MSN Switchboard Sock-read Header
 * filename: /src/switch.c
 * Coded by James Greig
 * James@mor-pah.net
 * Date: 30/06/03
 */
#include <ncurses.h>
#include <string.h>
#include "headers/defs.h"
#include "headers/switch.h"
void switch_b(int sock_num)
{
	char recvbuff[1024];
	char sendbuff[512];
	int ctr, tmp;
	readData(sock_num, recvbuff, sizeof(recvbuff));

/* Closing a conversation */

if ( recvbuff[0] == 'B' && recvbuff[1] == 'Y' && recvbuff[2] == 'E'){
	char user[MAX_ACCOUNT_LEN];

	memset(user, '\0', sizeof(user));
	
	get_element(recvbuff, strlen(recvbuff), user, 1);

	if ( strcmp(convoa[sock_num-1],user) == 0 ) {
		memset(convoa[sock_num-1], '\0', sizeof(convoa[sock_num-1]));
		memset(convot[sock_num-1], '\0', sizeof(convot[sock_num-1]));

		c_msg(utop(user), "User %s closed the connection (or closed the window)\n", user);

		tcp_sockdcon(sock_num);
	} 
	else {
		c_msg(utop(convoa[sock_num-1]), "Participant %s has left conversation\n", user);
		dufromp(sock_num-1, user);
	}

	return;
}

/* Opening a conversation */

if( recvbuff[0] == 'I' && recvbuff[1] == 'R' && recvbuff[2] == 'O'){
	char user[MAX_ACCOUNT_LEN];

	memset(user, '\0', sizeof(user));
	get_element(recvbuff, strlen(recvbuff), user, 4);

//        strncpy(convoa[sock_num-1], user, MAX_ACCOUNT_LEN);

//	s_msg("Conversation open with %s : Convo %d : Socket %d\n", user, (utop(user) + 1), (int) sock_num);

        if ( strlen(convoa[sock_num - 1]) > 0 ) {
                c_msg(utop(convoa[sock_num-1]),"%s has joined the conversation\n", user);
                autop(convoa[sock_num-1], user);
                strcpy(convot[sock_num-1], "Public");
                strcat(convoa[sock_num-1], " ");
        }
        else{
                utop(user);
                update_status();
                s_msg("Conversation open with %s : Convo %d : Socket %d\n", user, (utop(user) + 1), (int) sock_num);
                strcpy(convot[sock_num-1], "Private");
                switchwin(utop(user)+1);
        }
        strcat(convoa[sock_num-1], user);

	return;
}

/* Conversation opening error Occurs when ANS is incorrect */

if ( recvbuff[0] == '9' && recvbuff[1] == '1' && recvbuff[2] == '1' ){
        s_msg("ERROR: Connection requested from %s FAILED\n", convoa[sock_num-1], NULL);
	tcp_sockdcon(sock_num);
	return;
}

/* MSG Message Recieved */
if ( recvbuff[0] == 'M' && recvbuff[1] == 'S' && recvbuff[2] == 'G' ) {
        char content[128];
	char ipaddressx[64];
	char portx[12];
	int ipptr = 0, portptr = 0, fportx = 0;
        ctr = 0;
        tmp = 0;

        memset(content, '\0', sizeof(content));

        do {
                if ( recvbuff[ctr] == '\r' ) tmp++;
                if ( tmp == 2 ){
                        tmp++;
                        ctr += 2;
                }
                if ( tmp == 3 ){
                        content[strlen(content)] = recvbuff[ctr];
                }
                ctr++;
        } while( recvbuff[ctr] != '\0' && tmp < 4);

        if (! strcmp(content, "Content-Type: text/x-msmsgsinvite; charset=UTF-8") ) {
                return;
        }
	if (! strcmp(content, "Content-Type: text/x-msmsgscontrol") ) {
		return;
	}
	if (! strcmp(content, "Content-Type: application/x-msnmsgrp2p") ) {
		return;
	}
	if (! strcmp(content, "Content-Type: text/plain; charset=UTF-8") ) {
		char user[MAX_ACCOUNT_LEN];
		char says[MAX_MESSAGE_LEN];                                 /* Buffer for user messages received blah says */
		int space_count = 0;
		int ctr = 0;
		memset(says, '\0', sizeof(says));
		memset(user, '\0', sizeof(user));
		get_element(recvbuff, strlen(recvbuff), user, 1);

		do {
			if ( recvbuff[ctr] == '\r' ) space_count++;
			if ( space_count >= 5 ) break;
			ctr++;
		} while ( recvbuff[ctr] != '\0');

		ctr += 2;

		do {
			if ( recvbuff[ctr] != '\r' ) says[strlen(says)] = recvbuff[ctr];
			ctr++;
		} while ( recvbuff[ctr] != '\0');

        	if( says[1] != '\n'){
	                u_msg(says, user, convoa[sock_num-1]);
	        }
		return;
	}

	return;
}	

/* Message sending success */

if ( recvbuff[0] == 'A' && recvbuff[1] == 'C' && recvbuff[2] == 'K'){
	int ctr;
	if ( cookie ) {
		cookie = 0;
		return;
	}
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' ){
			r_msg(msgqn[ctr], msgqm[ctr]);
			memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
			memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			return;
		}
	}
	return;
}

/* Message sending failure */

if ( recvbuff[0] == 'N' && recvbuff[1] == 'A' && recvbuff[2] == 'K'){
	int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' ){
                        s_msg("FAILED SENDING: %s | to %s\n", msgqm[ctr], msgqn[ctr]);
                        wrefresh(console);
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
                        return;
                }
        }
        return;
}

if ( recvbuff[0] == 'U' && recvbuff[1] == 'S' && recvbuff[2] == 'R' ){
	int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' && msgqm[ctr][0] == '\0' && ! strlen(msgqm[ctr]) ){
			snprintf(sendbuff, sizeof(sendbuff), "CAL %d %s\r\n", tid, msgqn[ctr]);
			sendData(sock_num, sendbuff, strlen(sendbuff));

                        return;
                }
        }
	s_msg("ERROR: Lost Swithboard Callee'\n", NULL, NULL);

	return;
}

if ( recvbuff[0] == 'J' && recvbuff[1] == 'O' && recvbuff[2] == 'I' ){
	char user[MAX_ACCOUNT_LEN];

	memset(user, '\0', sizeof(user));

	get_element(recvbuff, strlen(recvbuff), user, 1);

	/* Remove this from the queue as it's successful*/
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if ( msgqn[ctr][0] != '\0' && msgqm[ctr][0] == '\0' && ! strlen(msgqm[ctr]) && ! strcmp(user, msgqn[ctr])) {
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			break;
                }
			
        } 
	if ( strlen(convoa[sock_num - 1]) > 0 ) {
		c_msg(utop(convoa[sock_num-1]),"%s has joined the conversation\n", user);
		autop(convoa[sock_num-1], user);
		strcpy(convot[sock_num-1], "Public");
		strcat(convoa[sock_num-1], " ");
	}
	else{
		utop(user);
		update_status();
		s_msg("Conversation open with %s : Convo %d : Socket %d\n", user, (utop(user) + 1), (int) sock_num);
		strcpy(convot[sock_num-1], "Private");
		switchwin(utop(user)+1);
	}
	strcat(convoa[sock_num-1], user);

	/* Send any messages in the queue */
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
        	if (msgqn[ctr][0] != '\0' ){
                	msg_send(msgqn[ctr], msgqm[ctr]);
		}
	}

	return;
}
if ( recvbuff[0] == '2' && recvbuff[1] == '1' && recvbuff[2] == '7'){
        int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' && msgqm[ctr][0] == '\0' && ! strlen(msgqm[ctr]) ){
                        s_msg("Error 217: Connection request failed, the user may not be Online.\n", msgqm[ctr], msgqn[ctr]);
                        wrefresh(console);
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			tcp_sockdcon(sock_num);
                        return;
                }
        }
        return;

}
if ( recvbuff[0] == '2' && recvbuff[1] == '1' && recvbuff[2] == '6'){
        int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' ){
                        s_msg("Error 216: The user %s is Online, however, you may not contact them\n", msgqm[ctr], msgqn[ctr]);
                        wrefresh(console);
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			tcp_sockdcon(sock_num);
                        return;
                }
        }
        return;
}

if ( recvbuff[0] == '2' && recvbuff[1] == '1' && recvbuff[2] == '5'){
        int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' && msgqm[ctr][0] == '\0' && ! strlen(msgqm[ctr]) ){
                        s_msg("Error 215: Unable to open a conversation with %s\n", msgqm[ctr], msgqn[ctr]);
                        wrefresh(console);
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			tcp_sockdcon(sock_num);
                        return;
                }
        }
        return;
}

if ( recvbuff[0] == '2' && recvbuff[1] == '0' && recvbuff[2] == '8'){
        int ctr;
        for(ctr=0;ctr < MAX_QUEUE_SIZE;ctr++){
                if (msgqn[ctr][0] != '\0' && msgqm[ctr][0] == '\0' && ! strlen(msgqm[ctr]) ){
                        s_msg("Error 208: Unable to open a conversation with %s as account name is invalid\n", msgqm[ctr], msgqn[ctr]);
                        wrefresh(console);
                        memset(msgqn[ctr], '\0', sizeof(msgqn[ctr]));
                        memset(msgqm[ctr], '\0', sizeof(msgqm[ctr]));
			tcp_sockdcon(sock_num);
                        return;
                }
        }
        return;
}


}
