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
read_globals = {
@ -11,4 +10,5 @@ exclude_files = {
}
globals = {
"irc",
}

16
API.md
View File

@ -13,14 +13,14 @@ to your mod's `depends.txt` file.
Reference
---------
irc:say([name,] message)
irc.say([name,] message)
Sends <message> to either the channel (if <name> is nil or not specified),
or to the given user (if <name> is specified).
Example:
irc:say("Hello, Channel!")
irc:say("john1234", "How are you?")
irc.say("Hello, Channel!")
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>.
When an user sends a private message to the bot with the command name, the
command's function is called.
@ -38,7 +38,7 @@ irc:register_bot_command(name, cmdDef)
end,
};
Example:
irc:register_bot_command("hello", {
irc.register_bot_command("hello", {
params = "",
description = "Greet user",
func = function(user, param)
@ -55,12 +55,12 @@ irc.joined_players[name]
-- Joe is talking on IRC
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
of the event, and <func> is the function to be called. See HOOKS below
for more information
Example:
irc:register_hook("OnSend", function(line)
irc.register_hook("OnSend", function(line)
print("SEND: "..line)
end)
@ -83,7 +83,7 @@ not modify these settings at runtime or you might crash the server!
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
ones with a few added (mostly for internal use).
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)
end
function irc:check_botcmd(msg)
function irc.check_botcmd(msg)
local prefix = irc.config.command_prefix
local nick = irc.conn.nick
local text = msg.args[2]
@ -25,18 +25,18 @@ function irc:check_botcmd(msg)
-- First check for a nick prefix
if nickequals(nickpart, nick)
and (suffix == ": " or suffix == ", ") then
self:bot_command(msg, text:sub(#nick + 3))
irc.bot_command(msg, text:sub(#nick + 3))
return true
-- Then check for the configured prefix
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
end
return false
end
function irc:bot_command(msg, text)
function irc.bot_command(msg, text)
-- Remove leading whitespace
text = text:match("^%s*(.*)")
if text:sub(1, 1) == "@" then
@ -44,15 +44,15 @@ function irc:bot_command(msg, text)
if not player_to then
return
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
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
end
minetest.chat_send_player(player_to,
"PM from "..msg.user.nick.."@IRC: "..message, false)
irc:reply("Message sent!")
irc.reply("Message sent!")
return
end
local pos = text:find(" ", 1, true)
@ -65,36 +65,36 @@ function irc:bot_command(msg, text)
args = ""
end
if not self.bot_commands[cmd] then
self:reply("Unknown command '"..cmd.."'. Try 'help'."
if not irc.bot_commands[cmd] then
irc.reply("Unknown command '"..cmd.."'. Try 'help'."
.." Or use @playername <message> to send a private message")
return
end
local _, message = self.bot_commands[cmd].func(msg.user, args)
local _, message = irc.bot_commands[cmd].func(msg.user, args)
if message then
self:reply(message)
irc.reply(message)
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
error("Erroneous bot command definition. def.func missing.", 2)
elseif name:sub(1, 1) == "@" then
error("Erroneous bot command name. Command name begins with '@'.", 2)
end
self.bot_commands[name] = def
irc.bot_commands[name] = def
end
irc:register_bot_command("help", {
irc.register_bot_command("help", {
params = "<command>",
description = "Get help about a command",
func = function(user, args)
func = function(_, args)
if args == "" then
local cmdlist = { }
for name, cmd in pairs(irc.bot_commands) do
for name in pairs(irc.bot_commands) do
cmdlist[#cmdlist+1] = name
end
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 = "",
description = "List available commands.",
func = function(user, args)
func = function()
return false, "The `list` command has been merged into `help`."
.." Use `help` with no arguments to get a list."
end
})
irc:register_bot_command("whereis", {
irc.register_bot_command("whereis", {
params = "<player>",
description = "Tell the location of <player>",
func = function(user, args)
func = function(_, args)
if args == "" then
return false, "Player name required."
end
@ -145,9 +145,9 @@ irc:register_bot_command("whereis", {
local starttime = os.time()
irc:register_bot_command("uptime", {
irc.register_bot_command("uptime", {
description = "Tell how much time the server has been up",
func = function(user, args)
func = function()
local cur_time = os.time()
local diff = os.difftime(cur_time, starttime)
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",
func = function(user, args)
func = function()
local players = minetest.get_connected_players()
local names = {}
for _, player in pairs(players) do

View File

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

View File

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

View File

@ -20,8 +20,8 @@ local function normalize(text)
end
function irc:doHook(conn)
for name, hook in pairs(self.registered_hooks) do
function irc.doHook(conn)
for name, hook in pairs(irc.registered_hooks) do
for _, func in pairs(hook) do
conn:hook(name, func)
end
@ -29,9 +29,9 @@ function irc:doHook(conn)
end
function irc:register_hook(name, func)
self.registered_hooks[name] = self.registered_hooks[name] or {}
table.insert(self.registered_hooks[name], func)
function irc.register_hook(name, func)
irc.registered_hooks[name] = irc.registered_hooks[name] or {}
table.insert(irc.registered_hooks[name], func)
end
@ -80,13 +80,13 @@ function irc.hooks.ctcp(msg)
local command = args[1]:upper()
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)))
end
if command == "ACTION" and msg.args[1] == irc.config.channel then
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
reply(("Minetest version %s, IRC mod version %s.")
: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"
if msg.user.nick == "IRC" then
irc:sendLocal("<IRC@IRC> "..text)
irc.sendLocal("<IRC@IRC> "..text)
return
end
@ -125,19 +125,19 @@ function irc.hooks.channelChat(msg)
if text:sub(1, 5) == "[off]" then
return
elseif foundchat then
irc:sendLocal(("<%s@%s> %s")
irc.sendLocal(("<%s@%s> %s")
:format(chatnick, msg.user.nick, chatmessage))
elseif foundjoin then
irc:sendLocal(("*** %s joined %s")
irc.sendLocal(("*** %s joined %s")
:format(joinnick, msg.user.nick))
elseif foundleave then
irc:sendLocal(("*** %s left %s")
irc.sendLocal(("*** %s left %s")
:format(leavenick, msg.user.nick))
elseif foundaction then
irc:sendLocal(("* %s@%s %s")
irc.sendLocal(("* %s@%s %s")
:format(actionnick, msg.user.nick, actionmessage))
else
irc:sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text))
irc.sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text))
end
end
@ -149,16 +149,16 @@ function irc.hooks.pm(msg)
if prefix and text:sub(1, #prefix) == prefix then
text = text:sub(#prefix + 1)
end
irc:bot_command(msg, text)
irc.bot_command(msg, text)
end
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..".")
irc:disconnect("Kicked")
irc.disconnect("Kicked")
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))
end
end
@ -166,7 +166,7 @@ end
function irc.hooks.notice(user, target, message)
if user.nick and target == irc.config.channel then
irc:sendLocal("-"..user.nick.."@IRC- "..message)
irc.sendLocal("-"..user.nick.."@IRC- "..message)
end
end
@ -187,31 +187,31 @@ end
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))
end
function irc.hooks.join(user, channel)
irc:sendLocal(("-!- %s joined %s")
irc.sendLocal(("-!- %s joined %s")
:format(user.nick, channel))
end
function irc.hooks.part(user, channel, reason)
reason = reason or ""
irc:sendLocal(("-!- %s has left %s [%s]")
irc.sendLocal(("-!- %s has left %s [%s]")
:format(user.nick, channel, reason))
end
function irc.hooks.quit(user, reason)
irc:sendLocal(("-!- %s has quit [%s]")
irc.sendLocal(("-!- %s has quit [%s]")
:format(user.nick, reason))
end
function irc.hooks.disconnect(message, isError)
function irc.hooks.disconnect(_, isError)
irc.connected = false
if isError then
minetest.log("error", "IRC: Error: Disconnected, reconnecting in one minute.")
@ -239,19 +239,19 @@ function irc.hooks.preregister(conn)
end
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)
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)

117
init.lua
View File

@ -54,6 +54,37 @@ irc = {
-- Compatibility
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.."/messages.lua")
loadfile(modpath.."/hooks.lua")(ie)
@ -68,7 +99,7 @@ rawset(_G, "module", old_module)
if irc.config.enable_player_part then
dofile(modpath.."/player_part.lua")
else
setmetatable(irc.joined_players, {__index = function(index) return true end})
setmetatable(irc.joined_players, {__index = function() return true end})
end
minetest.register_privilege("irc_admin", {
@ -78,20 +109,20 @@ minetest.register_privilege("irc_admin", {
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 self.config.auto_connect then
self:connect()
if irc.config.auto_connect then
irc.connect()
end
end
stepnum = stepnum + 1
if not self.connected then return end
if not irc.connected then return end
-- 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
print(err)
return
@ -99,17 +130,17 @@ function irc:step(dtime)
end
function irc:connect()
if self.connected then
function irc.connect()
if irc.connected then
minetest.log("error", "IRC: Ignoring attempt to connect when already connected.")
return
end
self.conn = irc.lib.new({
nick = self.config.nick,
irc.conn = irc.lib.new({
nick = irc.config.nick,
username = "Minetest",
realname = "Minetest",
})
self:doHook(self.conn)
irc.doHook(irc.conn)
-- We need to swap the `require` function again since
-- LuaIRC `require`s `ssl` if `irc.secure` is true.
@ -117,13 +148,13 @@ function irc:connect()
require = ie.require
local good, message = pcall(function()
self.conn:connect({
host = self.config.server,
port = self.config.port,
password = self.config.password,
timeout = self.config.timeout,
reconnect = self.config.reconnect,
secure = self.config.secure
irc.conn:connect({
host = irc.config.server,
port = irc.config.port,
password = irc.config.password,
timeout = irc.config.timeout,
reconnect = irc.config.reconnect,
secure = irc.config.secure
})
end)
@ -131,57 +162,57 @@ function irc:connect()
if not good then
minetest.log("error", ("IRC: Connection error: %s: %s -- Reconnecting in %d seconds...")
:format(self.config.server, message, self.config.reconnect))
minetest.after(self.config.reconnect, function() self:connect() end)
:format(irc.config.server, message, irc.config.reconnect))
minetest.after(irc.config.reconnect, function() irc.connect() end)
return
end
if self.config.NSPass then
self.conn:queue(irc.msgs.privmsg(
"NickServ", "IDENTIFY "..self.config.NSPass))
if irc.config.NSPass then
irc.conn:queue(irc.msgs.privmsg(
"NickServ", "IDENTIFY "..irc.config.NSPass))
end
self.conn:join(self.config.channel, self.config.key)
self.connected = true
irc.conn:join(irc.config.channel, irc.config.key)
irc.connected = true
minetest.log("action", "IRC: Connected!")
minetest.chat_send_all("IRC: Connected!")
end
function irc:disconnect(message)
if self.connected then
--The OnDisconnect hook will clear self.connected and print a disconnect message
self.conn:disconnect(message)
function irc.disconnect(message)
if irc.connected then
--The OnDisconnect hook will clear irc.connected and print a disconnect message
irc.conn:disconnect(message)
end
end
function irc:say(to, message)
function irc.say(to, message)
if not message then
message = to
to = self.config.channel
to = irc.config.channel
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
function irc:reply(message)
if not self.last_from then
function irc.reply(message)
if not irc.last_from then
return
end
message = message:gsub("[\r\n%z]", " \\n ")
self:say(self.last_from, message)
irc.say(irc.last_from, message)
end
function irc:send(msg)
if not self.connected then return end
self.conn:send(msg)
function irc.send(msg)
if not irc.connected then return end
irc.conn:send(msg)
end
function irc:queue(msg)
if not self.connected then return end
self.conn:queue(msg)
function irc.queue(msg)
if not irc.connected then return end
irc.conn:queue(msg)
end

View File

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

View File

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