Update to the new IRC mod API

This commit is contained in:
ShadowNinja 2013-08-25 00:00:14 -04:00
parent ad3d067587
commit 0e0ce48738
1 changed files with 59 additions and 50 deletions

109
init.lua
View File

@ -1,76 +1,83 @@
irc_users = {} irc_users = {}
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 local old_chat_send_player = minetest.chat_send_player
minetest.chat_send_player = function(name, message) minetest.chat_send_player = function(name, message)
for nick, user in pairs(irc_users) do for nick, logedInAs in pairs(irc_users) do
if name == user then if name == logedInAs and not minetest.get_player_by_name(name) then
mt_irc.say(nick, message) notify(nick, message)
end end
end end
return old_chat_send_player(name, message) return old_chat_send_player(name, message)
end end
mt_irc.register_callback("nick_change", function (old_nick, new_nick) mt_irc:register_hook("NickChange", function(user, newNick)
for nick, user in pairs(irc_users) do for nick, player in pairs(irc_users) do
if nick == old_nick then if nick == user.nick then
irc_users[new_nick] = irc_users[old_nick] irc_users[newNick] = irc_users[user.nick]
irc_users[old_nick] = nil irc_users[user.nick] = nil
end end
end end
end) end)
mt_irc.register_callback("part", function (nick, part_msg) mt_irc:register_hook("OnQuit", function(user, reason)
if irc_users[nick] then irc_users[user.nick] = nil
irc_users[nick] = nil
end
end) end)
mt_irc.register_bot_command("login", { mt_irc:register_bot_command("login", {
params = "<username> <password>", params = "<username> <password>",
description = "Login as a user to run commands", description = "Login as a user to run commands",
func = function (from, args) func = function(user, args)
if args == "" then if args == "" then
mt_irc.say(from, "You need a username and password") notify(user.nick, "You need a username and password")
return return
end end
local found, _, username, password = args:find("^([^%s]+)%s([^%s]+)$") local found, _, playername, password = args:find("^([^%s]+)%s([^%s]+)$")
if not found then if not found then
username = args playername = args
password = "" password = ""
end end
if minetest.auth_table[username] and if minetest.auth_table[playername] and
minetest.auth_table[username].password == minetest.get_password_hash(username, password) then minetest.auth_table[playername].password ==
minetest.debug("User "..from.." from IRC logs in as "..username) minetest.get_password_hash(playername, password) then
irc_users[from] = username minetest.log("action", "User "..user.nick
mt_irc.say(from, "You are now logged in as "..username) .." from IRC logs in as "..playername)
irc_users[user.nick] = playername
notify(user.nick, "You are now logged in as "..playername)
else else
minetest.debug("User "..from.." from IRC attempted log in as "..username.." unsuccessfully") minetest.log("action", user.nick.."@IRC attempted to log in as "
mt_irc.say(from, "Incorrect password or player does not exist") ..playername.." unsuccessfully")
notify(user.nick, "Incorrect password or player does not exist")
end end
end}) end})
mt_irc.register_bot_command("logout", { mt_irc:register_bot_command("logout", {
description = "Logout", description = "Logout",
func = function (from, args) func = function (user, args)
if irc_users[from] then if irc_users[user.nick] then
minetest.debug("User "..from.." from IRC logs out of "..irc_users[from]) minetest.log("action", user.nick.."@IRC logs out from "
irc_users[from] = nil ..irc_users[user.nick])
mt_irc.say(from, "You are now logged off") irc_users[user.nick] = nil
notify(user.nick, "You are now logged off")
else else
mt_irc.say(from, "You are not logged in") notify(from, "You are not logged in")
end end
end}) end})
mt_irc.register_bot_command("cmd", { mt_irc:register_bot_command("cmd", {
params = "<command>", params = "<command>",
description = "Run a command on the server", description = "Run a command on the server",
func = function (from, args) func = function (user, args)
if args == "" then if args == "" then
mt_irc.say(from, "You need a command") notify(user.nick, "You need a command")
return return
end end
if not irc_users[from] then if not irc_users[user.nick] then
mt_irc.say(from, "You are not loged in") notify(user.nick, "You are not loged in")
return return
end end
local found, _, commandname, params = args:find("^([^%s]+)%s(.+)$") local found, _, commandname, params = args:find("^([^%s]+)%s(.+)$")
@ -79,31 +86,33 @@ mt_irc.register_bot_command("cmd", {
end end
local command = minetest.chatcommands[commandname] local command = minetest.chatcommands[commandname]
if not command then if not command then
mt_irc.say(from, "Not a valid command") notify(user.nick, "Not a valid command")
return return
end end
if minetest.check_player_privs(irc_users[from], command.privs) then if minetest.check_player_privs(irc_users[user.nick], command.privs) then
minetest.debug("User "..from.." from IRC runs "..args.." as "..irc_users[from]) minetest.log("action", user.nick.."@IRC runs "
command.func(irc_users[from], (params or "")) ..args.." as "..irc_users[user.nick])
mt_irc.say(from, "Command run successfuly") command.func(irc_users[user.nick], (params or ""))
end end
end}) end})
mt_irc.register_bot_command("say", { mt_irc:register_bot_command("say", {
params = "message", params = "message",
description = "Say something", description = "Say something",
func = function (from, args) func = function (user, args)
if args == "" then if args == "" then
mt_irc.say(from, "You need a message") notify(from, "You need a message")
return return
end end
if not irc_users[from] then if not irc_users[user.nick] then
mt_irc.say(from, "You are not loged in") notify(user.nick, "You are not loged in")
return return
end end
if minetest.check_player_privs(irc_users[from], {shout=true}) then if minetest.check_player_privs(irc_users[user.nick], {shout=true}) then
minetest.debug("User "..from.." from IRC says "..args.." as "..irc_users[from]) minetest.log("action", user.nick.."@IRC says "
minetest.chat_send_all("<"..irc_users[from].."@IRC> "..args) ..args.." as "..irc_users[user.nick])
mt_irc.say(from, "Message sent successfuly") minetest.chat_send_all("<"..irc_users[user.nick].."@IRC> "..args)
notify(user.nick, "Message sent successfuly")
end end
end}) end})