Panakotta00

IRC_Client.cpp

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