From b20927bac93b8e612be6c66e86c440e4d682bcda Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 15 Dec 2018 18:20:28 +0100
Subject: [PATCH] Add setup for play.plomlompom.com

---
 .../systemd/system/encrypt_chatlogs.service   |  6 +++
 .../encrypt_chatlogs.timer                    |  8 ++++
 .../web/etc/nginx/nginx.conf                  |  2 +-
 all_new_2018/setup_play.sh                    | 40 +++++++++++++++++++
 all_new_2018/setup_plomlombot.sh              |  2 +-
 all_new_2018/user_files/encrypter.sh          |  4 ++
 .../plomlombot_daemon.sh                      |  0
 all_new_2018/user_files/weechat-wrapper.sh    |  8 ++++
 all_new_2018/user_files/weechatrc             |  7 ++++
 9 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 all_new_2018/linkable_etc_files/play/etc/systemd/system/encrypt_chatlogs.service
 create mode 100644 all_new_2018/linkable_etc_files/play/etc/systemd/system/timers.target.wants/encrypt_chatlogs.timer
 create mode 100644 all_new_2018/setup_play.sh
 create mode 100644 all_new_2018/user_files/encrypter.sh
 rename all_new_2018/{user_scripts => user_files}/plomlombot_daemon.sh (100%)
 create mode 100755 all_new_2018/user_files/weechat-wrapper.sh
 create mode 100644 all_new_2018/user_files/weechatrc

diff --git a/all_new_2018/linkable_etc_files/play/etc/systemd/system/encrypt_chatlogs.service b/all_new_2018/linkable_etc_files/play/etc/systemd/system/encrypt_chatlogs.service
new file mode 100644
index 0000000..d0fcb9c
--- /dev/null
+++ b/all_new_2018/linkable_etc_files/play/etc/systemd/system/encrypt_chatlogs.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Pull website repo
+[Service]
+Type=oneshot
+User=plom
+ExecStart=/bin/sh -c '~/encrypter.sh'
diff --git a/all_new_2018/linkable_etc_files/play/etc/systemd/system/timers.target.wants/encrypt_chatlogs.timer b/all_new_2018/linkable_etc_files/play/etc/systemd/system/timers.target.wants/encrypt_chatlogs.timer
new file mode 100644
index 0000000..79a6e1e
--- /dev/null
+++ b/all_new_2018/linkable_etc_files/play/etc/systemd/system/timers.target.wants/encrypt_chatlogs.timer
@@ -0,0 +1,8 @@
+[Unit]
+Description=Attempt encryption of old chatlogs once every minute.
+
+[Timer]
+OnCalendar=*-*-* *:*:00
+
+[Install]
+WantedBy=timers.target
\ No newline at end of file
diff --git a/all_new_2018/linkable_etc_files/web/etc/nginx/nginx.conf b/all_new_2018/linkable_etc_files/web/etc/nginx/nginx.conf
index aec7622..f1be9e6 100644
--- a/all_new_2018/linkable_etc_files/web/etc/nginx/nginx.conf
+++ b/all_new_2018/linkable_etc_files/web/etc/nginx/nginx.conf
@@ -16,7 +16,7 @@ http {
         text/html html htm shtml;
         text/css css;
         text/xml xml;
-        text/plain txt sh rst md;
+        text/plain txt sh rst md asc;
         application/xhtml+xml xhtml;
         application/pdf pdf;
         image/jpeg jpg jpeg;
diff --git a/all_new_2018/setup_play.sh b/all_new_2018/setup_play.sh
new file mode 100644
index 0000000..b5e801b
--- /dev/null
+++ b/all_new_2018/setup_play.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+set -e
+
+# Ensure we have a GPG target to encrypt to.
+if [ $# -lt 1 ]; then
+    echo "Need public key ID as argument."
+    false
+fi
+gpg_key="$1"
+
+config_tree_prefix="${HOME}/config/all_new_2018/"
+apt -y install weechat screen vim
+cp "${config_tree_prefix}user_files/encrypter.sh /home/plom/"
+chown plom:plom /home/plom/encrypter.sh
+cp "${config_tree_prefix}user_files/weechat-wrapper.sh /home/plom/"
+chown plom:plom /home/plom/weechat-wrapper.sh
+cp "${config_tree_prefix}user_files/weechatrc /home/plom/.weechatrc"
+chown plom:plom /home/plom/.weechatrc
+apt -y install screen gnupg dirmngr
+keyservers='sks-keyservers.net/ keys.gnupg.net'
+set +e
+while true; do
+    do_break=0
+    for keyserver in $(echo "${keyservers}"); do
+        su plom -c "gpg --no-tty --keyserver $keyserver --recv-key ${gpg_key}"
+        if [ $? -eq "0" ]; then
+            do_break=1
+            break
+        fi
+        echo "Attempt with keyserver ${keyserver} unsuccessful, trying other."
+    done
+    if [ "${do_break}" -eq "1" ]; then
+        break
+    fi
+done
+set -e
+echo "$gpg_key" > /home/plom/.encrypt_target
+chown plom:plom /home/plom/.encrypt_target
+# TODO: We may remove dirmngr here if only this script installed it.
+service encrypt_chatlogs.timer start
diff --git a/all_new_2018/setup_plomlombot.sh b/all_new_2018/setup_plomlombot.sh
index 135bbd2..3c9efa3 100755
--- a/all_new_2018/setup_plomlombot.sh
+++ b/all_new_2018/setup_plomlombot.sh
@@ -11,7 +11,7 @@ gpg_key="$1"
 config_tree_prefix="${HOME}/config/all_new_2018/"
 irclogs_dir=/var/www/html/irclogs
 irclogs_pw_dir=/var/www/irclogs_pw
-cp "${config_tree_prefix}"/user_scripts/plomlombot_daemon.sh /home/plom/
+cp "${config_tree_prefix}"/user_files/plomlombot_daemon.sh /home/plom/
 chown plom:plom /home/plom/plomlombot_daemon.sh
 apt -y install screen python3-venv gnupg dirmngr
 keyservers='sks-keyservers.net/ keys.gnupg.net'
diff --git a/all_new_2018/user_files/encrypter.sh b/all_new_2018/user_files/encrypter.sh
new file mode 100644
index 0000000..6a35954
--- /dev/null
+++ b/all_new_2018/user_files/encrypter.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -e
+
+gpg_key=$(cat ~/.encrypt_target)
diff --git a/all_new_2018/user_scripts/plomlombot_daemon.sh b/all_new_2018/user_files/plomlombot_daemon.sh
similarity index 100%
rename from all_new_2018/user_scripts/plomlombot_daemon.sh
rename to all_new_2018/user_files/plomlombot_daemon.sh
diff --git a/all_new_2018/user_files/weechat-wrapper.sh b/all_new_2018/user_files/weechat-wrapper.sh
new file mode 100755
index 0000000..4625dd8
--- /dev/null
+++ b/all_new_2018/user_files/weechat-wrapper.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# Enforce ~/.weechatrc as sole persistent weechat config file.
+#~/config/bin/simplemail.sh ~/config/mails/weechat_restart_reminder
+rm -rf ~/.weechat/
+WEECHATCONF=`tr '\n' ';' < ~/.weechatrc`
+weechat -r "$WEECHATCONF"
+rm -rf ~/.weechat/
diff --git a/all_new_2018/user_files/weechatrc b/all_new_2018/user_files/weechatrc
new file mode 100644
index 0000000..ab30c17
--- /dev/null
+++ b/all_new_2018/user_files/weechatrc
@@ -0,0 +1,7 @@
+/set logger.file.path ~/weechatlogs
+/set logger.file.flush_delay 0
+/set logger.mask.irc "irc/$server/$channel.%Y-%m-%d.weechatlog"
+/set weechat.bar.status.items "[time],[buffer_last_number],[buffer_plugin],buffer_number+:+buffer_name+(buffer_modes)+{buffer_nicklist_count}+buffer_zoom+buffer_filter,[lag],[hotlist],completion,scroll,[otr]"
+/set weechat.color.chat_nick_colors "lightcyan"
+/server add freenode irc.freenode.net -nicks=plimlompom,plimlomp0m,pliml0mp0m -realname="foo bar" -autojoin=#plomlompomtest
+/connect freenode
-- 
2.30.2