aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-10-07 09:53:39 +0200
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-10-07 09:53:39 +0200
commitacef961bebcb14c7e09581415de15256bb2251b3 (patch)
tree113b5559a914199ca1c05bb1e0e246319b5b6932
parent94c49df7dd27629d6a28f50028db483085907c96 (diff)
downloadsnippets-master.tar.gz
added the IRC log bot used to log #scitecoHEADmaster
It could be easily extended to serve other channels as well.
-rwxr-xr-xsciteco-bot.lua83
1 files changed, 83 insertions, 0 deletions
diff --git a/sciteco-bot.lua b/sciteco-bot.lua
new file mode 100755
index 0000000..a8ecd79
--- /dev/null
+++ b/sciteco-bot.lua
@@ -0,0 +1,83 @@
+#!/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()