Advertisement
Panakotta00

IRC_Client.cpp

Aug 20th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include "IRC_Client.h"
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstdio>//
  6. #include <cstring>//
  7.  
  8. #include <sys/socket.h>
  9. #include <sys/types.h>//
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>//
  12. #include <netdb.h>
  13. #include <unistd.h>//
  14.  
  15. const unsigned int MAX_LINE = 1024;
  16.  
  17. string host;
  18. int port;
  19. string user;
  20. string nick;
  21. string passwd;
  22.  
  23. int sockfd;
  24. bool run = true;
  25. pthread_t thread;
  26.  
  27. IRC_Client::IRC_Client(string host, int port, string user, string nick, string passwd) {
  28.     this->host = host;
  29.     this->port = port;
  30.     this->user = user;
  31.     this->nick = nick;
  32.     this->passwd = passwd;
  33. }
  34.  
  35. int IRC_Client::irc_connect() {
  36.     cout << "Connecting . . ." << endl;
  37.    
  38.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  39.    
  40.     if(static_cast<int>(sockfd) < 0) {
  41.         perror("socket()");
  42.         irc_disconnect();
  43.         return 1;
  44.     }
  45.    
  46.     hostent *hp = gethostbyname(host.c_str());
  47.    
  48.     if(!hp) {
  49.         cerr << "gethostbyname()" << endl;
  50.         irc_disconnect();
  51.         return 1;
  52.     }
  53.    
  54.     sockaddr_in sin;
  55.     memset((char*)&sin, 0, sizeof(sin));
  56.     sin.sin_family = AF_INET;
  57.     memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
  58.     sin.sin_port = htons(port);
  59.     memset(&(sin.sin_zero), 0, 8*sizeof(char));
  60.    
  61.     if(connect(sockfd, (sockaddr*) &sin, sizeof(sin))==-1) {
  62.         perror("connect()");
  63.         irc_disconnect();
  64.         return 1;
  65.     }
  66.    
  67.     cout << "Connected!" << endl;
  68.    
  69.     irc_identify();
  70.    
  71.     return 0;
  72. }
  73.  
  74. void IRC_Client::irc_disconnect() {
  75.     cout << "Disconnecting . . ." << endl;
  76.     close(sockfd);
  77.     cout << "Disconnected!" << endl;
  78. }
  79.  
  80. void IRC_Client::irc_identify() {
  81.     sendCommand("USER custom 0 0 "+user+"\r\n"); // Userdaten
  82.     sendCommand("PASS "+passwd+"\r\n"); // Identifizieren
  83.     sendCommand("NICK "+nick+"\r\n"); // Nickname
  84.     sendCommand("JOIN #"+ch+"\r\n"); // Channel betreten
  85.     sendCommand("PRIVMSG #"+ch+" :Hi!\r\n"); // Begruessungsnachricht
  86.     cout << "Logged in!" << endl;
  87. }
  88.  
  89. void IRC_Client::sendCommand(string cmd) { //send to uplink
  90.     send(sockfd, cmd.c_str(), strlen(cmd.c_str()), 0);
  91. }
  92.  
  93. void IRC_Client::sendMsg(string msg, string p) {
  94.     if (p == "") {
  95.         sendCommand("PRIVMSG #"+ch+" :"+msg+"\r\n");
  96.     } else {
  97.         sendCommand("PRIVMSG #"+ch+" :/w "+p+" "+msg+"\r\n");
  98.     }
  99. }
  100.  
  101. void IRC_Client::channel(string ch) {
  102.     sendCommand("JOIN #"+ch);
  103. }
  104.  
  105. int IRC_Client::startCommandListener() {
  106.     int rc = pthread_create(thread*, NULL, commandListener, (void*)this);
  107.     if (rc) {
  108.         return -1;
  109.     }
  110.     return 0;
  111. }
  112.  
  113. void *IRC_Client::commandListener(void* arg) {
  114.     for(;;) {
  115.         char buffer[MAX_LINE+1] = {0};
  116.        
  117.         if(recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
  118.             perror("recv()");
  119.             ((IRC_Client*)arg)->irc_disconnect();
  120.             exit(1);
  121.         }
  122.        
  123.         cout<<buffer;
  124.        
  125.         string temp = buffer;
  126.         try {
  127.             ((IRC_Client*)arg)->cmd(buffer);
  128.             temp.erase(0, temp.find(" ")+1);
  129.             temp.erase(temp.find(" "));
  130.            
  131.             ((IRC_Client*)arg)->pcmd(temp, buffer);
  132.         } catch(int e) {}
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement