iid.cpp 2.22 KB
Newer Older
louiz’'s avatar
louiz’ committed
1
#include <utils/tolower.hpp>
2
#include <config/config.hpp>
louiz’'s avatar
louiz’ committed
3

4 5
#include <irc/iid.hpp>

louiz’'s avatar
louiz’ committed
6 7 8
Iid::Iid(const std::string& iid):
  is_channel(false),
  is_user(false)
9 10 11 12 13 14 15 16 17 18
{
  const std::string fixed_irc_server = Config::get("fixed_irc_server", "");
  if (fixed_irc_server.empty())
    this->init(iid);
  else
    this->init_with_fixed_server(iid, fixed_irc_server);
}


void Iid::init(const std::string& iid)
19
{
louiz’'s avatar
louiz’ committed
20
  const std::string::size_type sep = iid.find_first_of("%!");
21 22
  if (sep != std::string::npos)
    {
louiz’'s avatar
louiz’ committed
23 24 25 26 27 28
      if (iid[sep] == '%')
        this->is_channel = true;
      else
        this->is_user = true;
      this->set_local(iid.substr(0, sep));
      this->set_server(iid.substr(sep + 1));
29 30
    }
  else
louiz’'s avatar
louiz’ committed
31 32 33
    this->set_server(iid);
}

34 35 36 37
void Iid::init_with_fixed_server(const std::string& iid, const std::string& hostname)
{
  this->set_server(hostname);

38
  const std::string::size_type sep = iid.find("!");
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

  // Without any separator, we consider that it's a channel
  if (sep == std::string::npos)
    {
      this->is_channel = true;
      this->set_local(iid);
    }
  else // A separator can be present to differenciate a channel from a user,
       // but the part behind it (the hostname) is ignored
    {
      this->set_local(iid.substr(0, sep));
        this->is_user = true;
    }
}

54 55 56 57 58 59 60 61
Iid::Iid(const Iid& other):
  is_channel(other.is_channel),
  is_user(other.is_user),
  local(other.local),
  server(other.server)
{
}

louiz’'s avatar
louiz’ committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Iid::Iid():
  is_channel(false),
  is_user(false)
{
}

void Iid::set_local(const std::string& loc)
{
  this->local = utils::tolower(loc);
}

void Iid::set_server(const std::string& serv)
{
  this->server = utils::tolower(serv);
}

const std::string& Iid::get_local() const
{
  return this->local;
81
}
louiz’'s avatar
louiz’ committed
82

louiz’'s avatar
louiz’ committed
83
const std::string& Iid::get_server() const
louiz’'s avatar
louiz’ committed
84
{
louiz’'s avatar
louiz’ committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  return this->server;
}

std::string Iid::get_sep() const
{
  if (this->is_channel)
    return "%";
  else if (this->is_user)
    return "!";
  return "";
}

namespace std {
  const std::string to_string(const Iid& iid)
  {
100 101 102 103 104 105 106 107 108
    if (Config::get("fixed_irc_server", "").empty())
      return iid.get_local() + iid.get_sep() + iid.get_server();
    else
      {
        if (iid.get_sep() == "!")
          return iid.get_local() + iid.get_sep();
        else
          return iid.get_local();
      }
louiz’'s avatar
louiz’ committed
109
  }
louiz’'s avatar
louiz’ committed
110
}