irc/init.lua

173 lines
4.1 KiB
Lua
Raw Normal View History

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.
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 =
-- To find LuaIRC's init.lua
modpath.."/?/init.lua;"
-- For LuaIRC to find its files
..modpath.."/?.lua;"
2015-05-16 16:54:24 -07:00
..ie.package.path
2013-04-29 15:07:44 -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.
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..
";/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..
";/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
local lib = ie.require("irc")
irc = {
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 = {},
modpath = modpath,
2015-05-16 16:54:24 -07:00
lib = lib,
2013-04-29 15:07:44 -07:00
}
-- Compatibility
mt_irc = irc
2013-04-29 15:07:44 -07:00
dofile(modpath.."/config.lua")
dofile(modpath.."/messages.lua")
2015-05-16 16:54:24 -07:00
loadfile(modpath.."/hooks.lua")(ie)
dofile(modpath.."/callback.lua")
dofile(modpath.."/chatcmds.lua")
dofile(modpath.."/botcmds.lua")
-- Restore old (safe) require
require = old_require
if irc.config.enable_player_part then
dofile(modpath.."/player_part.lua")
2013-04-29 15:07:44 -07:00
else
setmetatable(irc.joined_players, {__index = function(index) return true end})
2013-04-29 15:07:44 -07:00
end
2013-01-08 07:50:47 -08:00
2013-04-29 15:07:44 -07:00
minetest.register_privilege("irc_admin", {
description = "Allow IRC administrative tasks to be performed.",
give_to_singleplayer = true
})
2012-11-30 19:06:15 -08:00
local stepnum = 0
2012-11-30 19:06:15 -08:00
minetest.register_globalstep(function(dtime) return irc:step(dtime) end)
2012-11-30 19:06:15 -08:00
function irc:step(dtime)
if stepnum == 3 then
if self.config.auto_connect then
self:connect()
end
end
stepnum = stepnum + 1
2013-04-29 15:07:44 -07:00
if not self.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
2014-05-06 12:26:13 -07:00
local good, err = xpcall(function() self.conn:think() end, debug.traceback)
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
2013-04-29 15:07:44 -07:00
function irc:connect()
2013-04-29 15:07:44 -07:00
if self.connected then
minetest.log("error", "IRC: Ignoring attempt to connect when already connected.")
return
2013-04-25 14:00:44 -07:00
end
self.conn = irc.lib.new({
2013-04-29 15:07:44 -07:00
nick = self.config.nick,
username = "Minetest",
realname = "Minetest",
})
self:doHook(self.conn)
2014-05-28 08:05:08 -07:00
local good, message = pcall(function()
2013-10-19 15:45:39 -07:00
self.conn:connect({
host = self.config.server,
port = self.config.port,
password = self.config.password,
2013-10-19 15:45:39 -07:00
timeout = self.config.timeout,
reconnect = self.config.reconnect,
2013-10-19 15:45:39 -07:00
secure = self.config.secure
2013-04-29 15:07:44 -07:00
})
end)
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)
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
2013-04-29 15:07:44 -07:00
if self.config.NSPass then
self:say("NickServ", "IDENTIFY "..self.config.NSPass)
2013-04-25 14:00:44 -07:00
end
2013-04-29 15:07:44 -07:00
self.conn:join(self.config.channel, self.config.key)
self.connected = true
minetest.log("action", "IRC: Connected!")
minetest.chat_send_all("IRC: Connected!")
end
function irc:disconnect(message)
2013-04-29 15:07:44 -07:00
if self.connected then
--The OnDisconnect hook will clear self.connected and print a disconnect message
self.conn:disconnect(message)
2013-04-25 14:00:44 -07:00
end
2012-12-28 03:41:10 -08:00
end
function irc:say(to, message)
2013-04-29 15:07:44 -07:00
if not message then
message = to
to = self.config.channel
end
to = to or self.config.channel
2014-05-06 12:26:13 -07:00
self:queue(irc.msgs.privmsg(to, message))
end
2012-12-21 19:16:28 -08:00
function irc:reply(message)
2013-12-02 15:15:29 -08:00
if not self.last_from then
return
end
2014-07-07 20:22:52 -07:00
message = message:gsub("[\r\n%z]", " \\n ")
2013-12-02 15:15:29 -08:00
self:say(self.last_from, message)
end
function irc:send(msg)
if not self.connected then return end
2014-05-06 12:26:13 -07:00
self.conn:send(msg)
2012-12-21 19:16:28 -08:00
end
2013-04-29 15:07:44 -07:00
function irc:queue(msg)
if not self.connected then return end
2014-05-06 12:26:13 -07:00
self.conn:queue(msg)
end
2013-04-29 15:07:44 -07:00