Advertisement
Guest User

Untitled

a guest
Dec 10th, 2014
2,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. /*
  2. * UndeadBot.cpp
  3. *
  4. *  Created on: 10.12.2014
  5. *      Author: UndeadLeech
  6. *  Credits to: Tyler Allen
  7. */
  8.  
  9. #define _CRT_SECURE_NO_WARNINGS
  10.  
  11. #include "UndeadBot.h"
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <winsock2.h>
  15. #include <WS2tcpip.h>
  16. #include <time.h>
  17. #include <iostream>
  18.  
  19. #pragma comment(lib, "Ws2_32.lib")
  20.  
  21. using namespace std;
  22.  
  23. #define MAXDATASIZE 100
  24.  
  25. UndeadBot::UndeadBot(char * _nick, char * _usr, char * _password)
  26. {
  27.     nick = _nick;
  28.     usr = _usr;
  29.     password = _password;
  30. }
  31.  
  32. UndeadBot::~UndeadBot()
  33. {
  34.     closesocket(mySocket);
  35. }
  36.  
  37. void UndeadBot::start()
  38. {
  39.     struct addrinfo hints, *servinfo;
  40.  
  41.     setup = true;
  42.  
  43.     port = "6667";
  44.  
  45.     //Ensure that servinfo is clear
  46.     memset(&hints, 0, sizeof hints); //make sure the struct is empty
  47.  
  48.     //setup hints
  49.     hints.ai_family = AF_UNSPEC;    //IPv4 or IPv6 doesnt matter
  50.     hints.ai_socktype = SOCK_STREAM;    //TCP stream sockets
  51.  
  52.     //Start WSA to be able to make DNS lookup
  53.     WSADATA wsaData;
  54.     WSAStartup(MAKEWORD(2, 2), &wsaData);
  55.  
  56.     //Setup the structs if error, print why
  57.     int res;
  58.     if ((res = getaddrinfo("irc.twitch.tv", port, &hints, &servinfo)) != 0)
  59.     {
  60.         setup = false;
  61.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
  62.         WSACleanup();
  63.     }
  64.  
  65.     //setup socket
  66.     if ((mySocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)))
  67.     {
  68.         perror("client: socket");
  69.     }
  70.  
  71.     //Connect
  72.     if (connect(mySocket, servinfo->ai_addr, servinfo->ai_addrlen) == -1)
  73.     {
  74.         closesocket(mySocket);
  75.         perror("Client Connected");
  76.     }
  77.  
  78.     //We dont need this anymore
  79.     freeaddrinfo(servinfo);
  80.  
  81.     //Recv some data
  82.     int numbytes;
  83.     char buf[MAXDATASIZE];
  84.  
  85.     int count = 0;
  86.     while (true)
  87.     {
  88.         count++;
  89.    
  90.         switch (count)
  91.         {
  92.         case 3:
  93.             //after 3 receives send data to server (as per IRC prot)
  94.             sendData(password);
  95.             sendData(usr);
  96.             sendData(nick);
  97.             break;
  98.         case 4:
  99.             //Join a channel after we connect
  100.             sendData("JOIN #twitchchannel\r\n");
  101.             break;
  102.         default:
  103.             break;
  104.         }
  105.  
  106.         //Recv & print Data
  107.         numbytes = recv(mySocket, buf, MAXDATASIZE - 1, 0);
  108.         buf[numbytes] = '\0';
  109.         cout << buf;
  110.         //buf is the data that is received
  111.  
  112.         //pass buf to the message handler
  113.         msgHandle(buf);
  114.  
  115.         //if ping received
  116.         if (charSearch(buf, "PING"))
  117.         {
  118.             sendPong(buf);
  119.         }
  120.  
  121.         //break if connection closed
  122.         if (numbytes == 0)
  123.         {
  124.             cout << "---------------------CONNECTION CLOSED---------------------";
  125.             cout << timeNow() << endl;
  126.         }
  127.     }
  128. }
  129.  
  130. bool UndeadBot::isConnected(char *buf)
  131. {
  132.     if (charSearch(buf, "/MOTD"))
  133.         return true;
  134.  
  135.     return false;
  136. }
  137.  
  138. char * UndeadBot::timeNow()
  139. {
  140.     time_t rawtime;
  141.     struct tm * timeinfo;
  142.  
  143.     time(&rawtime);
  144.     timeinfo = localtime(&rawtime);
  145.  
  146.     return asctime(timeinfo);
  147. }
  148.  
  149. bool UndeadBot::sendData(char *msg)
  150. {
  151.     int len = strlen(msg);
  152.     int bytes_sent = send(mySocket, msg, len, NULL);
  153.    
  154.     if (bytes_sent == 0)
  155.         return false;
  156.  
  157.     return true;
  158. }
  159.  
  160. void UndeadBot::msgHandle(char * buf)
  161. {
  162.     if (charSearch(buf, "hay"))
  163.     {
  164.         sendData("PRIVMSG #ubuntu :I am awake\r\n");
  165.     }
  166. }
  167.  
  168. void UndeadBot::sendPong(char *buf)
  169. {
  170.     //Get the reply address
  171.     //loop through bug and find the location of PING
  172.     //Search through each char in toSearch
  173.  
  174.     char * toSearch = "PING ";
  175.  
  176.     for (int i = 0; i < strlen(buf); i++)
  177.     {
  178.         //If the active char is equal to the first search item then search toSearch
  179.         if (buf[i] == toSearch[0])
  180.         {
  181.             bool found = true;
  182.             //search the char array for search field
  183.             for (int x = 1; x < 4; x++)
  184.             {
  185.                 if (buf[i + x] != toSearch[x])
  186.                 {
  187.                     found = false;
  188.                 }
  189.             }
  190.  
  191.             //if found return true;
  192.             if (found == true)
  193.             {
  194.                 int count = 0;
  195.                 //Count the chars
  196.                 for (int x = (i + strlen(toSearch)); x < strlen(buf); x++)
  197.                 {
  198.                     count++;
  199.                 }
  200.  
  201.                 //Create the new char array
  202.                 char* returnHost = new char[count + 5];
  203.                 returnHost[0] = 'P';
  204.                 returnHost[1] = 'O';
  205.                 returnHost[2] = 'N';
  206.                 returnHost[3] = 'G';
  207.                 returnHost[4] = ' ';
  208.  
  209.                 count = 0;
  210.                 //set the hostname data
  211.                 for (int x = (i + strlen(toSearch)); x < strlen(buf); x++)
  212.                 {
  213.                     returnHost[count + 5] = buf[x];
  214.                     count++;
  215.                 }
  216.  
  217.                 //send the pong
  218.                 if (sendData(returnHost))
  219.                 {
  220.                     cout << timeNow() << "  Ping Pong" << endl;
  221.                 }
  222.  
  223.                 delete returnHost;
  224.             }
  225.         }
  226.     }
  227. }
  228.  
  229. bool UndeadBot::charSearch(char *toSearch, char *searchFor)
  230. {
  231.     int len = strlen(toSearch);
  232.     int forLen = strlen(searchFor); //The length of the searchfor field
  233.  
  234.     //Search through each char in toSearch
  235.     for (int i = 0; i < len; i++)
  236.     {
  237.         bool found = true;
  238.  
  239.         //search the char array for search field
  240.         for (int a = 0; a < forLen; a++)
  241.         {
  242.             if (searchFor[a] != toSearch[i + a])
  243.             {
  244.                 found = false;
  245.             }
  246.         }
  247.  
  248.         if (found)
  249.             return 1;
  250.     }
  251.     return 0;
  252. }
  253.  
  254. int main()
  255. {
  256.     UndeadBot bot = UndeadBot("NICK myUserName\r\n", "USER myUserName\r\n", "PASS oauth:some123numbers123\r\n");
  257.     bot.start();
  258.  
  259.     return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement