C++ IRC Bot Buffer Error

185 views Asked by At
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
#include <cstdlib>
#include <fstream>
#include <pthread.h>
#include <ostream>
#include <unistd.h>
#include <boost/array.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <array>
#define NUM_THREADS 3

void *Bot(void *threadid)
{
 while(1)
 {
  using boost::asio::ip::tcp;
  try {
   std::ofstream store;
   boost::asio::io_service io_service;
   tcp::resolver resolver(io_service);
   tcp::resolver::query query("###########", "6667");
   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
   tcp::resolver::iterator end;
   tcp::socket socket(io_service);
   boost::system::error_code error = boost::asio::error::host_not_found;
   while (error && endpoint_iterator != end)
   {
    socket.close();
    socket.connect(*endpoint_iterator++, error);
   }
   if (error)
    throw boost::system::system_error(error);
   sleep(5);
   boost::system::error_code ignored_error;
   boost::asio::streambuf request;
   std::ostream send_stream(&request);
   send_stream << "USER Cribot * * :Cribot\r\nNICK Cribot\r\nJOIN #hide\r\nPRIVMSG #hide :
              Hello master, your wish is my command.\r\n";
   boost::asio::write(socket, request, ignored_error);
   while(1)
   {
    store.open("IRC.log", std::ios::app);
    static std::string lel;
    boost::system::error_code error;
    if (error == boost::asio::error::eof)
     break;
    else if (error)
     throw boost::system::system_error(error);
    boost::array<char, 128> buf;
    size_t len = socket.read_some(boost::asio::buffer(buf), error);
    if(lel == "PING")
    {
     boost::system::error_code ignored_error;
     boost::asio::streambuf request;
     std::ostream send_stream(&request);
     send_stream << "PONG\r\n";
     boost::asio::write(socket, request, ignored_error);
    }
    store.write(buf.data(), len); # THIS ENTERS NOTHING IN MY FILE
    std::cout.write(buf.data(), len); #THIS WORKS
    store.close();
   }
  } catch (std::exception &e) {}
 }
}

int main()
{
 pthread_t threads[NUM_THREADS];
 pthread_create(&threads[1], NULL, Bot, (void *)1);
 pthread_join(threads[1], NULL);
 return 0;
}

Hello, basically I'm making a simple IRC bot that sits in my channel and logs everything that happens, I made it, it works but my only issue is I can't write the chat into a file "store" but using the same code, I'm able to print what's coming from the IRC server

0

There are 0 answers