Advertisement
Panakotta00

IRC_Client.cpp

Sep 6th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma warning(disable:4503)
  4.  
  5. #include "Wurst.h"
  6. #include "IRC_Client.h"
  7.  
  8. static const int MAX_LINE = 1024;
  9. int PORT;
  10. char *HOST;
  11. char *NICK;
  12. char *PASSWORD;
  13. char *CHANNEL;
  14.  
  15. SOCKET sockfd;
  16. //int sockfd;
  17.  
  18. map<string, vector<function<void(string)>>> IRC_funcs;
  19. map<string, vector<function<void(map<string, string>, string)>>> chat_funcs;
  20.  
  21. IRC_Client::IRC_Client(int PORT, char *HOST, char *NICK, char *PASSWORD, char *CHANNEL) {
  22.     this->PORT = PORT;
  23.     this->HOST = HOST;
  24.     this->NICK = NICK;
  25.     this->PASSWORD = PASSWORD;
  26.     this->CHANNEL = CHANNEL;
  27. }
  28.  
  29. IRC_Client::~IRC_Client() {
  30.     irc_msg("Ich muss leider off... BB");
  31.     irc_disconnect();
  32. }
  33.  
  34. void IRC_Client::irc_connect() {
  35.     WSADATA wsa;
  36.     if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0)
  37.         exit(1);
  38.  
  39.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  40.  
  41.     if (static_cast<int>(sockfd) < 0) {
  42.         perror("socket()");
  43.         irc_disconnect();
  44.         exit(1);
  45.     }
  46.     hostent *hp = gethostbyname(HOST);
  47.     if (!hp) {
  48.         cerr << "gethostbyname()" << endl;
  49.         irc_disconnect();
  50.         exit(1);
  51.     }
  52.     sockaddr_in sin;
  53.     memset((char*)&sin, 0, sizeof(sin));
  54.     sin.sin_family = AF_INET;
  55.     memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
  56.     sin.sin_port = htons(PORT);
  57.     memset(&(sin.sin_zero), 0, 8 * sizeof(char));
  58.     if (connect(sockfd, (sockaddr*)&sin, sizeof(sin)) == -1) {
  59.         perror("connect()");
  60.         irc_disconnect();
  61.         exit(1);
  62.     }
  63.  
  64.     irc_identify();
  65.  
  66.     _beginthread(&irc_reciveLoop, 0, NULL);
  67. }
  68.  
  69. void IRC_Client::irc_disconnect() {
  70.     closesocket(sockfd);
  71.     WSACleanup();
  72.  
  73.     //close(sockfd);
  74. }
  75.  
  76. void IRC_Client::irc_send(const char *msg) {
  77.     send(sockfd, msg, strlen(msg), 0);
  78. }
  79.  
  80. void IRC_Client::irc_msg(string msg) {
  81.     irc_send(("PRIVMSG #" + (string)CHANNEL + " :" + msg + "\r\n").c_str());
  82. }
  83.  
  84. void IRC_Client::irc_pong(const string &buffer) {
  85.     size_t pingPos = buffer.find("PING");
  86.     if (pingPos != string::npos) {
  87.         string pong("PONG" + buffer.substr(pingPos + 4) + "\r\n");
  88.         cout << pong;
  89.         irc_send(pong.c_str());
  90.     }
  91. }
  92.  
  93. void IRC_Client::irc_reciveLoop(void *param) {
  94.     for (;;) {
  95.         char buffer[MAX_LINE + 1] = { 0 };
  96.         if (recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
  97.             perror("recv()");
  98.             irc_disconnect();
  99.             exit(1);
  100.         }
  101.         cout << buffer;
  102.         irc_parse(buffer);
  103.     }
  104.     _endthread();
  105. }
  106.  
  107. void IRC_Client::irc_parse(string buffer) {
  108.     if (buffer.find("\r\n") == buffer.length() - 2) buffer.erase(buffer.length() - 2);
  109.     irc_pong(buffer);
  110.     irc_functions(buffer);
  111.     chat_functions(buffer);
  112. }
  113.  
  114. void IRC_Client::irc_identify() {
  115.     irc_send(("PASS " + (string)PASSWORD + "\r\n").c_str());
  116.     irc_send(("NICK " + (string)NICK + "\r\n").c_str());
  117.     irc_send(("JOIN #" + (string)CHANNEL + "\r\n").c_str());
  118.     irc_msg("Hallo! Ich bins, CoderDE's Bot! Ab jetzt gibts wieder tolle Bot-Funktionen!");
  119. }
  120.  
  121. void IRC_Client::irc_functions(string buffer) {
  122.     try {
  123.         string input = string(buffer);
  124.         input = input.erase(0, input.find(" ") + 1);
  125.         string cmd = string(input);
  126.         string args = string(input);
  127.         cmd = cmd.substr(0, cmd.find(" "));
  128.         args.erase(0, args.find(" ") + 1);
  129.  
  130.         for (function<void(string)> func : IRC_funcs[cmd]) {
  131.             func(args);
  132.         }
  133.     } catch(int&) {}
  134. }
  135.  
  136. void IRC_Client::chat_functions(string buffer) {
  137.     try {
  138.         string input = string(buffer);
  139.         input = input.erase(0, input.find(" ") + 1);
  140.         string cmd = string(input);
  141.         string args = string(input);
  142.         cmd = cmd.substr(0, cmd.find(" "));
  143.         args.erase(0, args.find(" ") + 1);
  144.  
  145.         if (cmd == "PRIVMSG") {
  146.             args.erase(0, args.find(":") + 1);
  147.             if (args.c_str()[0] == '!') {
  148.                 args.erase(0, args.find("!") + 1);
  149.                 string chatCmd = args;
  150.                 string chatArgs = args;
  151.                 if (chatCmd.find(" ") > 0 && chatCmd.find(" ") < chatCmd.length()) {
  152.                     chatCmd.erase(chatCmd.find(" "));
  153.                     chatArgs.erase(0, chatArgs.find(" ") + 1);
  154.                 }
  155.                 else {
  156.                     chatArgs = "";
  157.                 }
  158.  
  159.                 string data = string(buffer);
  160.                 data.erase(data.find(" "));
  161.  
  162.                 for (function<void(map<string, string>, string)> func : chat_funcs[chatCmd]) {
  163.                     func(irc_getUserData(data), chatArgs);
  164.                 }
  165.             }
  166.         }
  167.     } catch (int&) {}
  168. }
  169.  
  170. void IRC_Client::addIrcCmd(string cmd, function<void(string)> func) {
  171.     IRC_funcs[cmd].push_back(func);
  172. }
  173.  
  174. void IRC_Client::addChatCmd(string cmd, function<void(map<string, string>, string)> func) {
  175.     chat_funcs[cmd].push_back(func);
  176. }
  177.  
  178. map<string, string> IRC_Client::irc_getUserData(string str) {
  179.     map<string, string> data;
  180.     string user = str;
  181.     user.erase(0, 1);
  182.     user.erase(user.find("!"));
  183.     data["user"] = user;
  184.  
  185.     return data;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement