/* MSN Input0 read source file
 * filename: /src/_cmd.c
 * Coded by James Greig
 * James@mor-pah.net
 * Date: 29/04/05
 */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <ncurses.h>
#include <ctype.h>
#include "headers/defs.h"
#include "headers/command.h"
extern int TOP_PAN;
extern char panusr[MAX_TCP_CON][MAX_ACCOUNT_LEN];
extern void switchwin();
void input_case(char *msgbody)
{
	/* Moving into the future with get_element */


	/* Send message to currently open query */
	if ( msgbody[0] != '/' && strlen(msgbody) > 1){
		char message[255]; /* Temporary Message Storage */
		int ii = 0;
		if ( TOP_PAN ){
			strcpy(message, msgbody);
			message[strlen(message) - 1] = '\0';

			/* Check if a conversation is open, if not open it */
			for(ii = 0;ii<MAX_LIST_SIZE + 1;ii++){
				if ( strcmp(panusr[TOP_PAN - 1], convoa[ii]) == 0 ) break;
				if ( ii == MAX_LIST_SIZE ){
					char sendbuff[128];

					/* Move name and message to queue */
					if ( msgqueue(panusr[TOP_PAN - 1],"") ) return;
					msgqueue(panusr[TOP_PAN - 1], message);

                                        snprintf(sendbuff, sizeof(sendbuff), "XFR %d SB\r\n", tid);
                                        sendData(0, sendbuff, strlen(sendbuff));

					return;
        	                }
			}
			msgqueue(panusr[TOP_PAN - 1], message);
			msg_send(panusr[TOP_PAN - 1], message);
		}
		cmdhistory();
		return;
	}
	
/* /msg function */

if ( msgbody[0] == '/' && msgbody[1] == 'm' && msgbody[2] == 's' && msgbody[3] == 'g' && ( msgbody[4] == ' ' || msgbody[4] == '\n' )){
        if ( strlen(msgbody) <= 5){
                cli_msg("Usage: /MSG USER@HOST MESSAGE\n");
                cmdhistory();
                return;
        }
{
	int ctr2 = 0;
	int ctr = 5;
	int ii = 0;
	char message[255];
	char who[MAX_ACCOUNT_LEN];
	memset(who, '\0', sizeof(who));

	do {
		if (msgbody[ctr] != ' ' && msgbody[ctr] != '\n' ){
			if ( (msgbody[ctr] >= 'A') && (msgbody[ctr] <= 'Z')) who[ctr2] += lc;
			else who[ctr2] = msgbody[ctr];
			ctr2++;
		}
		ctr++;

	} while (msgbody[ctr] != '\0' && msgbody[ctr] != ' ' );

	if (msgbody[ctr] != ' ' ){
		cli_msg("Usage: /MSG USER@HOST MESSAGE\n");
		cmdhistory();
		return;
	}

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

	ctr += 1;
        do {
        	if ( msgbody[ctr] != '\n' ) message[strlen(message)] = msgbody[ctr];
        	ctr++;
        } while ( msgbody[ctr] != '\n' && msgbody[ctr] != '\0' );

	/* Check if a conversation is open, if not open it */
	for(ii = 0;ii<MAX_LIST_SIZE + 1;ii++){
		if ( strcmp(who, convoa[ii]) == 0 ) break;
		if ( ii == MAX_LIST_SIZE ){
			char sendbuff[128];
			msgqueue(who,"");
			snprintf(sendbuff, sizeof(sendbuff), "XFR %d SB\r\n", tid);
			sendData(0, sendbuff, strlen(sendbuff));

				/* Move name and message to queue */
			msgqueue(who, message);
			return;

		}
	}
	msgqueue(who, message);
        msg_send(who, message);
        cmdhistory();
        return;
}
}	

/* /dump */

if ( msgbody[0] == '/' && msgbody[1] == 'd' && msgbody[2] == 'u' && msgbody[3] == 'm' && msgbody[4] == 'p' && strlen(msgbody) == 6 ){
	queuedump();
	return;
}

/* /rem */

if ( msgbody[0] == '/' && msgbody[1] == 'r' && msgbody[2] == 'e' && msgbody[3] == 'm'){
        if ( strlen(msgbody) <= 5 || msgbody[5] == ' '){
                cli_msg("Usage: /REM USER@HOST\n");
                cmdhistory();
                return;
        }
        else {
		char who[MAX_ACCOUNT_LEN];
		char sendbuff[255];
                int ctr = 5;
                memset(who, '\0', sizeof(who));
                do {
                if ( msgbody[ctr] != '\n' && msgbody[ctr] != ' ') who[ctr-5] = msgbody[ctr];
                ctr++;
                } while ( msgbody[ctr] != '\n' && msgbody[ctr] != '\0' );
		for ( ctr=0;ctr < MAX_LIST_SIZE;ctr++){
			if (! strcmp(who, contactn[ctr]) ){
		                snprintf(sendbuff, sizeof(sendbuff), "REM %d AL %s\r\n", tid, who);
        		        sendData(0, sendbuff, strlen(sendbuff));

				if ( strlen(contactc[ctr]) > 1 ){
	                		snprintf(sendbuff, sizeof(sendbuff), "REM %d FL %s\r\n", tid, contactc[ctr]);
	                		sendData(0, sendbuff, strlen(sendbuff));
				}

				cmdhistory();
				return;
			}
		}
		cli_msg("Non-existent contact: %s\n", who);
                cmdhistory();
                return;
	}
}
/* /Time */
if ( msgbody[0] == '/' && msgbody[1] == 't' && msgbody[2] == 'i' && msgbody[3] == 'm' && msgbody[4] == 'e' && strlen(msgbody) == 6){
	elapsed_time();
	cmdhistory();
	return;
}

/* /bye */

if ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'y' && msgbody[3] == 'e' ) {
	char sendbuff[16];
	int i = 0;
	cli_msg("Logging off\n");
	memset(sendbuff, '\0', sizeof(sendbuff));
	snprintf(sendbuff, sizeof(sendbuff), "BYE\r\n");
	sendData(0, sendbuff, strlen(sendbuff));

	clear_contact();
	
	cli_msg("Closing connections\n");
	for(i=0;i<10;i++){
		if ( tcp_sock[i] ){
			if ( i ) cli_msg("Closing connection with %s\n", convoa[i - 1]);
			close(tcp_sock[i]);
			tcp_sock[i] = 0;
		}
	}
	cmdhistory();
	return;
}

/* /connect */

if ( msgbody[0] == '/' && msgbody[1] == 'c' && msgbody[2] == 'o' && msgbody[3] == 'n' && msgbody[4] == 'n' && msgbody[5] == 'e' && msgbody[6] == 'c' && msgbody[7] == 't'){
	char *server;
	int ctr = 9;
	char sendbuff[16];
	if ( msgbody[8] != ' ' || strlen(msgbody) <= 10 || msgbody[strlen(msgbody) - 2] == ' '){
                cli_msg("Usage: /CONNECT USER@HOST\n");
                cmdhistory();
                return;
	}
	clear_contact();	/* Clear contact list database for a fresh login */
	online = 0;	/* Reset Online Status */
	memset(username, '\0', sizeof(username));
	memset(password, '\0', sizeof(password));
	do {
		username[ctr-9] = msgbody[ctr];
		ctr++;
	} while(msgbody[ctr] != '\0' && msgbody[ctr] != '\n');
        noecho();

        wprintw(_cmd, "Enter Your Password: ");
        wrefresh(_cmd);

        wgetstr(_cmd, password);
        wclear(_cmd);
        wrefresh(_cmd);
	cli_msg("Logging in as %s...\n", username);
	echo();
	i = 0;
	if ( tcp_sock[0] ) tcp_sockdcon(0);
	if (! ( server = dispatch(username)) ){
		cmdhistory();
		return;
	}
	else {
		if ( notification(username, password, server) ) {
			cmdhistory();
			return;
		}
	}

        if ( strlen(nick) > 10 ){
                mvwprintw(_status, 1, 0, "Status: Online\t\tNickName: %c%c%c%c%c%c%c%c%c%c...\n", nick[0], \
                nick[1], nick[2], nick[3], nick[4], nick[5], nick[6], nick[7], nick[8], nick[9]);
        }
        else {
                mvwprintw(_status, 1, 0, "Status: Online\t\tNickName: %s\n", nick);
        }

        wrefresh(_status);
	cli_msg("Login Successful, %s online\n", username);
	cmdhistory();
	return;
}

/* /hide contacts */
if ( msgbody[0] == '/' && msgbody[1] == 'h' && msgbody[2] == 'i' && msgbody[3] == 'd' && msgbody[4] == 'e' && strlen(msgbody) == 6){
	contacts_hide();
	cmdhistory();
	return;
}

/* /show contacts */
if ( msgbody[0] == '/' && msgbody[1] == 's' && msgbody[2] == 'h' && msgbody[3] == 'o' && msgbody[4] == 'w' && strlen(msgbody) == 6){
        contacts_show();
        cmdhistory();
	return;
}


/* /Refresh */
if ( msgbody[0] == '/' && msgbody[1] == 'r' && msgbody[2] == 'e' && msgbody[3] == 'f' && msgbody[4] == 'r' && msgbody[5] == 'e' && msgbody[6] == 's' && msgbody[7] == 'h' && strlen(msgbody) == 9){
        redrawwin(_cmdph);
        wrefresh(_cmdph);
	redrawwin(console);
//        wrefresh(console);
      redrawwin(_status);
        wrefresh(_status);
	if ( contact_list_show ){
		 redrawwin(_contacts);
		wrefresh(_contacts);
	}
/*	redrawwin(_cmd); */
	wrefresh(_cmd); 
	cli_msg("Terminal Refreshed\n");
	cmdhistory();
	return;
}
/* Away status */
if ( msgbody[0] == '/' && msgbody[1] == 'a' && msgbody[2] == 'w' && msgbody[3] == 'a' && msgbody[4] == 'y' && strlen(msgbody) == 6){
	status("AWY");
	cmdhistory();
	return;
}


/* Idle status */
if ( msgbody[0] == '/' && msgbody[1] == 'i' && msgbody[2] == 'd' && msgbody[3] == 'l' && msgbody[4] == 'e' && strlen(msgbody) == 6){
	status("IDL");
        cmdhistory();
        return;
}


/* Online status (back) */

if ( ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'a' && msgbody[3] == 'c' && msgbody[4] == 'k' && strlen(msgbody) == 6) || \
( msgbody[0] == '/' && msgbody[1] == 'o' && msgbody[2] == 'n' && msgbody[3] == 'l' && msgbody[4] == 'i' && msgbody[5] == 'n' && msgbody[6] == 'e' && strlen(msgbody) == 8) ){
//	snprintf(sendbuff, sizeof(sendbuff), "CHG %d NLN 268435456\r\n", tid);
	status("NLN");
        cmdhistory();
	return;
}

/* Appear Offline status /hidden */

if ( msgbody[0] == '/' && msgbody[1] == 'h' && msgbody[2] == 'i' && msgbody[3] == 'd' && msgbody[4] == 'd' && msgbody[5] == 'e' && msgbody[6] == 'n' && strlen(msgbody) == 8){
	status("HDN");
        cmdhistory();
	return;
}

/* Busy status */

if ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'u' && msgbody[3] == 's' && msgbody[4] == 'y' && strlen(msgbody) == 6){
	status("BSY");
        cmdhistory();
	return;
}

/* Be Right Back status */

if ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'r' && msgbody[3] == 'b' && strlen(msgbody) == 5){
	status("BRB");
        cmdhistory();
	return;
}

/* On The Phone status */

if ( msgbody[0] == '/' && msgbody[1] == 'p' && msgbody[2] == 'h' && msgbody[3] == 'n' && strlen(msgbody) == 5){
	status("PHN");
        cmdhistory();
	return;
}

/* Out To Lunch status */

if ( msgbody[0] == '/' && msgbody[1] == 'o' && msgbody[2] == 'u' && msgbody[3] == 't' && strlen(msgbody) == 5){
	status("LUN");
        cmdhistory();
	return;
}

/* File Transfer (send) */

if ( msgbody[0] == '/' &&  msgbody[1] == 's' && msgbody[2] == 'e' && msgbody[3] == 'n' && msgbody[4] == 'd' ){
        if ( strlen(msgbody) <= 6 || msgbody[6] == ' '){
                cli_msg("Usage: /SEND USER@HOST FILE\n");
		cmdhistory();
		return;
        }else {
	int ctr = 0;
        char who[MAX_ACCOUNT_LEN];
        char msg[512];
        char msga[255];
        char sendbuff[255];
        int ii, i;
        int tmp;
        struct stat sb;

	
	/* The user@host */
        memset(who, '\0', sizeof(who));
        ctr = 6;
        do {
        	if ( msgbody[ctr] != ' ' ) who[ctr-6] = msgbody[ctr];
        	ctr++;
        } while ( msgbody[ctr] != ' ' && msgbody[ctr] != '\0');

	/* The file name */
        tmp = ctr;
        memset(msg, '\0', sizeof(msg));
        memset(msga, '\0', sizeof(msga));

        do {
	        if ( msgbody[ctr] != '\n' ) msga[ctr-tmp] = msgbody[ctr];
	        ctr++;
        } while ( msgbody[ctr] != '\n' );
	
	stat(msga, &sb);
	srand(time(0));
	cookie = rand()%7295+1;
	snprintf(msg, sizeof(msg), "MIME-Version: 1.0\r\nContent-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\nApplication-Name: File Transfer\r\nApplication-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\nInvitation-Command: INVITE\r\nInvitation-Cookie: %d\r\nApplication-File: %s\r\nApplication-FileSize: %d\r\nConnectivity: N\r\n", cookie, msga, (int) sb.st_size);
	snprintf(sendbuff, sizeof(sendbuff), "MSG %d A %d\r\n%s", tid, strlen(msg), msg);
/*
	snprintf(content, sizeof(content), "MIME-Version: 1.0\r\nContent-Type: application/x-msnmsgrp2p\r\nP2P-Dest: %s\r\n\r\n", who);
	snprintf(sendbuff, sizeof(sendbuff), "MSG %d D %d\r\n%s", tid, strlen(content));
*/
        /* Check if a conversation is open, if not open it */

        tmp = 0;

        for(ii=0;ii<10;ii++){
                if(! strcmp(who, convoa[ii]) )  {
			i = ii + 1;
			sendData(i, sendbuff, strlen(sendbuff));
			cli_msg("Waiting for %s to accept file send: %s\n", who, msga);

			cmdhistory();
			return;
		}
        }

	cli_msg("Please make a connection first\n");

	cmdhistory();
	return;
	}
}

/* The notorious help command */

if ( msgbody[0] == '/' && msgbody[1] == 'h' && msgbody[2] == 'e' && msgbody[3] == 'l' && msgbody[4] == 'p' ){
	help(msgbody);
	cmdhistory();
	return;
}

/* Logging */
if ( msgbody[0] == '/' && msgbody[1] == 'l' && msgbody[2] == 'o' && msgbody[3] == 'g' ){
        if ( strlen(msgbody) != 7 ){
                cli_msg("Usage: /LOG 1 or /LOG 0\n");
                cmdhistory();
                return;
        }
        if ( msgbody[5] == '1' ){
                cli_msg("Logging Enabled\n");
                args[2][0] = 1;
        }
        if ( msgbody[5] == '0' ){
                cli_msg("Logging Disabled\n");
                args[2][0] = 0;
        }
        cmdhistory();
        return;
}

/* Debugging */
if ( msgbody[0] == '/' && msgbody[1] == 'd' && msgbody[2] == 'e' && msgbody[3] == 'b' \
&& msgbody[4] == 'u' && msgbody[5] == 'g' ){
	if ( strlen(msgbody) != 9 ){
		cli_msg("Usage: /DEBUG 1 or /DEBUG 0\n");
		cmdhistory();
		return;
	}
	if ( msgbody[7] == '1' ){
		cli_msg("Debugging Enabled\n");
		args[0][0] = 1;
	}
        if ( msgbody[7] == '0' ){
                cli_msg("Debugging Disabled\n");
                args[0][0] = 0;
        }
	cmdhistory();
	return;
}

/* Add a user to your allow and contact lists */

if ( msgbody[0] == '/' && msgbody[1] == 'a' && msgbody[2] == 'd' && msgbody[3] == 'd' ){
	if ( strlen(msgbody) <= 5 || msgbody[5] == ' '){
                cli_msg("Usage: /ADD USER@HOST\n", NULL, NULL, NULL, NULL);
		cmdhistory();
		return;
        }
        else {
		char sendbuff[255];
		char who[MAX_ACCOUNT_LEN];
		int ctr = 5;
                memset(who, '\0', sizeof(who));
                do {
	                if ( msgbody[ctr] != '\n' ) who[ctr-5] = msgbody[ctr];
	                ctr++;
                } while ( msgbody[ctr] != '\n' && msgbody[ctr] != '\0');

                snprintf(sendbuff, sizeof(sendbuff), "ADC %d AL N=%s\r\n", tid, who);
                sendData(0, sendbuff, strlen(sendbuff));

                snprintf(sendbuff, sizeof(sendbuff), "ADC %d FL N=%s F=%s\r\n", tid, who, who);
                sendData(0, sendbuff, strlen(sendbuff));

		cmdhistory();
		return;
	}
}


/* User@host blocking function */

if ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'l' && msgbody[3] == 'o' && msgbody[4] == 'c' && msgbody[5] == 'k' ){
	if ( strlen(msgbody) <= 8 || msgbody[7] == ' '){
		cli_msg("Please enter a vaild account name\n", NULL, NULL, NULL, NULL);
		cmdhistory();
		return;
	}
	else {
	int ii, tmp;
	char sendbuff[255];
	char who[MAX_ACCOUNT_LEN];
	int ctr = 7;
        do {
	        if ( (msgbody[ctr] >= 'A') && (msgbody[ctr] <= 'Z')){
        	        msgbody[ctr] += lc;
        	}
        ctr++;
        } while ( msgbody[ctr] != '\n');

	        memset(who, '\0', sizeof(who));
        	ctr = 7;
	        do {
        	if ( msgbody[ctr] != '\n' && msgbody[ctr] != ' ' && msgbody[ctr] != '\0') who[ctr-7] = msgbody[ctr];
        	ctr++;
        	} while ( msgbody[ctr] != '\n' && msgbody[ctr] != ' ' && msgbody[ctr] != '\0');
		for(ctr=0;ctr <= MAX_LIST_SIZE;ctr++){
			if (! strcmp(contactn[ctr],who) ){
		                snprintf(sendbuff, sizeof(sendbuff), "REM %d AL %s\r\n", tid, who);
		                sendData(0, sendbuff, strlen(sendbuff));

		                snprintf(sendbuff, sizeof(sendbuff), "ADC %d BL N=%s\r\n", tid, who);
		                sendData(0, sendbuff, strlen(sendbuff));

				blocked[ctr] = 1;
		                s_msg("User %s moved to 'block' list\n", who, NULL);
				cmdhistory();
				return;
			}

		}
		s_msg("ERROR: User %s is not in your contact list\n", who);
	}
	cmdhistory();
	return;
}

/* User@host 'UNblocking function /ublock */

if ( msgbody[0] == '/' && msgbody[1] == 'u' && msgbody[2] == 'b' && msgbody[3] == 'l' && \
 msgbody[4] == 'o' && msgbody[5] == 'c' && msgbody[6] == 'k'){
	if ( strlen(msgbody) <= 9 || msgbody[8] == ' '){
        	cli_msg("Please enter a vaild account name\n", NULL, NULL, NULL, NULL);
		cmdhistory();
		return;
	}
        else {
	char who[MAX_ACCOUNT_LEN];
        int ctr = 7;
	int ii, tmp;
	char sendbuff[255];
        do {
        if ( (msgbody[ctr] >= 'A') && (msgbody[ctr] <= 'Z')){
                msgbody[ctr] += lc;
        }
        ctr++;
        } while ( msgbody[ctr] != '\n');

        	memset(who, '\0', sizeof(who));
               	ctr = 8;
                do {
                if ( msgbody[ctr] != '\n' && msgbody[ctr] != ' ' && msgbody[ctr] != '\0' ) who[ctr-8] = msgbody[ctr];
                ctr++;
                } while ( msgbody[ctr] != '\n' && msgbody[ctr] != ' ' && msgbody[ctr] != '\0' );
                for (ctr=0;ctr<MAX_LIST_SIZE;ctr++){
                        if(! ( strcmp(who, contactn[ctr]) ) ){
				if ( blocked[ctr] == 0 ){
					s_msg("User %s is not in your block list\n", who);					
					
					cmdhistory();
					return;
				}
                                break;
                        }
                }
		blocked[ctr] = 0;
                snprintf(sendbuff, sizeof(sendbuff), "REM %d BL %s\r\n", tid, who);
                sendData(0, sendbuff, strlen(sendbuff));

                snprintf(sendbuff, sizeof(sendbuff), "ADC %d AL N=%s\r\n", tid, who);
                sendData(0, sendbuff, strlen(sendbuff));

                s_msg("User %s moved to 'Allow' list\n", who, NULL);


	}
	cmdhistory();
	return;
}

/* /nick Nick change function */
	
if ( msgbody[0] == '/' && msgbody[1] == 'n' && msgbody[2] == 'i' && msgbody[3] == 'c' && msgbody[4] == 'k' ){
	if ( strlen(msgbody) <= 7 || msgbody[6] == ' ' || strlen(msgbody) > MAX_NICK_LEN + 6){
        	cli_msg("Please enter a valid nickname (Maximum length 129 characters)\n");
		cli_msg("Current Nick: %s\n", nick);
		cmdhistory();
		return;
	}
        else {
                int ctr = 6, tmp = 6, tmp2 = 6;
		char ch;
		char msg[MAX_NICK_LEN];
		char sendbuff[255];
		memset(msg, '\0', sizeof(msg));
                do {
                ch = msgbody[ctr];
                if ( ch == ' ' ) {;
			msg[tmp-tmp2] = '%';
			tmp++;
			msg[tmp-tmp2] = '2';
			tmp++;
			msg[tmp-tmp2] = '0';
			}
		else msg[tmp-tmp2] = ch;
		ctr++;
		tmp++;
                } while ( ch != '\n' );
		msg[strlen(msg) - 1] = '\0';

                snprintf(sendbuff, sizeof(sendbuff), "PRP %d MFN %s\r\n", tid, msg);
                sendData(0, sendbuff, strlen(sendbuff));

		/* Stores NickName for later usage (NON-URL ENCODED) */
                memset(nick, '\0', sizeof(nick));
                ctr = 0;
                tmp = 0;
		msg[strlen(msg)] = '\n';
                do {
                ch = msg[ctr];
                if ( ch == '%' && msg[ctr + 1] == '2' && msg[ctr + 2] == '0' ){
         		ch = ' ';
               		nick[ctr-tmp] = ch;
                	ctr += 2;
                	tmp += 2;
	        }
                if ( ch != '\n' && ch != ' ' )nick[ctr-tmp] = ch;
                ctr++;
                } while( ch != '\n');
	}
	cmdhistory();
	return;
}

/* /listn */
if ( msgbody[0] == '/' && msgbody[1] == 'l' && msgbody[2] == 'i' && msgbody[3] == 's' && msgbody[4] == 't' && msgbody[5] == 'n' && strlen(msgbody) == 7){
	int ctr;
	box_title("\nFriendly Nickname List\n\n");
	for(ctr=0;ctr<150;ctr++){
		if ( strlen(contactp[ctr]) > 0 ) {
			box_title(contactf[ctr]);
	               	wprintw(console, "\n%s\n", contactn[ctr]);
		}
	}
	wprintw(console, "\n");

	switchwin(TOP_PAN);

	cmdhistory();
	return;
}
/* /list */
if ( msgbody[0] == '/' && msgbody[1] == 'l' && msgbody[2] == 'i' && msgbody[3] == 's' && msgbody[4] == 't' && strlen(msgbody) == 6){
	outlist();
	cmdhistory();
	return;
}
/* /lista */
if ( msgbody[0] == '/' && msgbody[1] == 'l' && msgbody[2] == 'i' && msgbody[3] == 's' && msgbody[4] == 't' && msgbody[5] == 'a' && strlen(msgbody) == 7){
	int ctr = 0;
	box_title("\nFull Contact List\n");
	for(ctr=0;ctr <MAX_LIST_SIZE;ctr++){
		if ( strlen(contactn[ctr]) > 0 ) {
			box_title(contactf[ctr]);
			wprintw(console, "\n");
			if ( strlen(contactf[ctr]) > 0 ) wprintw(console, "%s\n", contactn[ctr]);
			if ( strlen(contactc[ctr]) > 0 ) wprintw(console, "%s\n", contactc[ctr]);
		}
	}

	switchwin(TOP_PAN);

        cmdhistory();
        return;
}

/* Close specified switchboard connection upon /CLOSE */

if ( msgbody[0] == '/' && msgbody[1] == 'c' && msgbody[2] == 'l' && msgbody[3] == 'o' && msgbody[4] == 's' && msgbody[5] == 'e' ){
	if ( strlen(msgbody) <= 7 || msgbody[6] != ' ' ){
		if ( TOP_PAN ){
			int ctr;
			char sendbuff[255];
			char user[MAX_ACCOUNT_LEN];
			strcpy(user, panusr[TOP_PAN-1]);
			close_convo(panusr[TOP_PAN-1]);
                        for(ctr=0;ctr < MAX_TCP_CON;ctr++){
                        	if ( ! strcmp(convoa[ctr],user) ){
                                        snprintf(sendbuff, sizeof(sendbuff), "OUT\r\n");
                                        sendData(ctr+1, sendbuff, strlen(sendbuff));

                                        tcp_sock[ctr+1] = 0;
                                        memset(convoa[ctr], '\0', sizeof(convoa[ctr]));
                                        memset(convot[ctr], '\0', sizeof(convot[ctr]));
                                }
			}
                        s_msg("Conversation closed with %s\n", user);
                        cmdhistory();
                        return;
		}
		cli_msg("Usage: /CLOSE [USER@HOST | CONVO #]\n");	
		cmdhistory();
		return;
	} else {
		char sendbuff[255];
		char user[128];	
		int ctr, ctr2;

		do {
		        if ( (msgbody[ctr] >= 'A') && (msgbody[ctr] <= 'Z')){
		                msgbody[ctr] += lc;
		        }
		        ctr++;
	        } while ( msgbody[ctr] != '\n');
		memset(user, '\0', sizeof(user));
		get_element(msgbody, strlen(msgbody), user, 1);
		for(ctr=0;ctr < MAX_TCP_CON;ctr++){
			if ( ! strcmp(panusr[ctr],user) ){ 
				close_convo(user);
		                for(ctr2=0;ctr2 < MAX_TCP_CON;ctr2++){
		                        if ( ! strcmp(convoa[ctr2],user) ){
					snprintf(sendbuff, sizeof(sendbuff), "OUT\r\n");
					sendData(ctr2+1, sendbuff, strlen(sendbuff));

                                        tcp_sock[ctr2+1] = 0;
                                        memset(convoa[ctr2], '\0', sizeof(convoa[ctr2]));
                                        memset(convot[ctr2], '\0', sizeof(convot[ctr2]));
		                        }
		                }
				s_msg("Conversation closed with %s\n", user);
				cmdhistory();
				return;
			}
		}
		s_msg("Close Error: No open conversations with %s\n", user);
		cmdhistory();
		return;
	}
}

/* /Plist function lists open conversation panels */

if ( msgbody[0] == '/' && msgbody[1] == 'p' && msgbody[2] == 'l' \
&& msgbody[3] == 'i' && msgbody[4] == 's' && msgbody[5] == 't' ){
	int ctr;
	box_title("\nOpen Conversation List\n");
	box_title("User\t\t\t\tConversation #\n");
	for(ctr=0;ctr < MAX_TCP_CON;ctr++){
		if ( strlen(panusr[ctr])) wprintw(console, "%s\t\t%d\t\n", panusr[ctr], ctr+1);
	}
        waddch(console, '\n');
	switchwin(TOP_PAN);

        cmdhistory();
	return;
}

/* /Clist function lists open connections */

if ( msgbody[0] == '/' && msgbody[1] == 'c' && msgbody[2] == 'l' \
&& msgbody[3] == 'i' && msgbody[4] == 's' && msgbody[5] == 't' ){
	int ii;
	box_title("\nOpen Connection List\n");
	box_title("User\t\t\t\tSocket #\tType\n");
	for(ii=0;ii<MAX_TCP_CON;ii++){
		if ( strlen(convoa[ii]) > 1 ){
			wprintw(console, "%s\t\t%d\t\t%s\n", convoa[ii], ii+1, convot[ii]);
		}
	}
	waddch(console, '\n');

	switchwin(TOP_PAN);

	cmdhistory();
	return;
}

/* BLP */

if ( msgbody[0] == '/' && msgbody[1] == 'b' && msgbody[2] == 'l' && msgbody[3] == 'p'){
	if ( (! (msgbody[5] == 'a' || msgbody[5] == 'b')) || strlen(msgbody) != 7 ){
		cli_msg("Please enter a valid list type\n");
		cli_msg("Usage: \"/blp a\" OR \"/blp b\"\n");
		cmdhistory();
		return;
	}{
	char sendbuff[16];
	
	if ( msgbody[5] == 'a' ){
		snprintf(sendbuff, sizeof(sendbuff), "BLP %d AL\r\n", tid);
		sendData(0, sendbuff, strlen(sendbuff));
	}
	if ( msgbody[5] == 'b' ){
		snprintf(sendbuff, sizeof(sendbuff), "BLP %d BL\r\n", tid);
		sendData(0, sendbuff, strlen(sendbuff));
	}
	}
	cmdhistory();
	return;
}

/* /quit */

if ( ( msgbody[0] == '/' && msgbody[1] == 'q' && msgbody[2] == 'u' \
&& msgbody[3] == 'i' && msgbody[4] == 't' ) || ( msgbody[0] == '/' \
&& msgbody[1] == 'e' && msgbody[2] == 'x' && msgbody[3] == 'i' \
&& msgbody[4] == 't' ) ){
	elapsed_time();
	cli_msg("Exiting\n", NULL, NULL, NULL, NULL);
	destroy_window();
	exit(0);
}

/* /Invite a user into an already open conversation */

if ( msgbody[0] == '/' && msgbody[1] == 'i' && msgbody[2] == 'n' \
&& msgbody[3] == 'v' && msgbody[4] == 'i' && msgbody[5] == 't' && msgbody[6] == 'e'){
        if ( strlen(msgbody) <= 8 || msgbody[8] == ' ' || ! TOP_PAN){
                c_msg(TOP_PAN, "Usage: /INVITE USER@HOST\n");
		cmdhistory();
		return;
        }
	else {
                if ( TOP_PAN ){
	                int ctr = 0;
                        char user[MAX_ACCOUNT_LEN];
                        char sendbuff[255];
	                do {
	                        if ( (msgbody[ctr] >= 'A') && (msgbody[ctr] <= 'Z')){
	                                msgbody[ctr] += lc;
	                        }
	                        ctr++;
	                } while ( msgbody[ctr] != '\n');
	                memset(user, '\0', sizeof(user));
	                get_element(msgbody, strlen(msgbody), user, 1);
			ctr = 0;
			do { 
                                if ( strcmp(panusr[TOP_PAN - 1], convoa[ctr]) == 0 ){
	        	                c_msg(TOP_PAN-1, "Inviting %s into conversation\n", user);
					memset(sendbuff, '\0', sizeof(sendbuff));

	        	                snprintf(sendbuff, sizeof(sendbuff), "CAL %d %s\r\n", tid, user);
	        	                sendData(ctr+1, sendbuff, strlen(sendbuff));
					cmdhistory();
					return;
	        	        }
				ctr++;
			} while ( ctr < MAX_TCP_CON );
                        cmdhistory();
                        return;
                }
                cli_msg("Usage: /INVITE USER@HOST\nMust be performed in a conversation window\n");
                cmdhistory();
                return;
	}
}

/* /raw */
if ( msgbody[0] == '/' && msgbody[1] == 'r' && msgbody[2] == 'a' && msgbody[3] == 'w' && strlen(msgbody) < 64){
	char sendbuff[64];
	int ctr = 5;
	memset(sendbuff, '\0', sizeof(sendbuff));
	do {
		sendbuff[strlen(sendbuff)] = msgbody[ctr];
		ctr++;
	} while ( msgbody[ctr] != '\n' && msgbody[ctr] != '\0');
	strcat(sendbuff, "\r\n");
	sendData(0, sendbuff, strlen(sendbuff));
	cmdhistory();
	return;
}

/* Switch window */

if ( msgbody[0] == '/' && msgbody[1] == 's' ){
	switchwin(msgbody[3] - 48);
	return;
}
/* /query Requesting a switchboard session */

if ( msgbody[0] == '/' && msgbody[1] == 'q' && msgbody[2] == 'u' && msgbody[3] == 'e' && msgbody[4] == 'r' && msgbody[5] == 'y'){
	if ( strlen(msgbody) <= 8 || msgbody[8] == ' '){
		cli_msg("Please enter a vaild account name\n", NULL, NULL, NULL, NULL);
		cmdhistory();
		return;
	}
	else {

	char who[MAX_ACCOUNT_LEN];
	int ii;
	char sendbuff[255];
        memset(who, '\0', sizeof(who));

	get_element(msgbody, strlen(msgbody), who, 1);

	/* is there already a panel open? */
	if ( panopen(who) ){
		switchwin(utop(who)+1);
		s_msg("There's already a conversation open with %s, please check /qlist\n",who);
		cmdhistory();
		return;
	}

	cli_msg("Opening conversation with %s\n", who);

        for(ii = 0;ii<MAX_LIST_SIZE + 1;ii++){
                if (! strcmp(who, convoa[ii]) ){
		        /* is there already a panel open? */
        		if ( panopen(who) ){
                		switchwin(utop(who)+1);
                		s_msg("There's already a conversation open with %s, please check /qlist\n",who);
                		cmdhistory();
                		return;
        		}

			return;
		}
                if ( ii == 150 ){
			msgqueue(who,"");
                        snprintf(sendbuff, sizeof(sendbuff), "XFR %d SB\r\n", tid);
                        sendData(0, sendbuff, strlen(sendbuff));

                        cmdhistory();
                        return;
                }
        }

	}
}
	if ( msgbody[0] == '/' ){
		cli_msg("Unknown Command: %s\n", msgbody, NULL, NULL, NULL);
		return;
	}
	return;
}

void outlist()
{
        int tmp;
	int ctr;
	int set = 0;
        box_title("\nStatus\t\t\tNick\t\t\tUser\n\n");
        for(tmp=0;tmp<MAX_LIST_SIZE;tmp++){
                if ( strlen(contactn[tmp]) > 0 && strlen(contactp[tmp]) > 0) {
			if (! strcmp("NLN", contactp[tmp]) ){
				wprintw(console, "Online ");
				set += 1;
			}
			if (! strcmp("BSY", contactp[tmp]) ) wprintw(console, "Busy ");
			if (! strcmp("IDL", contactp[tmp]) ) wprintw(console, "Idle ");
			if (! strcmp("BRB", contactp[tmp]) ) wprintw(console, "Brb ");
			if (! strcmp("AWY", contactp[tmp]) ) wprintw(console, "Away ");
			if (! strcmp("PHN", contactp[tmp]) ) wprintw(console, "Phone ");
			if (! strcmp("LUN", contactp[tmp]) ) wprintw(console, "Lunch ");

			if ( blocked[tmp] == 1){
				wprintw(console,"(Blocked)");
				set += 10;
			}

			if ( set == 11 ) wprintw(console, "\t");
			if ( set == 10 ) wprintw(console, "\t\t");
			if ( set == 0 || set == 1 ) wprintw(console, "\t\t\t");
			set = 0;
                        if ( strlen(contactf[tmp]) > 10 ){
                                wprintw(console, "%.10s...\t\t%s\n", contactf[tmp], contactn[tmp]);
                        }
                        else if ( strlen(contactf[tmp]) > 7 && strlen(contactf[tmp]) <= 10 ){
                                wprintw(console, "%s\t\t%s\n", contactf[tmp], contactn[tmp]);
                        }
                        else wprintw(console, "%s\t\t\t%s\n", contactf[tmp], contactn[tmp]);
                }
        }

	switchwin(TOP_PAN);
	return;
}
