irc_commands/init.lua

119 lines
3.3 KiB
Lua
Raw Normal View History

2013-08-25 06:00:14 +02:00
2013-03-26 05:54:29 +01:00
irc_users = {}
2013-08-25 06:00:14 +02:00
local function notify(nick, message)
return mt_irc:queueMsg(mt_irc.msgs.privmsg(nick, message))
end
local old_chat_send_player = minetest.chat_send_player
minetest.chat_send_player = function(name, message)
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
2013-08-25 06:00:14 +02:00
notify(nick, message)
end
end
return old_chat_send_player(name, message)
end
2013-08-25 06:00:14 +02:00
mt_irc:register_hook("NickChange", function(user, newNick)
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)
2013-08-25 06:00:14 +02:00
mt_irc:register_hook("OnQuit", function(user, reason)
irc_users[user.nick] = nil
end)
2013-08-25 06:00:14 +02:00
mt_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
2013-08-25 06:00:14 +02:00
notify(user.nick, "You need a username and password")
2013-03-26 05:54:29 +01:00
return
end
2013-08-25 06:00:14 +02:00
local found, _, playername, password = args:find("^([^%s]+)%s([^%s]+)$")
2013-03-26 05:54:29 +01:00
if not found then
2013-08-25 06:00:14 +02:00
playername = args
2013-03-28 03:34:26 +01:00
password = ""
2013-03-26 05:54:29 +01:00
end
2013-08-25 06:00:14 +02:00
if minetest.auth_table[playername] and
minetest.auth_table[playername].password ==
minetest.get_password_hash(playername, password) then
minetest.log("action", "User "..user.nick
.." from IRC logs in as "..playername)
irc_users[user.nick] = playername
notify(user.nick, "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 "
..playername.." unsuccessfully")
notify(user.nick, "Incorrect password or player does not exist")
2013-03-26 05:54:29 +01:00
end
end})
2013-08-25 06:00:14 +02:00
mt_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
notify(user.nick, "You are now logged off")
else
2013-08-25 06:00:14 +02:00
notify(from, "You are not logged in")
end
2013-03-26 05:54:29 +01:00
end})
2013-08-25 06:00:14 +02:00
mt_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
2013-08-25 06:00:14 +02:00
notify(user.nick, "You need a command")
2013-03-26 05:54:29 +01:00
return
end
2013-08-25 06:00:14 +02:00
if not irc_users[user.nick] then
2013-11-18 01:59:10 +01:00
notify(user.nick, "You are not logged in")
2013-03-26 05:54:29 +01:00
return
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
2013-08-25 06:00:14 +02:00
notify(user.nick, "Not a valid command")
2013-03-26 05:54:29 +01:00
return
end
2013-08-25 06:00:14 +02:00
if minetest.check_player_privs(irc_users[user.nick], command.privs) then
minetest.log("action", user.nick.."@IRC runs "
..args.." as "..irc_users[user.nick])
command.func(irc_users[user.nick], (params or ""))
2013-03-26 05:54:29 +01:00
end
end})
2013-03-28 03:22:28 +01:00
2013-08-25 06:00:14 +02:00
mt_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
2013-08-25 06:00:14 +02:00
notify(from, "You need a message")
2013-03-28 03:22:28 +01:00
return
end
2013-08-25 06:00:14 +02:00
if not irc_users[user.nick] then
2013-11-18 01:59:10 +01:00
notify(user.nick, "You are not logged in")
2013-03-28 03:22:28 +01:00
return
end
2013-08-25 06:00:14 +02:00
if minetest.check_player_privs(irc_users[user.nick], {shout=true}) then
minetest.log("action", user.nick.."@IRC says "
..args.." as "..irc_users[user.nick])
minetest.chat_send_all("<"..irc_users[user.nick].."@IRC> "..args)
notify(user.nick, "Message sent successfuly")
2013-03-28 03:22:28 +01:00
end
end})
2013-08-25 06:00:14 +02:00