home · contact · privacy
7847d25ce3e3af59f04da98572fc483b76b53346
[config] / buster / other_files / plomlombot_daemon.sh
1 #!/bin/sh
2 set -e
3
4 # Repeatedly parse config file for GPG key and bot screen configs.
5 path=~/.plomlombot
6 db_dir="${HOME}/plomlombot_db"
7 irclogs_dir=/var/www/html/irclogs
8 irclogs_pw_dir=/var/www/irclogs_pw
9 hostname_mod_epoch=$(stat -c%Y /etc/hostname)
10 while true; do
11     if [ -f "${path}" ]; then
12         cat "${path}" | while read line; do
13             first_word=$(echo -n "${line}" | cut -d' ' -f1)
14
15             # Read "bot:" line, start bot screen session from it if not yet existing,
16             # set up irclogs dir if not yet existing.
17             if [ "${first_word}" = "bot:" ]; then
18                 session_name=$(echo -n "${line}" | cut -d' ' -f2)
19                 bot_name=$(echo -n "${line}" | cut -d' ' -f3)
20                 channel_name=$(echo -n "${line}" | cut -d' ' -f4)
21                 shortened_channel_name="${channel_name}"
22                 first_char=$(echo -n "${channel_name}" | cut -c1)
23                 if [ "${first_char}" = "#" ]; then
24                     shortened_channel_name=$(echo -n "${channel_name}" | cut -c2-)
25                 fi
26                 server_name=$(echo -n "${line}" | cut -d' ' -f5)
27                 login_user=$(echo -n "${line}" | cut -d' ' -f6)
28                 login_pw=$(echo -n "${line}" | cut -d' ' -f7)
29                 set +e
30                 screen -S "${session_name}" -Q select . > /dev/null
31                 start_screen=$?
32                 set -e
33                 if [ "${start_screen}" -eq "1" ]; then
34                     cd ~/plomlombot-irc
35                     LANG="en_US.UTF-8" screen -d -m -S "${session_name}" ./run.sh -r 604800 -n "${bot_name}" -s "${server_name}" "${channel_name}"
36                 fi
37                 md5_server=$(echo -n "${server_name}" | md5sum | cut -d' ' -f1)
38                 md5_channel=$(echo -n "${channel_name}" | md5sum | cut -d' ' -f1)
39                 logs_dir="${db_dir}/${md5_server}/${md5_channel}/logs"
40                 # FIXME: Note the trouble we will have if we have the same channel
41                 # name on different servers …
42                 ln -sfn "${logs_dir}" "${irclogs_dir}/${shortened_channel_name}"
43                 echo "${login_user}":'{PLAIN}'"${login_pw}" > "${irclogs_pw_dir}/${shortened_channel_name}"
44
45             # If "gpg_key" line, encrypt old raw logs to that GPG key.
46             elif [ "${first_word}" = "gpg_key" ]; then
47                 key=$(echo -n "${line}" | cut -d' ' -f2)
48                 mkdir -p ~/plomlombot_db
49                 cd ~/plomlombot_db
50                 # Dirty hack: To avoid trouble with GPG key expiration, fake
51                 # system to something reasonbly old (younger than key creation,
52                 # older than expiration) by taking the mod datetime of
53                 # /etc/hostname, which should have last be changed when the
54                 # system was set up.
55                 find . -path '*/*/raw_logs/*.txt' -mtime +1 -type f -exec gpg --recipient "${key}" --trust-model always --faked-system-time="${hostname_mod_epoch}" --encrypt {} \; -exec rm {} \;
56             fi
57
58         done
59         sleep 1
60     fi
61 done