summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2013-11-12 23:47:00 +0100
committerFlorent Le Coz <louiz@louiz.org>2013-11-12 23:48:10 +0100
commit3cfaab9a2debe03829b1ff26fe94e775e1d18e0a (patch)
tree9776772d45ed47f69018245e8e333e82f80fcab9 /src
parent5817a95b5ee89480788832be35679dfcd2ed833b (diff)
downloadbiboumi-3cfaab9a2debe03829b1ff26fe94e775e1d18e0a.tar.gz
biboumi-3cfaab9a2debe03829b1ff26fe94e775e1d18e0a.tar.bz2
biboumi-3cfaab9a2debe03829b1ff26fe94e775e1d18e0a.tar.xz
biboumi-3cfaab9a2debe03829b1ff26fe94e775e1d18e0a.zip
Map irc commands to callbacks, in a clean way
Diffstat (limited to 'src')
-rw-r--r--src/irc/irc_client.cpp30
-rw-r--r--src/irc/irc_client.hpp24
2 files changed, 28 insertions, 26 deletions
diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp
index 0d8c614..1d2e487 100644
--- a/src/irc/irc_client.cpp
+++ b/src/irc/irc_client.cpp
@@ -74,31 +74,11 @@ void IrcClient::parse_in_buffer()
IrcMessage message(this->in_buf.substr(0, pos));
this->in_buf = this->in_buf.substr(pos + 2, std::string::npos);
std::cout << message << std::endl;
- // TODO map function and command name properly
- if (message.command == "PING")
- this->send_pong_command(message);
- else if (message.command == "NOTICE" ||
- message.command == "375" ||
- message.command == "372")
- this->forward_server_message(message);
- else if (message.command == "JOIN")
- this->on_channel_join(message);
- else if (message.command == "PRIVMSG")
- this->on_channel_message(message);
- else if (message.command == "353")
- this->set_and_forward_user_list(message);
- else if (message.command == "332")
- this->on_topic_received(message);
- else if (message.command == "366")
- this->on_channel_completely_joined(message);
- else if (message.command == "001")
- this->on_welcome_message(message);
- else if (message.command == "PART")
- this->on_part(message);
- else if (message.command == "QUIT")
- this->on_quit(message);
- else if (message.command == "NICK")
- this->on_nick(message);
+ auto cb = irc_callbacks.find(message.command);
+ if (cb != irc_callbacks.end())
+ (this->*(cb->second))(message);
+ else
+ std::cout << "No handler for command " << message.command << std::endl;
}
}
diff --git a/src/irc/irc_client.hpp b/src/irc/irc_client.hpp
index 722f850..07ff02c 100644
--- a/src/irc/irc_client.hpp
+++ b/src/irc/irc_client.hpp
@@ -167,11 +167,33 @@ private:
*/
std::vector<std::string> channels_to_join;
bool welcomed;
-
IrcClient(const IrcClient&) = delete;
IrcClient(IrcClient&&) = delete;
IrcClient& operator=(const IrcClient&) = delete;
IrcClient& operator=(IrcClient&&) = delete;
};
+/**
+ * Define a map of functions to be called for each IRC command we can
+ * handle.
+ */
+typedef void (IrcClient::*irc_callback_t)(const IrcMessage&);
+
+static const std::unordered_map<std::string, irc_callback_t> irc_callbacks = {
+ {"NOTICE", &IrcClient::forward_server_message},
+ {"375", &IrcClient::forward_server_message},
+ {"372", &IrcClient::forward_server_message},
+ {"JOIN", &IrcClient::on_channel_join},
+ {"PRIVMSG", &IrcClient::on_channel_message},
+ {"353", &IrcClient::set_and_forward_user_list},
+ {"332", &IrcClient::on_topic_received},
+ {"366", &IrcClient::on_channel_completely_joined},
+ {"001", &IrcClient::on_welcome_message},
+ {"PART", &IrcClient::on_part},
+ {"QUIT", &IrcClient::on_quit},
+ {"NICK", &IrcClient::on_nick},
+ {"MODE", &IrcClient::on_mode},
+ {"PING", &IrcClient::send_pong_command},
+};
+
#endif // IRC_CLIENT_INCLUDED