Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "IRC_Client.h"
- const unsigned int MAX_LINE = 1024;
- int PORT;
- char *HOST;
- char *NICK;
- char *PASSWORD;
- char *CHANNEL;
- int sockfd;
- map<string, vector<function<void(string)>>> IRC_funcs;
- map<string, vector<function<void(map<string, string>, string)>>> chat_funcs;
- IRC_Client::IRC_Client(int PORT, char *HOST, char *NICK, char *PASSWORD, char *CHANNEL) {
- this->PORT = PORT;
- this->HOST = HOST;
- this->NICK = NICK;
- this->PASSWORD = PASSWORD;
- this->CHANNEL = CHANNEL;
- }
- IRC_Client::~IRC_Client() {
- irc_msg("Ich muss leider off... BB");
- irc_disconnect();
- }
- void IRC_Client::irc_connect() {
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (static_cast<int>(sockfd) < 0) {
- perror("socket()");
- irc_disconnect();
- exit(1);
- }
- hostent *hp = gethostbyname(HOST);
- if (!hp) {
- cerr << "gethostbyname()" << endl;
- irc_disconnect();
- exit(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();
- exit(1);
- }
- irc_identify();
- irc_reciveLoop();
- }
- void IRC_Client::irc_disconnect() {
- close(sockfd);
- }
- void IRC_Client::irc_send(const char *msg) {
- send(sockfd, msg, strlen(msg), 0);
- }
- void IRC_Client::irc_msg(string msg) {
- irc_send(("PRIVMSG #" + (string)CHANNEL + " :" + msg + "\r\n").c_str());
- }
- void IRC_Client::irc_pong(const string &buffer) {
- size_t pingPos = buffer.find("PING");
- if (pingPos != string::npos) {
- string pong("PONG" + buffer.substr(pingPos + 4) + "\r\n");
- cout << pong;
- irc_send(pong.c_str());
- }
- }
- void IRC_Client::irc_reciveLoop() {
- for (;;) {
- char buffer[MAX_LINE + 1] = { 0 };
- if (recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
- perror("recv()");
- irc_disconnect();
- exit(1);
- }
- cout << buffer;
- irc_parse(buffer);
- }
- }
- void IRC_Client::irc_parse(string buffer) {
- if (buffer.find("\r\n") == buffer.length() - 2) buffer.erase(buffer.length() - 2);
- irc_pong(buffer);
- irc_functions(buffer);
- chat_functions(buffer);
- }
- void IRC_Client::irc_identify() {
- irc_send(("PASS " + (string)PASSWORD + "\r\n").c_str());
- irc_send(("NICK " + (string)NICK + "\r\n").c_str());
- irc_send(("JOIN #" + (string)CHANNEL + "\r\n").c_str());
- irc_msg("Hallo! Ich bins, CoderDE's Bot! Ab jetzt gibts wieder tolle Bot-Funktionen!");
- }
- void IRC_Client::irc_functions(string buffer) {
- try {
- string input = string(buffer);
- input = input.erase(0, input.find(" ") + 1);
- string cmd = string(input);
- string args = string(input);
- cmd = cmd.substr(0, cmd.find(" "));
- args.erase(0, args.find(" ") + 1);
- for (function<void(string)> func : IRC_funcs[cmd]) {
- func(args);
- }
- }
- catch (int e) {}
- }
- void IRC_Client::chat_functions(string buffer) {
- try {
- string input = string(buffer);
- input = input.erase(0, input.find(" ") + 1);
- string cmd = string(input);
- string args = string(input);
- cmd = cmd.substr(0, cmd.find(" "));
- args.erase(0, args.find(" ") + 1);
- if (cmd == "PRIVMSG") {
- args.erase(0, args.find(":") + 1);
- if (args.c_str()[0] == '!') {
- args.erase(0, args.find("!") + 1);
- string chatCmd = args;
- string chatArgs = args;
- if (chatCmd.find(" ") > 0 && chatCmd.find(" ") < chatCmd.length()) {
- chatCmd.erase(chatCmd.find(" "));
- chatArgs.erase(0, chatArgs.find(" ") + 1);
- }
- else {
- chatArgs = "";
- }
- string data = string(buffer);
- data.erase(data.find(" "));
- for (function<void(map<string, string>, string)> func : chat_funcs[chatCmd]) {
- func(irc_getUserData(data), chatArgs);
- }
- }
- }
- }
- catch (int e) {}
- }
- void IRC_Client::addIrcCmd(string cmd, function<void(string)> func) {
- IRC_funcs[cmd].push_back(func);
- }
- void IRC_Client::addChatCmd(string cmd, function<void(map<string, string>, string)> func) {
- chat_funcs[cmd].push_back(func);
- }
- map<string, string> IRC_Client::irc_getUserData(string str) {
- map<string, string> data;
- string user = str;
- user.erase(0, 1);
- user.erase(user.find("!"));
- data["user"] = user;
- return data;
- }
Add Comment
Please, Sign In to add comment