<commit_before>/*
 * The MIT License (MIT) Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 */

#include "irc_client.h"

IRCClient::IRCClient(Log* l): log(l), irc(IRCSocket(l)) { }

IRCClient::~IRCClient() {
    disconnect();
}

void IRCClient::set_owner(std::string owner) {
    _owner = owner;
}

bool IRCClient::connect(char * hostname, int port) {
    return irc.connect(hostname, port);
}

void IRCClient::disconnect() {
    return irc.disconnect();
}

bool IRCClient::send(std::string msg) {
    msg.append("\n");
    return irc.swrite(msg);
}

std::string IRCClient::read() {
    std::string buffer = irc.sread();
    return buffer;
}

bool IRCClient::login(std::string nick, std::string pass) {
    _nick = nick;
    _pass = pass;

    if (send("PASS " + pass)) {
        if (send("NICK " + nick)) {
            return true;
        }
    }

    return false;
}

bool IRCClient::join(std::string channel) {
    _channel = channel;

    if (send("JOIN #" + channel)) {
        return true;
    }

    return false;
}

bool IRCClient::priv_me(std::string msg) {
    if (!_nick.empty() && !_channel.empty()) {
        if (send("PRIVMSG #jtv :/w " + _owner + " " + msg)) {
            return true;
        }
    }

    return false;
}

void IRCClient::parse() {
    std::string buffer;
    buffer = read();
    
    log->d(buffer);
    // Check watch commands
    if (buffer.substr(0,4).compare("PING") == 0) {
        log->d("PING recieved");
        send("PONG" + buffer.substr(4));
        log->d("PONG replied");
        return;
    }
}<commit_msg>Parse messages properly without extra sent text.<commit_after>/*
 * The MIT License (MIT) Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 */

#include "irc_client.h"

IRCClient::IRCClient(Log* l): log(l), irc(IRCSocket(l)) { }

IRCClient::~IRCClient() {
    disconnect();
}

void IRCClient::set_owner(std::string owner) {
    _owner = owner;
}

bool IRCClient::connect(char * hostname, int port) {
    return irc.connect(hostname, port);
}

void IRCClient::disconnect() {
    return irc.disconnect();
}

bool IRCClient::send(std::string msg) {
    msg.append("\n");
    return irc.swrite(msg);
}

std::string IRCClient::read() {
    std::string buffer = irc.sread();
    return buffer;
}

bool IRCClient::login(std::string nick, std::string pass) {
    _nick = nick;
    _pass = pass;

    if (send("PASS " + pass)) {
        if (send("NICK " + nick)) {
            return true;
        }
    }

    return false;
}

bool IRCClient::join(std::string channel) {
    _channel = channel;

    if (send("JOIN #" + channel)) {
        return true;
    }

    return false;
}

bool IRCClient::priv_me(std::string msg) {
    if (!_nick.empty() && !_channel.empty()) {
        if (send("PRIVMSG #jtv :/w " + _owner + " " + msg)) {
            return true;
        }
    }

    return false;
}

void IRCClient::parse() {
    std::string buffer;
    buffer = read();
    
//    log->d(buffer);
    // Check watch commands
    if (buffer.substr(0,4).compare("PING") == 0) {
        log->d("PING recieved");
        send("PONG" + buffer.substr(4));
        log->d("PONG replied");
        return;
    }

    // Parse text normally
    std::string message;
    std::string user;
    
    // Search for message
    std::string search_str = "PRIVMSG #" + _channel;
    std::size_t fnd = buffer.find(search_str);
    if (fnd != std::string::npos) {
        message = buffer.substr(fnd+search_str.length()+2);
        if (!message.empty()) {
            // Parse user
            fnd = buffer.find("!");
            if (fnd != std::string::npos) {
                user = buffer.substr(1,fnd-1);
                log->d(user + ": " + message);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.14835044789163207, 'fraction_numerical': 0.004151190736290146, 'mean_word_length': 2.761709120788825, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1402701', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1447, 'n_words': 401}