irc_commands/init.lua

142 lines
4.1 KiB
Lua
Raw Normal View History

2013-08-25 06:00:14 +02:00
2014-05-29 02:29:32 +02:00
local irc_users = {}
2013-03-26 05:54:29 +01:00
local old_chat_send_player = minetest.chat_send_player
2017-02-16 07:42:49 +01:00
minetest.chat_send_player = function(name, message) -- luacheck: ignore
2013-11-18 01:59:10 +01:00
for nick, loggedInAs in pairs(irc_users) do
if name == loggedInAs and not minetest.get_player_by_name(name) then
2014-05-29 02:29:32 +02:00
irc:say(nick, message)
end
end
return old_chat_send_player(name, message)
end
irc.register_hook("NickChange", function(user, newNick)
2013-08-25 06:00:14 +02:00
for nick, player in pairs(irc_users) do
if nick == user.nick then
irc_users[newNick] = irc_users[user.nick]
irc_users[user.nick] = nil
end
end
end)
irc.register_hook("OnPart", function(user, channel, reason)
2013-08-25 06:00:14 +02:00
irc_users[user.nick] = nil
end)
irc.register_hook("OnKick", function(user, channel, target, reason)
2014-05-29 02:29:32 +02:00
irc_users[target] = nil
end)
irc.register_hook("OnQuit", function(user, reason)
2014-05-29 02:29:32 +02:00
irc_users[user.nick] = nil
end)
irc.register_bot_command("login", {
2013-03-26 05:54:29 +01:00
params = "<username> <password>",
description = "Login as a user to run commands",
2013-08-25 06:00:14 +02:00
func = function(user, args)
2013-03-28 03:27:15 +01:00
if args == "" then
2014-05-29 02:29:32 +02:00
return false, "You need a username and password."
2013-03-26 05:54:29 +01:00
end
2018-02-18 20:40:54 +01:00
local playerName, password = args:match("^(%S+)%s(.+)$")
2014-05-29 02:29:32 +02:00
if not playerName then
return false, "Player name and password required."
end
local inChannel = false
local channel = irc.conn.channels[irc.config.channel]
if not channel then
return false, "The server needs to be in its "..
"channel for anyone to log in."
end
for cnick, cuser in pairs(channel.users) do
2014-05-29 02:29:32 +02:00
if user.nick == cnick then
inChannel = true
break
end
2013-03-26 05:54:29 +01:00
end
2014-05-29 02:29:32 +02:00
if not inChannel then
2016-06-01 17:57:57 +02:00
return false, "You need to be in the server's channel to log in."
2014-05-29 02:29:32 +02:00
end
2016-06-01 18:17:21 +02:00
local handler = minetest.get_auth_handler()
local auth = handler.get_auth(playerName)
if auth and minetest.check_password_entry(playerName, auth.password, password) then
2013-08-25 06:00:14 +02:00
minetest.log("action", "User "..user.nick
2014-05-29 02:29:32 +02:00
.." from IRC logs in as "..playerName)
irc_users[user.nick] = playerName
2016-06-01 18:17:21 +02:00
handler.record_login(playerName)
2014-05-29 02:29:32 +02:00
return true, "You are now logged in as "..playerName
2013-03-26 05:54:29 +01:00
else
2013-08-25 06:00:14 +02:00
minetest.log("action", user.nick.."@IRC attempted to log in as "
2014-05-29 02:29:32 +02:00
..playerName.." unsuccessfully")
return false, "Incorrect password or player does not exist."
2013-03-26 05:54:29 +01:00
end
2014-05-29 02:29:32 +02:00
end
})
2013-03-26 05:54:29 +01:00
irc.register_bot_command("logout", {
2013-03-26 05:54:29 +01:00
description = "Logout",
2013-08-25 06:00:14 +02:00
func = function (user, args)
if irc_users[user.nick] then
minetest.log("action", user.nick.."@IRC logs out from "
..irc_users[user.nick])
irc_users[user.nick] = nil
2014-05-29 02:29:32 +02:00
return true, "You are now logged off."
else
2014-05-29 02:29:32 +02:00
return false, "You are not logged in."
end
2013-12-03 22:37:02 +01:00
end,
})
2013-03-26 05:54:29 +01:00
irc.register_bot_command("cmd", {
2013-03-26 05:54:29 +01:00
params = "<command>",
2013-03-28 03:27:15 +01:00
description = "Run a command on the server",
2013-08-25 06:00:14 +02:00
func = function (user, args)
2013-03-26 05:54:29 +01:00
if args == "" then
2014-05-29 02:29:32 +02:00
return false, "You need a command."
2013-03-26 05:54:29 +01:00
end
2013-08-25 06:00:14 +02:00
if not irc_users[user.nick] then
2014-05-29 02:29:32 +02:00
return false, "You are not logged in."
2013-03-26 05:54:29 +01:00
end
local found, _, commandname, params = args:find("^([^%s]+)%s(.+)$")
if not found then
commandname = args
end
local command = minetest.chatcommands[commandname]
if not command then
2014-05-29 02:29:32 +02:00
return false, "Not a valid command."
2013-03-26 05:54:29 +01:00
end
if not minetest.check_player_privs(irc_users[user.nick], command.privs) then
2014-05-29 02:29:32 +02:00
return false, "Your privileges are insufficient."
2013-03-26 05:54:29 +01:00
end
minetest.log("action", user.nick.."@IRC runs "
..args.." as "..irc_users[user.nick])
2014-05-29 02:29:32 +02:00
return command.func(irc_users[user.nick], (params or ""))
end
})
2013-03-28 03:22:28 +01:00
irc.register_bot_command("say", {
2013-03-28 03:22:28 +01:00
params = "message",
description = "Say something",
2013-08-25 06:00:14 +02:00
func = function (user, args)
2013-03-28 03:22:28 +01:00
if args == "" then
2014-05-29 02:29:32 +02:00
return false, "You need a message."
2013-03-28 03:22:28 +01:00
end
2013-08-25 06:00:14 +02:00
if not irc_users[user.nick] then
2014-05-29 02:29:32 +02:00
return false, "You are not logged in."
2013-03-28 03:22:28 +01:00
end
2014-05-29 02:29:32 +02:00
if not minetest.check_player_privs(irc_users[user.nick], {shout=true}) then
minetest.log("action", ("%s@IRC tried to say %q as %s"
.." without the shout privilege.")
:format(user.nick, args, irc_users[user.nick]))
return false, "You can not shout."
2013-03-28 03:22:28 +01:00
end
2014-05-29 02:29:32 +02:00
minetest.log("action", ("%s@IRC says %q as %s.")
:format(user.nick, args, irc_users[user.nick]))
minetest.chat_send_all("<"..irc_users[user.nick].."@IRC> "..args)
return true, "Message sent successfuly."
end
})
2013-08-25 06:00:14 +02:00
minetest.log("action", "[irc_commands] loaded.")