// IRCBot.cpp // Requires libwinsock32.a (feed -lwinsock32 to linker) #include #include #include #include #include #include #include "IRCBot.h" // Program defines #define VERSION "0.1.0" #define ENVIRONMENT "Win32(XP)" // Bot info defines #define NICK "Bananabot" #define USERNAME "Bananabot" #define REALNAME "USERNAME VERSION ENVIRONMENT" int main(int argc, char **argv){ SOCKET sClient; if(argc < 6 || argc > 8){ std::cout << "Usage: ircbot server port channel masterhost masternick [nickname, [username]]" << std::endl; return 2; } char* mastHost = new char[strlen(argv[4])+1]; char* mastNick = new char[strlen(argv[5])+1]; strcpy(mastHost, argv[4]); strcpy(mastNick, argv[5]); if(connect(sClient, (char*)argv[1], (int)atoi(argv[2])) != NO_ERROR){ closesocket(sClient); WSACleanup(); return 1; } // Now we can send/recv data! debug("Connected to server!"); nick(sClient, (char*)(argc>6 ? argv[6] : NICK)); user(sClient, (char*)(argc==8 ? argv[7] : USERNAME), REALNAME); // Receive loop int iBytes = SOCKET_ERROR; char* pszData = new char[562]; while(true){ iBytes = recv(sClient, pszData, 1024, "\r\n"); // Error check if(iBytes == SOCKET_ERROR){ error("Connection failed."); closesocket(sClient); delete[] pszData; WSACleanup(); return 1; } if (iBytes == 0 || iBytes == WSAECONNRESET) { error("Disconnected!"); delete[] pszData; closesocket(sClient); WSACleanup(); return 1; } // Set nullbytes pszData[iBytes-2] = '\0'; info(pszData, true); // Parse message IRCMessage *message; message = parseIRCData(pszData); // Initial join if(strcmp(message->command, "376") == 0) join(sClient, argv[3]); // Answer ping char* tmp = new char[5]; strncpy(tmp, pszData, 4); tmp[4] = '\0'; if(strcmp(tmp, "PING") == 0) pong(sClient, message); delete[] tmp; // Answer versions if(strcmp(message->text, "\001VERSION\001") == 0){ tmp = new char[510]; sprintf(tmp, "\001VERSION Asuka:%s:%s\001", VERSION, ENVIRONMENT); msg(sClient, message->sender->username, tmp); delete[] tmp; } /******************************************* This is where all the stuff will be done. *******************************************/ // Notices for unseen control if(strcmp(message->command, "NOTICE") == 0){ tmp = new char[512]; char *tmp2, *tmp3; strcpy(tmp, message->text); tmp2 = strtok(tmp, " "); tmp = strtok(NULL, "\0"); if(strccmp(tmp2, "SAY") == 0){ tmp3 = strtok(tmp, " "); tmp = strtok(NULL, "\0"); msg(sClient, tmp3, tmp); delete[] tmp3; } if(strcmp(tmp2, "JOIN") == 0) join(sClient, tmp); if(strcmp(tmp2, "LEAVE") == 0) leave(sClient, tmp); if(strcmp(tmp2, "QUIT") == 0 && strcmp(message->sender->hostname, mastHost) == 0 && strcmp(message->sender->username, mastNick) == 0){ tmp3 = new char[512]; sprintf(tmp3, "QUIT :%s", tmp); send(sClient, tmp3); delete[] tmp3; } delete[] tmp2; delete[] tmp; } // Do more stuff below. /******************************************* End of where all the stuff will be done. *******************************************/ // Sleep to preserve CPU if(iBytes < 1){ Sleep(300); continue; } delete[] pszData; pszData = new char[511]; Sleep(100); } delete[] pszData; closesocket(sClient); WSACleanup(); return 0; }