<commit_before>// -*- C++ -*-
// Copyright 2012, <NAME> <<EMAIL>>

#include "./bot.h"

#include <glog/logging.h>

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
#include <boost/random.hpp>

#include <cassert>
#include <ctime>
#include <fstream>
#include <sstream>

using boost::asio::ssl::context;
using boost::asio::ip::tcp;

typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;

namespace drakebot {

boost::mt19937 rng;
boost::uniform_real<double> uniform(0.0, 1.0);

void InitializeRNG() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  rng.seed(tv.tv_usec);
}

IRCRobot::IRCRobot(boost::asio::io_service &service,
                   boost::asio::ssl::context &context,
                   const std::string &quotations_file,
                   unsigned int interval,
                   const std::string &nick,
                   const std::string &password)
    :io_service_(service), timer_(service), quotations_file_(quotations_file),
     socket_(service, context), rand_(rng, uniform), nick_(nick),
     password_(password), state_(SEND_PASS), interval_(interval) {
  reply_ = new char[MAX_LENGTH];
  request_ = new char[MAX_LENGTH];
  memset(static_cast<void *>(reply_), 0, sizeof(reply_));
  memset(static_cast<void *>(request_), 0, sizeof(request_));
}

IRCRobot::~IRCRobot() {
  delete request_;
  delete reply_;
}

void IRCRobot::Connect(boost::asio::ip::tcp::resolver::iterator
                       endpoint_iterator) {

  boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
  socket_.lowest_layer().async_connect(
      endpoint,
      boost::bind(&IRCRobot::HandleConnect, this,
                  boost::asio::placeholders::error, ++endpoint_iterator));
}

void IRCRobot::HandleConnect(const boost::system::error_code& error,
                             tcp::resolver::iterator
                             endpoint_iterator) {
  if (!error) {
    socket_.async_handshake(boost::asio::ssl::stream_base::client,
                            boost::bind(&IRCRobot::HandleHandshake, this,
                                        boost::asio::placeholders::error));
  } else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
    socket_.lowest_layer().close();
    boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
    socket_.lowest_layer().async_connect(
        endpoint,
        boost::bind(&IRCRobot::HandleConnect, this,
                    boost::asio::placeholders::error, ++endpoint_iterator));
  } else {
    LOG(FATAL) << "Connect failed: " << error;
    exit(1);
  }
}

void IRCRobot::HandleHandshake(const boost::system::error_code& error) {
  if (!error) {
    if (password_.size()) {
      std::string s = "PASS ";
      s.append(password_);
      SendLine(s);
    } else {
      state_ = SEND_NICK;
      std::string s = "NICK ";
      s.append(nick_);
      SendLine(s);
    }
    EstablishReadCallback();
  } else {
    LOG(FATAL) << "Handshake failed: " << error;
  }
}

void IRCRobot::EstablishReadCallback() {
  boost::asio::async_read(socket_,
                          boost::asio::buffer(reply_, MAX_LENGTH),
                          boost::bind(&IRCRobot::ReadCompletedTest, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred),
                          boost::bind(&IRCRobot::HandleRead, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
}

void IRCRobot::SendLine(const std::string &msg) {
  assert(msg.size() < MAX_LENGTH);
  memcpy(request_, msg.c_str(), msg.size());
  request_[msg.size()] = '\n';
  boost::asio::async_write(socket_,
                           boost::asio::buffer(request_, msg.size() + 1),
                           boost::bind(&IRCRobot::HandleWrite, this,
                                       boost::asio::placeholders::error,
                                       boost::asio::placeholders::bytes_transferred));
}

bool IRCRobot::ReadCompletedTest(const boost::system::error_code& error,
                                 size_t bytes_transferred) {
  if (error) {
    LOG(FATAL) << "read error: " << error;
    return true;
  }
  if (bytes_transferred == MAX_LENGTH) {
    reply_[MAX_LENGTH - 1] = '\n';
    return true;
  } else if (bytes_transferred == 0) {
    return false;
  }
  return memchr(reply_, '\n', MAX_LENGTH) != NULL;
}

void IRCRobot::HandleRead(const boost::system::error_code& error,
                          size_t bytes_transferred) {
  if (error) {
    LOG(FATAL) << "read error: " << error;
    exit(1);
  }

  bool keep_going = true;
  size_t pos = 0;
  std::string line;
  std::string reply(reply_, bytes_transferred);
  while (keep_going) {
    size_t next = reply.find('\n', pos);
    if (next == std::string::npos) {
      line = reply.substr(pos, next);
      keep_going = false;
    } else {
      line = reply.substr(pos, next - pos);
      pos = next + 1;
    }

    LineCallback(line);

  }
  EstablishReadCallback();
}
<commit_msg>fix memchr invocation<commit_after>// -*- C++ -*-
// Copyright 2012, <NAME> <<EMAIL>>

#include "./bot.h"

#include <glog/logging.h>

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
#include <boost/random.hpp>

#include <cassert>
#include <ctime>
#include <fstream>
#include <sstream>

using boost::asio::ssl::context;
using boost::asio::ip::tcp;

typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;

namespace drakebot {

boost::mt19937 rng;
boost::uniform_real<double> uniform(0.0, 1.0);

void InitializeRNG() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  rng.seed(tv.tv_usec);
}

IRCRobot::IRCRobot(boost::asio::io_service &service,
                   boost::asio::ssl::context &context,
                   const std::string &quotations_file,
                   unsigned int interval,
                   const std::string &nick,
                   const std::string &password)
    :io_service_(service), timer_(service), quotations_file_(quotations_file),
     socket_(service, context), rand_(rng, uniform), nick_(nick),
     password_(password), state_(SEND_PASS), interval_(interval) {
  reply_ = new char[MAX_LENGTH];
  request_ = new char[MAX_LENGTH];
  memset(static_cast<void *>(reply_), 0, sizeof(reply_));
  memset(static_cast<void *>(request_), 0, sizeof(request_));
}

IRCRobot::~IRCRobot() {
  delete request_;
  delete reply_;
}

void IRCRobot::Connect(boost::asio::ip::tcp::resolver::iterator
                       endpoint_iterator) {

  boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
  socket_.lowest_layer().async_connect(
      endpoint,
      boost::bind(&IRCRobot::HandleConnect, this,
                  boost::asio::placeholders::error, ++endpoint_iterator));
}

void IRCRobot::HandleConnect(const boost::system::error_code& error,
                             tcp::resolver::iterator
                             endpoint_iterator) {
  if (!error) {
    socket_.async_handshake(boost::asio::ssl::stream_base::client,
                            boost::bind(&IRCRobot::HandleHandshake, this,
                                        boost::asio::placeholders::error));
  } else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
    socket_.lowest_layer().close();
    boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
    socket_.lowest_layer().async_connect(
        endpoint,
        boost::bind(&IRCRobot::HandleConnect, this,
                    boost::asio::placeholders::error, ++endpoint_iterator));
  } else {
    LOG(FATAL) << "Connect failed: " << error;
    exit(1);
  }
}

void IRCRobot::HandleHandshake(const boost::system::error_code& error) {
  if (!error) {
    if (password_.size()) {
      std::string s = "PASS ";
      s.append(password_);
      SendLine(s);
    } else {
      state_ = SEND_NICK;
      std::string s = "NICK ";
      s.append(nick_);
      SendLine(s);
    }
    EstablishReadCallback();
  } else {
    LOG(FATAL) << "Handshake failed: " << error;
  }
}

void IRCRobot::EstablishReadCallback() {
  boost::asio::async_read(socket_,
                          boost::asio::buffer(reply_, MAX_LENGTH),
                          boost::bind(&IRCRobot::ReadCompletedTest, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred),
                          boost::bind(&IRCRobot::HandleRead, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
}

void IRCRobot::SendLine(const std::string &msg) {
  assert(msg.size() < MAX_LENGTH);
  memcpy(request_, msg.c_str(), msg.size());
  request_[msg.size()] = '\n';
  boost::asio::async_write(socket_,
                           boost::asio::buffer(request_, msg.size() + 1),
                           boost::bind(&IRCRobot::HandleWrite, this,
                                       boost::asio::placeholders::error,
                                       boost::asio::placeholders::bytes_transferred));
}

bool IRCRobot::ReadCompletedTest(const boost::system::error_code& error,
                                 size_t bytes_transferred) {
  if (error) {
    LOG(FATAL) << "read error: " << error;
    return true;
  }
  if (bytes_transferred == MAX_LENGTH) {
    reply_[MAX_LENGTH - 1] = '\n';
    return true;
  } else if (bytes_transferred == 0) {
    return false;
  }
  return memrchr(reply_, '\n', bytes_transferred) != NULL;
}

void IRCRobot::HandleRead(const boost::system::error_code& error,
                          size_t bytes_transferred) {
  if (error) {
    LOG(FATAL) << "read error: " << error;
    exit(1);
  }

  bool keep_going = true;
  size_t pos = 0;
  std::string line;
  std::string reply(reply_, bytes_transferred);
  while (keep_going) {
    size_t next = reply.find('\n', pos);
    if (next == std::string::npos) {
      line = reply.substr(pos, next);
      keep_going = false;
    } else {
      line = reply.substr(pos, next - pos);
      pos = next + 1;
    }

    LineCallback(line);

  }
  EstablishReadCallback();
}

{'fraction_non_alphanumeric': 0.14910075420614968, 'fraction_numerical': 0.0042544962289692515, 'mean_word_length': 2.6355008787346224, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6994069', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3145, 'n_words': 558}