diff --git a/NETWORKING.md b/NETWORKING.md
index 6348be058..a51b7b75b 100644
--- a/NETWORKING.md
+++ b/NETWORKING.md
@@ -63,6 +63,9 @@ The current server configuration xml looks like this:
+
+
+
diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp
index f17bc4884..073f89eed 100644
--- a/src/network/protocols/server_lobby.cpp
+++ b/src/network/protocols/server_lobby.cpp
@@ -699,19 +699,29 @@ void ServerLobby::handleChat(Event* event)
event->getPeer()->updateLastActivity();
const bool sender_in_game = event->getPeer()->isWaitingForGame();
- int last_message = event->getPeer()->getLastMessage();
- int elipsed_time = StkTime::getMonoTimeMs() - last_message;
+ int64_t last_message = event->getPeer()->getLastMessage();
+ int64_t elapsed_time = (int64_t)StkTime::getMonoTimeMs() - last_message;
- // Increment consecutive_messages if last message is less than 5s ago
- if (elipsed_time < 5000)
+ // Read ServerConfig for formula and details
+ if (ServerConfig::m_chat_consecutive_interval > 0 &&
+ elapsed_time < ServerConfig::m_chat_consecutive_interval * 1000)
event->getPeer()->updateConsecutiveMessages(true);
else
event->getPeer()->updateConsecutiveMessages(false);
- // Ignore message if there is already 3 consecutive messages
- if (event->getPeer()->getConsecutiveMessages() >= 3)
+ if (ServerConfig::m_chat_consecutive_interval > 0 &&
+ event->getPeer()->getConsecutiveMessages() >
+ ServerConfig::m_chat_consecutive_interval / 2)
+ {
+ NetworkString* chat = getNetworkString();
+ chat->setSynchronous(true);
+ core::stringw warn = "Spam detected";
+ chat->addUInt8(LE_CHAT).encodeString16(warn);
+ event->getPeer()->sendPacket(chat, true/*reliable*/);
+ delete chat;
return;
-
+ }
+
core::stringw message;
event->data().decodeString16(&message, 360/*max_len*/);
if (message.size() > 0)
diff --git a/src/network/server_config.hpp b/src/network/server_config.hpp
index f7caa4438..487f1fa20 100644
--- a/src/network/server_config.hpp
+++ b/src/network/server_config.hpp
@@ -149,6 +149,12 @@ namespace ServerConfig
"If this value is set to false, the server will ignore chat messages "
"from all players."));
+ SERVER_CFG_PREFIX IntServerConfigParam m_chat_consecutive_interval
+ SERVER_CFG_DEFAULT(IntServerConfigParam(8, "chat-consecutive-interval",
+ "If client sends more than chat-consecutive-interval / 2 chats within "
+ "this value (read in seconds), it will be ignore, negative value to "
+ "disable."));
+
SERVER_CFG_PREFIX BoolServerConfigParam m_track_voting
SERVER_CFG_DEFAULT(BoolServerConfigParam(true, "track-voting",
"Allow players to vote for which track to play. If this value is set "
diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp
index 27710dc97..5796cc3f7 100644
--- a/src/network/stk_peer.hpp
+++ b/src/network/stk_peer.hpp
@@ -281,19 +281,18 @@ public:
void updateLastMessage()
{ m_last_message.store((int64_t)StkTime::getMonoTimeMs()); }
// ------------------------------------------------------------------------
- int getLastMessage()
+ int64_t getLastMessage() const
{ return m_last_message; }
// ------------------------------------------------------------------------
- void updateConsecutiveMessages(bool tooFast)
+ void updateConsecutiveMessages(bool too_fast)
{
- if (tooFast)
+ if (too_fast)
m_consecutive_messages++;
else
m_consecutive_messages = 0;
}
// ------------------------------------------------------------------------
- int getConsecutiveMessages()
- { return m_consecutive_messages; }
+ int getConsecutiveMessages() const { return m_consecutive_messages; }
}; // STKPeer
#endif // STK_PEER_HPP