Since this mod is CC0 and I like the idea of a chat logfile I created my own version of this mod basing on the same idea. I release my version also under CC0.
Code: Select all
-- Helper function for loading optional values
local function getValid(value, default)
v = minetest.setting_get(value)
return (v == nil and default or v)
end
-- Default values
local defaultFile = 'chatlog.%Y-%m-%d.log'
local defaultDate = '%X'
local defaultLine = '[%date%] <%name%> %message%\n'
-- Read values from minetest.conf or set default values
chatlogFilename = getValid('chatlog_logfile_name', defaultFile)
chatlogDateString = getValid('chatlog_date_string', defaultDate)
chatlogLineFormat = getValid('chatlog_line_format', defaultLine)
function chatlogWriteLine(name, message)
local logfileName = os.date(chatlogFilename)
local line = chatlogLineFormat
line = line:gsub('%%date%%', os.date(chatlogDateString))
line = line:gsub('%%name%%', name)
line = line:gsub('%%message%%', message)
f = io.open(minetest.get_worldpath()..'/'..logfileName, 'a')
f:write(line)
f:close()
end
minetest.register_on_chat_message(chatlogWriteLine)
A default chatlog line is
[%date%] <%name%> %message% … where
%date%,
%name% and
%message% get replaced with the actual date, name and message of the chat line being processed. The default file name is
chatlog.%Y-%m-%d.log within the world directory.
Since Lua does not have a function to create folders without loading additional modules (which is insecure because the modules in question might not be installed) or system calls (which are not portable between different operating systems) it is not possible to create sub-directories for log files (except you create them manually beforehand).
Changing the default variables is possible in two locations the first location is in
init.lua itself in the section “Default values” but that is not desirable (because one should always separate code and configuration). The second and better solution is to define them in your server’s configuration file or the
minetest.conf file.
chatlog_logfile_name allows changing the logfile’s file name. You can use the known date variables here (But as described before: no sub-directories except you create them manually). With
chatlog_date_string you describe how the date should be represented in the single chatlog entries. And
chatlog_line_format defines the actual chatlog lines.
For
chatlog_line_format three additional variables are available:
%date% gets replaced by the date defined via
chatlog_date_string or its default value,
%name% gets replaced with the name of the user sending the chat message and
%message% gets replaced with the message itself. Other variables are not being replaced.