2013-04-30 00:07:44 +02:00
|
|
|
-- This file is licensed under the terms of the BSD 2-clause license.
|
|
|
|
-- See LICENSE.txt for details.
|
|
|
|
|
2014-05-16 02:09:42 +02:00
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
|
2015-05-17 01:54:24 +02:00
|
|
|
-- Handle mod security if needed
|
|
|
|
local ie, req_ie = _G, minetest.request_insecure_environment
|
|
|
|
if req_ie then ie = req_ie() end
|
|
|
|
if not ie then
|
|
|
|
error("The IRC mod requires access to insecure functions in order "..
|
|
|
|
"to work. Please add the irc mod to your secure.trusted_mods "..
|
|
|
|
"setting or disable the irc mod.")
|
|
|
|
end
|
|
|
|
|
|
|
|
ie.package.path =
|
2014-05-16 02:09:42 +02:00
|
|
|
-- To find LuaIRC's init.lua
|
2014-06-06 17:06:18 +02:00
|
|
|
modpath.."/?/init.lua;"
|
2014-05-16 02:09:42 +02:00
|
|
|
-- For LuaIRC to find its files
|
2014-06-06 17:06:18 +02:00
|
|
|
..modpath.."/?.lua;"
|
2015-05-17 01:54:24 +02:00
|
|
|
..ie.package.path
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2014-09-10 20:00:49 +02:00
|
|
|
-- The build of Lua that Minetest comes with only looks for libraries under
|
|
|
|
-- /usr/local/share and /usr/local/lib but LuaSocket is often installed under
|
|
|
|
-- /usr/share and /usr/lib.
|
2015-04-03 18:42:57 +02:00
|
|
|
if not rawget(_G, "jit") and package.config:sub(1, 1) == "/" then
|
2015-05-17 01:54:24 +02:00
|
|
|
ie.package.path = ie.package.path..
|
2014-09-10 20:00:49 +02:00
|
|
|
";/usr/share/lua/5.1/?.lua"..
|
|
|
|
";/usr/share/lua/5.1/?/init.lua"
|
2015-05-17 01:54:24 +02:00
|
|
|
ie.package.cpath = ie.package.cpath..
|
2014-09-10 20:00:49 +02:00
|
|
|
";/usr/lib/lua/5.1/?.so"
|
|
|
|
end
|
|
|
|
|
2015-05-17 01:54:24 +02:00
|
|
|
-- Temporarily set require so that LuaIRC can access it
|
|
|
|
local old_require = require
|
|
|
|
require = ie.require
|
2016-12-01 06:57:56 +01:00
|
|
|
|
|
|
|
-- Silence warnings about `module` in `ltn12`.
|
|
|
|
local old_module = rawget(_G, "module")
|
|
|
|
rawset(_G, "module", ie.module)
|
|
|
|
|
2015-05-17 01:54:24 +02:00
|
|
|
local lib = ie.require("irc")
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
irc = {
|
2014-05-06 21:26:13 +02:00
|
|
|
version = "0.2.0",
|
2013-04-30 00:07:44 +02:00
|
|
|
connected = false,
|
|
|
|
cur_time = 0,
|
|
|
|
message_buffer = {},
|
|
|
|
recent_message_count = 0,
|
|
|
|
joined_players = {},
|
2014-05-16 02:09:42 +02:00
|
|
|
modpath = modpath,
|
2015-05-17 01:54:24 +02:00
|
|
|
lib = lib,
|
2013-04-30 00:07:44 +02:00
|
|
|
}
|
2014-05-26 04:52:24 +02:00
|
|
|
|
|
|
|
-- Compatibility
|
2017-02-16 06:36:04 +01:00
|
|
|
rawset(_G, "mt_irc", irc)
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2014-05-16 02:09:42 +02:00
|
|
|
dofile(modpath.."/config.lua")
|
|
|
|
dofile(modpath.."/messages.lua")
|
2015-05-17 01:54:24 +02:00
|
|
|
loadfile(modpath.."/hooks.lua")(ie)
|
2014-05-16 02:09:42 +02:00
|
|
|
dofile(modpath.."/callback.lua")
|
|
|
|
dofile(modpath.."/chatcmds.lua")
|
|
|
|
dofile(modpath.."/botcmds.lua")
|
2016-11-10 02:42:51 +01:00
|
|
|
|
2016-12-01 06:57:56 +01:00
|
|
|
-- Restore old (safe) functions
|
2016-11-10 02:42:51 +01:00
|
|
|
require = old_require
|
2016-12-01 06:57:56 +01:00
|
|
|
rawset(_G, "module", old_module)
|
2016-11-10 02:42:51 +01:00
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
if irc.config.enable_player_part then
|
2014-05-16 02:09:42 +02:00
|
|
|
dofile(modpath.."/player_part.lua")
|
2013-04-30 00:07:44 +02:00
|
|
|
else
|
2017-03-16 01:33:47 +01:00
|
|
|
setmetatable(irc.joined_players, {__index = function() return true end})
|
2013-04-30 00:07:44 +02:00
|
|
|
end
|
2013-01-08 16:50:47 +01:00
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
minetest.register_privilege("irc_admin", {
|
|
|
|
description = "Allow IRC administrative tasks to be performed.",
|
|
|
|
give_to_singleplayer = true
|
|
|
|
})
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2014-02-27 06:21:58 +01:00
|
|
|
local stepnum = 0
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
minetest.register_globalstep(function(dtime) return irc.step(dtime) end)
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.step()
|
2014-02-27 06:21:58 +01:00
|
|
|
if stepnum == 3 then
|
2017-03-16 01:33:47 +01:00
|
|
|
if irc.config.auto_connect then
|
|
|
|
irc.connect()
|
2014-02-22 19:49:47 +01:00
|
|
|
end
|
|
|
|
end
|
2014-02-27 06:21:58 +01:00
|
|
|
stepnum = stepnum + 1
|
2014-02-22 19:49:47 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
if not irc.connected then return end
|
2012-12-14 23:33:44 +01:00
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
-- Hooks will manage incoming messages and errors
|
2017-03-16 01:33:47 +01:00
|
|
|
local good, err = xpcall(function() irc.conn:think() end, debug.traceback)
|
2014-05-06 21:26:13 +02:00
|
|
|
if not good then
|
|
|
|
print(err)
|
2013-04-30 00:07:44 +02:00
|
|
|
return
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2012-12-22 04:16:28 +01:00
|
|
|
end
|
2012-12-16 16:56:02 +01:00
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.connect()
|
|
|
|
if irc.connected then
|
2013-04-30 00:07:44 +02:00
|
|
|
minetest.log("error", "IRC: Ignoring attempt to connect when already connected.")
|
|
|
|
return
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2017-03-16 01:33:47 +01:00
|
|
|
irc.conn = irc.lib.new({
|
|
|
|
nick = irc.config.nick,
|
2013-04-30 00:07:44 +02:00
|
|
|
username = "Minetest",
|
|
|
|
realname = "Minetest",
|
|
|
|
})
|
2017-03-16 01:33:47 +01:00
|
|
|
irc.doHook(irc.conn)
|
2016-12-03 07:13:48 +01:00
|
|
|
|
|
|
|
-- We need to swap the `require` function again since
|
|
|
|
-- LuaIRC `require`s `ssl` if `irc.secure` is true.
|
2017-02-16 06:36:04 +01:00
|
|
|
old_require = require
|
2016-12-03 07:13:48 +01:00
|
|
|
require = ie.require
|
|
|
|
|
2014-05-28 17:05:08 +02:00
|
|
|
local good, message = pcall(function()
|
2017-03-16 01:33:47 +01:00
|
|
|
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
|
2013-04-30 00:07:44 +02:00
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
2016-12-03 07:13:48 +01:00
|
|
|
require = old_require
|
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
if not good then
|
2016-05-22 07:43:14 +02:00
|
|
|
minetest.log("error", ("IRC: Connection error: %s: %s -- Reconnecting in %d seconds...")
|
2017-03-16 01:33:47 +01:00
|
|
|
:format(irc.config.server, message, irc.config.reconnect))
|
|
|
|
minetest.after(irc.config.reconnect, function() irc.connect() end)
|
2013-04-30 00:07:44 +02:00
|
|
|
return
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2012-12-22 04:16:28 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
if irc.config.NSPass then
|
|
|
|
irc.conn:queue(irc.msgs.privmsg(
|
|
|
|
"NickServ", "IDENTIFY "..irc.config.NSPass))
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
irc.conn:join(irc.config.channel, irc.config.key)
|
|
|
|
irc.connected = true
|
2013-04-30 00:07:44 +02:00
|
|
|
minetest.log("action", "IRC: Connected!")
|
|
|
|
minetest.chat_send_all("IRC: Connected!")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.disconnect(message)
|
|
|
|
if irc.connected then
|
|
|
|
--The OnDisconnect hook will clear irc.connected and print a disconnect message
|
|
|
|
irc.conn:disconnect(message)
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2012-12-28 12:41:10 +01:00
|
|
|
end
|
|
|
|
|
2012-12-31 16:53:01 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.say(to, message)
|
2013-04-30 00:07:44 +02:00
|
|
|
if not message then
|
|
|
|
message = to
|
2017-03-16 01:33:47 +01:00
|
|
|
to = irc.config.channel
|
2013-04-30 00:07:44 +02:00
|
|
|
end
|
2017-03-16 01:33:47 +01:00
|
|
|
to = to or irc.config.channel
|
2013-01-11 17:20:30 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
irc.queue(irc.msgs.privmsg(to, message))
|
2013-01-11 17:20:30 +01:00
|
|
|
end
|
|
|
|
|
2012-12-22 04:16:28 +01:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.reply(message)
|
|
|
|
if not irc.last_from then
|
2013-12-03 00:15:29 +01:00
|
|
|
return
|
|
|
|
end
|
2014-07-08 05:22:52 +02:00
|
|
|
message = message:gsub("[\r\n%z]", " \\n ")
|
2017-03-16 01:33:47 +01:00
|
|
|
irc.say(irc.last_from, message)
|
2013-12-03 00:15:29 +01:00
|
|
|
end
|
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.send(msg)
|
|
|
|
if not irc.connected then return end
|
|
|
|
irc.conn:send(msg)
|
2012-12-22 04:16:28 +01:00
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2017-03-16 01:33:47 +01:00
|
|
|
function irc.queue(msg)
|
|
|
|
if not irc.connected then return end
|
|
|
|
irc.conn:queue(msg)
|
2014-05-06 21:26:13 +02:00
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|