Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL.h>
  3. #include <SDL_net.h>
  4. #include <string.h>
  5. #include <sstream>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <Windows.h>
  9. #include <conio.h>
  10.  
  11. using namespace std;
  12.  
  13. const int BUFF_SIZE = 516;
  14. bool killbot = true;
  15.  
  16. int main(int argc, char** argv)
  17. {
  18.     SDL_Init(SDL_INIT_EVERYTHING);
  19.     SDLNet_Init();
  20.  
  21.     IPaddress ip;
  22.  
  23.     string server = "irc.afternet.org";
  24.     char buffer[BUFF_SIZE];
  25.  
  26.     //create socket set
  27.     SDLNet_SocketSet socketset = SDLNet_AllocSocketSet(1);
  28.  
  29.     SDLNet_ResolveHost(&ip, server.c_str(), 6667);
  30.  
  31.     Uint8 * dotQuad = (Uint8*)&ip.host;
  32.     cout << "Successfully resolved host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3];
  33.     cout << " port " << SDLNet_Read16(&ip.port) << endl << endl;
  34.  
  35.     TCPsocket client = SDLNet_TCP_Open(&ip);
  36.     SDLNet_TCP_AddSocket(socketset, client);
  37.  
  38.     //wait 10 seconds for server response
  39.     int activesockets = SDLNet_CheckSockets(socketset, 10000);
  40.  
  41.     int gotresponse = SDLNet_SocketReady(client);
  42.    
  43.     int messagenum = 0;
  44.     while (messagenum < 2)
  45.     {
  46.         if (gotresponse != 0)
  47.         {
  48.             int serverbytecount = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
  49.             string received = buffer;
  50.  
  51.             if (received.length() > serverbytecount)
  52.                 received.erase(serverbytecount);
  53.  
  54.             if (received.find("PING") != string::npos)
  55.             {
  56.                 received.replace(1, 1, "O");
  57.                 strcpy_s(buffer, received.c_str());
  58.                 SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  59.                 cout << "sent: " << received.c_str() << endl;
  60.             }
  61.  
  62.             cout << received.c_str() << endl;
  63.             //killbot = false
  64.             messagenum++;
  65.         }
  66.     }
  67.     killbot = false;
  68.    
  69.     for (int i = 0; i < 3; i++)
  70.     {
  71.         if (i == 0)
  72.         {
  73.             strcpy_s(buffer, "NICK Bot_Test\r\n");
  74.             SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  75.         }
  76.  
  77.         if (i == 1)
  78.         {
  79.             strcpy_s(buffer, "USER Bot_Test 0 * :MyBot\r\n");
  80.             SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  81.         }
  82.  
  83.         if (i == 2)
  84.         {
  85.             strcpy_s(buffer, "JOIN :#ludumdare\r\n");
  86.             SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  87.         }
  88.         int activesockets = SDLNet_CheckSockets(socketset, 5000);
  89.         int gotresponse = SDLNet_SocketReady(client);
  90.         if (gotresponse != 0)
  91.         {
  92.             strcpy_s(buffer, "");
  93.             int serverbytecount = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
  94.             string received = buffer;
  95.  
  96.             if (received.length() > serverbytecount)
  97.                 received.erase(serverbytecount);
  98.  
  99.             cout << received.c_str() << endl;
  100.  
  101.             if (received.find("PING") != string::npos)
  102.             {
  103.                 received.replace(1, 1, "O");
  104.                 strcpy_s(buffer, received.c_str());
  105.                 SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  106.                 cout << "sent: " << received.c_str() << endl;
  107.             }
  108.  
  109.         }
  110.     }
  111.    
  112.  
  113.     /*strcpy_s(buffer, "PRIVMSG #ludumdare :hello world\r\n");
  114.     SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  115.     cout << "sent: " << buffer << endl;
  116.     */
  117.  
  118.     cout << "end loop" << endl;
  119.     int timer = 0;
  120.     while (!killbot)
  121.     {
  122.         int socketactive = SDLNet_CheckSockets(socketset, 0);
  123.  
  124.         if (socketactive != 0)
  125.         {
  126.             //see if we got response from server
  127.             int response = SDLNet_SocketReady(client);
  128.  
  129.             if (response != 0)
  130.             {
  131.  
  132.                 strcpy_s(buffer, "");
  133.                 int responsebytes = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
  134.                 string received = buffer;
  135.                 if (received.length() > responsebytes)
  136.                 {
  137.                     received.erase(responsebytes);
  138.                 }
  139.                 cout << received.c_str() << endl;
  140.  
  141.                 if (received.find("PING") != string::npos)
  142.                 {
  143.                     received.replace(1, 1, "O");
  144.                     strcpy_s(buffer, received.c_str());
  145.                     SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
  146.                     cout << "sent: " << received.c_str() << endl;
  147.  
  148.                    
  149.                 }
  150.  
  151.                 if (responsebytes <= 0)
  152.                 {
  153.                     cout << "Sir, the server has been hit! We're going down!" << endl;
  154.                     cout << "Press ENTER to continue..." << endl;
  155.                     cin.get();
  156.                     killbot = true;
  157.                 }
  158.             }
  159.         }
  160.  
  161.        
  162.        
  163.         timer++;
  164.     }
  165.     return 1;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement