Major cleanup.

- Use `irc.foo` notation instead of `irc:foo`. It still
  supports the `irc:foo` usage, but will helpfully issue
  a warning with the location of the offending code.
- Remove unused arguments from functions.
This commit is contained in:
Diego Martínez 2017-03-15 21:33:47 -03:00
parent 5f8850bc15
commit 6bf4e111c2
9 changed files with 180 additions and 149 deletions

View File

@ -1,5 +1,4 @@
unused_args = false
allow_defined_top = true allow_defined_top = true
read_globals = { read_globals = {
@ -11,4 +10,5 @@ exclude_files = {
} }
globals = { globals = {
"irc",
} }

16
API.md
View File

@ -13,14 +13,14 @@ to your mod's `depends.txt` file.
Reference Reference
--------- ---------
irc:say([name,] message) irc.say([name,] message)
Sends <message> to either the channel (if <name> is nil or not specified), Sends <message> to either the channel (if <name> is nil or not specified),
or to the given user (if <name> is specified). or to the given user (if <name> is specified).
Example: Example:
irc:say("Hello, Channel!") irc.say("Hello, Channel!")
irc:say("john1234", "How are you?") irc.say("john1234", "How are you?")
irc:register_bot_command(name, cmdDef) irc.register_bot_command(name, cmdDef)
Registers a new bot command named <name>. Registers a new bot command named <name>.
When an user sends a private message to the bot with the command name, the When an user sends a private message to the bot with the command name, the
command's function is called. command's function is called.
@ -38,7 +38,7 @@ irc:register_bot_command(name, cmdDef)
end, end,
}; };
Example: Example:
irc:register_bot_command("hello", { irc.register_bot_command("hello", {
params = "", params = "",
description = "Greet user", description = "Greet user",
func = function(user, param) func = function(user, param)
@ -55,12 +55,12 @@ irc.joined_players[name]
-- Joe is talking on IRC -- Joe is talking on IRC
end end
irc:register_hook(name, func) irc.register_hook(name, func)
Registers a function to be called when an event happens. <name> is the name Registers a function to be called when an event happens. <name> is the name
of the event, and <func> is the function to be called. See HOOKS below of the event, and <func> is the function to be called. See HOOKS below
for more information for more information
Example: Example:
irc:register_hook("OnSend", function(line) irc.register_hook("OnSend", function(line)
print("SEND: "..line) print("SEND: "..line)
end) end)
@ -83,7 +83,7 @@ not modify these settings at runtime or you might crash the server!
Hooks Hooks
----- -----
The `irc:register_hook` function can register functions to be called The `irc.register_hook` function can register functions to be called
when some events happen. The events supported are the same as the LuaIRC when some events happen. The events supported are the same as the LuaIRC
ones with a few added (mostly for internal use). ones with a few added (mostly for internal use).
See src/LuaIRC/doc/irc.luadoc for more information. See src/LuaIRC/doc/irc.luadoc for more information.

View File

@ -15,7 +15,7 @@ local function nickequals(nick1, nick2)
return irclower(nick1) == irclower(nick2) return irclower(nick1) == irclower(nick2)
end end
function irc:check_botcmd(msg) function irc.check_botcmd(msg)
local prefix = irc.config.command_prefix local prefix = irc.config.command_prefix
local nick = irc.conn.nick local nick = irc.conn.nick
local text = msg.args[2] local text = msg.args[2]
@ -25,18 +25,18 @@ function irc:check_botcmd(msg)
-- First check for a nick prefix -- First check for a nick prefix
if nickequals(nickpart, nick) if nickequals(nickpart, nick)
and (suffix == ": " or suffix == ", ") then and (suffix == ": " or suffix == ", ") then
self:bot_command(msg, text:sub(#nick + 3)) irc.bot_command(msg, text:sub(#nick + 3))
return true return true
-- Then check for the configured prefix -- Then check for the configured prefix
elseif prefix and text:sub(1, #prefix):lower() == prefix:lower() then elseif prefix and text:sub(1, #prefix):lower() == prefix:lower() then
self:bot_command(msg, text:sub(#prefix + 1)) irc.bot_command(msg, text:sub(#prefix + 1))
return true return true
end end
return false return false
end end
function irc:bot_command(msg, text) function irc.bot_command(msg, text)
-- Remove leading whitespace -- Remove leading whitespace
text = text:match("^%s*(.*)") text = text:match("^%s*(.*)")
if text:sub(1, 1) == "@" then if text:sub(1, 1) == "@" then
@ -44,15 +44,15 @@ function irc:bot_command(msg, text)
if not player_to then if not player_to then
return return
elseif not minetest.get_player_by_name(player_to) then elseif not minetest.get_player_by_name(player_to) then
irc:reply("User '"..player_to.."' is not in the game.") irc.reply("User '"..player_to.."' is not in the game.")
return return
elseif not irc.joined_players[player_to] then elseif not irc.joined_players[player_to] then
irc:reply("User '"..player_to.."' is not using IRC.") irc.reply("User '"..player_to.."' is not using IRC.")
return return
end end
minetest.chat_send_player(player_to, minetest.chat_send_player(player_to,
"PM from "..msg.user.nick.."@IRC: "..message, false) "PM from "..msg.user.nick.."@IRC: "..message, false)
irc:reply("Message sent!") irc.reply("Message sent!")
return return
end end
local pos = text:find(" ", 1, true) local pos = text:find(" ", 1, true)
@ -65,36 +65,36 @@ function irc:bot_command(msg, text)
args = "" args = ""
end end
if not self.bot_commands[cmd] then if not irc.bot_commands[cmd] then
self:reply("Unknown command '"..cmd.."'. Try 'help'." irc.reply("Unknown command '"..cmd.."'. Try 'help'."
.." Or use @playername <message> to send a private message") .." Or use @playername <message> to send a private message")
return return
end end
local _, message = self.bot_commands[cmd].func(msg.user, args) local _, message = irc.bot_commands[cmd].func(msg.user, args)
if message then if message then
self:reply(message) irc.reply(message)
end end
end end
function irc:register_bot_command(name, def) function irc.register_bot_command(name, def)
if (not def.func) or (type(def.func) ~= "function") then if (not def.func) or (type(def.func) ~= "function") then
error("Erroneous bot command definition. def.func missing.", 2) error("Erroneous bot command definition. def.func missing.", 2)
elseif name:sub(1, 1) == "@" then elseif name:sub(1, 1) == "@" then
error("Erroneous bot command name. Command name begins with '@'.", 2) error("Erroneous bot command name. Command name begins with '@'.", 2)
end end
self.bot_commands[name] = def irc.bot_commands[name] = def
end end
irc:register_bot_command("help", { irc.register_bot_command("help", {
params = "<command>", params = "<command>",
description = "Get help about a command", description = "Get help about a command",
func = function(user, args) func = function(_, args)
if args == "" then if args == "" then
local cmdlist = { } local cmdlist = { }
for name, cmd in pairs(irc.bot_commands) do for name in pairs(irc.bot_commands) do
cmdlist[#cmdlist+1] = name cmdlist[#cmdlist+1] = name
end end
return true, "Available commands: "..table.concat(cmdlist, ", ") return true, "Available commands: "..table.concat(cmdlist, ", ")
@ -116,20 +116,20 @@ irc:register_bot_command("help", {
}) })
irc:register_bot_command("list", { irc.register_bot_command("list", {
params = "", params = "",
description = "List available commands.", description = "List available commands.",
func = function(user, args) func = function()
return false, "The `list` command has been merged into `help`." return false, "The `list` command has been merged into `help`."
.." Use `help` with no arguments to get a list." .." Use `help` with no arguments to get a list."
end end
}) })
irc:register_bot_command("whereis", { irc.register_bot_command("whereis", {
params = "<player>", params = "<player>",
description = "Tell the location of <player>", description = "Tell the location of <player>",
func = function(user, args) func = function(_, args)
if args == "" then if args == "" then
return false, "Player name required." return false, "Player name required."
end end
@ -145,9 +145,9 @@ irc:register_bot_command("whereis", {
local starttime = os.time() local starttime = os.time()
irc:register_bot_command("uptime", { irc.register_bot_command("uptime", {
description = "Tell how much time the server has been up", description = "Tell how much time the server has been up",
func = function(user, args) func = function()
local cur_time = os.time() local cur_time = os.time()
local diff = os.difftime(cur_time, starttime) local diff = os.difftime(cur_time, starttime)
local fmt = "Server has been running for %d:%02d:%02d" local fmt = "Server has been running for %d:%02d:%02d"
@ -160,9 +160,9 @@ irc:register_bot_command("uptime", {
}) })
irc:register_bot_command("players", { irc.register_bot_command("players", {
description = "List the players on the server", description = "List the players on the server",
func = function(user, args) func = function()
local players = minetest.get_connected_players() local players = minetest.get_connected_players()
local names = {} local names = {}
for _, player in pairs(players) do for _, player in pairs(players) do

View File

@ -5,7 +5,7 @@
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
if irc.connected and irc.config.send_join_part then if irc.connected and irc.config.send_join_part then
irc:say("*** "..name.." joined the game") irc.say("*** "..name.." joined the game")
end end
end) end)
@ -13,7 +13,7 @@ end)
minetest.register_on_leaveplayer(function(player, timed_out) minetest.register_on_leaveplayer(function(player, timed_out)
local name = player:get_player_name() local name = player:get_player_name()
if irc.connected and irc.config.send_join_part then if irc.connected and irc.config.send_join_part then
irc:say("*** "..name.." left the game".. irc.say("*** "..name.." left the game"..
(timed_out and " (Timed out)" or "")) (timed_out and " (Timed out)" or ""))
end end
end) end)
@ -31,11 +31,11 @@ minetest.register_on_chat_message(function(name, message)
if nl then if nl then
message = message:sub(1, nl - 1) message = message:sub(1, nl - 1)
end end
irc:say(irc:playerMessage(name, message)) irc.say(irc.playerMessage(name, message))
end) end)
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()
irc:disconnect("Game shutting down.") irc.disconnect("Game shutting down.")
end) end)

View File

@ -20,7 +20,7 @@ minetest.register_chatcommand("irc_msg", {
local toname_l = toname:lower() local toname_l = toname:lower()
local validNick = false local validNick = false
local hint = "They have to be in the channel" local hint = "They have to be in the channel"
for nick, user in pairs(irc.conn.channels[irc.config.channel].users) do for nick in pairs(irc.conn.channels[irc.config.channel].users) do
if nick:lower() == toname_l then if nick:lower() == toname_l then
validNick = true validNick = true
break break
@ -33,7 +33,7 @@ minetest.register_chatcommand("irc_msg", {
if not validNick then if not validNick then
return false, "You can not message that user. ("..hint..")" return false, "You can not message that user. ("..hint..")"
end end
irc:say(toname, irc:playerMessage(name, message)) irc.say(toname, irc.playerMessage(name, message))
return true, "Message sent!" return true, "Message sent!"
end end
}) })
@ -42,13 +42,13 @@ minetest.register_chatcommand("irc_msg", {
minetest.register_chatcommand("irc_names", { minetest.register_chatcommand("irc_names", {
params = "", params = "",
description = "List the users in IRC.", description = "List the users in IRC.",
func = function(name, params) func = function()
if not irc.connected then if not irc.connected then
return false, "Not connected to IRC. Use /irc_connect to connect." return false, "Not connected to IRC. Use /irc_connect to connect."
end end
local users = { } local users = { }
for k, v in pairs(irc.conn.channels[irc.config.channel].users) do for nick in pairs(irc.conn.channels[irc.config.channel].users) do
table.insert(users, k) table.insert(users, nick)
end end
return true, "Users in IRC: "..table.concat(users, ", ") return true, "Users in IRC: "..table.concat(users, ", ")
end end
@ -58,12 +58,12 @@ minetest.register_chatcommand("irc_names", {
minetest.register_chatcommand("irc_connect", { minetest.register_chatcommand("irc_connect", {
description = "Connect to the IRC server.", description = "Connect to the IRC server.",
privs = {irc_admin=true}, privs = {irc_admin=true},
func = function(name, param) func = function(name)
if irc.connected then if irc.connected then
return false, "You are already connected to IRC." return false, "You are already connected to IRC."
end end
minetest.chat_send_player(name, "IRC: Connecting...") minetest.chat_send_player(name, "IRC: Connecting...")
irc:connect() irc.connect()
end end
}) })
@ -79,7 +79,7 @@ minetest.register_chatcommand("irc_disconnect", {
if param == "" then if param == "" then
param = "Manual disconnect by "..name param = "Manual disconnect by "..name
end end
irc:disconnect(param) irc.disconnect(param)
end end
}) })
@ -87,13 +87,13 @@ minetest.register_chatcommand("irc_disconnect", {
minetest.register_chatcommand("irc_reconnect", { minetest.register_chatcommand("irc_reconnect", {
description = "Reconnect to the IRC server.", description = "Reconnect to the IRC server.",
privs = {irc_admin=true}, privs = {irc_admin=true},
func = function(name, param) func = function(name)
if not irc.connected then if not irc.connected then
return false, "Not connected to IRC. Use /irc_connect to connect." return false, "Not connected to IRC. Use /irc_connect to connect."
end end
minetest.chat_send_player(name, "IRC: Reconnecting...") minetest.chat_send_player(name, "IRC: Reconnecting...")
irc:disconnect("Reconnecting...") irc.disconnect("Reconnecting...")
irc:connect() irc.connect()
end end
}) })
@ -106,7 +106,7 @@ minetest.register_chatcommand("irc_quote", {
if not irc.connected then if not irc.connected then
return false, "Not connected to IRC. Use /irc_connect to connect." return false, "Not connected to IRC. Use /irc_connect to connect."
end end
irc:queue(param) irc.queue(param)
minetest.chat_send_player(name, "Command sent!") minetest.chat_send_player(name, "Command sent!")
end end
}) })
@ -115,7 +115,7 @@ minetest.register_chatcommand("irc_quote", {
local oldme = minetest.chatcommands["me"].func local oldme = minetest.chatcommands["me"].func
-- luacheck: ignore -- luacheck: ignore
minetest.chatcommands["me"].func = function(name, param, ...) minetest.chatcommands["me"].func = function(name, param, ...)
irc:say(("* %s %s"):format(name, param)) irc.say(("* %s %s"):format(name, param))
return oldme(name, param, ...) return oldme(name, param, ...)
end end
@ -127,7 +127,7 @@ if irc.config.send_kicks and minetest.chatcommands["kick"] then
if not plname then if not plname then
return false, "Usage: /kick player [reason]" return false, "Usage: /kick player [reason]"
end end
irc:say(("*** Kicked %s.%s"):format(name, irc.say(("*** Kicked %s.%s"):format(name,
reason~="" and " Reason: "..reason or "")) reason~="" and " Reason: "..reason or ""))
return oldkick(name, param, ...) return oldkick(name, param, ...)
end end

View File

@ -20,8 +20,8 @@ local function normalize(text)
end end
function irc:doHook(conn) function irc.doHook(conn)
for name, hook in pairs(self.registered_hooks) do for name, hook in pairs(irc.registered_hooks) do
for _, func in pairs(hook) do for _, func in pairs(hook) do
conn:hook(name, func) conn:hook(name, func)
end end
@ -29,9 +29,9 @@ function irc:doHook(conn)
end end
function irc:register_hook(name, func) function irc.register_hook(name, func)
self.registered_hooks[name] = self.registered_hooks[name] or {} irc.registered_hooks[name] = irc.registered_hooks[name] or {}
table.insert(self.registered_hooks[name], func) table.insert(irc.registered_hooks[name], func)
end end
@ -80,13 +80,13 @@ function irc.hooks.ctcp(msg)
local command = args[1]:upper() local command = args[1]:upper()
local function reply(s) local function reply(s)
irc:queue(irc.msgs.notice(msg.user.nick, irc.queue(irc.msgs.notice(msg.user.nick,
("\1%s %s\1"):format(command, s))) ("\1%s %s\1"):format(command, s)))
end end
if command == "ACTION" and msg.args[1] == irc.config.channel then if command == "ACTION" and msg.args[1] == irc.config.channel then
local action = text:sub(8, -1) local action = text:sub(8, -1)
irc:sendLocal(("* %s@IRC %s"):format(msg.user.nick, action)) irc.sendLocal(("* %s@IRC %s"):format(msg.user.nick, action))
elseif command == "VERSION" then elseif command == "VERSION" then
reply(("Minetest version %s, IRC mod version %s.") reply(("Minetest version %s, IRC mod version %s.")
:format(get_core_version(), irc.version)) :format(get_core_version(), irc.version))
@ -105,7 +105,7 @@ function irc.hooks.channelChat(msg)
-- Don't let a user impersonate someone else by using the nick "IRC" -- Don't let a user impersonate someone else by using the nick "IRC"
if msg.user.nick == "IRC" then if msg.user.nick == "IRC" then
irc:sendLocal("<IRC@IRC> "..text) irc.sendLocal("<IRC@IRC> "..text)
return return
end end
@ -125,19 +125,19 @@ function irc.hooks.channelChat(msg)
if text:sub(1, 5) == "[off]" then if text:sub(1, 5) == "[off]" then
return return
elseif foundchat then elseif foundchat then
irc:sendLocal(("<%s@%s> %s") irc.sendLocal(("<%s@%s> %s")
:format(chatnick, msg.user.nick, chatmessage)) :format(chatnick, msg.user.nick, chatmessage))
elseif foundjoin then elseif foundjoin then
irc:sendLocal(("*** %s joined %s") irc.sendLocal(("*** %s joined %s")
:format(joinnick, msg.user.nick)) :format(joinnick, msg.user.nick))
elseif foundleave then elseif foundleave then
irc:sendLocal(("*** %s left %s") irc.sendLocal(("*** %s left %s")
:format(leavenick, msg.user.nick)) :format(leavenick, msg.user.nick))
elseif foundaction then elseif foundaction then
irc:sendLocal(("* %s@%s %s") irc.sendLocal(("* %s@%s %s")
:format(actionnick, msg.user.nick, actionmessage)) :format(actionnick, msg.user.nick, actionmessage))
else else
irc:sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text)) irc.sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text))
end end
end end
@ -149,16 +149,16 @@ function irc.hooks.pm(msg)
if prefix and text:sub(1, #prefix) == prefix then if prefix and text:sub(1, #prefix) == prefix then
text = text:sub(#prefix + 1) text = text:sub(#prefix + 1)
end end
irc:bot_command(msg, text) irc.bot_command(msg, text)
end end
function irc.hooks.kick(channel, target, prefix, reason) function irc.hooks.kick(channel, target, prefix, reason)
if target == irc.conn.nick then if target == irc.conn.nick then
minetest.chat_send_all("IRC: kicked from "..channel.." by "..prefix.nick..".") minetest.chat_send_all("IRC: kicked from "..channel.." by "..prefix.nick..".")
irc:disconnect("Kicked") irc.disconnect("Kicked")
else else
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)) :format(target, channel, prefix.nick, reason))
end end
end end
@ -166,7 +166,7 @@ end
function irc.hooks.notice(user, target, message) function irc.hooks.notice(user, target, message)
if user.nick and target == irc.config.channel then if user.nick and target == irc.config.channel then
irc:sendLocal("-"..user.nick.."@IRC- "..message) irc.sendLocal("-"..user.nick.."@IRC- "..message)
end end
end end
@ -187,31 +187,31 @@ end
function irc.hooks.nick(user, newNick) function irc.hooks.nick(user, newNick)
irc:sendLocal(("-!- %s is now known as %s") irc.sendLocal(("-!- %s is now known as %s")
:format(user.nick, newNick)) :format(user.nick, newNick))
end end
function irc.hooks.join(user, channel) function irc.hooks.join(user, channel)
irc:sendLocal(("-!- %s joined %s") irc.sendLocal(("-!- %s joined %s")
:format(user.nick, channel)) :format(user.nick, channel))
end end
function irc.hooks.part(user, channel, reason) function irc.hooks.part(user, channel, reason)
reason = reason or "" reason = reason or ""
irc:sendLocal(("-!- %s has left %s [%s]") irc.sendLocal(("-!- %s has left %s [%s]")
:format(user.nick, channel, reason)) :format(user.nick, channel, reason))
end end
function irc.hooks.quit(user, reason) function irc.hooks.quit(user, reason)
irc:sendLocal(("-!- %s has quit [%s]") irc.sendLocal(("-!- %s has quit [%s]")
:format(user.nick, reason)) :format(user.nick, reason))
end end
function irc.hooks.disconnect(message, isError) function irc.hooks.disconnect(_, isError)
irc.connected = false irc.connected = false
if isError then if isError then
minetest.log("error", "IRC: Error: Disconnected, reconnecting in one minute.") minetest.log("error", "IRC: Error: Disconnected, reconnecting in one minute.")
@ -239,19 +239,19 @@ function irc.hooks.preregister(conn)
end end
irc:register_hook("PreRegister", irc.hooks.preregister) irc.register_hook("PreRegister", irc.hooks.preregister)
irc:register_hook("OnRaw", irc.hooks.raw) irc.register_hook("OnRaw", irc.hooks.raw)
irc:register_hook("OnSend", irc.hooks.send) irc.register_hook("OnSend", irc.hooks.send)
irc:register_hook("DoPrivmsg", irc.hooks.chat) irc.register_hook("DoPrivmsg", irc.hooks.chat)
irc:register_hook("OnPart", irc.hooks.part) irc.register_hook("OnPart", irc.hooks.part)
irc:register_hook("OnKick", irc.hooks.kick) irc.register_hook("OnKick", irc.hooks.kick)
irc:register_hook("OnJoin", irc.hooks.join) irc.register_hook("OnJoin", irc.hooks.join)
irc:register_hook("OnQuit", irc.hooks.quit) irc.register_hook("OnQuit", irc.hooks.quit)
irc:register_hook("NickChange", irc.hooks.nick) irc.register_hook("NickChange", irc.hooks.nick)
irc:register_hook("OnCTCP", irc.hooks.ctcp) irc.register_hook("OnCTCP", irc.hooks.ctcp)
irc:register_hook("PrivateMessage", irc.hooks.pm) irc.register_hook("PrivateMessage", irc.hooks.pm)
irc:register_hook("OnNotice", irc.hooks.notice) irc.register_hook("OnNotice", irc.hooks.notice)
irc:register_hook("OnChannelChat", irc.hooks.channelChat) irc.register_hook("OnChannelChat", irc.hooks.channelChat)
irc:register_hook("OnModeChange", irc.hooks.mode) irc.register_hook("OnModeChange", irc.hooks.mode)
irc:register_hook("OnDisconnect", irc.hooks.disconnect) irc.register_hook("OnDisconnect", irc.hooks.disconnect)

117
init.lua
View File

@ -54,6 +54,37 @@ irc = {
-- Compatibility -- Compatibility
rawset(_G, "mt_irc", irc) rawset(_G, "mt_irc", irc)
local getinfo = debug.getinfo
local warned = { }
local function warn_deprecated(k)
local info = getinfo(3)
local loc = info.source..":"..info.currentline
if warned[loc] then return end
warned[loc] = true
print("COLON: "..tostring(k))
minetest.log("warning", "Deprecated use of colon notation when calling"
.." method `"..tostring(k).."` at "..loc)
end
-- This is a hack.
setmetatable(irc, {
__newindex = function(t, k, v)
if type(v) == "function" then
local f = v
v = function(me, ...)
if rawequal(me, t) then
warn_deprecated(k)
return f(...)
else
return f(me, ...)
end
end
end
rawset(t, k, v)
end,
})
dofile(modpath.."/config.lua") dofile(modpath.."/config.lua")
dofile(modpath.."/messages.lua") dofile(modpath.."/messages.lua")
loadfile(modpath.."/hooks.lua")(ie) loadfile(modpath.."/hooks.lua")(ie)
@ -68,7 +99,7 @@ rawset(_G, "module", old_module)
if irc.config.enable_player_part then if irc.config.enable_player_part then
dofile(modpath.."/player_part.lua") dofile(modpath.."/player_part.lua")
else else
setmetatable(irc.joined_players, {__index = function(index) return true end}) setmetatable(irc.joined_players, {__index = function() return true end})
end end
minetest.register_privilege("irc_admin", { minetest.register_privilege("irc_admin", {
@ -78,20 +109,20 @@ minetest.register_privilege("irc_admin", {
local stepnum = 0 local stepnum = 0
minetest.register_globalstep(function(dtime) return irc:step(dtime) end) minetest.register_globalstep(function(dtime) return irc.step(dtime) end)
function irc:step(dtime) function irc.step()
if stepnum == 3 then if stepnum == 3 then
if self.config.auto_connect then if irc.config.auto_connect then
self:connect() irc.connect()
end end
end end
stepnum = stepnum + 1 stepnum = stepnum + 1
if not self.connected then return end if not irc.connected then return end
-- Hooks will manage incoming messages and errors -- Hooks will manage incoming messages and errors
local good, err = xpcall(function() self.conn:think() end, debug.traceback) local good, err = xpcall(function() irc.conn:think() end, debug.traceback)
if not good then if not good then
print(err) print(err)
return return
@ -99,17 +130,17 @@ function irc:step(dtime)
end end
function irc:connect() function irc.connect()
if self.connected then if irc.connected then
minetest.log("error", "IRC: Ignoring attempt to connect when already connected.") minetest.log("error", "IRC: Ignoring attempt to connect when already connected.")
return return
end end
self.conn = irc.lib.new({ irc.conn = irc.lib.new({
nick = self.config.nick, nick = irc.config.nick,
username = "Minetest", username = "Minetest",
realname = "Minetest", realname = "Minetest",
}) })
self:doHook(self.conn) irc.doHook(irc.conn)
-- We need to swap the `require` function again since -- We need to swap the `require` function again since
-- LuaIRC `require`s `ssl` if `irc.secure` is true. -- LuaIRC `require`s `ssl` if `irc.secure` is true.
@ -117,13 +148,13 @@ function irc:connect()
require = ie.require require = ie.require
local good, message = pcall(function() local good, message = pcall(function()
self.conn:connect({ irc.conn:connect({
host = self.config.server, host = irc.config.server,
port = self.config.port, port = irc.config.port,
password = self.config.password, password = irc.config.password,
timeout = self.config.timeout, timeout = irc.config.timeout,
reconnect = self.config.reconnect, reconnect = irc.config.reconnect,
secure = self.config.secure secure = irc.config.secure
}) })
end) end)
@ -131,57 +162,57 @@ function irc:connect()
if not good then if not good then
minetest.log("error", ("IRC: Connection error: %s: %s -- Reconnecting in %d seconds...") minetest.log("error", ("IRC: Connection error: %s: %s -- Reconnecting in %d seconds...")
:format(self.config.server, message, self.config.reconnect)) :format(irc.config.server, message, irc.config.reconnect))
minetest.after(self.config.reconnect, function() self:connect() end) minetest.after(irc.config.reconnect, function() irc.connect() end)
return return
end end
if self.config.NSPass then if irc.config.NSPass then
self.conn:queue(irc.msgs.privmsg( irc.conn:queue(irc.msgs.privmsg(
"NickServ", "IDENTIFY "..self.config.NSPass)) "NickServ", "IDENTIFY "..irc.config.NSPass))
end end
self.conn:join(self.config.channel, self.config.key) irc.conn:join(irc.config.channel, irc.config.key)
self.connected = true irc.connected = true
minetest.log("action", "IRC: Connected!") minetest.log("action", "IRC: Connected!")
minetest.chat_send_all("IRC: Connected!") minetest.chat_send_all("IRC: Connected!")
end end
function irc:disconnect(message) function irc.disconnect(message)
if self.connected then if irc.connected then
--The OnDisconnect hook will clear self.connected and print a disconnect message --The OnDisconnect hook will clear irc.connected and print a disconnect message
self.conn:disconnect(message) irc.conn:disconnect(message)
end end
end end
function irc:say(to, message) function irc.say(to, message)
if not message then if not message then
message = to message = to
to = self.config.channel to = irc.config.channel
end end
to = to or self.config.channel to = to or irc.config.channel
self:queue(irc.msgs.privmsg(to, message)) irc.queue(irc.msgs.privmsg(to, message))
end end
function irc:reply(message) function irc.reply(message)
if not self.last_from then if not irc.last_from then
return return
end end
message = message:gsub("[\r\n%z]", " \\n ") message = message:gsub("[\r\n%z]", " \\n ")
self:say(self.last_from, message) irc.say(irc.last_from, message)
end end
function irc:send(msg) function irc.send(msg)
if not self.connected then return end if not irc.connected then return end
self.conn:send(msg) irc.conn:send(msg)
end end
function irc:queue(msg) function irc.queue(msg)
if not self.connected then return end if not irc.connected then return end
self.conn:queue(msg) irc.conn:queue(msg)
end end

View File

@ -3,15 +3,15 @@
irc.msgs = irc.lib.msgs irc.msgs = irc.lib.msgs
function irc:logChat(message) function irc.logChat(message)
minetest.log("action", "IRC CHAT: "..message) minetest.log("action", "IRC CHAT: "..message)
end end
function irc:sendLocal(message) function irc.sendLocal(message)
minetest.chat_send_all(message) minetest.chat_send_all(message)
irc:logChat(message) irc.logChat(message)
end end
function irc:playerMessage(name, message) function irc.playerMessage(name, message)
return ("<%s> %s"):format(name, message) return ("<%s> %s"):format(name, message)
end end

View File

@ -2,19 +2,19 @@
-- See LICENSE.txt for details. -- See LICENSE.txt for details.
function irc:player_part(name) function irc.player_part(name)
if not self.joined_players[name] then if not irc.joined_players[name] then
return false, "You are not in the channel" return false, "You are not in the channel"
end end
self.joined_players[name] = nil irc.joined_players[name] = nil
return true, "You left the channel" return true, "You left the channel"
end end
function irc:player_join(name) function irc.player_join(name)
if self.joined_players[name] then if irc.joined_players[name] then
return false, "You are already in the channel" return false, "You are already in the channel"
end end
self.joined_players[name] = true irc.joined_players[name] = true
return true, "You joined the channel" return true, "You joined the channel"
end end
@ -22,23 +22,23 @@ end
minetest.register_chatcommand("join", { minetest.register_chatcommand("join", {
description = "Join the IRC channel", description = "Join the IRC channel",
privs = {shout=true}, privs = {shout=true},
func = function(name, param) func = function(name)
return irc:player_join(name) return irc.player_join(name)
end end
}) })
minetest.register_chatcommand("part", { minetest.register_chatcommand("part", {
description = "Part the IRC channel", description = "Part the IRC channel",
privs = {shout=true}, privs = {shout=true},
func = function(name, param) func = function(name)
return irc:player_part(name) return irc.player_part(name)
end end
}) })
minetest.register_chatcommand("who", { minetest.register_chatcommand("who", {
description = "Tell who is currently on the channel", description = "Tell who is currently on the channel",
privs = {}, privs = {},
func = function(name, param) func = function()
local out, n = { }, 0 local out, n = { }, 0
for plname in pairs(irc.joined_players) do for plname in pairs(irc.joined_players) do
n = n + 1 n = n + 1
@ -61,9 +61,9 @@ minetest.register_on_leaveplayer(function(player)
irc.joined_players[name] = nil irc.joined_players[name] = nil
end) end)
function irc:sendLocal(message) function irc.sendLocal(message)
for name, _ in pairs(self.joined_players) do for name, _ in pairs(irc.joined_players) do
minetest.chat_send_player(name, message) minetest.chat_send_player(name, message)
end end
irc:logChat(message) irc.logChat(message)
end end