Overwriting problem in IRC game bot

This is a discussion on Overwriting problem in IRC game bot within the C++ Programming forums, part of the General Programming Boards category; Hey, so I'm writing a bot for IRC that plays blackjack. The first command is ?join which adds the player ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    Overwriting problem in IRC game bot

    Hey, so I'm writing a bot for IRC that plays blackjack. The first command is ?join which adds the player to the game. The problem is that, after (player 1) joins the game and (player 2) tries to join, the program thinks (player 2) is already in the game. When I looked into this, I found that the program is putting (player 2)'s name in (player 1)'s spot without running any of the code. I'm not sure if this is a pointer problem or if I messed up somewhere, but I'd really appreciate any help! Here's the code, stripped down to just the ?bot program and the ?join command.

    Code:
    #include <winsock.h>
    #include <cstdlib>
    #include <cstdio>
    #include <iostream>
    #include <vector>
    
    #define SERVER "irc.SERVERNAMEHERE.com"
    #define PORT 6667
    #define TIMEOUT 10
    #define NICK "NICKHERE"
    #define CHAN "#CHANNELHERE"
    
    using namespace std;
    
    
    vector<char*> playerName;
    
    // UNCHANGED BOT PROGRAM
    typedef struct {
        char *cUserNick;                 // Nickname of user (eg: Tux)
        char *cUserHost;                 // Userhost of user (eg: lenience.com)
        char *cUserName;                 // Username of user (eg: root)
        char *cTarget;                   // Pointer to channel or private chat nick...
        char *cMessage;                  // Message sent by user
    } Message_Struct;
    
    bool arrayShift(char *cBuffer);
    int socketConnect(SOCKET *connection, char *HName, int Port);
    int sockPRINT(SOCKET *strSocket, char* cMessage, ...);
    int parseARGS(char **args, char *line);
    int parseMessage(SOCKET *strSocket, Message_Struct *sMessage);
    
    int main(int argc, char *argv[]) {
        char cBuffer[512];
        char cOutBuffer[512];
        char *lineSPLIT[512];
        char *args[10];
        int bRecv;
        SOCKET strSocket;
        Message_Struct sMessage;
    
        int tries;
        for(tries=1; socketConnect(&strSocket, SERVER, PORT)!=0; tries++) {
            printf("Failed attempt: %d\r\n",tries);
            Sleep(TIMEOUT*1000);
        }
    
        sockPRINT(&strSocket, "USER %s \"\" \"127.0.0.1\" :%s\r\n", NICK, NICK);
        sockPRINT(&strSocket, "NICK %s\r\n", NICK);
        Sleep(100);
    
        sockPRINT(&strSocket, "JOIN %s\r\n", CHAN);
        printf("Success!\n");
    
        while(1) {
            memset(cBuffer, 0, 512);
            memset(cOutBuffer, 0, 512);
    
            bRecv = recv(strSocket, cBuffer, 512, 0);
            if( (bRecv == 0) || (bRecv == SOCKET_ERROR) ) break;
    
            if(cBuffer[0]!=':') {
                strcpy(cOutBuffer, "PONG :");
                sockPRINT(&strSocket, cOutBuffer);
            } else {
                sMessage.cUserNick = strtok(cBuffer, "!");
                arrayShift(sMessage.cUserNick);
                sMessage.cUserName = strtok(NULL, "@");
                sMessage.cUserHost = strtok(NULL, " "); //THE SENDER IP
                strtok(NULL, " "); //THE PRIVMSG
                sMessage.cTarget = strtok(NULL, " "); //RECEIVED IN CHAT ON CHANNEL / NICK
                sMessage.cMessage = strtok(NULL, "\r\n"); //THE MESSAGE WITH A :
    
                parseMessage(&strSocket, &sMessage);
            }  
        }
        closesocket(strSocket);
        return (1);
    }
    
    bool arrayShift(char *cBuffer) {
        for(int i=0; i<strlen(cBuffer); i++) cBuffer[i]=cBuffer[i+1];
        cBuffer[strlen(cBuffer)]='\0';
        return (1);
    }
    
    int socketConnect(SOCKET *connection, char *HName, int Port) {
        SOCKADDR_IN serverInfo;
        WSADATA wsaData;
        LPHOSTENT hostEntry;
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) == -1) return (-1);
        if (!(hostEntry = gethostbyname(HName))) {
            WSACleanup();
            return (-1);
        }
        if ((*connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
            WSACleanup();
            return (-1);
        }
        serverInfo.sin_family = AF_INET;
        serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
        serverInfo.sin_port = htons(Port);
        if (connect(*connection, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)) == SOCKET_ERROR) {
            WSACleanup();
            return (-1);
        }
        return (0);
    }
    
    int sockPRINT(SOCKET *strSocket, char* cMessage, ...) {
        char cBuffer[512];
        int iError;
        va_list va;
        va_start(va, cMessage);
        vsprintf(cBuffer, cMessage, va);
        va_end(va);
        send(*strSocket, cBuffer, strlen(cBuffer), 0);
        return 1;
    }
    
    int parseARGS(char **args, char *line){
    int tmp=0;
    args[tmp] = strtok( line, "|" );
    while ( (args[++tmp] = strtok(NULL, "|" ) ) != NULL );
    return tmp - 1;
    }
    //END UNCHANGED BOT PROGRAM
    
    int parseMessage(SOCKET *strSocket, Message_Struct *sMessage) {
        char *argv[512];
        int argc = parseARGS(argv, sMessage->cMessage);
        
        //JOIN ROUTINE
        else if (argv[0]!= NULL && !strcmp(argv[0], ":?join")) {
             
             int i;
             bool joinError = FALSE;
             
             for (i=0; i<playerName.size(); ++i) {
                 if (playerName[i] == sMessage->cUserNick) joinError = TRUE; //  checks to see if player is already in game
             }
             
             if (joinError == FALSE) {
                 sockPRINT(strSocket, "\r\nPRIVMSG %s :%c4,0%s has joined the table!\r\n", sMessage->cTarget, 3, sMessage->cUserNick);
                 playerName.push_back(sMessage->cUserNick);
             } else sockPRINT(strSocket, "\r\nPRIVMSG %s :%c4,0%s has already joined the table!.\r\n", sMessage->cTarget, 3, sMessage->cUserNick);
        }
        return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Code:
    if (playerName[i] == sMessage->cUserNick)
    Probably want this to be a string compare.

    Also if you are using C++ there is much code you could clean up. But anyways.
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Right, I figured that and tried it also. I cleaned up the code a bit, but I don't know how much more I can change since I'm using Winsocks. I changed the == to strcmp() and still have the same problem. Here's what I have now:
    Code:
    #include <winsock.h>
    #include <cstdlib>
    #include <cstdio>
    #include <iostream>
    #include <vector>
    
    #define SERVER "irc.SERVERNAMEHERE.com"
    #define PORT 6667
    #define TIMEOUT 10
    #define NICK "NICKHERE"
    #define CHAN "#CHANNELHERE"
    
    using namespace std;
    
    
    vector<char*> playerName;
    
    
    char *cUserNick; // Nickname of user (eg: Tux)
    char *cUserHost; // Userhost of user (eg: lenience.com)
    char *cUserName; // Username of user (eg: root)
    char *cTarget;   // Pointer to channel or private chat nick...
    char *cMessage;  // Message sent by user
    
    // declarations of functions
    bool arrayShift(char *cBuffer);
    int socketConnect(SOCKET *connection, char *HName, int Port);
    int sockPRINT(SOCKET *strSocket, char* cMessage, ...);
    int parseARGS(char **args, char *line);
    int parseMessage(SOCKET *strSocket, char *cUserNick, char *cTarget, char *cMessage);
    
    // BEGIN MAIN
    int main(int argc, char *argv[]) {
        char cBuffer[512];
        char cOutBuffer[512];
        char *lineSPLIT[512];
        char *args[10];
        int bRecv;
        SOCKET strSocket;
    
        int tries; // attempts to connect to server
        for(tries=1; socketConnect(&strSocket, SERVER, PORT)!=0; tries++) {
            printf("Failed attempt: %d\r\n",tries);
            Sleep(TIMEOUT*1000);
        }
        
        // log in
        sockPRINT(&strSocket, "USER %s \"\" \"127.0.0.1\" :%s\r\n", NICK, NICK);
        sockPRINT(&strSocket, "NICK %s\r\n", NICK);
        Sleep(100);
        sockPRINT(&strSocket, "JOIN %s\r\n", CHAN);
        printf("Success!\n");
        
        // main bot code
        while(1) {
            memset(cBuffer, 0, 512);
            memset(cOutBuffer, 0, 512);
    
            bRecv = recv(strSocket, cBuffer, 512, 0);
            if( (bRecv == 0) || (bRecv == SOCKET_ERROR) ) break; // if there's a socket problem, quit
    
            if(cBuffer[0]!=':') { // PING response
                strcpy(cOutBuffer, "PONG :");
                sockPRINT(&strSocket, cOutBuffer);
            } 
            
            else { // await commands
                cUserNick = strtok(cBuffer, "!");// stores sender nick
                arrayShift(cUserNick);// shifts sender nick
                cUserName = strtok(NULL, "@");// stores sender Name
                cUserHost = strtok(NULL, " "); //stores sender IP
                strtok(NULL, " "); //ignores PRIVMSG
                cTarget = strtok(NULL, " "); //stores channel incoming location
                cMessage = strtok(NULL, "\r\n"); //stores message with an ':'
                
                cout << cUserNick << endl;
                if (playerName.size() != 0) cout << playerName[0] << endl;
                                  
                parseMessage(&strSocket, cUserNick, cTarget, cMessage);
                
                cout << cUserNick << endl;
                if (playerName.size() != 0) cout << playerName[0] << endl;
                
            }// end main bot code
             
        }
        closesocket(strSocket);
        return (1);
    } // END MAIN
    
    //shifts array
    bool arrayShift(char *cBuffer) {
        for(int i=0; i<strlen(cBuffer); i++) cBuffer[i]=cBuffer[i+1];
        cBuffer[strlen(cBuffer)]='\0';
        return (1);
    }
    
    // connects to server
    int socketConnect(SOCKET *connection, char *HName, int Port) {
        SOCKADDR_IN serverInfo;
        WSADATA wsaData;
        LPHOSTENT hostEntry;
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) == -1) return (-1);
        if (!(hostEntry = gethostbyname(HName))) {
            WSACleanup();
            return (-1);
        }
        if ((*connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
            WSACleanup();
            return (-1);
        }
        serverInfo.sin_family = AF_INET;
        serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
        serverInfo.sin_port = htons(Port);
        if (connect(*connection, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)) == SOCKET_ERROR) {
            WSACleanup();
            return (-1);
        }
        return (0);
    }
    
    //say something
    int sockPRINT(SOCKET *strSocket, char* cMessage, ...) {
        char cBuffer[512];
        int iError;
        va_list va;
        va_start(va, cMessage);
        vsprintf(cBuffer, cMessage, va);
        va_end(va);
        send(*strSocket, cBuffer, strlen(cBuffer), 0);
        return 1;
    }
    
    //do something
    int parseARGS(char **args, char *line) {
    int tmp=0;
    args[tmp] = strtok( line, "|" );
    while ( (args[++tmp] = strtok(NULL, "|" ) ) != NULL );
    return tmp - 1;
    }
    
    int parseMessage(SOCKET *strSocket, char *cUserNick, char *cTarget, char *cMessage) {
        char *argv[512];
        int argc = parseARGS(argv, cMessage);
    
    //JOIN ROUTINE
        if (argv[0]!= NULL && !strcmp(argv[0], ":?join")) {
             
             int i;
             bool joinError = FALSE;
             
             for (i=0; i<playerName.size(); ++i) {
                 if ( strcmp(playerName[i], cUserNick) == 0 ) joinError = TRUE; // check if player already joined game
             }
             
             if (joinError == FALSE) {
                 sockPRINT(strSocket, "\r\nPRIVMSG %s :%c4,0%s has joined the table!\r\n", cTarget, 3, cUserNick);
                 playerName.push_back(cUserNick);
                 playerBank.push_back(0);
             } else sockPRINT(strSocket, "\r\nPRIVMSG %s :%c4,0%s has already joined the table!.\r\n", cTarget, 3, cUserNick);
        }
        else return 0;
        
        return 0;
    Any other suggestion to fix the problem, or to clean up the code? Thanks!

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    I found the problem. I was copying a char* to another char*, so it was copying the pointer to another pointer. Therefore the pointer changed to that players name when he tried to join.

    XD

    fix by making playerName vector a string vector instead of a char*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Event driven game engine
    By abachler in forum Game Programming
    Replies: 9
    Last Post: 12-01-2009, 05:03 AM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  3. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  4. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21