Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "IRC_Client.h"
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>//
- #include <cstring>//
- #include <sys/socket.h>
- #include <sys/types.h>//
- #include <netinet/in.h>
- #include <arpa/inet.h>//
- #include <netdb.h>
- #include <unistd.h>//
- const unsigned int MAX_LINE = 1024;
- string host;
- int port;
- string user;
- string nick;
- string passwd;
- int sockfd;
- bool run = true;
- pthread_t thread;
- IRC_Client::IRC_Client(string host, int port, string user, string nick, string passwd) {
- this->host = host;
- this->port = port;
- this->user = user;
- this->nick = nick;
- this->passwd = passwd;
- }
- int IRC_Client::irc_connect() {
- cout << "Connecting . . ." << endl;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(static_cast<int>(sockfd) < 0) {
- perror("socket()");
- irc_disconnect();
- return 1;
- }
- hostent *hp = gethostbyname(host.c_str());
- if(!hp) {
- cerr << "gethostbyname()" << endl;
- irc_disconnect();
- return 1;
- }
- sockaddr_in sin;
- memset((char*)&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
- sin.sin_port = htons(port);
- memset(&(sin.sin_zero), 0, 8*sizeof(char));
- if(connect(sockfd, (sockaddr*) &sin, sizeof(sin))==-1) {
- perror("connect()");
- irc_disconnect();
- return 1;
- }
- cout << "Connected!" << endl;
- irc_identify();
- return 0;
- }
- void IRC_Client::irc_disconnect() {
- cout << "Disconnecting . . ." << endl;
- close(sockfd);
- cout << "Disconnected!" << endl;
- }
- void IRC_Client::irc_identify() {
- sendCommand("USER custom 0 0 "+user+"\r\n"); // Userdaten
- sendCommand("PASS "+passwd+"\r\n"); // Identifizieren
- sendCommand("NICK "+nick+"\r\n"); // Nickname
- sendCommand("JOIN #"+ch+"\r\n"); // Channel betreten
- sendCommand("PRIVMSG #"+ch+" :Hi!\r\n"); // Begruessungsnachricht
- cout << "Logged in!" << endl;
- }
- void IRC_Client::sendCommand(string cmd) { //send to uplink
- send(sockfd, cmd.c_str(), strlen(cmd.c_str()), 0);
- }
- void IRC_Client::sendMsg(string msg, string p) {
- if (p == "") {
- sendCommand("PRIVMSG #"+ch+" :"+msg+"\r\n");
- } else {
- sendCommand("PRIVMSG #"+ch+" :/w "+p+" "+msg+"\r\n");
- }
- }
- void IRC_Client::channel(string ch) {
- sendCommand("JOIN #"+ch);
- }
- int IRC_Client::startCommandListener() {
- int rc = pthread_create(thread*, NULL, commandListener, (void*)this);
- if (rc) {
- return -1;
- }
- return 0;
- }
- void *IRC_Client::commandListener(void* arg) {
- for(;;) {
- char buffer[MAX_LINE+1] = {0};
- if(recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
- perror("recv()");
- ((IRC_Client*)arg)->irc_disconnect();
- exit(1);
- }
- cout<<buffer;
- string temp = buffer;
- try {
- ((IRC_Client*)arg)->cmd(buffer);
- temp.erase(0, temp.find(" ")+1);
- temp.erase(temp.find(" "));
- ((IRC_Client*)arg)->pcmd(temp, buffer);
- } catch(int e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement