Need IRC (internet relay chat) C help.

Discussion in 'C' started by Jurassic_Park, Dec 26, 2008.

  1. Jurassic_Park

    Jurassic_Park New Member

    Joined:
    Dec 26, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    I have the following problem:

    For the last years I have been using a simple C based program to help me keep channels in my friends IRCnet. It is a totally client based program, has not much anything related to the IRCD. I execute it, it runs in the back and it keeps ops in channels and tells jokes and stuff like that. I have had this following problem for many months now but now im searching for a way to fix it.

    One of those ircserver programs(ircd) crashes from time to time on these servers and then my channel bot needs to connect to another server. Problem is that it doesnt know how to connect.

    I didnt code this channelbot originally but in the source i have the following:

    server1: IP <- works.
    server2: IP <- works only if server1 address cannot be resolved. For example if the server1 ip is 100.100.100.100, well it cannot resolve it and after 15 seconds, it connects to server2.

    Well this doesnt apply in my case because the IP of the IRCD is resolvable. And many times the computer is up there. Just the IRCD crashes. But when the ircd crashes on server1, my bot keeps looping and looping and it tries to connect to server1 for forever.

    Sometimes the server is coming back up but its totally laggy and not connected to other servers so thats why its best if it could just go straight to server2.
    So im asking for some help if someone who knows this irc stuff could help me add some lines to my channelbot, so that for example:

    If bot has not joined channel1 in server1 after 5 minutes of trying, then go to server2 and channel 2 (which is the same channel).

    Here is a part of the code that I believe is responsible for the channelbot behaviour when it connects to IRC.

    Code:
        // copy settings into main irc structure
        strncpy(mainirc.host, server, sizeof(mainirc.host)-1);
        mainirc.port = port;
        strncpy(mainirc.channel, channel, sizeof(mainirc.channel)-1);
        strncpy(mainirc.chanpass, chanpass, sizeof(mainirc.chanpass)-1);
        mainirc.spy = 0;
    
        while (1) {
            for (i = 0; i < 6; i++) {
                #ifndef NO_CHECKCONNECTION
                // check if we're connected to the internet... if not, then wait 5mins and try again
                if (!noigcs) if (fInternetGetConnectedState(&cstat, 0) == FALSE) {
                    Sleep(30000);
                    continue;
                }
                #endif
    
                err = irc_connect((void *)&mainirc);
                success = FALSE;
                if (err == 2) break; // break out of the loop
    
                if (success) i--; // if we're successful in connecting, decrease i by 1;
    
                // irc_connect didn't return 2, so we need to sleep then reconnect
                Sleep(3000);
            }
    
            if (err == 2) break; // break out of the loop and close
    
            if (bkpserver) {
                strncpy(mainirc.host, server, sizeof(mainirc.host)-1);
                mainirc.port = port;
                strncpy(mainirc.channel, channel, sizeof(mainirc.channel)-1);
                strncpy(mainirc.chanpass, chanpass, sizeof(mainirc.chanpass)-1);
                bkpserver = FALSE;
            }
            else if (!bkpserver && server2[0] != '\0') {
                strncpy(mainirc.host, server2, sizeof(mainirc.host)-1);
                mainirc.port = port2;
                strncpy(mainirc.channel, channel2, sizeof(mainirc.channel)-1);
                strncpy(mainirc.chanpass, chanpass2, sizeof(mainirc.chanpass)-1);
                bkpserver = TRUE;
            }
        }
    
        // cleanup;
        for (i = 0; i < 64; i++) closesocket(csock[i]);
        WSACleanup();
    
        return 0;
     }
    

    In the configuration area I have:

    Code:
     const char server[19] = "ircserver1.isp.com"; 
     const  int port = 6667;
    
     const char server2[19] = "ircserver2.isp.com"; 
     const  int port2 = 6667;
    
    and in one other area i have also a mentioning about bkpserver
    Code:
        BOOL bkpserver = FALSE;

    I have tested many times the scenarios where it connects to server2. It goes to server2 only when server1 address cannot be resolved. If I put 127.0.0.1 it loops it also forever even if there is nothing there.

    IF someone can help me even a little bit, then thank you very much!
     
  2. Jurassic_Park

    Jurassic_Park New Member

    Joined:
    Dec 26, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    ok here is an update:
    i found an old fix on my hard drive that i never implemented on this joke bot. i had used it on other program but i just tried it on this and it works.

    i replaced
    Code:
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    		csock[irc.threadnum] = sock;
    		err = connect(sock, (LPSOCKADDR)&ssin, sizeof(SOCKADDR_IN));
    		if (err == SOCKET_ERROR) {
    			closesocket(sock);
    			Sleep(2000);
    			continue;
    with

    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    csock[irc.threadnum] = sock;
    err = connect(sock, (LPSOCKADDR)&ssin, sizeof(SOCKADDR_IN));
    if (err == SOCKET_ERROR) {
    closesocket(sock);
    rval = 0;
    break;








    Now it connects to server2 when server1 is 1.1.1.1 or 127.0.0.1.

    So now it connects to server2 if server1 is unresolvable and the new thing: if its not an irc server.

    But it would be great if there could still be a fix for the bot on how to go server2 if server1 channel1 is unjoinable.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice