MrC0de.cpp IRC BOT

Mrc0de compiles Under MSVC++ 2005. Easily Ported To GCC.

MySocket.h Defines the MySocket Class. A Simple Class with a few simple function members.
An Instance of MySocket is used as a member of IrcBot Class defined in irc_bot.h which is the type
of object created in MrC0de.cpp:

MrC0de.cpp

// MrC0de.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <winsock.h> #include <iostream> #include <string> using namespace std; #include <algorithm> #include "MrC0de.h" int _tmain(int argc, _TCHAR* argv[]) { //SETUP WSA WSADATA wsaData; // if this doesn't work //WSAData wsaData; // then try this instead if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } //WSA SETUP DONE //Make An IrcBot And Start It. IrcBot MrC0de("irc.neuroticnetworks.com",6667,"MrC0de","b0tb0t"); if (MrC0de.Start() != -1) { //Main Loop while(!MrC0de.Die) { MrC0de.ProcessIrc(); } } else { cout<<"Cannot Start() MrC0de"<<endl; } WSACleanup(); //Clean Your Socks return 0; }

MrC0de.h

#define DEFAULT_CHANNEL "JOIN #neuroticnetworks\n" #include "MySocket.h" #include "irc_bot.h"

MySocket.h

//MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket //MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket //MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket //MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket class MySocket { public: bool CONNECTED; bool debugmode; int Port; string Hostname; int packetsize; int sockfd, numbytes; char buf[4096]; char sendbuf[1024]; MySocket(); int Connect(); void SetHost(string host); void SetPort(int port); int getz(string *getdata); int putz(string putdata); void Disconnect(); string GetIP(string hname); private: struct hostent *he; struct sockaddr_in their_addr; }; int MySocket::putz(string putdata) { strcpy(sendbuf,putdata.c_str()); //cout<<"SENDBUF = " << sendbuf<<endl; if (send(sockfd, sendbuf,putdata.length(), 0) == -1) { closesocket(sockfd); // bye! return -1; } else { return 0; } }; int MySocket::getz(string *getdata){ int fdmax; fd_set master; fd_set read_fds; FD_ZERO(&read_fds); FD_ZERO(&master); FD_SET(sockfd, &master); fdmax = sockfd; int nbytes; read_fds = master; // copy it if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) { perror("select"); return -1; } if (FD_ISSET(sockfd, &read_fds)) { // handle data from a client if ((nbytes = recv(sockfd, buf, sizeof buf, 0)) <= 0) { // got error or connection closed by client if (nbytes == 0) { // connection closed if(debugmode){cout<<":SOCKET> Server Hung up On Socket "<<sockfd<<endl;} } else { if(debugmode){cout<<":SOCKET> Server Connection Error.\n";} perror("recv"); } closesocket(sockfd); // bye! FD_CLR(sockfd, &master); // remove from master set return -1; } else { //finally got some data. buf[nbytes] = '\0'; getdata->assign(buf); } } return 0; }; void MySocket::SetPort(int port) {Port = port;}; void MySocket::SetHost(string host) {Hostname = host;}; string MySocket::GetIP(string hname){ string ipaddress; struct hostent *h; if ((h=gethostbyname(hname.c_str())) == NULL) { // get the host info cout<<":SOCKET> Error Getting Host\n"; } ipaddress = (inet_ntoa(*((struct in_addr *)h->h_addr))); return ipaddress; }; MySocket::MySocket() { CONNECTED = 0; if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } }; int MySocket::Connect() { string ipaddress = GetIP(Hostname); if(debugmode){cout<<":SOCKET> Hostname "<<Hostname.c_str()<<" Resolved To "<<ipaddress<<"\n";} if(debugmode){cout<<":SOCKET> Connecting To "<<ipaddress<<" On Port "<<Port<<endl;} if ((he=gethostbyname(Hostname.c_str())) == NULL) { // get the host info cout<<":SOCKET> Error Getting Host\n"; } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(Port); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero); if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof their_addr) == -1) { perror("connect"); return -1; } else { if(debugmode){cout<<":SOCKET> Connected!\n\n";} CONNECTED = 1; return 0; } };

irc_bot.h

void ConvertToLowerCase(std::string& str) { std::transform(str.begin(),str.end(),str.begin(),tolower); // You may need to cast the above line to (int(*)(int)) tolower } ///IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT ///IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT ///IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT IRCBOT class IrcBot { public: string indata; string Hostname; int Port; char myhost[256]; string MyNick; string MyPass; MySocket IrcSock; IrcBot(string host,int port,string nick, string pass); int Start(); void ProcessIrc(); void Parse(string data); void putz(string data); bool Die; private: string GodMode; string God; bool Connected; }; IrcBot::IrcBot(string host,int port,string nick, string pass) { IrcSock.SetHost(host); IrcSock.SetPort(port); Hostname = host; Port = port; MyNick = nick; MyPass = pass; Connected = 0; Die = 0; }; int IrcBot::Start() { IrcSock.debugmode = 1; if(IrcSock.Connect() != -1) { return 1; } else { return 0; } }; void IrcBot::putz(string data) { if ( IrcSock.putz(data) != -1 ) { //Success } else { //Disconnected cout<<":MRC0DE> OH SHIT LOST CONNECTION"<<endl; while(1){} } }; void IrcBot::ProcessIrc() { if(IrcSock.getz(&indata) != -1) { if(indata.length() > 0) { string line; while(indata.find("\n") != string::npos && indata.length() > 0) { line = indata.substr(0,indata.find("\n")); Parse(line); indata.replace(0,indata.find("\n")+1,""); } } } else { //OH SHIT LOST CONNECTION cout<<":MRC0DE> OH SHIT LOST CONNECTION"<<endl; while(1){} } }; void IrcBot::Parse(string data) { bool noparse = 1; string fulldata = data; string dtag; if (data.find(":") == 0) { data = data.substr(1,data.length()); //Strip Prefix ':' dtag = data.substr(0,data.find(" ")); //capture dtag (usually hostname of server) ConvertToLowerCase(dtag); ConvertToLowerCase(Hostname); if (dtag == Hostname) { data = data.substr(data.find(" ")+1,data.length()); //Strip Up To first WhiteSpace And Save * After //At this point both the : and hostname have been stripped... IF PRESENT. //This shit is tricky cuz some servers send different stuff @ different times in different ways //So we had to find the one pattern that could be matched by all and would miss none of them //If we succeed. The Bot Works Everywhere. If Not, it sinks. } } if ( data.find("NOTICE AUTH :") == 0) { //Wow... Undernet Doesnt Send any host data. Just NOTICE AUTH No Space //Remains of Data //" NOTICE AUTH :*** Looking up your hostname..." if (!Connected) { Connected = 1; string login = "NICK " + MyNick + "\n"; cout<<"<NICK: NICK " << MyNick<<endl; putz(login); gethostname(myhost, (sizeof myhost)); login = "USER " + MyNick + " " + myhost + " " + MyNick + " :Bot Forged @ NeuroticNetworks.com" + "\n"; cout<<"<USER: USER "<<MyNick<<" "<<myhost<<" "<<MyNick<< " :Bot Forged @ NeuroticNetworks.com"<<endl; putz(login); } cout<<":NOTICE_AUTH> "<<data.substr(data.find(":") + 1)<<endl; noparse = 0; } else if (data.find("PING :") == 0) { string pong = data.substr(data.find(":",0)+1,data.length()); cout<<":PING> "<<pong<<endl; cout<<"<PONG: "<<pong<<endl; pong = "PONG " + pong + "\n"; putz(pong); noparse = 0; } else if (data.find("001 ") == 0) { //001 Welcome Line string line001 = data.substr(data.find(":"),data.length()); cout<<":001> "<<line001<<endl; noparse = 0; } else if (data.find("002 ") == 0) { //002 Welcome Line string line002 = data.substr(data.find(":"),data.length()); cout<<":002> "<<line002<<endl; noparse = 0; } else if (data.find("003 ") == 0) { //003 Welcome Line string line003 = data.substr(data.find(":"),data.length()); cout<<":003> "<<line003<<endl; noparse = 0; } else if (data.find("004 ") == 0) { string line004 = data.substr(data.find(MyNick),data.length()); line004 = line004.substr(line004.find(" ") + 1,line004.length()); cout<<":004> "<<line004<<endl; noparse = 0; } else if (data.find("005 ") == 0) { string line005 = data.substr(data.find(MyNick),data.length()); line005 = line005.substr(line005.find(" ") + 1,line005.length()); cout<<":005> "<<line005<<endl; noparse = 0; } else if (data.find("251 ") == 0) { string line251 = data.substr(data.find(":"),data.length()); cout<<":251> "<<line251<<endl; noparse = 0; } else if (data.find("252 ") == 0) { string line252 = data.substr(data.find(MyNick),data.length()); line252 = line252.substr(line252.find(" ") + 1,line252.length()); cout<<":252> "<<line252<<endl; noparse = 0; } else if (data.find("254 ") == 0) { string line254 = data.substr(data.find(MyNick),data.length()); line254 = line254.substr(line254.find(" ") + 1,line254.length()); cout<<":254> "<<line254<<endl; noparse = 0; } else if (data.find("255 ") == 0) { string line255 = data.substr(data.find(":"),data.length()); cout<<":255> "<<line255<<endl; noparse = 0; } else if (data.find("265 ") == 0) { string line265 = data.substr(data.find(":"),data.length()); cout<<":265> "<<line265<<endl; noparse = 0; } else if (data.find("266 ") == 0) { string line266 = data.substr(data.find(":"),data.length()); cout<<":266> "<<line266<<endl; noparse = 0; } else if (data.find("422 ") == 0) { string line422 = data.substr(data.find(":"),data.length()); cout<<":422> "<<line422<<endl; putz(DEFAULT_CHANNEL); noparse = 0; } else if (data.find((MyNick + " ")) == 0) { //I did something Mode Like string action = data.substr(data.find(" ")+1,data.length()); action = (action.substr(0,action.find(" "))); //cout<<"ACTION:"<<action<<endl; if (action == "MODE") { //I set a mode. data=(data.substr((data.find(action) + 5))); cout<<":"<<MyNick<<">"<<" I Set Mode On "<<(data.substr(0,data.find(" ")))<<" "<<(data.substr(data.find(":")+1))<<endl; noparse = 0; } } else if (data.find((MyNick + "!")) == 0) { //Stuff like JOINs have a mask included data = (data.substr((data.find(" ")+1))); //strip it cuz I already know its me if (data.find("JOIN ") == 0) { //I joined a channel cout<<":"<<MyNick<<">"<<" I Have Joined "<<(data.substr((data.find(":")+1)))<<endl; } noparse=0; } else if (data.find("353 ") == 0) { data = (data.substr(data.find("#"))); string nchan = (data.substr(0,data.find(" :"))); string nlist = (data.substr((data.find(":")+1))); cout<<":"<<nchan<<"> "<<nlist<<endl; noparse = 0; } else if (data.find("366 ") == 0) { data = (data.substr((data.find("#")))); string nchan = (data.substr(0,data.find(" :"))); cout<<":"<<nchan<<"> "<< (data.substr((data.find(":")+1)))<<endl; noparse = 0; } else if (data.find(" PRIVMSG ") != -1 && data.find(" PRIVMSG ") < data.find(":") ) { string pmfrom = (data.substr(0,data.find(" "))); //Full From nick!ident@host mask pmfrom = (pmfrom.substr(0,data.find("!"))); //Strip down to just nick //Is a /msg or channel text? data = (data.substr((data.find(" PRIVMSG ") + 9))); string pmto = (data.substr(0,data.find(" :"))); if (pmto == MyNick) { //Private Message cout<<"<"<<pmfrom<<"> "<<(data.substr((data.find(":")+1)))<<endl; noparse = 0; } else { //Channel Text cout<<":"<<pmto<<"> <"<<pmfrom<<"> "<<(data.substr((data.find(":")+1)))<<endl; noparse = 0; } } if(noparse){cout<<":SERVER> "<<data<<endl;} //:SERVER> Neurosys!Neurosys@[HAHA]-D6A88D3B.sd.sd.cox.net PRIVMSG #bannerftp :Channel Message //:SERVER> Neurosys!Neurosys@[HAHA]-D6A88D3B.sd.sd.cox.net PRIVMSG MrC0de :private message };