0

I am working on programming an IRC server, and am having issues with the overload of operator<<.

I have IrcServer and IrcClient classes, and I want to be able to output a server and a client.

What I want is to get this kind of output :

 nick |  user    |    host  |   server   |   name

With my own client that I use to connect to the server, it works fine, and I get this output :

NICKNAME->mynick | USERNAME->myuser | HOSTNAME->myhost | SERVERNAME->127.0.0.1 | REALNAME->my name

But with irssi, it doesn't work, and I only get this output (NICKNAME is missing):

| USERNAME->myuser | HOSTNAME->myhost | SERVERNAME->127.0.0.1 | REALNAME->my name

This is the code that I use :

ostream_irc_client.hpp :

#ifndef OSTREAM_IRC_CLIENT_HPP
# define OSTREAM_IRC_CLIENT_HPP

# include <iostream>

# include "irc_client.hpp"
# include "../utils/colors.h"

std::ostream &operator << (std::ostream &output, const IrcClient &cl);

#endif

ostream_irc_client.cpp :

#include "ostream_irc_client.hpp"
#include <sstream>

std::ostream &operator << (std::ostream &output , const IrcClient &cl)
{
    output << "NICKNAME->" << cl.getNickName() << " | ";
    output << "USERNAME->" << cl.getUserName() << " | ";
    output << "HOSTNAME->" << cl.getHostName() << " | ";
    output << "SERVERNAME->"<< cl.getServerName() << " | ";
    output << "REALNAME->"<< cl.getRealName() << " | ";
    return (output);
}

ostream_irc_server.hpp :

#ifndef OSTREAM_IRC_SERVER_HPP
# define OSTREAM_IRC_SERVER_HPP

# include <iostream>

# include "irc_server.hpp"
# include "../utils/colors.h"

std::ostream &operator << (std::ostream &output, const IrcServer &s);


#endif

ostream_irc_server.cpp :

#include "ostream_irc_server.hpp"
#include "../irc_client/ostream_irc_client.hpp"

#include <sstream>
#include <map>

std::ostream &operator << (std::ostream &output , const IrcServer &s)
{
    output << BOLD_BLUE << "server:>" << RESET << "\n";
    output << "| NICKNAME | USERNAME | HOSTNAME | SERVERNAME | STATUS |\n";
    std::map<std::string, IrcClient*>::iterator usr;
    for (usr = s.getUsers()->begin(); usr != s.getUsers()->end(); usr++)
    {
        IrcClient *cl = usr->second;
        output << *cl;
    }
    if (s.getServer().getMessages()->size() > 0)
    {
        output << "\nMessages:\n";
        std::vector<Message*>::iterator it;
        for (it = s.getServer().getMessages()->begin(); it != s.getServer().getMessages()->end(); it++)
        {
            Message *msg =  *it;
            output << msg->getData() << "\n";
        }
    }
    return (output);
}
Harou
  • 1
  • 1
  • Welcome to Stack Overflow. Please read our page on [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example). The bug -- if there is one -- does not appear to be in the code you have shown us. – Beta Nov 21 '21 at 17:56
  • Your `for` loops in the `IrcServer` operator are potentially very dangerous, depending on the implementations of `getUsers()`, `getServer()`, and `getMessages()`. You should call each of them only once, either saving the result to a variable, eg: `auto &users = s.getUsers(); for(usr = users.begin(); usr != users.end(); ++usr)` or using a range-for loop: `for(auto &elem : s.getUsers())` Same with `s.getServer().getMessages()`, too – Remy Lebeau Nov 21 '21 at 19:29
  • Thank you , I don't call my getters in loops anymore but still getting the same issue. – Harou Nov 21 '21 at 20:39

0 Answers0