summaryrefslogtreecommitdiff
path: root/src/irc/iid.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/irc/iid.cpp')
-rw-r--r--src/irc/iid.cpp111
1 files changed, 56 insertions, 55 deletions
diff --git a/src/irc/iid.cpp b/src/irc/iid.cpp
index 0e2841e..d442013 100644
--- a/src/irc/iid.cpp
+++ b/src/irc/iid.cpp
@@ -1,62 +1,72 @@
#include <utils/tolower.hpp>
#include <config/config.hpp>
-
+#include <bridge/bridge.hpp>
#include <irc/iid.hpp>
#include <utils/encoding.hpp>
-Iid::Iid(const std::string& iid):
- is_channel(false),
- is_user(false)
+constexpr char Iid::separator[];
+
+Iid::Iid(const std::string& local, const std::string& server, Iid::Type type):
+ type(type),
+ local(local),
+ server(server)
{
- 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);
}
+Iid::Iid(const std::string& iid, const std::set<char>& chantypes)
+{
+ this->init(iid);
+ this->set_type(std::set<char>(chantypes));
+}
-void Iid::init(const std::string& iid)
+Iid::Iid(const std::string& iid, const std::initializer_list<char>& chantypes):
+ Iid(iid, std::set<char>(chantypes))
{
- const std::string::size_type sep = iid.find_first_of("%!");
- if (sep != std::string::npos)
- {
- 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));
- }
- else
- this->set_server(iid);
}
-void Iid::init_with_fixed_server(const std::string& iid, const std::string& hostname)
+Iid::Iid(const std::string& iid, const Bridge *bridge)
+{
+ this->init(iid);
+ const auto chantypes = bridge->get_chantypes(this->server);
+ this->set_type(chantypes);
+}
+
+void Iid::set_type(const std::set<char>& chantypes)
{
- this->set_server(hostname);
+ if (this->local.empty())
+ return;
- const std::string::size_type sep = iid.find("!");
+ if (chantypes.count(this->local[0]) == 1)
+ this->type = Iid::Type::Channel;
+ else
+ this->type = Iid::Type::User;
+}
+
+void Iid::init(const std::string& iid)
+{
+ const std::string fixed_irc_server = Config::get("fixed_irc_server", "");
- // Without any separator, we consider that it's a channel
- if (sep == std::string::npos)
+ if (fixed_irc_server.empty())
+ {
+ const std::string::size_type sep = iid.find('%');
+ if (sep != std::string::npos)
{
- this->is_channel = true;
- this->set_local(iid);
+ this->set_local(iid.substr(0, sep));
+ this->set_server(iid.substr(sep + 1));
+ this->type = Iid::Type::Channel;
}
- else // A separator can be present to differenciate a channel from a user,
- // but the part behind it (the hostname) is ignored
+ else
{
- this->set_local(iid.substr(0, sep));
- this->is_user = true;
+ this->set_server(iid);
+ this->type = Iid::Type::Server;
}
-}
-
-Iid::Iid():
- is_channel(false),
- is_user(false)
-{
+ }
+ else
+ {
+ this->set_server(fixed_irc_server);
+ this->set_local(iid);
+ }
}
void Iid::set_local(const std::string& loc)
@@ -88,27 +98,18 @@ const std::string& Iid::get_server() const
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)
{
if (Config::get("fixed_irc_server", "").empty())
- return iid.get_encoded_local() + iid.get_sep() + iid.get_server();
+ {
+ if (iid.type == Iid::Type::Server)
+ return iid.get_server();
+ else
+ return iid.get_encoded_local() + iid.separator + iid.get_server();
+ }
else
- {
- if (iid.get_sep() == "!")
- return iid.get_encoded_local() + iid.get_sep();
- else
- return iid.get_encoded_local();
- }
+ return iid.get_encoded_local();
}
}