diff --git a/API.txt b/API.md similarity index 73% rename from API.txt rename to API.md index 2e0ff5b..c4934d4 100644 --- a/API.txt +++ b/API.md @@ -1,24 +1,26 @@ IRC Mod API ------------ +=========== + This file documents the Minetest IRC mod API. -BASICS +Basics ------ -In order to allow your mod to interface with this mod, you must add 'irc' - (without the quotes) to your mod's 'depends.txt' file. + +In order to allow your mod to interface with this mod, you must add `irc` +to your mod's `depends.txt` file. -REFERENCE +Reference --------- -mt_irc:say([name, ]message) +irc:say([name,] message) Sends to either the channel (if is nil or not specified), or to the given user (if is specified). Example: - mt_irc:say("Hello, Channel!") - mt_irc:say("john1234", "How are you?") + irc:say("Hello, Channel!") + irc:say("john1234", "How are you?") -mt_irc:register_bot_command(name, cmdDef) +irc:register_bot_command(name, cmdDef) Registers a new bot command named . When an user sends a private message to the bot with the command name, the command's function is called. @@ -35,29 +37,29 @@ mt_irc:register_bot_command(name, cmdDef) end, }; Example: - mt_irc:register_bot_command("hello", { + irc:register_bot_command("hello", { params = "", description = "Greet user", func = function(user, param) - mt_irc:say(user.nick, "Hello!") + irc:say(user.nick, "Hello!") end, }); -mt_irc.joined_players[name] +irc.joined_players[name] This table holds the players who are currently on the channel (may be less than the players in the game). It is modified by the /part and /join chat commands. Example: - if mt_irc.joined_players["joe"] then + if irc.joined_players["joe"] then -- Joe is talking on IRC end -mt_irc:register_hook(name, func) +irc:register_hook(name, func) Registers a function to be called when an event happens. is the name of the event, and is the function to be called. See HOOKS below for more information Example: - mt_irc:register_hook("OnSend", function(line) + irc:register_hook("OnSend", function(line) print("SEND: "..line) end) @@ -69,18 +71,18 @@ string.expandvars(string, vars) are left verbatim in the string. Example: local tpl = "$(foo) $(bar) $(baz)" - local s = tpl:expandvars({ foo=1, bar="Hello" }) + local s = tpl:expandvars({foo=1, bar="Hello"}) assert(s == "1 Hello $(baz)") -In addition, all the configuration options decribed in `README.txt' are -available to other mods, though they should be considered "read only". Do -not modify these settings at runtime or you will most likely crash the -server! +In addition, all the configuration options decribed in `README.txt` are +available to other mods, though they should be considered read-only. Do +not modify these settings at runtime or you might crash the server! -HOOKS ---------- -The 'mt_irc:register_hook' function can register functions to be called +Hooks +----- + +The `irc:register_hook` function can register functions to be called when some events happen. The events supported are the same as the LuaIRC ones with a few added (mostly for internal use). See src/LuaIRC/doc/irc.luadoc for more information. diff --git a/botcmds.lua b/botcmds.lua index a574338..71ae5c9 100644 --- a/botcmds.lua +++ b/botcmds.lua @@ -1,9 +1,9 @@ -mt_irc.bot_commands = {} +irc.bot_commands = {} -function mt_irc:check_botcmd(msg) - local prefix = mt_irc.config.command_prefix - local nick = mt_irc.conn.nick:lower() +function irc:check_botcmd(msg) + local prefix = irc.config.command_prefix + local nick = irc.conn.nick:lower() local text = msg.args[2] local nickpart = text:sub(1, #nick + 2):lower() @@ -21,19 +21,19 @@ function mt_irc:check_botcmd(msg) end -function mt_irc:bot_command(msg, text) +function irc:bot_command(msg, text) if text:sub(1, 1) == "@" then local found, _, player_to, message = text:find("^.([^%s]+)%s(.+)$") if not minetest.get_player_by_name(player_to) then - mt_irc:reply("User '"..player_to.."' is not in the game.") + irc:reply("User '"..player_to.."' is not in the game.") return - elseif not mt_irc.joined_players[player_to] then - mt_irc:reply("User '"..player_to.."' is not using IRC.") + elseif not irc.joined_players[player_to] then + irc:reply("User '"..player_to.."' is not using IRC.") return end minetest.chat_send_player(player_to, "PM from "..msg.user.nick.."@IRC: "..message, false) - mt_irc:reply("Message sent!") + irc:reply("Message sent!") return end local pos = text:find(" ", 1, true) @@ -56,7 +56,7 @@ function mt_irc:bot_command(msg, text) end -function mt_irc:register_bot_command(name, def) +function irc:register_bot_command(name, def) if (not def.func) or (type(def.func) ~= "function") then error("Erroneous bot command definition. def.func missing.", 2) elseif name:sub(1, 1) == "@" then @@ -66,23 +66,23 @@ function mt_irc:register_bot_command(name, def) end -mt_irc:register_bot_command("help", { +irc:register_bot_command("help", { params = "", description = "Get help about a command", func = function(user, args) if args == "" then - mt_irc:reply("No command name specified. Use 'list' for a list of commands") + irc:reply("No command name specified. Use 'list' for a list of commands") return end - local cmd = mt_irc.bot_commands[args] + local cmd = irc.bot_commands[args] if not cmd then - mt_irc:reply("Unknown command '"..cmdname.."'.") + irc:reply("Unknown command '"..cmdname.."'.") return end - mt_irc:reply(("Usage: %c%s %s -- %s"):format( - mt_irc.config.command_prefix, + irc:reply(("Usage: %c%s %s -- %s"):format( + irc.config.command_prefix, args, cmd.params or "", cmd.description or "")) @@ -90,48 +90,48 @@ mt_irc:register_bot_command("help", { }) -mt_irc:register_bot_command("list", { +irc:register_bot_command("list", { params = "", description = "List available commands.", func = function(user, args) local cmdlist = "Available commands: " - for name, cmd in pairs(mt_irc.bot_commands) do + for name, cmd in pairs(irc.bot_commands) do cmdlist = cmdlist..name..", " end - mt_irc:reply(cmdlist.." -- Use 'help ' to get" + irc:reply(cmdlist.." -- Use 'help ' to get" .." help about a specific command.") end }) -mt_irc:register_bot_command("whereis", { +irc:register_bot_command("whereis", { params = "", description = "Tell the location of ", func = function(user, args) if args == "" then - mt_irc:bot_help(user, "whereis") + irc:bot_help(user, "whereis") return end local player = minetest.env:get_player_by_name(args) if player then local fmt = "Player %s is at (%.2f,%.2f,%.2f)" local pos = player:getpos() - mt_irc:reply(fmt:format(args, pos.x, pos.y, pos.z)) + irc:reply(fmt:format(args, pos.x, pos.y, pos.z)) return end - mt_irc:reply("There is no player named '"..args.."'") + irc:reply("There is no player named '"..args.."'") end }) local starttime = os.time() -mt_irc:register_bot_command("uptime", { +irc:register_bot_command("uptime", { 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" - mt_irc:reply(fmt:format( + irc:reply(fmt:format( math.floor(diff / 60 / 60), math.mod(math.floor(diff / 60), 60), math.mod(math.floor(diff), 60) @@ -140,7 +140,7 @@ mt_irc:register_bot_command("uptime", { }) -mt_irc:register_bot_command("players", { +irc:register_bot_command("players", { description = "List the players on the server", func = function(user, args) local players = minetest.get_connected_players() @@ -148,7 +148,7 @@ mt_irc:register_bot_command("players", { for _, player in pairs(players) do table.insert(names, player:get_player_name()) end - mt_irc:reply("Connected players: " + irc:reply("Connected players: " ..table.concat(names, ", ")) end }) diff --git a/callback.lua b/callback.lua index f85a636..0356f91 100644 --- a/callback.lua +++ b/callback.lua @@ -4,25 +4,25 @@ minetest.register_on_joinplayer(function(player) local name = player:get_player_name() - if mt_irc.connected and mt_irc.config.send_join_part then - mt_irc:say("*** "..name.." joined the game") + if irc.connected and irc.config.send_join_part then + irc:say("*** "..name.." joined the game") end end) minetest.register_on_leaveplayer(function(player) local name = player:get_player_name() - if mt_irc.connected and mt_irc.config.send_join_part then - mt_irc:say("*** "..name.." left the game") + if irc.connected and irc.config.send_join_part then + irc:say("*** "..name.." left the game") end end) minetest.register_on_chat_message(function(name, message) - if not mt_irc.connected + if not irc.connected or message:sub(1, 1) == "/" or message:sub(1, 5) == "[off]" - or not mt_irc.joined_players[name] + or not irc.joined_players[name] or (not minetest.check_player_privs(name, {shout=true})) then return end @@ -30,11 +30,11 @@ minetest.register_on_chat_message(function(name, message) if nl then message = message:sub(1, nl - 1) end - mt_irc:say(mt_irc:playerMessage(name, message)) + irc:say(irc:playerMessage(name, message)) end) minetest.register_on_shutdown(function() - mt_irc:disconnect("Game shutting down.") + irc:disconnect("Game shutting down.") end) diff --git a/chatcmds.lua b/chatcmds.lua index 790688e..03599e9 100644 --- a/chatcmds.lua +++ b/chatcmds.lua @@ -10,7 +10,7 @@ minetest.register_chatcommand("irc_msg", { description = "Send a private message to an IRC user", privs = {shout=true}, func = function(name, param) - if not mt_irc.connected then + if not irc.connected then minetest.chat_send_player(name, "Not connected to IRC. Use /irc_connect to connect.") return end @@ -21,7 +21,7 @@ minetest.register_chatcommand("irc_msg", { end local toname_l = toname:lower() local validNick = false - for nick, user in pairs(mt_irc.conn.channels[mt_irc.config.channel].users) do + for nick, user in pairs(irc.conn.channels[irc.config.channel].users) do if nick:lower() == toname_l then validNick = true break @@ -35,7 +35,7 @@ minetest.register_chatcommand("irc_msg", { "You can not message that user. (Hint: They have to be in the channel)") return end - mt_irc:say(toname, mt_irc:playerMessage(name, message)) + irc:say(toname, irc:playerMessage(name, message)) minetest.chat_send_player(name, "Message sent!") end }) @@ -46,7 +46,7 @@ minetest.register_chatcommand("irc_names", { description = "List the users in IRC.", func = function(name, params) local users = { } - for k, v in pairs(mt_irc.conn.channels[mt_irc.config.channel].users) do + for k, v in pairs(irc.conn.channels[irc.config.channel].users) do table.insert(users, k) end minetest.chat_send_player(name, "Users in IRC: "..table.concat(users, ", ")) @@ -58,12 +58,12 @@ minetest.register_chatcommand("irc_connect", { description = "Connect to the IRC server.", privs = {irc_admin=true}, func = function(name, param) - if mt_irc.connected then + if irc.connected then minetest.chat_send_player(name, "You are already connected to IRC.") return end minetest.chat_send_player(name, "IRC: Connecting...") - mt_irc:connect() + irc:connect() end }) @@ -73,14 +73,14 @@ minetest.register_chatcommand("irc_disconnect", { description = "Disconnect from the IRC server.", privs = {irc_admin=true}, func = function(name, param) - if not mt_irc.connected then + if not irc.connected then minetest.chat_send_player(name, "You are not connected to IRC.") return end if params == "" then params = "Manual disconnect by "..name end - mt_irc:disconnect(param) + irc:disconnect(param) end }) @@ -89,12 +89,12 @@ minetest.register_chatcommand("irc_reconnect", { description = "Reconnect to the IRC server.", privs = {irc_admin=true}, func = function(name, param) - if not mt_irc.connected then + if not irc.connected then minetest.chat_send_player(name, "You are not connected to IRC.") return end - mt_irc:disconnect("Reconnecting...") - mt_irc:connect() + irc:disconnect("Reconnecting...") + irc:connect() end }) @@ -104,11 +104,11 @@ minetest.register_chatcommand("irc_quote", { description = "Send a raw command to the IRC server.", privs = {irc_admin=true}, func = function(name, param) - if not mt_irc.connected then + if not irc.connected then minetest.chat_send_player(name, "You are not connected to IRC.") return end - mt_irc:queue(param) + irc:queue(param) minetest.chat_send_player(name, "Command sent!") end }) @@ -117,6 +117,6 @@ minetest.register_chatcommand("irc_quote", { local oldme = minetest.chatcommands["me"].func minetest.chatcommands["me"].func = function(name, param, ...) oldme(name, param, ...) - mt_irc:say(("* %s %s"):format(name, param)) + irc:say(("* %s %s"):format(name, param)) end diff --git a/config.lua b/config.lua index 8f4ff92..8dfd381 100644 --- a/config.lua +++ b/config.lua @@ -2,7 +2,7 @@ -- See LICENSE.txt for details. -mt_irc.config = {} +irc.config = {} local function setting(stype, name, default) local value @@ -16,7 +16,7 @@ local function setting(stype, name, default) if value == nil then value = default end - mt_irc.config[name] = value + irc.config[name] = value end ------------------------- @@ -27,7 +27,7 @@ setting("string", "nick") -- Nickname (default "MT-", 6 random hexi setting("string", "server", "irc.freenode.net") -- Server to connect on joinplayer setting("number", "port", 6667) -- Port to connect on joinplayer setting("string", "NSPass") -- NickServ password -setting("string", "sasl.user", mt_irc.config.nick) -- SASL username +setting("string", "sasl.user", irc.config.nick) -- SASL username setting("string", "sasl.pass") -- SASL password setting("string", "channel", "##mt-irc-mod") -- Channel to join setting("string", "key") -- Key for the channel @@ -47,10 +47,10 @@ setting("bool", "auto_join", true) -- Whether to automatically show players in setting("bool", "auto_connect", true) -- Whether to automatically connect to the server on mod load -- Generate a random nickname if one isn't specified. -if not mt_irc.config.nick then +if not irc.config.nick then local pr = PseudoRandom(os.time()) -- Workaround for bad distribution in minetest PRNG implementation. - mt_irc.config.nick = ("MT-%02X%02X%02X"):format( + irc.config.nick = ("MT-%02X%02X%02X"):format( pr:next(0, 255), pr:next(0, 255), pr:next(0, 255) diff --git a/hooks.lua b/hooks.lua index 1820198..a6e6a46 100644 --- a/hooks.lua +++ b/hooks.lua @@ -2,8 +2,8 @@ -- See LICENSE.txt for details. -mt_irc.hooks = {} -mt_irc.registered_hooks = {} +irc.hooks = {} +irc.registered_hooks = {} -- TODO: Add proper conversion from CP1252 to UTF-8. @@ -21,7 +21,7 @@ local function normalize(text) end -function mt_irc:doHook(conn) +function irc:doHook(conn) for name, hook in pairs(self.registered_hooks) do for _, func in pairs(hook) do conn:hook(name, func) @@ -30,59 +30,59 @@ function mt_irc:doHook(conn) end -function mt_irc:register_hook(name, func) +function irc:register_hook(name, func) self.registered_hooks[name] = self.registered_hooks[name] or {} table.insert(self.registered_hooks[name], func) end -function mt_irc.hooks.raw(line) - if mt_irc.config.debug then +function irc.hooks.raw(line) + if irc.config.debug then print("RECV: "..line) end end -function mt_irc.hooks.send(line) - if mt_irc.config.debug then +function irc.hooks.send(line) + if irc.config.debug then print("SEND: "..line) end end -function mt_irc.hooks.chat(msg) +function irc.hooks.chat(msg) local channel, text = msg.args[1], msg.args[2] if text:sub(1, 1) == string.char(1) then - mt_irc.conn:invoke("OnCTCP", msg) + irc.conn:invoke("OnCTCP", msg) return end - if channel == mt_irc.conn.nick then - mt_irc.last_from = msg.user.nick - mt_irc.conn:invoke("PrivateMessage", msg) + if channel == irc.conn.nick then + irc.last_from = msg.user.nick + irc.conn:invoke("PrivateMessage", msg) else - mt_irc.last_from = channel - mt_irc.conn:invoke("OnChannelChat", msg) + irc.last_from = channel + irc.conn:invoke("OnChannelChat", msg) end end -function mt_irc.hooks.ctcp(msg) +function irc.hooks.ctcp(msg) local text = msg.args[2]:sub(2, -2) -- Remove ^C local args = text:split(' ') local command = args[1]:upper() local function reply(s) - mt_irc:queue(irc.msgs.notice(msg.user.nick, + irc:queue(irc.msgs.notice(msg.user.nick, ("\1%s %s\1"):format(command, s))) end - if command == "ACTION" and msg.args[1] == mt_irc.config.channel then + if command == "ACTION" and msg.args[1] == irc.config.channel then local action = text:sub(8, -1) - mt_irc:sendLocal(("* %s@IRC %s"):format(msg.user.nick, action)) + irc:sendLocal(("* %s@IRC %s"):format(msg.user.nick, action)) elseif command == "VERSION" then reply(("Minetest IRC mod version %s.") - :format(mt_irc.version)) + :format(irc.version)) elseif command == "PING" then reply(args[2]) elseif command == "TIME" then @@ -91,7 +91,7 @@ function mt_irc.hooks.ctcp(msg) end -function mt_irc.hooks.channelChat(msg) +function irc.hooks.channelChat(msg) local text = normalize(msg.args[2]) -- Support multiple servers in a channel better by converting: @@ -107,58 +107,58 @@ function mt_irc.hooks.channelChat(msg) local foundaction, _, actionnick, actionmessage = text:find("^%* ([^%s]+) (.*)$") - mt_irc:check_botcmd(msg) + irc:check_botcmd(msg) if text:sub(1, 5) == "[off]" then return elseif foundchat then - mt_irc:sendLocal(("<%s@%s> %s") + irc:sendLocal(("<%s@%s> %s") :format(chatnick, msg.user.nick, chatmessage)) elseif foundjoin then - mt_irc:sendLocal(("*** %s joined %s") + irc:sendLocal(("*** %s joined %s") :format(joinnick, msg.user.nick)) elseif foundleave then - mt_irc:sendLocal(("*** %s left %s") + irc:sendLocal(("*** %s left %s") :format(leavenick, msg.user.nick)) elseif foundaction then - mt_irc:sendLocal(("* %s@%s %s") + irc:sendLocal(("* %s@%s %s") :format(actionnick, msg.user.nick, actionmessage)) else - mt_irc:sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text)) + irc:sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text)) end end -function mt_irc.hooks.pm(msg) +function irc.hooks.pm(msg) -- Trim prefix if it is found local text = msg.args[2] - local prefix = mt_irc.config.command_prefix + local prefix = irc.config.command_prefix if prefix and text:sub(1, #prefix) == prefix then text = text:sub(#prefix + 1) end - mt_irc:bot_command(msg, text) + irc:bot_command(msg, text) end -function mt_irc.hooks.kick(channel, target, prefix, reason) - if target == mt_irc.conn.nick then +function irc.hooks.kick(channel, target, prefix, reason) + if target == irc.conn.nick then minetest.chat_send_all("IRC: kicked from "..channel.." by "..prefix.nick..".") - mt_irc:disconnect("Kicked") + irc:disconnect("Kicked") else - mt_irc:sendLocal(("-!- %s was kicked from %s by %s [%s]") + irc:sendLocal(("-!- %s was kicked from %s by %s [%s]") :format(target, channel, prefix.nick, reason)) end end -function mt_irc.hooks.notice(user, target, message) - if user.nick and target == mt_irc.config.channel then - mt_irc:sendLocal("-"..user.nick.."@IRC- "..message) +function irc.hooks.notice(user, target, message) + if user.nick and target == irc.config.channel then + irc:sendLocal("-"..user.nick.."@IRC- "..message) end end -function mt_irc.hooks.mode(user, target, modes, ...) +function irc.hooks.mode(user, target, modes, ...) local by = "" if user.nick then by = " by "..user.nick @@ -173,37 +173,37 @@ function mt_irc.hooks.mode(user, target, modes, ...) end -function mt_irc.hooks.nick(user, newNick) - mt_irc:sendLocal(("-!- %s is now known as %s") +function irc.hooks.nick(user, newNick) + irc:sendLocal(("-!- %s is now known as %s") :format(user.nick, newNick)) end -function mt_irc.hooks.join(user, channel) - mt_irc:sendLocal(("-!- %s joined %s") +function irc.hooks.join(user, channel) + irc:sendLocal(("-!- %s joined %s") :format(user.nick, channel)) end -function mt_irc.hooks.part(user, channel, reason) +function irc.hooks.part(user, channel, reason) reason = reason or "" - mt_irc:sendLocal(("-!- %s has left %s [%s]") + irc:sendLocal(("-!- %s has left %s [%s]") :format(user.nick, channel, reason)) end -function mt_irc.hooks.quit(user, reason) - mt_irc:sendLocal(("-!- %s has quit [%s]") +function irc.hooks.quit(user, reason) + irc:sendLocal(("-!- %s has quit [%s]") :format(user.nick, reason)) end -function mt_irc.hooks.disconnect(message, isError) - mt_irc.connected = false +function irc.hooks.disconnect(message, isError) + irc.connected = false if isError then minetest.log("error", "IRC: Error: Disconnected, reconnecting in one minute.") minetest.chat_send_all("IRC: Error: Disconnected, reconnecting in one minute.") - minetest.after(60, mt_irc.connect, mt_irc) + minetest.after(60, irc.connect, irc) else minetest.log("action", "IRC: Disconnected.") minetest.chat_send_all("IRC: Disconnected.") @@ -211,13 +211,13 @@ function mt_irc.hooks.disconnect(message, isError) end -function mt_irc.hooks.preregister(conn) - if not (mt_irc.config["sasl.user"] and mt_irc.config["sasl.pass"]) then return end - local authString = mt_irc.b64e( +function irc.hooks.preregister(conn) + if not (irc.config["sasl.user"] and irc.config["sasl.pass"]) then return end + local authString = irc.b64e( ("%s\x00%s\x00%s"):format( - mt_irc.config["sasl.user"], - mt_irc.config["sasl.user"], - mt_irc.config["sasl.pass"]) + irc.config["sasl.user"], + irc.config["sasl.user"], + irc.config["sasl.pass"]) ) conn:send("CAP REQ sasl") conn:send("AUTHENTICATE PLAIN") @@ -226,19 +226,19 @@ function mt_irc.hooks.preregister(conn) end -mt_irc:register_hook("PreRegister", mt_irc.hooks.preregister) -mt_irc:register_hook("OnRaw", mt_irc.hooks.raw) -mt_irc:register_hook("OnSend", mt_irc.hooks.send) -mt_irc:register_hook("DoPrivmsg", mt_irc.hooks.chat) -mt_irc:register_hook("OnPart", mt_irc.hooks.part) -mt_irc:register_hook("OnKick", mt_irc.hooks.kick) -mt_irc:register_hook("OnJoin", mt_irc.hooks.join) -mt_irc:register_hook("OnQuit", mt_irc.hooks.quit) -mt_irc:register_hook("NickChange", mt_irc.hooks.nick) -mt_irc:register_hook("OnCTCP", mt_irc.hooks.ctcp) -mt_irc:register_hook("PrivateMessage", mt_irc.hooks.pm) -mt_irc:register_hook("OnNotice", mt_irc.hooks.notice) -mt_irc:register_hook("OnChannelChat", mt_irc.hooks.channelChat) -mt_irc:register_hook("OnModeChange", mt_irc.hooks.mode) -mt_irc:register_hook("OnDisconnect", mt_irc.hooks.disconnect) +irc:register_hook("PreRegister", irc.hooks.preregister) +irc:register_hook("OnRaw", irc.hooks.raw) +irc:register_hook("OnSend", irc.hooks.send) +irc:register_hook("DoPrivmsg", irc.hooks.chat) +irc:register_hook("OnPart", irc.hooks.part) +irc:register_hook("OnKick", irc.hooks.kick) +irc:register_hook("OnJoin", irc.hooks.join) +irc:register_hook("OnQuit", irc.hooks.quit) +irc:register_hook("NickChange", irc.hooks.nick) +irc:register_hook("OnCTCP", irc.hooks.ctcp) +irc:register_hook("PrivateMessage", irc.hooks.pm) +irc:register_hook("OnNotice", irc.hooks.notice) +irc:register_hook("OnChannelChat", irc.hooks.channelChat) +irc:register_hook("OnModeChange", irc.hooks.mode) +irc:register_hook("OnDisconnect", irc.hooks.disconnect) diff --git a/init.lua b/init.lua index 3d8ae8f..2aa89bd 100644 --- a/init.lua +++ b/init.lua @@ -10,7 +10,7 @@ package.path = -- For LuaIRC to find its files ..modpath.."/?.lua" -mt_irc = { +irc = { version = "0.2.0", connected = false, cur_time = 0, @@ -20,7 +20,9 @@ mt_irc = { modpath = modpath, lib = require("irc"), } -local irc = mt_irc.lib + +-- Compatibility +mt_irc = irc dofile(modpath.."/config.lua") dofile(modpath.."/messages.lua") @@ -29,10 +31,10 @@ dofile(modpath.."/callback.lua") dofile(modpath.."/chatcmds.lua") dofile(modpath.."/botcmds.lua") dofile(modpath.."/util.lua") -if mt_irc.config.enable_player_part then +if irc.config.enable_player_part then dofile(modpath.."/player_part.lua") else - setmetatable(mt_irc.joined_players, {__index = function(index) return true end}) + setmetatable(irc.joined_players, {__index = function(index) return true end}) end minetest.register_privilege("irc_admin", { @@ -42,9 +44,9 @@ minetest.register_privilege("irc_admin", { local stepnum = 0 -minetest.register_globalstep(function(dtime) return mt_irc:step(dtime) end) +minetest.register_globalstep(function(dtime) return irc:step(dtime) end) -function mt_irc:step(dtime) +function irc:step(dtime) if stepnum == 3 then if self.config.auto_connect then self:connect() @@ -63,12 +65,12 @@ function mt_irc:step(dtime) end -function mt_irc:connect() +function irc:connect() if self.connected then minetest.log("error", "IRC: Ignoring attempt to connect when already connected.") return end - self.conn = irc.new({ + self.conn = irc.lib.new({ nick = self.config.nick, username = "Minetest", realname = "Minetest", @@ -102,7 +104,7 @@ function mt_irc:connect() end -function mt_irc:disconnect(message) +function irc:disconnect(message) if self.connected then --The OnDisconnect hook will clear self.connected and print a disconnect message self.conn:disconnect(message) @@ -110,7 +112,7 @@ function mt_irc:disconnect(message) end -function mt_irc:say(to, message) +function irc:say(to, message) if not message then message = to to = self.config.channel @@ -121,18 +123,18 @@ function mt_irc:say(to, message) end -function mt_irc:reply(message) +function irc:reply(message) if not self.last_from then return end self:say(self.last_from, message) end -function mt_irc:send(msg) +function irc:send(msg) self.conn:send(msg) end -function mt_irc:queue(msg) +function irc:queue(msg) self.conn:queue(msg) end diff --git a/messages.lua b/messages.lua index ebb1abc..45ef4c4 100644 --- a/messages.lua +++ b/messages.lua @@ -1,13 +1,13 @@ -- This file is licensed under the terms of the BSD 2-clause license. -- See LICENSE.txt for details. -mt_irc.msgs = mt_irc.lib.msgs +irc.msgs = irc.lib.msgs -function mt_irc:sendLocal(message) +function irc:sendLocal(message) minetest.chat_send_all(message) end -function mt_irc:playerMessage(name, message) +function irc:playerMessage(name, message) return ("<%s> %s"):format(name, message) end diff --git a/player_part.lua b/player_part.lua index 3cdc388..d39d2ab 100644 --- a/player_part.lua +++ b/player_part.lua @@ -2,7 +2,7 @@ -- See LICENSE.txt for details. -function mt_irc:player_part(name) +function irc:player_part(name) if not self.joined_players[name] then minetest.chat_send_player(name, "IRC: You are not in the channel.") return @@ -11,7 +11,7 @@ function mt_irc:player_part(name) minetest.chat_send_player(name, "IRC: You are now out of the channel.") end -function mt_irc:player_join(name) +function irc:player_join(name) if self.joined_players[name] then minetest.chat_send_player(name, "IRC: You are already in the channel.") return @@ -25,7 +25,7 @@ minetest.register_chatcommand("join", { description = "Join the IRC channel", privs = {shout=true}, func = function(name, param) - mt_irc:player_join(name) + irc:player_join(name) end }) @@ -33,7 +33,7 @@ minetest.register_chatcommand("part", { description = "Part the IRC channel", privs = {shout=true}, func = function(name, param) - mt_irc:player_part(name) + irc:player_part(name) end }) @@ -42,7 +42,7 @@ minetest.register_chatcommand("who", { privs = {}, func = function(name, param) local s = "" - for name, _ in pairs(mt_irc.joined_players) do + for name, _ in pairs(irc.joined_players) do s = s..", "..name end minetest.chat_send_player(name, "Players On Channel:"..s) @@ -52,16 +52,16 @@ minetest.register_chatcommand("who", { minetest.register_on_joinplayer(function(player) local name = player:get_player_name() - mt_irc.joined_players[name] = mt_irc.config.auto_join + irc.joined_players[name] = irc.config.auto_join end) minetest.register_on_leaveplayer(function(player) local name = player:get_player_name() - mt_irc.joined_players[name] = nil + irc.joined_players[name] = nil end) -function mt_irc:sendLocal(message) +function irc:sendLocal(message) for name, _ in pairs(self.joined_players) do minetest.chat_send_player(name, message) end diff --git a/util.lua b/util.lua index 72710b5..bc5b90b 100644 --- a/util.lua +++ b/util.lua @@ -1,6 +1,6 @@ --Base 64 encode -- for SASL authentication local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -function mt_irc.b64e(data) +function irc.b64e(data) return ((data:gsub('.', function(x) local r,b='',x:byte() for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end