1
0
Fork
You've already forked luairc
0
dead simple lua irc library
  • Lua 100%
Find a file
Yonle d344412b06 rename rockspec
Signed-off-by: Yonle <yonle@lecturify.net>
2025年08月23日 00:14:18 +07:00
irc hi 2025年08月23日 00:09:06 +07:00
.gitignore hi 2025年08月23日 00:09:06 +07:00
irc-0.0-1.rockspec rename rockspec 2025年08月23日 00:14:18 +07:00
LICENSE hi 2025年08月23日 00:09:06 +07:00
README.md hi 2025年08月23日 00:09:06 +07:00

luairc

lua irc library that's designed to be used with luasocket.

local socket = require("socket")
local irc = require("irc")
local commands = require("irc.commands")
local textformat = require("irc.format")
local cmds = commands.newInstance()
local c = assert(socket.tcp())
local config = {
	host = "irc.example.com",
	port = 6667,
	user = "luabot",
	real = "Lua Bot",
	nick = "luabot",
	prefix = "!"
}
cmds:registerCmd("hello", function(bot, ch, user, args)
	bot:chat(ch, "Hi " .. user.nick .. "!")
end)
local function checkErr(err)
	if err == "timeout" then
		return false
	end
	print("err: " .. err)
	c:close()
	return true
end
local function handleData(bot, b)
	print("--- " .. b)
	if bot:pong(b) then
		print("-BOT- PONGED!")
		return
	end
	if bot.nicknameIsUsed(b) then
		Nick = Nick .. "_"
		bot:Nick(Nick)
		return
	end
	if bot.welcome(b) then
		bot:join(table.concat(config.channels, ","))
		return
	end
	local user, s_cmd, flags, fullmsg = bot.parseLine(b)
	if s_cmd == "PRIVMSG" then
		local ch = flags[1]
		local from = user.nick
		if ch == Nick then
			ch = from
		end
		if string.sub(fullmsg, 1, 1) ~= config.prefix then
			return
		end
		cmds:readMessage(bot, ch, user, fullmsg)
		return
	end
end
local function connect()
	c:connect(config.host, config.port)
	c:settimeout(30)
	local bot = irc.new(function(data)
		c:send(data)
	end)
	Nick = config.nick
	bot:registerUser(config.user, config.real)
	bot:nick(Nick)
	while true do
		local b, err = c:receive("*l")
		if not b then
			if checkErr(err) then
				return
			end
		else
			handler(bot, b)
		end
	end
end
while true do
	connect()
end

some functions like whois and similar may need to be done manually.