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