From e1044537724a9307ae16e4d02cc6261507852b24 Mon Sep 17 00:00:00 2001 From: lotodore Date: Thu, 20 Aug 2009 15:19:41 +0000 Subject: [PATCH] Make that two bots. One for administration and one to forward the lobby chat. --- pokerth_lib.pro | 6 +- .../{serverircbot.cpp => serveradminbot.cpp} | 124 ++++++++---------- src/net/common/servermanager.cpp | 35 +++-- src/net/{serverircbot.h => serveradminbot.h} | 18 ++- src/net/servermanager.h | 11 +- src/session.cpp | 20 ++- 6 files changed, 113 insertions(+), 101 deletions(-) rename src/net/common/{serverircbot.cpp => serveradminbot.cpp} (63%) rename src/net/{serverircbot.h => serveradminbot.h} (88%) diff --git a/pokerth_lib.pro b/pokerth_lib.pro index 93225071..123f0fd0 100644 --- a/pokerth_lib.pro +++ b/pokerth_lib.pro @@ -81,7 +81,8 @@ HEADERS += \ src/net/serverlobbythread.h \ src/net/serverbanmanager.h \ src/net/servercallback.h \ - src/net/serverircbot.h \ + src/net/serveradminbot.h \ + src/net/serverlobbybot.h \ src/net/serverircbotcallback.h \ src/net/sessiondata.h \ src/net/sessiondatacallback.h \ @@ -185,7 +186,8 @@ SOURCES += \ src/net/common/serverlobbythread.cpp \ src/net/common/serverbanmanager.cpp \ src/net/common/servercallback.cpp \ - src/net/common/serverircbot.cpp \ + src/net/common/serveradminbot.cpp \ + src/net/common/serverlobbybot.cpp \ src/net/common/serverircbotcallback.cpp \ src/net/common/sessiondata.cpp \ src/net/common/sessiondatacallback.cpp \ diff --git a/src/net/common/serverircbot.cpp b/src/net/common/serveradminbot.cpp similarity index 63% rename from src/net/common/serverircbot.cpp rename to src/net/common/serveradminbot.cpp index 31a77caf..dd91c336 100644 --- a/src/net/common/serverircbot.cpp +++ b/src/net/common/serveradminbot.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2007 by Lothar May * + * Copyright (C) 2009 by Lothar May * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -17,7 +17,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include +#include #include #include #include @@ -33,38 +33,38 @@ using namespace std; -ServerIrcBot::ServerIrcBot() +ServerAdminBot::ServerAdminBot() { } -ServerIrcBot::~ServerIrcBot() +ServerAdminBot::~ServerAdminBot() { } void -ServerIrcBot::Init(boost::shared_ptr lobbyThread, boost::shared_ptr ircThread) +ServerAdminBot::Init(boost::shared_ptr lobbyThread, boost::shared_ptr ircAdminThread) { m_lobbyThread = lobbyThread; - m_ircThread = ircThread; + m_ircAdminThread = ircAdminThread; } void -ServerIrcBot::SignalIrcConnect(const std::string &server) +ServerAdminBot::SignalIrcConnect(const std::string &server) { - LOG_MSG("Connected to IRC server " << server << "."); + LOG_MSG("Admin bot: Connected to IRC server " << server << "."); } void -ServerIrcBot::SignalIrcSelfJoined(const std::string &nickName, const std::string &channel) +ServerAdminBot::SignalIrcSelfJoined(const std::string &nickName, const std::string &channel) { - LOG_MSG("Joined IRC channel " << channel << " as user " << nickName << "."); + LOG_MSG("Admin bot: Joined IRC channel " << channel << " as user " << nickName << "."); m_ircNick = nickName; } void -ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &msg) +ServerAdminBot::SignalIrcChatMsg(const std::string &nickName, const std::string &msg) { - if (m_ircThread) + if (m_ircAdminThread) { try { @@ -83,15 +83,15 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m if (!playerName.empty()) { if (GetLobbyThread().KickPlayerByName(playerName)) - m_ircThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server."); + m_ircAdminThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server."); else - m_ircThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server."); + m_ircAdminThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server."); } } else if (command == "cleaner-reconnect") { GetLobbyThread().ReconnectChatBot(); - m_ircThread->SendChatMessage(nickName + ": Cleaner bot reconnect initiated."); + m_ircAdminThread->SendChatMessage(nickName + ": Cleaner bot reconnect initiated."); } else if (command == "showip") { @@ -102,9 +102,9 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m { string ipAddress(GetLobbyThread().GetPlayerIPAddress(playerName)); if (!ipAddress.empty()) - m_ircThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is: \"" + ipAddress + "\""); + m_ircAdminThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is: \"" + ipAddress + "\""); else - m_ircThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is unknown."); + m_ircAdminThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is unknown."); } } else if (command == "bannick") @@ -115,7 +115,7 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m if (!playerRegex.empty()) { GetLobbyThread().GetBanManager().BanPlayerRegex(playerRegex); - m_ircThread->SendChatMessage(nickName + ": The regex \"" + playerRegex + "\" was added to the player ban list."); + m_ircAdminThread->SendChatMessage(nickName + ": The regex \"" + playerRegex + "\" was added to the player ban list."); } } else if (command == "banip") @@ -134,7 +134,7 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m ostringstream durationStr; durationStr << durationHours; GetLobbyThread().GetBanManager().BanIPAddress(ipAddress, durationHours); - m_ircThread->SendChatMessage(nickName + ": The IP address \"" + ipAddress + "\" was added to the IP address ban list for " + durationStr.str() + (durationHours == 1 ? " hour." : " hours.")); + m_ircAdminThread->SendChatMessage(nickName + ": The IP address \"" + ipAddress + "\" was added to the IP address ban list for " + durationStr.str() + (durationHours == 1 ? " hour." : " hours.")); } } else if (command == "listban") @@ -145,27 +145,27 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m list::const_iterator end = banList.end(); while (i != end) { - m_ircThread->SendChatMessage(*i); + m_ircAdminThread->SendChatMessage(*i); ++i; } ostringstream banListStream; banListStream << nickName << ": Total count of bans: " << banList.size(); - m_ircThread->SendChatMessage(banListStream.str()); + m_ircAdminThread->SendChatMessage(banListStream.str()); } else if (command == "removeban") { unsigned banId = 0; msgStream >> banId; if (GetLobbyThread().GetBanManager().UnBan(banId)) - m_ircThread->SendChatMessage(nickName + ": The ban was successfully removed."); + m_ircAdminThread->SendChatMessage(nickName + ": The ban was successfully removed."); else - m_ircThread->SendChatMessage(nickName + ": This ban does not exist."); + m_ircAdminThread->SendChatMessage(nickName + ": This ban does not exist."); } else if (command == "clearban") { GetLobbyThread().GetBanManager().ClearBanList(); - m_ircThread->SendChatMessage(nickName + ": All ban lists were cleared."); + m_ircAdminThread->SendChatMessage(nickName + ": All ban lists were cleared."); } else if (command == "stat") { @@ -175,33 +175,33 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m ostringstream statStream; statStream << "Server uptime................ " << timeDiff.hours() / 24 << " days " << timeDiff.hours() % 24 << " hours " << timeDiff.minutes() << " minutes " << timeDiff.seconds() << " seconds"; - m_ircThread->SendChatMessage(statStream.str()); + m_ircAdminThread->SendChatMessage(statStream.str()); } { ostringstream statStream; statStream << "Players currently on Server.. " << tmpStats.numberOfPlayersOnServer; - m_ircThread->SendChatMessage(statStream.str()); + m_ircAdminThread->SendChatMessage(statStream.str()); } { ostringstream statStream; statStream << "Games currently open......... " << tmpStats.numberOfGamesOpen; - m_ircThread->SendChatMessage(statStream.str()); + m_ircAdminThread->SendChatMessage(statStream.str()); } { ostringstream statStream; statStream << "Total players ever logged in. " << tmpStats.totalPlayersEverLoggedIn << " (Max at a time: " << tmpStats.maxPlayersLoggedIn << ")"; - m_ircThread->SendChatMessage(statStream.str()); + m_ircAdminThread->SendChatMessage(statStream.str()); } { ostringstream statStream; statStream << "Total games ever open........ " << tmpStats.totalGamesEverCreated << " (Max at a time: " << tmpStats.maxGamesOpen << ")"; - m_ircThread->SendChatMessage(statStream.str()); + m_ircAdminThread->SendChatMessage(statStream.str()); } } else if (command == "chat") @@ -212,10 +212,10 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m if (!chat.empty() && chat.size() < MAX_CHAT_TEXT_SIZE) { GetLobbyThread().SendGlobalChat(chat); - m_ircThread->SendChatMessage(nickName + ": Global chat message sent."); + m_ircAdminThread->SendChatMessage(nickName + ": Global chat message sent."); } else - m_ircThread->SendChatMessage(nickName + ": Invalid message."); + m_ircAdminThread->SendChatMessage(nickName + ": Invalid message."); } else if (command == "msg") { @@ -225,67 +225,53 @@ ServerIrcBot::SignalIrcChatMsg(const std::string &nickName, const std::string &m if (!message.empty() && message.size() < MAX_CHAT_TEXT_SIZE) { GetLobbyThread().SendGlobalMsgBox(message); - m_ircThread->SendChatMessage(nickName + ": Global message box sent."); + m_ircAdminThread->SendChatMessage(nickName + ": Global message box sent."); } else - m_ircThread->SendChatMessage(nickName + ": Invalid message."); + m_ircAdminThread->SendChatMessage(nickName + ": Invalid message."); } else - m_ircThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\"."); + m_ircAdminThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\"."); } } catch (...) { - m_ircThread->SendChatMessage(nickName + ": Syntax error. Please check the command."); + m_ircAdminThread->SendChatMessage(nickName + ": Syntax error. Please check the command."); } } } void -ServerIrcBot::SignalIrcError(int errorCode) +ServerAdminBot::SignalIrcError(int errorCode) { - LOG_MSG("IRC error " << errorCode << "."); + LOG_MSG("Admin bot: IRC error " << errorCode << "."); } void -ServerIrcBot::SignalIrcServerError(int errorCode) +ServerAdminBot::SignalIrcServerError(int errorCode) { - LOG_MSG("IRC server error " << errorCode << "."); + LOG_MSG("Admin bot: IRC server error " << errorCode << "."); } void -ServerIrcBot::SignalLobbyMessage(unsigned playerId, const std::string &playerName, const std::string &msg) +ServerAdminBot::Run() { - if (m_ircThread && !msg.empty()) - { - ostringstream ircMsg; - if (playerId) - ircMsg << playerName << " (" << playerId << "): " << msg; - else - ircMsg << playerName << ": " << msg; - m_ircThread->SendChatMessage(ircMsg.str()); - } + if (m_ircAdminThread) + m_ircAdminThread->Run(); } void -ServerIrcBot::Run() -{ - if (m_ircThread) - m_ircThread->Run(); -} - -void -ServerIrcBot::Process() +ServerAdminBot::Process() { if (m_ircRestartTimer.elapsed().total_seconds() > SERVER_RESTART_IRC_BOT_INTERVAL_SEC) { - if (m_ircThread) + if (m_ircAdminThread) { - m_ircThread->SignalTermination(); - if (m_ircThread->Join(NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC)) + m_ircAdminThread->SignalTermination(); + if (m_ircAdminThread->Join(NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC)) { - boost::shared_ptr tmpIrcThread(new IrcThread(*m_ircThread)); + boost::shared_ptr tmpIrcThread(new IrcThread(*m_ircAdminThread)); tmpIrcThread->Run(); - m_ircThread = tmpIrcThread; + m_ircAdminThread = tmpIrcThread; } } m_ircRestartTimer.reset(); @@ -294,23 +280,23 @@ ServerIrcBot::Process() } void -ServerIrcBot::SignalTermination() +ServerAdminBot::SignalTermination() { - if (m_ircThread) - m_ircThread->SignalTermination(); + if (m_ircAdminThread) + m_ircAdminThread->SignalTermination(); } bool -ServerIrcBot::Join(bool wait) +ServerAdminBot::Join(bool wait) { bool terminated = true; - if (m_ircThread) - terminated = m_ircThread->Join(wait ? NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC : 0); + if (m_ircAdminThread) + terminated = m_ircAdminThread->Join(wait ? NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC : 0); return terminated; } ServerLobbyThread & -ServerIrcBot::GetLobbyThread() +ServerAdminBot::GetLobbyThread() { assert(m_lobbyThread.get()); return *m_lobbyThread; diff --git a/src/net/common/servermanager.cpp b/src/net/common/servermanager.cpp index 415526e5..244bebdc 100644 --- a/src/net/common/servermanager.cpp +++ b/src/net/common/servermanager.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -36,7 +35,8 @@ ServerManager::ServerManager(GuiInterface &gui, ConfigFile *config, AvatarManage : m_gui(gui), m_playerConfig(config), m_avatarManager(avatarManager) { m_ioService.reset(new boost::asio::io_service); - m_ircBot.reset(new ServerIrcBot); + m_adminBot.reset(new ServerAdminBot); + m_lobbyBot.reset(new ServerLobbyBot); } ServerManager::~ServerManager() @@ -44,12 +44,13 @@ ServerManager::~ServerManager() } void -ServerManager::Init(unsigned serverPort, bool ipv6, ServerNetworkMode mode, const string &pwd, const string &logDir, boost::shared_ptr ircThread) +ServerManager::Init(unsigned serverPort, bool ipv6, ServerNetworkMode mode, const string &pwd, const string &logDir, boost::shared_ptr ircAdminThread, boost::shared_ptr ircLobbyThread) { - m_lobbyThread.reset(new ServerLobbyThread(GetGui(), *m_ircBot, m_playerConfig, m_avatarManager, m_ioService)); + m_lobbyThread.reset(new ServerLobbyThread(GetGui(), *m_lobbyBot, m_playerConfig, m_avatarManager, m_ioService)); GetLobbyThread().Init(pwd, logDir); - m_ircBot->Init(m_lobbyThread, ircThread); + m_adminBot->Init(m_lobbyThread, ircAdminThread); + m_lobbyBot->Init(m_lobbyThread, ircLobbyThread); if (mode & NETWORK_MODE_TCP) { @@ -71,36 +72,46 @@ ServerManager::GetGui() return m_gui; } -ServerIrcBot & -ServerManager::GetIrcBot() +ServerAdminBot & +ServerManager::GetAdminBot() { - return *m_ircBot; + return *m_adminBot; +} + +ServerLobbyBot & +ServerManager::GetLobbyBot() +{ + return *m_lobbyBot; } void ServerManager::RunAll() { - m_ircBot->Run(); + m_adminBot->Run(); + m_lobbyBot->Run(); GetLobbyThread().Run(); } void ServerManager::Process() { - m_ircBot->Process(); + m_adminBot->Process(); + m_lobbyBot->Process(); } void ServerManager::SignalTerminationAll() { - m_ircBot->SignalTermination(); + m_adminBot->SignalTermination(); + m_lobbyBot->SignalTermination(); GetLobbyThread().SignalTermination(); } bool ServerManager::JoinAll(bool wait) { - m_ircBot->Join(wait); + m_adminBot->Join(wait); + m_lobbyBot->Join(wait); return GetLobbyThread().Join(wait ? NET_LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC : 0); } diff --git a/src/net/serverircbot.h b/src/net/serveradminbot.h similarity index 88% rename from src/net/serverircbot.h rename to src/net/serveradminbot.h index bbbc5f23..dd1af229 100644 --- a/src/net/serverircbot.h +++ b/src/net/serveradminbot.h @@ -16,10 +16,10 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -/* IRC bot for the server. */ +/* IRC admin bot for the server. */ -#ifndef _SERVERIRCBOT_H_ -#define _SERVERIRCBOT_H_ +#ifndef _SERVERADMINBOT_H_ +#define _SERVERADMINBOT_H_ #include #include @@ -37,13 +37,13 @@ class ConfigFile; class AvatarManager; class IrcThread; -class ServerIrcBot : public IrcCallback, public ServerIrcBotCallback +class ServerAdminBot : public IrcCallback { public: - ServerIrcBot(); - virtual ~ServerIrcBot(); + ServerAdminBot(); + virtual ~ServerAdminBot(); - void Init(boost::shared_ptr lobbyThread, boost::shared_ptr ircThread); + void Init(boost::shared_ptr lobbyThread, boost::shared_ptr ircAdminThread); // Main start function. void Run(); @@ -64,8 +64,6 @@ public: virtual void SignalIrcError(int errorCode); virtual void SignalIrcServerError(int errorCode); - virtual void SignalLobbyMessage(unsigned playerId, const std::string &playerName, const std::string &msg); - protected: ServerLobbyThread &GetLobbyThread(); @@ -73,7 +71,7 @@ private: std::string m_ircNick; boost::shared_ptr m_lobbyThread; - boost::shared_ptr m_ircThread; + boost::shared_ptr m_ircAdminThread; boost::timers::portable::microsec_timer m_ircRestartTimer; }; diff --git a/src/net/servermanager.h b/src/net/servermanager.h index 2715d36d..b5fec8ef 100644 --- a/src/net/servermanager.h +++ b/src/net/servermanager.h @@ -22,7 +22,8 @@ #define _SERVERMANAGER_H_ #include -#include +#include +#include #include #include @@ -43,7 +44,7 @@ public: virtual ~ServerManager(); // Set the parameters. - void Init(unsigned serverPort, bool ipv6, ServerNetworkMode mode, const std::string &pwd, const std::string &logDir, boost::shared_ptr ircThread); + void Init(unsigned serverPort, bool ipv6, ServerNetworkMode mode, const std::string &pwd, const std::string &logDir, boost::shared_ptr ircAdminThread, boost::shared_ptr ircLobbyThread); // Main start function. void RunAll(); @@ -55,7 +56,8 @@ public: bool JoinAll(bool wait); GuiInterface &GetGui(); - ServerIrcBot &GetIrcBot(); + ServerAdminBot &GetAdminBot(); + ServerLobbyBot &GetLobbyBot(); protected: typedef std::list > AcceptHelperList; @@ -69,7 +71,8 @@ private: boost::shared_ptr m_ioService; boost::shared_ptr m_lobbyThread; - boost::shared_ptr m_ircBot; + boost::shared_ptr m_adminBot; + boost::shared_ptr m_lobbyBot; AcceptHelperList m_acceptHelperPool; }; diff --git a/src/session.cpp b/src/session.cpp index 23b7c8e3..eed8e5bc 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -276,12 +276,23 @@ void Session::startNetworkServer() myNetServer.reset(new ServerManager(*myGui, myConfig, *myAvatarManager)); - boost::shared_ptr tmpIrcThread; + boost::shared_ptr tmpIrcAdminThread; + boost::shared_ptr tmpIrcLobbyThread; if (myConfig->readConfigInt("UseAdminIRC")) { - tmpIrcThread = boost::shared_ptr(new IrcThread(&myNetServer->GetIrcBot())); + tmpIrcAdminThread = boost::shared_ptr(new IrcThread(&myNetServer->GetAdminBot())); - tmpIrcThread->Init( + tmpIrcAdminThread->Init( + myConfig->readConfigString("AdminIRCServerAddress"), + myConfig->readConfigInt("AdminIRCServerPort"), + myConfig->readConfigInt("AdminIRCServerUseIpv6") == 1, + myConfig->readConfigString("AdminIRCServerNick"), + myConfig->readConfigString("AdminIRCChannel"), + myConfig->readConfigString("AdminIRCChannelPassword")); + + tmpIrcLobbyThread = boost::shared_ptr(new IrcThread(&myNetServer->GetLobbyBot())); + + tmpIrcLobbyThread->Init( myConfig->readConfigString("AdminIRCServerAddress"), myConfig->readConfigInt("AdminIRCServerPort"), myConfig->readConfigInt("AdminIRCServerUseIpv6") == 1, @@ -296,7 +307,8 @@ void Session::startNetworkServer() myConfig->readConfigInt("ServerUseSctp") == 1 ? NETWORK_MODE_TCP_SCTP : NETWORK_MODE_TCP, myConfig->readConfigString("ServerPassword"), myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir")), - tmpIrcThread + tmpIrcAdminThread, + tmpIrcLobbyThread ); myNetServer->RunAll();