dead simple lua irc library
|
|
||
|---|---|---|
| irc | hi | |
| .gitignore | hi | |
| irc-0.0-1.rockspec | rename rockspec | |
| LICENSE | hi | |
| README.md | hi | |
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.