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