2013-12-03 00:15:29 +01:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc.bot_commands = {}
|
2012-12-27 02:20:29 +01:00
|
|
|
|
|
2016-11-28 05:05:46 +01:00
|
|
|
|
-- From RFC1459:
|
|
|
|
|
-- "Because of IRC’s scandanavian origin, the characters {}| are
|
|
|
|
|
-- considered to be the lower case equivalents of the characters
|
|
|
|
|
-- []\, respectively."
|
|
|
|
|
local irctolower = { ["["]="{", ["\\"]="|", ["]"]="}" }
|
|
|
|
|
|
|
|
|
|
local function irclower(s)
|
|
|
|
|
return (s:lower():gsub("[%[%]\\]", irctolower))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function nickequals(nick1, nick2)
|
|
|
|
|
return irclower(nick1) == irclower(nick2)
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
function irc:check_botcmd(msg)
|
|
|
|
|
local prefix = irc.config.command_prefix
|
2016-11-28 05:05:46 +01:00
|
|
|
|
local nick = irc.conn.nick
|
2014-05-06 21:26:13 +02:00
|
|
|
|
local text = msg.args[2]
|
2016-11-28 05:05:46 +01:00
|
|
|
|
local nickpart = text:sub(1, #nick)
|
|
|
|
|
local suffix = text:sub(#nick+1, #nick+2)
|
2013-12-03 00:15:29 +01:00
|
|
|
|
|
|
|
|
|
-- First check for a nick prefix
|
2016-11-28 05:05:46 +01:00
|
|
|
|
if nickequals(nickpart, nick)
|
|
|
|
|
and (suffix == ": " or suffix == ", ") then
|
2014-05-06 21:26:13 +02:00
|
|
|
|
self:bot_command(msg, text:sub(#nick + 3))
|
2013-12-03 00:15:29 +01:00
|
|
|
|
return true
|
|
|
|
|
-- Then check for the configured prefix
|
2014-05-06 21:26:13 +02:00
|
|
|
|
elseif prefix and text:sub(1, #prefix):lower() == prefix:lower() then
|
|
|
|
|
self:bot_command(msg, text:sub(#prefix + 1))
|
2013-12-03 00:15:29 +01:00
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-27 02:20:29 +01:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
function irc:bot_command(msg, text)
|
2017-01-29 19:05:42 +01:00
|
|
|
|
-- Remove leading whitespace
|
|
|
|
|
text = text:match("^%s*(.*)")
|
2014-05-06 21:26:13 +02:00
|
|
|
|
if text:sub(1, 1) == "@" then
|
2017-02-16 06:36:04 +01:00
|
|
|
|
local _, _, player_to, message = text:find("^.([^%s]+)%s(.+)$")
|
2017-02-16 06:38:44 +01:00
|
|
|
|
if not player_to then
|
|
|
|
|
return
|
|
|
|
|
elseif not minetest.get_player_by_name(player_to) then
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:reply("User '"..player_to.."' is not in the game.")
|
2013-12-03 00:15:29 +01:00
|
|
|
|
return
|
2014-05-26 04:52:24 +02:00
|
|
|
|
elseif not irc.joined_players[player_to] then
|
|
|
|
|
irc:reply("User '"..player_to.."' is not using IRC.")
|
2014-05-06 21:26:13 +02:00
|
|
|
|
return
|
2013-12-03 00:15:29 +01:00
|
|
|
|
end
|
|
|
|
|
minetest.chat_send_player(player_to,
|
2014-05-06 21:26:13 +02:00
|
|
|
|
"PM from "..msg.user.nick.."@IRC: "..message, false)
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:reply("Message sent!")
|
2013-12-03 00:15:29 +01:00
|
|
|
|
return
|
|
|
|
|
end
|
2014-05-06 21:26:13 +02:00
|
|
|
|
local pos = text:find(" ", 1, true)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
local cmd, args
|
|
|
|
|
if pos then
|
2014-05-06 21:26:13 +02:00
|
|
|
|
cmd = text:sub(1, pos - 1)
|
|
|
|
|
args = text:sub(pos + 1)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
else
|
2014-05-06 21:26:13 +02:00
|
|
|
|
cmd = text
|
2013-04-30 00:07:44 +02:00
|
|
|
|
args = ""
|
2013-04-25 23:00:44 +02:00
|
|
|
|
end
|
2017-02-16 06:36:04 +01:00
|
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
|
if not self.bot_commands[cmd] then
|
2017-03-02 04:32:20 +01:00
|
|
|
|
self:reply("Unknown command '"..cmd.."'. Try 'help'."
|
2013-04-30 00:07:44 +02:00
|
|
|
|
.." Or use @playername <message> to send a private message")
|
|
|
|
|
return
|
|
|
|
|
end
|
2017-02-16 06:36:04 +01:00
|
|
|
|
|
|
|
|
|
local _, message = self.bot_commands[cmd].func(msg.user, args)
|
2014-05-29 02:05:26 +02:00
|
|
|
|
if message then
|
|
|
|
|
self:reply(message)
|
|
|
|
|
end
|
2012-12-27 02:20:29 +01:00
|
|
|
|
end
|
|
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
function irc:register_bot_command(name, def)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
if (not def.func) or (type(def.func) ~= "function") then
|
|
|
|
|
error("Erroneous bot command definition. def.func missing.", 2)
|
2013-12-03 00:15:29 +01:00
|
|
|
|
elseif name:sub(1, 1) == "@" then
|
|
|
|
|
error("Erroneous bot command name. Command name begins with '@'.", 2)
|
2013-04-25 23:00:44 +02:00
|
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|
self.bot_commands[name] = def
|
2012-12-27 02:20:29 +01:00
|
|
|
|
end
|
|
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:register_bot_command("help", {
|
2013-04-30 00:07:44 +02:00
|
|
|
|
params = "<command>",
|
|
|
|
|
description = "Get help about a command",
|
|
|
|
|
func = function(user, args)
|
|
|
|
|
if args == "" then
|
2016-11-29 02:17:08 +01:00
|
|
|
|
local cmdlist = { }
|
|
|
|
|
for name, cmd in pairs(irc.bot_commands) do
|
|
|
|
|
cmdlist[#cmdlist+1] = name
|
|
|
|
|
end
|
|
|
|
|
return true, "Available commands: "..table.concat(cmdlist, ", ")
|
|
|
|
|
.." -- Use 'help <command name>' to get"
|
|
|
|
|
.." help about a specific command."
|
2013-04-25 23:00:44 +02:00
|
|
|
|
end
|
2012-12-27 02:20:29 +01:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
local cmd = irc.bot_commands[args]
|
2013-04-30 00:07:44 +02:00
|
|
|
|
if not cmd then
|
2016-12-03 22:35:17 +01:00
|
|
|
|
return false, "Unknown command '"..args.."'."
|
2013-04-25 23:00:44 +02:00
|
|
|
|
end
|
2012-12-27 02:20:29 +01:00
|
|
|
|
|
2016-12-03 22:35:17 +01:00
|
|
|
|
return true, ("Usage: %s%s %s -- %s"):format(
|
|
|
|
|
irc.config.command_prefix or "",
|
2013-04-30 00:07:44 +02:00
|
|
|
|
args,
|
|
|
|
|
cmd.params or "<no parameters>",
|
2014-05-29 02:05:26 +02:00
|
|
|
|
cmd.description or "<no description>")
|
2013-04-30 00:07:44 +02:00
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:register_bot_command("list", {
|
2013-04-30 00:07:44 +02:00
|
|
|
|
params = "",
|
|
|
|
|
description = "List available commands.",
|
|
|
|
|
func = function(user, args)
|
2016-11-29 02:17:08 +01:00
|
|
|
|
return false, "The `list` command has been merged into `help`."
|
|
|
|
|
.." Use `help` with no arguments to get a list."
|
2013-04-30 00:07:44 +02:00
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:register_bot_command("whereis", {
|
2013-04-30 00:07:44 +02:00
|
|
|
|
params = "<player>",
|
|
|
|
|
description = "Tell the location of <player>",
|
|
|
|
|
func = function(user, args)
|
|
|
|
|
if args == "" then
|
2014-05-29 02:05:26 +02:00
|
|
|
|
return false, "Player name required."
|
2013-04-30 00:07:44 +02:00
|
|
|
|
end
|
2014-05-29 02:05:26 +02:00
|
|
|
|
local player = minetest.get_player_by_name(args)
|
|
|
|
|
if not player then
|
|
|
|
|
return false, "There is no player named '"..args.."'"
|
2013-04-25 23:00:44 +02:00
|
|
|
|
end
|
2014-05-29 02:05:26 +02:00
|
|
|
|
local fmt = "Player %s is at (%.2f,%.2f,%.2f)"
|
|
|
|
|
local pos = player:getpos()
|
|
|
|
|
return true, fmt:format(args, pos.x, pos.y, pos.z)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
end
|
|
|
|
|
})
|
2013-01-02 00:15:45 +01:00
|
|
|
|
|
|
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
|
local starttime = os.time()
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:register_bot_command("uptime", {
|
2013-04-30 00:07:44 +02:00
|
|
|
|
description = "Tell how much time the server has been up",
|
|
|
|
|
func = function(user, args)
|
|
|
|
|
local cur_time = os.time()
|
|
|
|
|
local diff = os.difftime(cur_time, starttime)
|
|
|
|
|
local fmt = "Server has been running for %d:%02d:%02d"
|
2014-05-29 02:05:26 +02:00
|
|
|
|
return true, fmt:format(
|
2013-04-25 23:00:44 +02:00
|
|
|
|
math.floor(diff / 60 / 60),
|
2014-05-29 02:05:26 +02:00
|
|
|
|
math.floor(diff / 60) % 60,
|
|
|
|
|
math.floor(diff) % 60
|
|
|
|
|
)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|
2013-10-08 19:06:27 +02:00
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
|
irc:register_bot_command("players", {
|
2013-10-08 19:06:27 +02:00
|
|
|
|
description = "List the players on the server",
|
|
|
|
|
func = function(user, args)
|
|
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
|
local names = {}
|
|
|
|
|
for _, player in pairs(players) do
|
|
|
|
|
table.insert(names, player:get_player_name())
|
|
|
|
|
end
|
2014-05-29 02:05:26 +02:00
|
|
|
|
return true, "Connected players: "
|
|
|
|
|
..table.concat(names, ", ")
|
2013-10-08 19:06:27 +02:00
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|