Exemple #1
0
void CRemoteConsole :: ProcessInput( string cmd, string payload, string target, CRemoteConsoleClient *sender )
{
	// default server to relay the message to
	bool relayed = false;

	// loop through all connections to find the server the command should be issued on
	for( vector<CBNET *> :: iterator i = m_GHost->m_BNETs.begin( ); i != m_GHost->m_BNETs.end( ); i++ )
	{
		// is this the right one or should we just send it to the first in list?
		if ( target == (*i)->GetServer( ) || target.empty() )
		{
			// don't be so verbose!
			//CONSOLE_Print("[RCON] Relaying command [" + cmd + "] with payload [" + payload + "] to server [" + (*i)->GetServer( ) + "]");
			// spoof a whisper from the rootadmin belonging to this connection
			string msg = (*i)->GetCommandTrigger( ) + cmd;
			if( !payload.empty( ) )
				msg += " " + payload;
			CIncomingChatEvent chatCommand( CBNETProtocol::EID_WHISPER, 0, (*i)->GetRootAdmin( ), msg);
			(*i)->ProcessChatEvent( &chatCommand );
			relayed = true;
			break;
		}
	}
	if (!relayed)
		CONSOLE_Print("[RCON] Could not relay cmd [" + cmd + "] with payload [" + payload + "] to server [" + target + "]: server unknown");
}
/************************************************************************************************
    handleComands()
Arguments:
        myUser - struct with 'my' information
        saAddr - IP address of the SA
        saPortChar - port of SA
Description: The messages typed by the user in the terminal are handled in this function.
The commands permitted are join, leave, find, connect, message, disconnect, exit, lst.
*************************************************************************************************/
int handleComands(user myUser, char * saAddr, char * saPortChar){

    char command[256];
    char * parseCommand;
    int retval, saPort, i=0;

    saPort = atoi(saPortChar);

    /*retrieves comand and stores to string command*/
    memset(command, '\0', 256);

    fgets(command, 256, stdin);

    for(i = 0; i < 256; i++){
        if(command[i] == '\n'){
            parseCommand = (char *) malloc((i+2)*sizeof(char));
            break;
        }
        if(command[i] == ' '){
            parseCommand = (char *) malloc((i+2)*sizeof(char));
            break;
        }
    }

   if(parseCommand == NULL){
        printf("\n>Malloc on ParseCommand not succecefull\n");
        fflush(stdout);
        free(parseCommand);
        exit(EXIT_FAILURE);
    }

    memset(parseCommand, '\0', (i+2));

    retval = sscanf(command, "%s", parseCommand);

    if(retval != 1){
        printf("\n>Error parsing command, please retry\n");
        free(parseCommand);
        return 1;
    }

    /*Switch Commands*/


    /*Join*/
    if(strcmp("join", parseCommand) == 0){
        if(joinCommand(myUser,saAddr, saPort) < 0){
            fprintf(stderr, "Join Error");
            free(parseCommand);
            exit(EXIT_FAILURE);
        }
        printf("\n>You have started a session\n");
        inSession = 1;
    }

    /*Leave*/
    else if(strcmp(parseCommand, "leave") == 0){
        if(leaveCommand(myUser, saAddr, saPort) < 0){
            fprintf(stderr, "\n>Leave Error>");
            free(parseCommand);
            return 0;
        }
        terminateLeave();
        inSession = 0;
    }

    else if(strcmp(parseCommand, "find") == 0){
        if(findCommand(command, saAddr, saPort) < 0){
            fprintf(stderr, "\n>Leave Error\n");
            free(parseCommand);
            return 0;
        }
    }

    else if(strcmp(parseCommand, "connect") == 0){
        if(connectCommand(command, saAddr, saPort) <= 0){
            fprintf(stderr, "\n>connect Error\n");
            free(parseCommand);
            return 0;
        }
    }
    else if(strcmp(parseCommand, "message") == 0){
        if(chatCommand(command) <= 0){
            fprintf(stderr, "\n>message Error\n");
            free(parseCommand);
            return 0;
        }
    }
    else if(strcmp(parseCommand, "disconnect") == 0){
        if(disconnectCommand() <= 0){
            fprintf(stderr, "\n>disconnect Error\n");
            free(parseCommand);
            return 0;
        }
    }
    /*Exit*/
    else if(strcmp(parseCommand, "exit") == 0){
        if(exitCommand() <= 0){
            fprintf(stderr, "\n>Exit Error>");
            free(parseCommand);
            return 0;
        }
    }

    else if(strcmp(parseCommand, "lst") == 0){
        if(lst() < 0){
            fprintf(stderr, "\n>lst Error>");
            free(parseCommand);
            return 0;
        }

    }

    free(parseCommand);

    return 1;
}