capturetheflag/mods/chatplus/api.lua

241 lines
5.1 KiB
Lua
Raw Normal View History

2015-12-18 01:50:19 +00:00
-- Chat Plus
-- by rubenwardy
---------------------
-- api.lua
-- Core functionality
---------------------
chatplus = {
2016-04-05 00:44:11 +00:00
version = 2.3,
2015-12-18 01:50:19 +00:00
_logpath = minetest.get_worldpath().."/chatplus-log.txt",
_defsettings = {
log = true,
use_gui = true,
distance = 0,
badwords = ""
}
}
function chatplus.init()
chatplus.load()
chatplus.clean_players()
if not chatplus.players then
chatplus.players = {}
end
chatplus.count = 0
chatplus.loggedin = {}
chatplus._handlers = {}
end
function chatplus.setting(name)
local get = minetest.setting_get("chatplus_" .. name)
if get then
return get
elseif chatplus._defsettings[name]~= nil then
return chatplus._defsettings[name]
else
minetest.log("[Chatplus] Setting chatplus_" .. name .. " not found!")
return nil
end
end
function chatplus.log(msg)
if chatplus._log then
chatplus._log:write(os.date("%Y-%m-%d %H:%M:%S") .. " " .. msg .. "\r\n")
chatplus._log:flush()
end
end
function chatplus.load()
-- Initialize the log
if chatplus.setting("log") then
chatplus._log = io.open(chatplus._logpath, "a+")
if not chatplus._log then
minetest.log("error", "Unable to open the chatplus log file: " .. chatplus._logpath)
else
minetest.log("action", "Logging chat plus to: " .. chatplus._logpath)
end
chatplus.log("*** SERVER STARTED ***")
end
-- Load player data
minetest.log("[Chatplus] Loading data")
local file = io.open(minetest.get_worldpath() .. "/chatplus.txt", "r")
if file then
local from_file = minetest.deserialize(file:read("*all"))
file:close()
if type(from_file) == "table" then
if from_file.players and from_file.version >= 2 then
chatplus.players = from_file.players
else
2016-03-17 15:39:49 +00:00
chatplus.old_inbox = {}
2015-12-18 01:50:19 +00:00
chatplus.players = {}
for name, data in pairs(from_file) do
local inbox = data.inbox
2016-03-17 15:39:49 +00:00
data.inbox = nil
chatplus.players[name] = data
chatplus.old_inbox[name] = {}
2015-12-18 01:50:19 +00:00
for _, msg in pairs(inbox) do
2016-03-17 15:39:49 +00:00
table.insert(chatplus.old_inbox[name], {
2015-12-18 01:50:19 +00:00
date = "?",
from = "?",
msg = msg
})
end
end
2016-03-17 15:39:49 +00:00
if chatplus.on_old_inbox then
chatplus.on_old_inbox(chatplus.old_inbox)
end
2015-12-18 01:50:19 +00:00
end
return
end
end
end
function chatplus.save()
minetest.log("[Chatplus] Saving data")
local file = io.open(minetest.get_worldpath().."/chatplus.txt", "w")
if file then
file:write(minetest.serialize({ version = 2, players = chatplus.players}))
file:close()
end
end
function chatplus.clean_players()
if not chatplus.players then
chatplus.players = {}
return
end
minetest.log("[Chatplus] Cleaning player lists")
for key,value in pairs(chatplus.players) do
if value.messages then
value.inbox = value.messages
value.messages = nil
end
if (
(not value.inbox or #value.inbox==0) and
(not value.ignore or #value.ignore==0)
) then
value[key] = nil
end
end
chatplus.save()
end
local function cp_tick()
chatplus.clean_players()
minetest.after(30*60, cp_tick)
end
minetest.after(30*60, cp_tick)
function chatplus.poke(name,player)
local function check(name,value)
if not chatplus.players[name][value] then
chatplus.players[name][value] = {}
end
end
if not chatplus.players[name] then
chatplus.players[name] = {}
end
check(name, "ignore")
check(name, "inbox")
chatplus.players[name].enabled = true
if player then
if player=="end" then
chatplus.players[name].enabled = false
chatplus.loggedin[name] = nil
else
if not chatplus.loggedin[name] then
chatplus.loggedin[name] = {}
end
chatplus.loggedin[name].player = player
end
end
chatplus.save()
return chatplus.players[name]
end
2016-04-05 00:44:11 +00:00
minetest.register_on_joinplayer(function(player)
chatplus.poke(player:get_player_name(), player)
end)
minetest.register_on_leaveplayer(function(player)
chatplus.poke(player:get_player_name(), "end")
end)
2015-12-18 01:50:19 +00:00
function chatplus.register_handler(func,place)
if not place then
table.insert(chatplus._handlers, func)
else
table.insert(chatplus._handlers, place, func)
end
end
-- Allows overriding
function chatplus.log_message(from, msg)
chatplus.log("<" .. from .. "> " .. msg)
end
function chatplus.send(from, msg)
if msg:sub(1, 1) == "/" then
return false
end
2016-04-08 18:02:49 +00:00
2016-04-05 00:44:11 +00:00
if not minetest.check_player_privs(from, {shout = true}) then
return nil
end
2015-12-18 01:50:19 +00:00
chatplus.log_message(from, msg)
if #chatplus._handlers == 0 then
return nil
end
-- Loop through possible receivers
for to, value in pairs(chatplus.loggedin) do
if to ~= from then
-- Run handlers
local res = nil
for i = 1, #chatplus._handlers do
if chatplus._handlers[i] then
res = chatplus._handlers[i](from, to, msg)
if res ~= nil then
break
end
end
end
-- Send message
if res == nil or res == true then
minetest.chat_send_player(to, "<" .. from .. "> " .. msg)
2015-12-18 01:50:19 +00:00
end
end
end
return true
end
-- Minetest callbacks
2015-12-18 02:30:07 +00:00
minetest.register_on_chat_message(function(...)
local ret = chatplus.send(...)
2015-12-18 02:33:08 +00:00
return ret
2015-12-18 02:30:07 +00:00
end)
2015-12-18 01:50:19 +00:00
minetest.register_on_joinplayer(function(player)
chatplus.log(player:get_player_name() .. " joined")
end)
minetest.register_on_leaveplayer(function(player)
chatplus.poke(player:get_player_name(), "end")
chatplus.log(player:get_player_name() .. " disconnected")
end)
chatplus.init()