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())
|
|
|
|
|
|
|
|
package.path =
|
|
|
|
package.path..";"
|
|
|
|
-- To find LuaIRC's init.lua
|
|
|
|
..modpath.."/?/init.lua;"
|
|
|
|
-- For LuaIRC to find its files
|
|
|
|
..modpath.."/?.lua"
|
2013-04-30 00:07:44 +02:00
|
|
|
|
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,
|
|
|
|
lib = require("irc"),
|
2013-04-30 00:07:44 +02:00
|
|
|
}
|
2014-05-26 04:52:24 +02:00
|
|
|
|
|
|
|
-- Compatibility
|
|
|
|
mt_irc = irc
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2014-05-16 02:09:42 +02:00
|
|
|
dofile(modpath.."/config.lua")
|
|
|
|
dofile(modpath.."/messages.lua")
|
|
|
|
dofile(modpath.."/hooks.lua")
|
|
|
|
dofile(modpath.."/callback.lua")
|
|
|
|
dofile(modpath.."/chatcmds.lua")
|
|
|
|
dofile(modpath.."/botcmds.lua")
|
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
|
2014-05-26 04:52:24 +02:00
|
|
|
setmetatable(irc.joined_players, {__index = function(index) 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
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
minetest.register_globalstep(function(dtime) return irc:step(dtime) end)
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:step(dtime)
|
2014-02-27 06:21:58 +01:00
|
|
|
if stepnum == 3 then
|
2014-02-22 19:49:47 +01:00
|
|
|
if self.config.auto_connect then
|
|
|
|
self:connect()
|
|
|
|
end
|
|
|
|
end
|
2014-02-27 06:21:58 +01:00
|
|
|
stepnum = stepnum + 1
|
2014-02-22 19:49:47 +01:00
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
if not self.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
|
2014-05-06 21:26:13 +02:00
|
|
|
local good, err = xpcall(function() self.conn:think() end, debug.traceback)
|
|
|
|
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
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:connect()
|
2013-04-30 00:07:44 +02:00
|
|
|
if self.connected then
|
|
|
|
minetest.log("error", "IRC: Ignoring attempt to connect when already connected.")
|
|
|
|
return
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2014-05-26 04:52:24 +02:00
|
|
|
self.conn = irc.lib.new({
|
2013-04-30 00:07:44 +02:00
|
|
|
nick = self.config.nick,
|
|
|
|
username = "Minetest",
|
|
|
|
realname = "Minetest",
|
|
|
|
})
|
|
|
|
self:doHook(self.conn)
|
2014-05-28 17:05:08 +02:00
|
|
|
local good, message = pcall(function()
|
2013-10-20 00:45:39 +02:00
|
|
|
self.conn:connect({
|
|
|
|
host = self.config.server,
|
|
|
|
port = self.config.port,
|
2014-05-28 16:59:10 +02:00
|
|
|
password = self.config.password,
|
2013-10-20 00:45:39 +02:00
|
|
|
timeout = self.config.timeout,
|
|
|
|
secure = self.config.secure
|
2013-04-30 00:07:44 +02:00
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
if not good then
|
|
|
|
minetest.log("error", ("IRC: Connection error: %s: %s -- Reconnecting in ten minutes...")
|
|
|
|
:format(self.config.server, message))
|
2013-10-20 00:45:39 +02:00
|
|
|
minetest.after(600, function() self: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
|
|
|
|
2013-04-30 00:07:44 +02:00
|
|
|
if self.config.NSPass then
|
|
|
|
self:say("NickServ", "IDENTIFY "..self.config.NSPass)
|
2013-04-25 23:00:44 +02:00
|
|
|
end
|
2013-04-30 00:07:44 +02: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
|
|
|
|
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:disconnect(message)
|
2013-04-30 00:07:44 +02:00
|
|
|
if self.connected then
|
|
|
|
--The OnDisconnect hook will clear self.connected and print a disconnect message
|
|
|
|
self.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
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:say(to, message)
|
2013-04-30 00:07:44 +02:00
|
|
|
if not message then
|
|
|
|
message = to
|
|
|
|
to = self.config.channel
|
|
|
|
end
|
|
|
|
to = to or self.config.channel
|
2013-01-11 17:20:30 +01:00
|
|
|
|
2014-05-06 21:26:13 +02:00
|
|
|
self:queue(irc.msgs.privmsg(to, message))
|
2013-01-11 17:20:30 +01:00
|
|
|
end
|
|
|
|
|
2012-12-22 04:16:28 +01:00
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:reply(message)
|
2013-12-03 00:15:29 +01:00
|
|
|
if not self.last_from then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self:say(self.last_from, message)
|
|
|
|
end
|
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:send(msg)
|
2014-05-26 09:04:35 +02:00
|
|
|
if not self.connected then return end
|
2014-05-06 21:26:13 +02:00
|
|
|
self.conn:send(msg)
|
2012-12-22 04:16:28 +01:00
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|
2014-05-26 04:52:24 +02:00
|
|
|
function irc:queue(msg)
|
2014-05-26 09:04:35 +02:00
|
|
|
if not self.connected then return end
|
2014-05-06 21:26:13 +02:00
|
|
|
self.conn:queue(msg)
|
|
|
|
end
|
2013-04-30 00:07:44 +02:00
|
|
|
|