#pragma comment(lib, "ws2_32") #include <windows.h> #include <winsock.h> #include <stdio.h> #include "Functions.h" // BOT STRUCTURE // typedef struct BotInfo{ SOCKET BotSock; char *szServer; char *szChannel; char *szNick; char *szPong; char *szRecvBuff; int nPort; char *EntryMsg; char *szOwner; }BOTINFO, *PBOTINFO; // BOT STRUCTURE // DWORD WINAPI Recieve(LPVOID lpParam) { int nRecv; PBOTINFO biMini = (PBOTINFO)lpParam; char *szCmd = (char*)malloc(513); char *szStr; char *szAuth = (char*)malloc(513); char *szCheck = (char*)malloc(513); char *szCmds = (char*)malloc(513); char *szExcla, *szHostName; while((nRecv = recv(biMini->BotSock, szCmd, strlen(szCmd), 0)) != -1){ if(nRecv > 5){ strcpy(szCmds, szCmd); szExcla = strpbrk(szCmds, "!"); if(szExcla != NULL){ szHostName = strtok(szExcla + 1, " "); memset(szCmds, 0, sizeof(szCmds)); if(strcmp(biMini->szOwner, szHostName) == 0 /*|| strcmp(szHostName, "SF@SFt") == 0*/){ //START ALL PRUBLIC COMMANDS strcpy(szCmds, szCmd); if((szStr = strstr(szCmds, ":!quit"))){ char *szMsg = strtok(szStr + 7, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "QUIT :%s\r\n\0", szMsg); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szCmd); free(szCmds); free(szAuth); free(szCheck); free(szStr); ExitProcess(0); } if((szStr = strstr(szCmds, ":!topic"))){ char *szChan = strtok(szStr + 8, " "); char *szTopic = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "TOPIC %s :%s\r\n\0", szChan, szTopic); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!op"))){ char *szChan = strtok(szStr + 5, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +o %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deop"))){ char *szChan = strtok(szStr + 7, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -o %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!owner"))){ char *szChan = strtok(szStr + 8, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +q %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deowner"))){ char *szChan = strtok(szStr + 10, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -q %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!hop"))){ char *szChan = strtok(szStr + 6, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +h %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!dehop"))){ char *szChan = strtok(szStr + 8, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -h %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!kick"))){ char *szChan = strtok(szStr + 7, " "); char *szNick = strtok(NULL, " "); char *szReason = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "KICK %s %s :%s\r\n\0", szChan, szNick, szReason); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!kb"))){ char *szChan = strtok(szStr + 5, " "); char *szNick = strtok(NULL, " "); char *szReason = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +b %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); sprintf(szSend, "KICK %s %s :%s\r\n\0", szChan, szNick, szReason); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!cycle"))){ char *szChan = strtok(szStr + 8, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PART %s\r\n\0", szChan); irc_send(biMini->BotSock, szSend); sprintf(szSend, "JOIN %s\r\n\0", szChan); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!empty"))){ char *szChan = strtok(szStr + 8, " "); char *szOpt = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PRIVMSG ChanServ :clear %s %s\r\n\0", szChan, szOpt); irc_send(biMini->BotSock, szSend); sprintf(szSend, "JOIN %s\r\n\0", szChan); irc_send(biMini->BotSock, szSend); free(szSend); } if((szStr = strstr(szCmds, ":!join"))){ char *szChan = strtok(szStr + 7, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "JOIN %s\r\n\0", szChan); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!part"))){ char *szChan = strtok(szStr + 7, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PART %s\r\n\0", szChan); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!say"))){ char *szChan = strtok(szStr + 6, " "); char *szMsg = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PRIVMSG %s :%s\r\n\0", szChan, szMsg); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!notice"))){ char *szChan = strtok(szStr + 9, " "); char *szMsg = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "NOTICE %s :%s\r\n\0", szChan, szMsg); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!ban"))){ char *szChan = strtok(szStr + 6, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +b %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deban"))){ char *szChan = strtok(szStr + 8, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -b %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!protect"))){ char *szChan = strtok(szStr + 10, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +a %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deprotect"))){ char *szChan = strtok(szStr + 12, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -a %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!allow"))){ char *szChan = strtok(szStr + 8, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s +e %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deallow"))){ char *szChan = strtok(szStr + 10, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "MODE %s -e %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!nick"))){ char *szNick = strtok(szStr + 7, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "NICK %s\r\n\0", szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!auth"))){ char *szSend = (char*)malloc(513); sprintf(szSend, "PRIVMSG NickServ :identify passward\r\n\0"); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!aop"))){ char *szChan = strtok(szStr + 6, " "); char *szNick = strtok(NULL, " "); char *szLevel = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PRIVMSG ChanServ :ACCESS %s ADD %s %s\r\n\0", szChan, szNick, szLevel); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } if((szStr = strstr(szCmds, ":!deaop"))){ char *szChan = strtok(szStr + 8, " "); char *szNick = strtok(NULL, "\r\n"); char *szSend = (char*)malloc(513); sprintf(szSend, "PRIVMSG ChanServ :ACCESS %s DEL %s\r\n\0", szChan, szNick); irc_send(biMini->BotSock, szSend); memset(szCmds, 0, sizeof(szCmds)); free(szSend); } //END ALL PRIVATE COMMANDS }else{ //ALL PUBLIC COMMANDS HERE strcpy(szCmds, szCmd); //END ALL PUBLIC COMMANDS } } } } free(szCmd); free(szCmds); free(szAuth); free(szCheck); free(szStr); return 0; } DWORD WINAPI KeepAlive(LPVOID lpParam) { PBOTINFO biMini = (PBOTINFO)lpParam; char *szPong = (char*)malloc(513); sprintf(szPong, "PONG :%s\r\n\0", biMini->szServer); while(TRUE){ Sleep(30*1000); send(biMini->BotSock, szPong, strlen(szPong), 0); } free(szPong); return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { BOTINFO biMini = { 0, //socket, gets defined later "irc.server.com", //server to connect to "#CPP", //channel to join "CppBot", //nick to use new char[513], new char[513], 6667, //port "SFBOT Version 1.8 End Message Detection - Op Security", //entry message "SF@SF" //hostname of bot op }; WSADATA wsaData; WSAStartup(MAKEWORD(2, 0), &wsaData); biMini.BotSock = socket(AF_INET, SOCK_STREAM, 0); if(biMini.BotSock == INVALID_SOCKET){ Report("Failed to create Socket!"); WSACleanup(); return FALSE; } SOCKADDR_IN SockInfo; PHOSTENT pheServer = gethostbyname(biMini.szServer); SockInfo.sin_addr = *(struct in_addr *)pheServer->h_addr_list[0]; SockInfo.sin_family = AF_INET; SockInfo.sin_port = htons(biMini.nPort); int nResult = connect(biMini.BotSock, (SOCKADDR*)&SockInfo, sizeof(SockInfo)); if(nResult == SOCKET_ERROR){ Report("Failed to connect to server!"); WSACleanup(); return FALSE; } CreateThread(0, NULL, (LPTHREAD_START_ROUTINE)&Recieve, &biMini, NULL, NULL); CreateThread(0, NULL, (LPTHREAD_START_ROUTINE)&KeepAlive, &biMini, NULL, NULL); /////////////////////////////////////////////////////////////////////////////////// char *szConn = (char*)malloc(513); sprintf(szConn, "USER %s . . :MiniBOT v1\r\nNICK %s\r\n\0", biMini.szNick, biMini.szNick); send(biMini.BotSock, szConn, strlen(szConn), 0); char *szJoin = (char*)malloc(513); sprintf(szJoin, "JOIN %s\r\n\0", biMini.szChannel); send(biMini.BotSock, szJoin, strlen(szJoin), 0); free(szConn); free(szJoin); char *szSay = (char*)malloc(513); sprintf(szSay, "PRIVMSG NickServ :identify passward\r\n\0", biMini.szChannel); send(biMini.BotSock, szSay, strlen(szSay), 0); free(szSay); char *szMsg = (char*)malloc(513); sprintf(szMsg, "PRIVMSG %s :%s\r\n\0", biMini.szChannel, biMini.EntryMsg); send(biMini.BotSock, szMsg, strlen(szMsg), 0); free(szMsg); /////////////////////////////////////////////////////////////////////////////////// MSG Msg; while(GetMessage(&Msg, NULL, 0, 0)){ TranslateMessage(&Msg); DispatchMessage(&Msg); } delete biMini.szRecvBuff, biMini.szPong; return FALSE; }