#!/usr/local/bin/lua54 -- Logs the IRC channel at https://sciteco.fmsbw.de/irclogs/`date '+sciteco-%Y%m.log'` -- Monitored by daemon(8) and auto-rotates the logs. -- It does not require any cronjob. local socket = require "socket" local curdate, log local client = assert(socket.connect("irc.libera.chat", 6667)) client:setoption("keepalive", true) local nick = "FSB" local channel = "#sciteco" client:send("NICK "..nick.."\r\n".. "USER "..nick.." B * "..nick.."\r\n") socket.sleep(2) client:send("JOIN "..channel.."\r\n") local function handle_message(line) if line:sub(1, 5) == "PING " then -- we will be kicked if not responding to PINGs client:send("PONG "..line:sub(6).."\r\n") return end if os.date("%Y%m") ~= curdate then -- rotate log if log then log:close() end curdate = os.date("%Y%m") log = assert(io.open("/var/www/htdocs/projects/sciteco/irclogs/sciteco-"..curdate..".log", 'a+')) end local r_user, r_channel, r_msg = line:match("^:([^!]+)![^ ]+ PRIVMSG ([^ ]+) :(.*)") if r_channel == channel then -- message from user log:write(os.date("%d %b %H:%M "), "<", r_user, "> ", r_msg, "\n"):flush() return end r_user, r_channel = line:match("^:([^!]+)![^ ]+ JOIN (.*)") if r_channel == channel then -- user has joined log:write(os.date("%d %b %H:%M "), "-!- ", r_user, " has joined.\n"):flush() return end -- TODO: capture the optional message after `:` at the end r_user, r_channel = line:match("^:([^!]+)![^ ]+ PART ([^ ]+)") if r_channel == channel then -- user has left the channel log:write(os.date("%d %b %H:%M "), "-!- ", r_user, " has left.\n"):flush() return end -- TODO: capture the optional message after `:` at the end r_user = line:match("^:([^!]+)![^ ]+ QUIT") if r_user then -- user has quit log:write(os.date("%d %b %H:%M "), "-!- ", r_user, " has quit.\n"):flush() return end end while true do local line, err = client:receive('*l') if not line then io.stderr:write("ERROR: ", err, "\n") -- we will be supervised and restarted by daemon(8) -- FIXME: Does not reliably work. -- Perhaps decrease the keepalive timeout? os.exit(1) end -- print all messages from the server (for debugging) -- NOTE: line does not contain any EOL characters print(line) handle_message(line) end client:close() log:close()