Skip to content

Commit d1c27c7

Browse files
ClobberXDrubenwardy
authored andcommittedAug 8, 2019
Allow customizing chat message format (#8529)
1 parent cc610c7 commit d1c27c7

File tree

10 files changed

+111
-47
lines changed

10 files changed

+111
-47
lines changed
 

‎builtin/game/chatcommands.lua ‎builtin/game/chat.lua

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
-- Minetest: builtin/game/chatcommands.lua
1+
-- Minetest: builtin/game/chat.lua
2+
3+
--
4+
-- Chat message formatter
5+
--
6+
7+
-- Implemented in Lua to allow redefinition
8+
function core.format_chat_message(name, message)
9+
local str = core.settings:get("chat_message_format")
10+
local error_str = "Invalid chat message format - missing %s"
11+
local i
12+
13+
str, i = str:gsub("@name", name, 1)
14+
if i == 0 then
15+
error(error_str:format("@name"), 2)
16+
end
17+
18+
str, i = str:gsub("@message", message, 1)
19+
if i == 0 then
20+
error(error_str:format("@message"), 2)
21+
end
22+
23+
str = str:gsub("@timestamp", os.date("%H:%M:%S", os.time()), 1)
24+
25+
return str
26+
end
227

328
--
429
-- Chat command handler

‎builtin/game/init.lua

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11

22
local scriptpath = core.get_builtin_path()
3-
local commonpath = scriptpath.."common"..DIR_DELIM
4-
local gamepath = scriptpath.."game"..DIR_DELIM
3+
local commonpath = scriptpath .. "common" .. DIR_DELIM
4+
local gamepath = scriptpath .. "game".. DIR_DELIM
55

66
-- Shared between builtin files, but
77
-- not exposed to outer context
88
local builtin_shared = {}
99

10-
dofile(commonpath.."vector.lua")
10+
dofile(commonpath .. "vector.lua")
1111

12-
dofile(gamepath.."constants.lua")
13-
assert(loadfile(gamepath.."item.lua"))(builtin_shared)
14-
dofile(gamepath.."register.lua")
12+
dofile(gamepath .. "constants.lua")
13+
assert(loadfile(gamepath .. "item.lua"))(builtin_shared)
14+
dofile(gamepath .. "register.lua")
1515

1616
if core.settings:get_bool("profiler.load") then
17-
profiler = dofile(scriptpath.."profiler"..DIR_DELIM.."init.lua")
17+
profiler = dofile(scriptpath .. "profiler" .. DIR_DELIM .. "init.lua")
1818
end
1919

2020
dofile(commonpath .. "after.lua")
21-
dofile(gamepath.."item_entity.lua")
22-
dofile(gamepath.."deprecated.lua")
23-
dofile(gamepath.."misc.lua")
24-
dofile(gamepath.."privileges.lua")
25-
dofile(gamepath.."auth.lua")
21+
dofile(gamepath .. "item_entity.lua")
22+
dofile(gamepath .. "deprecated.lua")
23+
dofile(gamepath .. "misc.lua")
24+
dofile(gamepath .. "privileges.lua")
25+
dofile(gamepath .. "auth.lua")
2626
dofile(commonpath .. "chatcommands.lua")
27-
dofile(gamepath.."chatcommands.lua")
27+
dofile(gamepath .. "chat.lua")
2828
dofile(commonpath .. "information_formspecs.lua")
29-
dofile(gamepath.."static_spawn.lua")
30-
dofile(gamepath.."detached_inventory.lua")
31-
assert(loadfile(gamepath.."falling.lua"))(builtin_shared)
32-
dofile(gamepath.."features.lua")
33-
dofile(gamepath.."voxelarea.lua")
34-
dofile(gamepath.."forceloading.lua")
35-
dofile(gamepath.."statbars.lua")
29+
dofile(gamepath .. "static_spawn.lua")
30+
dofile(gamepath .. "detached_inventory.lua")
31+
assert(loadfile(gamepath .. "falling.lua"))(builtin_shared)
32+
dofile(gamepath .. "features.lua")
33+
dofile(gamepath .. "voxelarea.lua")
34+
dofile(gamepath .. "forceloading.lua")
35+
dofile(gamepath .. "statbars.lua")
3636

3737
profiler = nil

‎builtin/settingtypes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,10 @@ disable_anticheat (Disable anticheat) bool false
10581058
# This option is only read when server starts.
10591059
enable_rollback_recording (Rollback recording) bool false
10601060

1061+
# Format of player chat messages. The following strings are valid placeholders:
1062+
# @name, @message, @timestamp (optional)
1063+
chat_message_format (Chat message format) string <@name> @message
1064+
10611065
# A message to be displayed to all clients when the server shuts down.
10621066
kick_msg_shutdown (Shutdown message) string Server shutting down.
10631067

‎doc/lua_api.txt

+6
Original file line numberDiff line numberDiff line change
@@ -4156,6 +4156,12 @@ Chat
41564156

41574157
* `minetest.chat_send_all(text)`
41584158
* `minetest.chat_send_player(name, text)`
4159+
* `minetest.format_chat_message(name, message)`
4160+
* Used by the server to format a chat message, based on the setting `chat_message_format`.
4161+
Refer to the documentation of the setting for a list of valid placeholders.
4162+
* Takes player name and message, and returns the formatted string to be sent to players.
4163+
* Can be redefined by mods if required, for things like colored names or messages.
4164+
* **Only** the first occurrence of each placeholder will be replaced.
41594165

41604166
Environment access
41614167
------------------

‎minetest.conf.example

+5
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,11 @@
12771277
# type: bool
12781278
# enable_rollback_recording = false
12791279

1280+
# Format of player chat messages. The following strings are valid placeholders:
1281+
# @name, @message, @timestamp (optional)
1282+
# type: string
1283+
chat_message_format = <@name> @message
1284+
12801285
# A message to be displayed to all clients when the server shuts down.
12811286
# type: string
12821287
# kick_msg_shutdown = Server shutting down.

‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ void set_default_settings(Settings *settings)
356356
settings->setDefault("kick_msg_crash", "This server has experienced an internal error. You will now be disconnected.");
357357
settings->setDefault("ask_reconnect_on_crash", "false");
358358

359+
settings->setDefault("chat_message_format", "<@name> @message");
359360
settings->setDefault("profiler_print_interval", "0");
360361
settings->setDefault("active_object_send_range_blocks", "4");
361362
settings->setDefault("active_block_range", "3");

‎src/gettime.h

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#pragma once
2121

22-
#include "irrlichttypes.h"
2322
#include <ctime>
2423
#include <string>
2524

‎src/script/cpp_api/s_server.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,25 @@ void ScriptApiServer::on_shutdown()
168168
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
169169
}
170170

171+
std::string ScriptApiServer::formatChatMessage(const std::string &name,
172+
const std::string &message)
173+
{
174+
SCRIPTAPI_PRECHECKHEADER
175+
176+
// Push function onto stack
177+
lua_getglobal(L, "core");
178+
lua_getfield(L, -1, "format_chat_message");
179+
180+
// Push arguments onto stack
181+
lua_pushstring(L, name.c_str());
182+
lua_pushstring(L, message.c_str());
183+
184+
// Actually call the function
185+
lua_call(L, 2, 1);
186+
187+
// Fetch return value
188+
std::string ret = lua_tostring(L, -1);
189+
lua_pop(L, 1);
190+
191+
return ret;
192+
}

‎src/script/cpp_api/s_server.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ class ScriptApiServer
3636
// Calls on_shutdown handlers
3737
void on_shutdown();
3838

39+
// Calls core.format_chat_message
40+
std::string formatChatMessage(const std::string &name,
41+
const std::string &message);
42+
3943
/* auth */
4044
bool getAuth(const std::string &playername,
41-
std::string *dst_password,
42-
std::set<std::string> *dst_privs);
45+
std::string *dst_password,
46+
std::set<std::string> *dst_privs);
4347
void createAuth(const std::string &playername,
44-
const std::string &password);
48+
const std::string &password);
4549
bool setPassword(const std::string &playername,
46-
const std::string &password);
50+
const std::string &password);
4751
private:
4852
void getAuthHandler();
4953
void readPrivileges(int index, std::set<std::string> &result);

‎src/server.cpp

+19-21
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ void Server::SendChatMessage(session_t peer_id, const ChatMessage &message)
15711571
}
15721572

15731573
void Server::SendShowFormspecMessage(session_t peer_id, const std::string &formspec,
1574-
const std::string &formname)
1574+
const std::string &formname)
15751575
{
15761576
NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id);
15771577
if (formspec.empty()){
@@ -2863,28 +2863,28 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
28632863
{
28642864
// If something goes wrong, this player is to blame
28652865
RollbackScopeActor rollback_scope(m_rollback,
2866-
std::string("player:") + name);
2866+
std::string("player:") + name);
28672867

28682868
if (g_settings->getBool("strip_color_codes"))
28692869
wmessage = unescape_enriched(wmessage);
28702870

28712871
if (player) {
28722872
switch (player->canSendChatMessage()) {
2873-
case RPLAYER_CHATRESULT_FLOODING: {
2874-
std::wstringstream ws;
2875-
ws << L"You cannot send more messages. You are limited to "
2876-
<< g_settings->getFloat("chat_message_limit_per_10sec")
2877-
<< L" messages per 10 seconds.";
2878-
return ws.str();
2879-
}
2880-
case RPLAYER_CHATRESULT_KICK:
2881-
DenyAccess_Legacy(player->getPeerId(),
2882-
L"You have been kicked due to message flooding.");
2883-
return L"";
2884-
case RPLAYER_CHATRESULT_OK:
2885-
break;
2886-
default:
2887-
FATAL_ERROR("Unhandled chat filtering result found.");
2873+
case RPLAYER_CHATRESULT_FLOODING: {
2874+
std::wstringstream ws;
2875+
ws << L"You cannot send more messages. You are limited to "
2876+
<< g_settings->getFloat("chat_message_limit_per_10sec")
2877+
<< L" messages per 10 seconds.";
2878+
return ws.str();
2879+
}
2880+
case RPLAYER_CHATRESULT_KICK:
2881+
DenyAccess_Legacy(player->getPeerId(),
2882+
L"You have been kicked due to message flooding.");
2883+
return L"";
2884+
case RPLAYER_CHATRESULT_OK:
2885+
break;
2886+
default:
2887+
FATAL_ERROR("Unhandled chat filtering result found.");
28882888
}
28892889
}
28902890

@@ -2912,10 +2912,8 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
29122912
line += L"-!- You don't have permission to shout.";
29132913
broadcast_line = false;
29142914
} else {
2915-
line += L"<";
2916-
line += wname;
2917-
line += L"> ";
2918-
line += wmessage;
2915+
line += narrow_to_wide(m_script->formatChatMessage(name,
2916+
wide_to_narrow(wmessage)));
29192917
}
29202918

29212919
/*

1 commit comments

Comments
 (1)

niwla23 commented on Aug 9, 2019

@niwla23

cool!

Please sign in to comment.