home · contact · privacy
b7d07093f138ee82e8802ce432776ba011f50b7c
[config] / all_new_2018 / user_scripts / 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 while true; do
9     if [ -f "${path}" ]; then
10         cat "${path}" | while read line; do
11             first_word=$(echo -n "${line}" | cut -d' ' -f1)
12
13             # Read "bot:" line, start bot screen session from it if not yet existing,
14             # set up irclogs dir if not yet existing.
15             if [ "${first_word}" = "bot:" ]; then
16                 session_name=$(echo -n "${line}" | cut -d' ' -f2)
17                 bot_name=$(echo -n "${line}" | cut -d' ' -f3)
18                 channel_name=$(echo -n "${line}" | cut -d' ' -f4)
19                 server_name=$(echo -n "${line}" | cut -d' ' -f5)
20                 set +e
21                 screen -S "${session_name}" -Q select . > /dev/null
22                 start_screen=$?
23                 set -e
24                 if [ "${start_screen}" -eq "1" ]; then
25                 cd ~/plomlombot-irc
26                 screen -d -m -S "${session_name}" ./run.sh -n "${bot_name}" -s "${server_name}" "${channel_name}"
27                 fi
28                 md5_server=$(echo -n "${server_name}" | md5sum | cut -d' ' -f1)
29                 md5_channel=$(echo -n "${channel_name}" | md5sum | cut -d' ' -f1)
30                 logs_dir="${db_dir}/${md5_server}/${md5_channel}/logs"
31                 # FIXME: Note the trouble we will have if we have the same channel
32                 # name on different servers …
33                 shortened_channel_name="${channel_name}"
34                 first_char=$(echo -n "${channel_name}" | cut -c1)
35                 if [ "${first_char}" = "#" ]; then
36                     shortened_channel_name=$(echo -n "${channel_name}" | cut -c2-)
37                 fi
38                 ln -sf "${logs_dir}" "${irclogs_dir}/${shortened_channel_name}"
39
40             # If "key:" line, encrypt old raw logs to that GPG key.
41             elif [ "${first_word}" = "gpg_key": ]; then
42                 key=$(echo -n "${line}" | cut -d' ' -f2)
43                 mkdir -p ~/plomlombot_db
44                 cd ~/plomlombot_db
45                 find . -path '*/*/raw_logs/*.txt' -mtime +1 -type f -exec gpg --recipient "${key}" --trust-model always --encrypt {} \; -exec rm {} \;
46             fi
47
48         done
49         sleep 1
50     fi
51 done