// As GetLine, but trim leading and trailing blanks; skip empty lines bool TextFile::GetTrimLine(char szLine[], unsigned uBytes) { for (;;) { bool bEOF = GetLine(szLine, uBytes); if (bEOF) return true; TrimBlanks(szLine); if (0 != szLine[0]) break; } return false; }
int main(int argc, char *argv[]) { if(argc != 4) { printf("Please run bot as such:\nDnDRollBot Server IP Nick\n\n"); return(1); } SDLNet_SocketSet set; IPaddress ip; TCPsocket TCPsock; char *inbuffer = new char[1024], *buffer = new char[1024], *words[256] = {0}, *channels[16] = {0}; char temp = '\0', *ptmp = 0; int inbufferpos = 0, x = 0, wordc = 0, chanc = 0, nd = 0, vd = 0; bool Done = false, LineReady = false, DoneParsing = false; srand((unsigned int)time(NULL)); if(SDL_Init(0) == -1) { printf("SDL_Init: %s\n", SDL_GetError()); CLEANUP(); return(2); } if(SDLNet_Init() == -1) { printf("SDLNet_Init: %s\n", SDLNet_GetError()); CLEANUP(); return(3); } set = SDLNet_AllocSocketSet(1); if(!set) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); CLEANUP(); return(4); } printf("SDL and SDL_net initiated successfully, resolving server...\n"); if(SDLNet_ResolveHost(&ip, argv[1], atoi(argv[2])) == -1) { printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); CLEANUP(); return(5); } printf("Host resolved, attempting to connect...\n"); TCPsock = SDLNet_TCP_Open(&ip); if(!TCPsock) { printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); CLEANUP(); return(6); } printf("Connected to host, handshaking...\n"); sprintf(buffer, "NICK %s\n", argv[3]); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); sprintf(buffer, "USER DnD 0 DnD :DnDRollBot\n"); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); while(!Done) { while(!LineReady) { if(SDLNet_TCP_Recv(TCPsock, &temp, 1) <= 0) { printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); SDLNet_TCP_Close(TCPsock); CLEANUP(); return(0); } inbuffer[inbufferpos] = temp; if(inbuffer[inbufferpos] == '\n') { LineReady = true; inbuffer[++inbufferpos] = '\0'; } else { inbufferpos++; } if(inbufferpos >= 1020) { inbuffer[inbufferpos] = '\n'; inbuffer[++inbufferpos] = '\0'; LineReady = true; } } TrimBlanks(inbuffer); wordc = 0; printf("%s", inbuffer); //thanks again Hero, this is damn clever words[wordc++] = inbuffer; while(inbuffer[x] != '\0') { if(inbuffer[x] == ' ') { inbuffer[x] = '\0'; words[wordc++] = &inbuffer[++x]; continue; } x++; } //Do some fun parsing if(!strcasecmp(words[0], "PING")) { sprintf(buffer, "PONG %s", words[1]); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); } if(!strcasecmp(words[1], "PRIVMSG")) { if(!strcasecmp(words[3] + 1, "!JOIN")) { ptmp = GrabAfter(words[4], wordc - 4); sprintf(buffer, "JOIN %s\n", ptmp); delete [] ptmp; ptmp = 0; SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); } if(!strcasecmp(words[3] + 1, "!PART")) { ptmp = GrabAfter(words[4], wordc - 4); sprintf(buffer, "PART %s :%s\n", words[4], ptmp); delete [] ptmp; ptmp = 0; SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); } if(!strcasecmp(words[3] + 1, "!QUIT")) { ptmp = GrabAfter(words[4], wordc - 4); sprintf(buffer, "QUIT :%s\n", ptmp); delete [] ptmp; ptmp = 0; SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); Done = true; } if(!strcasecmp(words[3] + 1, "\001ACTION") && !strcasecmp(words[4], "slaps") && !strcasecmp(words[5], argv[3])) { strcpy(buffer, "QUIT :Ouch!\n"); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); Done = true; } if(!strcasecmp(words[3] + 1, "!ROLL")) { ptmp = strchr(words[4], 'd'); if(!ptmp || !atoi(words[4])) { sprintf(buffer, "PRIVMSG %s :#d# is proper format!\n", words[2]); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); for(x = 0; x <= wordc; x++) { words[x] = 0; } LineReady = DoneParsing = false; wordc = x = inbufferpos = 0; continue; } *ptmp = '\0'; ptmp++; nd = atoi(words[4]); vd = atoi(ptmp); if(nd == 0 || vd == 0) { sprintf(buffer, "PRIVMSG %s :#d# is proper format!\n", words[2]); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); for(x = 0; x <= wordc; x++) { words[x] = 0; } LineReady = DoneParsing = false; wordc = x = inbufferpos = 0; continue; } ptmp--; *ptmp = 'd'; ptmp = 0; sprintf(buffer, "PRIVMSG %s :Rolled a %d!\n", words[2], Roll(nd, vd)); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); } } for(x = 0; x <= wordc; x++) { words[x] = 0; } LineReady = DoneParsing = false; wordc = x = inbufferpos = 0; } sprintf(buffer, "QUIT\n"); SDLNet_TCP_Send(TCPsock, buffer, strlen(buffer)); SDLNet_TCP_Close(TCPsock); CLEANUP(); return(0); }