Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Quas ( 16 years ago )
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include <windows.h>
#include <winsock.h>
#include <ctime>
#include <vector.h>
#include <sstream>
#include <math.h>

using namespace std;
#ifndef __LUASCRIPT__
#define __LUASCRIPT__
#endif
extern "C"
{
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
}
 
u_long resolveHost(string host)
{
        LPHOSTENT entryHost = gethostbyname(host.c_str());

        if(!entryHost)
        {
                unsigned int addr = inet_addr(host.c_str());

                entryHost = gethostbyaddr((char*)&addr;, 4, AF_INET);

                if(!entryHost)
                        return 0;
        }
        return *((int*)*entryHost->h_addr_list);
}

string randomPW(int a)
{
       string haslo;
       srand((unsigned)time(0));
       int t,z;
       for(int i=0;i<a;i++)
       {
              t = (rand()%)+1;
              z = (rand()%2)+1;
              t += (z==1)? 64 : 96;
              haslo += (char) t;
       }
       return haslo;
} 

class IrcMsg
{
    vector< string > wektor;  
    string msgFull;
public:
    IrcMsg():msgFull(""){};
    IrcMsg(const IrcMsg& a){msgFull = a.msgFull; wektor.clear(); if(a.wektor.size())for(int i=0;i<a.wektor.size();i++) wektor.push_back(a.wektor[i]);}
   // zbiorN &zbiorN;::operator=(const zbiorN &item;)
    IrcMsg &operator;=(const char *a){msgFull = a;};
    void set(const string& a){msgFull = a;};
    string operator[](int a){if(a<0 || a>wektor.size()) return ""; return wektor[a];};
    friend ostream &operator;<<(ostream& out, IrcMsg& msg){out << msg.msgFull; return out;};
    int size(){return wektor.size();};
//    prepareMsg(string sp = " "){explode};
    void explode(string splitter = "\n");
    void explode(string input, string splitter = "\n");
    void getMsgInfo(string&, string&);
};
void IrcMsg::getMsgInfo(string& sender, string& msg)
{
     string temp = wektor[0];
     temp.erase(0,1);
     sender = temp.substr(0,temp.find("!"));
     temp.erase(0,temp.find(":")+1);
     msg = temp;
}
void IrcMsg::explode(string spliter)
{
 int a = 0, b = -1;
 bool first = true;
 wektor.clear();
 while((a = msgFull.find(spliter, b)) != string::npos)
 {
        if(first) a = 0;   
  b = msgFull.find(spliter, a + 1);
  if(b == string::npos)
   b = msgFull.length();
  int c = (first)?0:a+1; 
  first = false;
        wektor.push_back(msgFull.substr(c, b - a-1));
 }
 msgFull="";
}
void IrcMsg::explode(string input, string spliter)
{
 int a = 0, b = -1;
 bool first = true;
 wektor.clear();
 while((a = msgFull.find(spliter, b)) != string::npos)
 {
        if(first) a = 0;   
  b = msgFull.find(spliter, a + 1);
  if(b == string::npos)
   b = msgFull.length();
  int c = (first)?0:a+1; 
  first = false;
        wektor.push_back(msgFull.substr(c, b - a-1));
 }
 msgFull="";
}
class irc
{
public:
       irc():host(""),nick(""),port(0), hak(0),connected(false){};
       irc(string h, string n, string c, int p = 6667):host(h) ,nick(n), chanell(c), port(p), hak(0),connected(false),pong(false), isOnChanell(false),
       auth(false), proof(false), at(true)
       {pw=randomPW(5);
        L=lua_open();
       };    
       ~irc(){ircDisconnect(); lua_close(L);};
       void setPort(int p){port=p;};
       void setHost(string h){host=h;};
       bool ircConnect();
       void ircDisconnect(){if(connected) closesocket(hak);  WSACleanup();};
       bool sendConnectInfo();
       void handleIrcMsg();
       void parseIrcMsg();
       bool recvNew(int size);
       void ircSend(const string& ) const;
       void executeCode(string t);
private:
        string host, nick, chanell, pw;
        IrcMsg handleMsg;
        int port;
        unsigned int hak;
        bool connected, pong,isOnChanell, auth, proof, at;
        lua_State *L;
        
};

bool irc::ircConnect()
{
      WSADATA wsaData;
      WSAStartup((WORD) MAKEWORD(2, 2), &wsaData;);
            //return false;
      hak = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
           //  return false; 
      //if(port==0 || host=="") 
            // return false;
            
      struct sockaddr_in s;
      s.sin_family = AF_INET;
      s.sin_port = htons(port);
      s.sin_addr.s_addr = resolveHost(host);
    
     connect(hak, (struct sockaddr *) &s, sizeof s);
     connected=true;
//              return false;
      return sendConnectInfo();
}

bool irc::sendConnectInfo()
{
      if(hak==0) 
             return false;
      string buffer;
      buffer = "NICK "+nick+"\r\n";
      cout << ">Sending :" << buffer;
      send(hak, buffer.c_str(), buffer.length(), 0);
      buffer = "USER "+nick+" "+nick+" "+nick+" "+nick+" :"+nick+"\r\n";
      cout << ">Sending :" << buffer;
      send(hak, buffer.c_str(), buffer.length(), 0);
      return true;   
}
void irc::parseIrcMsg()
{
      if(!connected)
          return ;  
      if(recvNew(512))
      {
        handleMsg.explode();
        handleIrcMsg();
      }
      if(pong && !isOnChanell)
             ircSend("JOIN "+chanell+"\r\n\0"); 
      
}
string IntToStr(int lic)
{
int dz = 1;
string inttostr = "";

 while((dz * 10) <= lic)
 {
 dz*=10;
 }
 while(dz != 0)
 {
 inttostr += char((lic / dz)+48);
 lic %= dz;
 dz /= 10;
 }

return inttostr;
}

double StrToInt(string str)
{
       stringstream a;
       a<< str;
       double res;
       a>> res;
       return res;
}

bool isInt(char a)
{
     if((int) a > 47 && (int) a < 58)
              return true;
     return false;
     
}
int findInt(string a)
{
    for(int i=0;i<a.length();i++)
            if(isInt(a[i]))
                          return i;
    return -1;
    
}
void irc::executeCode(string t)
{
     if(t.substr(0,3) == "for")
     {
         t.erase(0,4);  
         string its_s,ite_s;
         for(int i= findInt(t);i<t.length();i++)
                 if(isInt(t[i]))
                      its_s+=t[i];
                 else
                 {
                     t.erase(0,i);
                     break;   
                 }
         for(int i = findInt(t);i<t.length();i++)
                 if(isInt(t[i]))
                      ite_s+=t[i];
                 else
                 {
                     t.erase(0,i);
                     break;   
                 }
         //mode.
         string sends;
         vector<string> vec;
         t.erase(0,t.find("{")+1);
         if(t.substr(0,t.find("(")) == "ircSay");
         {         
               t.erase(0,t.find("(")+1);
              
               if(t[0] == '"')
               {
                t.erase(0,1);
                int z=0;
                while(t[z] != '"' ||  (t[z] == '"' && t[z-1] =='\\'))
                {
                           sends+=t[z];
                           if(t[z-1] == '\\')
                                     t.erase(z-2,1);
                           z++;
                           if(z == t.length())
                                break;
                }
                t.erase(0,z);
                if(t[0]=='"')
                  t.erase(0,1);
                vec.push_back(sends);  
               }
               sends=t.substr(0,t.find(")"));                     
         }
         sends = "";
         for(int i=0;i<vec.size();i++)
                 sends += vec[i];
         if(sends != "")
                  for(int i= (int)StrToInt(its_s) ;i<(int)StrToInt(ite_s);i++)
                          {
                                     ircSend("PRIVMSG "+chanell+" :"+sends+"\r\n\0");   
                                     Sleep(80);
                                    // cout << ">Sending c++ request"+sends << endl;           
                                                  
                          }
     }
     

}
void irc::handleIrcMsg()
{
     if(!handleMsg.size()){cout << "NO MSG" << endl;
         return;}
     for(int i = 0;i<handleMsg.size();i++)
     {
             time_t sec;
             sec= time (NULL);
             sec %= (3600*24);
             string timee;
             timee += "["+IntToStr((int)sec/3600 +2);
             sec %=3600;
             timee += ":";
             timee += IntToStr((int)sec/60);
             sec %=60;
             timee += ":";
             timee += IntToStr(sec);
             timee += "] ";            
             
             
            if(pong && i>0) break;
            if(handleMsg[i].substr(0,6) == "PING :")
            {
                   string _pong = handleMsg[i];
                   _pong.replace(1,1,"O");
                   cout << ">"+timee+"Sending Pong Request :" << _pong << endl;
                   ircSend(_pong);
                   pong=true;                 
            }
            if(handleMsg[i].find("PRIVMSG") != string::npos)
            {
                string s,m;
                handleMsg.getMsgInfo(s,m);
                if(m.substr(0,3) == "wat")
                {
                     ircSend("PRIVMSG "+chanell+" :Wat?\r\n\0");
                     continue;              
                }
                if(m.substr(0,5)=="!math")
                    {
                          m.erase(0,6);
                          //handleMsg.explode(m," ");
                          int j = 0;
                          string a,b;
                          for(i=0;i<m.length();i++,j++)
                                 if(isInt(m[i]))
                                      a+=m[i];
                                 else
                                     break;
                          
                           m.erase(0,j);
                           if(m.length() && m[0] != '*' && m[0] != '^' && m[0] != '-' && m[0] != '+' && m[0]!='/' && m[0] != '%')
                           {
                                   ircSend("PRIVMSG "+chanell+" :Zzzomething izzz Wrong!\r\n\0"); 
                                   continue;
                           }
                           char c= m[0];
                           m.erase(0,1);
                           j=0;
                           for(i=0;i<m.length();i++,j++)
                                 if(isInt(m[i]))
                                      b+=m[i];
                                 else
                                     break;                          
                            if(j==0)
                            {
                                    ircSend("PRIVMSG "+chanell+" :Zzzomething izzz Wrong!\r\n\0"); 
                                    continue;
                            }
                            unsigned long long result, l1,l2;
                            l1 = StrToInt(a);
                            l2 = StrToInt(b);
                            switch(c)
                            {
                                case('+'):
                                          result = l1+l2;
                                          break;
                                case('-'):
                                          result = l1-l2;                                       
                                          break;               
                                case('*'):
                                          result = l1*l2;
                                          break; 
                                case('/'):
                                          if(!l1 || !l2)
                                          {
                                             ircSend("PRIVMSG "+chanell+" :Diverzzze by Zzero!\r\n\0"); 
                                             continue;     
                                          }
                                          result = l1/l2; 
                                          break;
                                case('%'):
                                          if(!l1 || !l2)
                                          {
                                             ircSend("PRIVMSG "+chanell+" :Diverzzze by Zzero!\r\n\0"); 
                                             continue;     
                                          }
                                          result = (int)l1%(int)l2;
                                          break; 
                                 case('^'): 
                                          result=pow(l1,l2);  
                                          break;
                            }
                             stringstream od;
                             od << "PRIVMSG "+chanell+" :Rezzult: ";
                             od << result << "\r\n\0";
                             ircSend(od.str()); 
                             continue;                  
                    }
                cout << timee+s <<": " << m << endl;                    
                if(s.find("QuaS_") != string::npos)
                {
                    if(!auth)
                    {
                             if(!proof)
                             {
                                       ircSend("PRIVMSG "+chanell+" :Prowzz that you're Quazzz and write urzz pazzword.\r\n\0");
                                       cout << ">"+timee+"Password: " << pw << endl;
                                       proof=true;         
                             }
                             if(m.find(pw)!=string::npos)
                             {
                                       ircSend("PRIVMSG "+chanell+" :Hello My Mazzterr.\r\n\0");                  
                                       auth=true;
                             }
                             continue;
                    }
                    if(m.substr(0,4)=="!c++")
                    {
                           m.erase(0,5);
                           executeCode(m);
                           continue;          
                    }
                    
                    if(m.find("spierdalaj bocie")!=string::npos)
                    {
                                      ircSend("QUIT :byee my mazzter\r\n\0");
                                      continue;
                    }
                    if(m.find("bot stop")!=string::npos)
                    {
                        at = false;
                        ircSend("PRIVMSG "+chanell+" :Allright Mazzter... I'll zztop.\r\n\0");
                         continue;
                    }
                    if(m.find("!qlua")!=string::npos)
                    {
                                                    
                        m.erase(0,6);
                        int err;
                        
                        if(!(err = luaL_loadstring(L,m.c_str() ) ) )
                             if (lua_pcall (L,0,LUA_MULTRET,0) == 0)
                             {
                                   ircSend("PRIVMSG "+chanell+" :"+lua_tostring(L, -1)+"\r\n\0");
                                   cout << timee+" >> Lua: " << lua_tostring(L, -1) << endl;
                             }
                             else
                                 ircSend("PRIVMSG "+chanell+" :Lua Module: Error!\r\n\0");
                        else{ string ttt;
                           switch (err)
                           {
                                  case LUA_ERRSYNTAX: ttt= "Lua Module: syntax error"; break;
                                  case LUA_ERRMEM: ttt= "Lua Module: memory error"; break;
                           }
                               ircSend("PRIVMSG "+chanell+" :"+ttt+"\r\n\0");
                           }
                         continue;
                    }
                   
                    if(m.find("bot join")!=string::npos)
                    {
                        string tmp1;
                        getline(cin,tmp1);
                        ircSend("JOIN "+tmp1+"\r\n\0");
                        cout << timee+" >>Join Request Sending:" << tmp1 << endl;
                        continue;
                    }
                    if(m.find("bot command")!=string::npos)
                    {
                        string tmp1;
                        getline(cin,tmp1);
                        ircSend(tmp1+"\r\n\0");
                        cout << timee+" >>Command Sending:" << tmp1 << endl;
                        continue;
                    }
                    if(m.find("bot start")!=string::npos)
                    {
                         at=true;            
                         ircSend("PRIVMSG "+chanell+" :Azzz You Wizzhh My Mazzter...\r\n\0");            
                         continue;
                    }
                    if(at){
                    m = "PRIVMSG "+chanell+" :"+m+"\r\n\0";  
                    //cout << ">Sending Msg :"+m;
                    ircSend(m);}
                    
                }
                continue;
            }
            if(handleMsg[i].find("JOIN "+chanell) != string::npos)
            {
                  string s,m;
                  handleMsg.getMsgInfo(s,m);
                  if(s.find("QuaS_")!=string::npos)
                  {
                     ircSend("PRIVMSG "+chanell+" :My Mazzter izz Back! I am zzo happy!\r\n\0");
                     Sleep(150);
                     ircSend("PRIVMSG "+chanell+" :Welcomee Back Mazzter!\r\n\0");
                     auth=false;
                     proof=false;
                     pw=randomPW(5);
                     continue;                          
                  }
                                         
            }
            if(handleMsg[i].find("KICK "+chanell) != string::npos)
            {
                 if (handleMsg[i].find("QuaS_") != string::npos)
                    {
                          ircSend("PRIVMSG "+chanell+" :Do not kick my Mazzter You Bazztard!\r\n\0");             
                    }
                 if (handleMsg[i].find(nick) != string::npos)
                    {
                          ircSend("JOIN "+chanell+"\r\n\0");             
                    }
            }
            cout << ">"+timee+"Packet: "+handleMsg[i] << endl;
            if(handleMsg[i].find(chanell) != string::npos && pong && !isOnChanell)
                   isOnChanell=true;
                   
     }
}

bool irc::recvNew(int size)
{
     if(!connected)
          return false;

     char* temp = new char[size];
     if(recv(hak, temp, size, 0)>0){
          handleMsg.set(string(temp)); delete[] temp; return true;}
     delete[] temp;
     return false;
}

void irc::ircSend(const string &sendInfo;) const
{
     send(hak,sendInfo.c_str(),sendInfo.length(),0);
}
int rec(unsigned int a, string& b)
{
    char o[1024];
    a = recv(a, o, 512, 0);
    b = string(o);
    return a;
    
}

 
int main(int argc, char * argv[])
{

//    luaopen_loadlib(L);

 irc IRC("irc.quakenet.org","QuasBot","#Tibia-Bot-Developement",6667);
 if(IRC.ircConnect())
 {
    while (1)
    {
          if(kbhit())
          {
           string tp;
           getline(cin,tp);
           tp = "PRIVMSG #Tibia :"+tp+"\r\n\0";
           IRC.ircSend(tp);
          }
          IRC.parseIrcMsg();
           Sleep(50);
    
    }   
  } 
  getchar();
}

 

Revise this Paste

Your Name: Code Language: