From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 25 Mar 2020 22:18:57 +0000 (+0100)
Subject: Add plomlombot-irc setup.
X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/foo.html?a=commitdiff_plain;h=b631588a45593882e718395ba87701d07de64249;p=config

Add plomlombot-irc setup.
---

diff --git a/buster/apt-mark/website b/buster/apt-mark/website
index 550011c..a309575 100644
--- a/buster/apt-mark/website
+++ b/buster/apt-mark/website
@@ -1,2 +1,6 @@
+# for gitweb
 gitweb
 fcgiwrap
+# for plomlombot
+python3-venv
+screen
diff --git a/buster/etc_files/website/etc/nginx/sites-available/website.nginx b/buster/etc_files/website/etc/nginx/sites-available/website.nginx
index 41487f9..2da877b 100644
--- a/buster/etc_files/website/etc/nginx/sites-available/website.nginx
+++ b/buster/etc_files/website/etc/nginx/sites-available/website.nginx
@@ -30,4 +30,11 @@ server {
         fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
         fastcgi_pass unix:/var/run/fcgiwrap.socket;
     }
+
+    # login-protected IRC logs
+    location ~ /irclogs/([^/]+)/ {
+        auth_basic "$1 logs";
+        auth_basic_user_file /var/www/irclogs_pw/$1;
+        autoindex on;
+    }
 }
diff --git a/buster/etc_files/website/etc/systemd/system/plomlombot.service b/buster/etc_files/website/etc/systemd/system/plomlombot.service
new file mode 100644
index 0000000..a4f6769
--- /dev/null
+++ b/buster/etc_files/website/etc/systemd/system/plomlombot.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=plomlombot screen
+
+[Service]
+Type=simple
+User=plom
+ExecStart=/bin/sh -c '~/plomlombot_daemon.sh'
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
diff --git a/buster/other_files/plomlombot_daemon.sh b/buster/other_files/plomlombot_daemon.sh
new file mode 100755
index 0000000..8cf58a1
--- /dev/null
+++ b/buster/other_files/plomlombot_daemon.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+set -e
+
+# Repeatedly parse config file for GPG key and bot screen configs.
+path=~/.plomlombot
+db_dir="${HOME}/plomlombot_db"
+irclogs_dir=/var/www/html/irclogs
+irclogs_pw_dir=/var/www/irclogs_pw
+while true; do
+    if [ -f "${path}" ]; then
+        cat "${path}" | while read line; do
+            first_word=$(echo -n "${line}" | cut -d' ' -f1)
+
+            # Read "bot:" line, start bot screen session from it if not yet existing,
+            # set up irclogs dir if not yet existing.
+            if [ "${first_word}" = "bot:" ]; then
+                session_name=$(echo -n "${line}" | cut -d' ' -f2)
+                bot_name=$(echo -n "${line}" | cut -d' ' -f3)
+                channel_name=$(echo -n "${line}" | cut -d' ' -f4)
+                shortened_channel_name="${channel_name}"
+                first_char=$(echo -n "${channel_name}" | cut -c1)
+                if [ "${first_char}" = "#" ]; then
+                    shortened_channel_name=$(echo -n "${channel_name}" | cut -c2-)
+                fi
+                server_name=$(echo -n "${line}" | cut -d' ' -f5)
+                login_user=$(echo -n "${line}" | cut -d' ' -f6)
+                login_pw=$(echo -n "${line}" | cut -d' ' -f7)
+                set +e
+                screen -S "${session_name}" -Q select . > /dev/null
+                start_screen=$?
+                set -e
+                if [ "${start_screen}" -eq "1" ]; then
+                    cd ~/plomlombot-irc
+                    LANG="en_US.UTF-8" screen -d -m -S "${session_name}" ./run.sh -r 604800 -n "${bot_name}" -s "${server_name}" "${channel_name}"
+                fi
+                md5_server=$(echo -n "${server_name}" | md5sum | cut -d' ' -f1)
+                md5_channel=$(echo -n "${channel_name}" | md5sum | cut -d' ' -f1)
+                logs_dir="${db_dir}/${md5_server}/${md5_channel}/logs"
+                # FIXME: Note the trouble we will have if we have the same channel
+                # name on different servers …
+                ln -sfn "${logs_dir}" "${irclogs_dir}/${shortened_channel_name}"
+                echo "${login_user}":'{PLAIN}'"${login_pw}" > "${irclogs_pw_dir}/${shortened_channel_name}"
+
+            # If "gpg" line, encrypt old raw logs to that GPG key.
+            elif [ "${first_word}" = "gpg_key" ]; then
+                key=$(echo -n "${line}" | cut -d' ' -f2)
+                mkdir -p ~/plomlombot_db
+                cd ~/plomlombot_db
+                find . -path '*/*/raw_logs/*.txt' -mtime +1 -type f -exec gpg --recipient "${key}" --trust-model always --encrypt {} \; -exec rm {} \;
+            fi
+
+        done
+        sleep 1
+    fi
+done
diff --git a/buster/other_files/plomlombot_hook_post-receive b/buster/other_files/plomlombot_hook_post-receive
new file mode 100755
index 0000000..c4627af
--- /dev/null
+++ b/buster/other_files/plomlombot_hook_post-receive
@@ -0,0 +1,2 @@
+#!/bin/sh
+GIT_WORK_TREE=/home/plom/plomlombot-irc git checkout -f
diff --git a/buster/setup_scripts/setup_website.sh b/buster/setup_scripts/setup_website.sh
index d8edc61..035d852 100755
--- a/buster/setup_scripts/setup_website.sh
+++ b/buster/setup_scripts/setup_website.sh
@@ -46,6 +46,22 @@ su -lc "cd /var/repos && git clone --mirror ${old_server}:repos/website" plom
 cp "${config_tree_prefix}/other_files/website_hook_post-receive" /var/repos/website.git/hooks/post-receive
 su -lc 'cd /var/www && git clone /var/repos/website.git .' plom
 
+# Set up plomlombot.
+mkdir /var/www/html/irclogs
+chown plom:plom /var/www/html/irclogs
+mkdir /var/www/irclogs_pw
+chown plom:plom /var/www/irclogs_pw
+su -lc "cd /var/repos && git clone --mirror https://plomlompom.com/repos/clone/plomlombot-irc" plom
+su -lc "touch /var/repos/plomlombot-irc.git/git-daemon-export-ok" plom
+cp "${config_tree_prefix}/other_files/plomlombot_hook_post-receive" /var/repos/plomlombot-irc.git/hooks/post-receive
+su -lc "git clone /var/repos/plomlombot-irc.git" plom
+cp "${config_tree_prefix}/other_files/plomlombot_daemon.sh" /home/plom/
+chown plom:plom /home/plom/plomlombot_daemon.sh
+echo 'bot: plomlombog plomlombog #plomlomtest irc.freenode.net foo bar' >> /home/plom/.plomlombot
+chown plom:plom /home/plom/.plomlombot
+systemctl enable plomlombot.service
+service plomlombot start
+
 # TODO:
 # - commit git-daemon-export-ok directly into the public repos; rename
 #   /home/plom/public_repos to /home/plom/repos