Totally rework annna. - annna - Annna the nice friendly bot.
HTML git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
---
DIR commit b2265362d675a8e447860c0b77a9a4c1d3009992
DIR parent a63f21cd1ad027bdff057acf9ee6d44273f778fa
HTML Author: Annna Robert-Houdin <annna@bitreich.org>
Date: Sat, 10 Feb 2018 11:38:10 +0000
Totally rework annna.
* Cleanup annna-say usage.
* Make the start routine event-based.
* Rework services handling using entr.
Diffstat:
D annna-checker | 21 ---------------------
D annna-checker-dbg | 25 -------------------------
M annna-say | 6 +++++-
D annna-shutdown | 10 ----------
M annna-start | 56 +------------------------------
A annna-start-checker | 21 +++++++++++++++++++++
A annna-start-checker-dbg | 25 +++++++++++++++++++++++++
A annna-start-main | 86 ++++++++++++++++++++++++++++++
M annna-start-services | 103 ++++++++++++++++---------------
M annna-stop | 7 ++++++-
M annna-stop-services | 2 +-
C annna-stop -> annna-stop-socat | 0
12 files changed, 197 insertions(+), 165 deletions(-)
---
DIR diff --git a/annna-checker b/annna-checker
@@ -1,21 +0,0 @@
-#!/bin/sh
-#
-# Watch for annna, she's worth it.
-#
-
-export PATH="$PATH:/home/annna/bin"
-
-while /bin/true;
-do
- # Is annna still in her world?
- pid="$(pgrep -U annna -x socat)"
-
- if [ -z "$pid" ];
- then
- annna-stop-services >/dev/null 2>&1
- annna-start >/dev/null 2>&1
- fi
-
- sleep 5
-done
-
DIR diff --git a/annna-checker-dbg b/annna-checker-dbg
@@ -1,25 +0,0 @@
-#!/bin/sh
-#
-# Watch for annna, she's worth it.
-#
-
-export PATH="$PATH:/home/annna/bin"
-
-logfile="/home/annna/output.log"
-
-while /bin/true;
-do
- # Is annna still in her world?
- pid="$(pgrep -U annna -x socat)"
-
- if [ -z "$pid" ];
- then
- annna-stop-services >/dev/null 2>&1
- annna-start 2>&1 \
- | grep -v -e ' PRIVMSG ' -e ' JOIN ' -e ' PART ' \
- >> "$logfile"
- fi
-
- sleep 5
-done
-
DIR diff --git a/annna-say b/annna-say
@@ -42,7 +42,11 @@ do
if [ -e "$ircpath/in" ];
then
printf "%s\n" "$@" > "${ircpath}/in"
- fi
+ else
+ # Might be a user.
+ printf "/j %s %s\n" "$channel" "$@" > "${iibase}/in"
+ fi
+
done
exit 0
DIR diff --git a/annna-shutdown b/annna-shutdown
@@ -1,10 +0,0 @@
-#!/bin/sh
-#
-# Dare you to execute this script! You are cruel!
-#
-
-export PATH="$PATH:/home/annna/bin"
-
-annna-stop
-annna-stop-services
-
DIR diff --git a/annna-start b/annna-start
@@ -2,59 +2,5 @@
export PATH="$PATH:/home/annna/bin"
-# #bitreich is a forward to #bitreich-en. Annna does not need to join it.
-#channels="#bitreich #bitreich-con #bitreich-radio
-# #bitreich-scm #bitreich-en #bitreich-de"
-channels="#bitreich-con #bitreich-radio
- #bitreich-scm #bitreich-en #bitreich-de
- #bitreich-fr #bitreich-cooking"
-
-rm -rf $HOME/irc
-mkdir -p $HOME/irc
-cd $HOME/irc
-rm -f $HOME/freenode.sock
-
-# Connect
-socat openssl:chat.freenode.net:6697,keepalive,keepcnt=5,keepidle=1,keepintvl=1 \
- unix-l:$HOME/freenode.sock &
-while [ ! -e $HOME/freenode.sock ];
-do
- sleep 0.5
-done
-
-ii -s chat.freenode.net -u $HOME/freenode.sock -n annna -f "Annna Robert-Houdin" &
-
-sleep 5
-
-# Nickserv Auth
-ircuser="$(grep user $HOME/irc-credentials.txt | cut -d' ' -f 2)"
-ircpass="$(grep pass $HOME/irc-credentials.txt | cut -d' ' -f 2)"
-printf "/privmsg nickserv :identify %s %s\n" "${ircuser}" "${ircpass}" \
- > chat.freenode.net/in
-sleep 0.5
-
-# Join Channels
-for chan in ${channels};
-do
- printf "/j %s\n" "${chan}" > chat.freenode.net/in
- sleep 0.5
-done
-
-# Give permissions to certain channels.
-chmod o+rx $HOME/irc
-chmod o+rx $HOME/irc/chat.freenode.net
-## EN Channel
-chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-en"
-chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-en/in"
-## SCM
-chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-scm"
-chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-scm/in"
-## Radio
-chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-radio"
-chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-radio/in"
-
-# Allow commands.
-sleep 6
-
-annna-start-services
+annna-start-checker
DIR diff --git a/annna-start-checker b/annna-start-checker
@@ -0,0 +1,21 @@
+#!/bin/sh
+#
+# Watch for annna, she's worth it.
+#
+
+export PATH="$PATH:/home/annna/bin"
+
+while /bin/true;
+do
+ # Is annna still in her world?
+ pid="$(pgrep -U annna -x socat)"
+
+ if [ -z "$pid" ];
+ then
+ annna-stop-services >/dev/null 2>&1
+ annna-start-main >/dev/null 2>&1
+ fi
+
+ sleep 5
+done
+
DIR diff --git a/annna-start-checker-dbg b/annna-start-checker-dbg
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# Watch for annna, she's worth it.
+#
+
+export PATH="$PATH:/home/annna/bin"
+
+logfile="/home/annna/output.log"
+
+while /bin/true;
+do
+ # Is annna still in her world?
+ pid="$(pgrep -U annna -x socat)"
+
+ if [ -z "$pid" ];
+ then
+ annna-stop-services >/dev/null 2>&1
+ annna-start-main 2>&1 \
+ | grep -v -e ' PRIVMSG ' -e ' JOIN ' -e ' PART ' \
+ >> "$logfile"
+ fi
+
+ sleep 5
+done
+
DIR diff --git a/annna-start-main b/annna-start-main
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+set -x
+
+export PATH="$PATH:/home/annna/bin"
+
+# #bitreich is a forward to #bitreich-en. Annna does not need to join it.
+#channels="#bitreich #bitreich-con #bitreich-radio
+# #bitreich-scm #bitreich-en #bitreich-de"
+channels="#bitreich-con #bitreich-radio
+ #bitreich-scm #bitreich-en #bitreich-de
+ #bitreich-fr #bitreich-cooking"
+
+rm -rf $HOME/irc
+mkdir -p $HOME/irc
+cd $HOME/irc
+rm -f $HOME/freenode.sock
+
+# Connect
+socat openssl:chat.freenode.net:6697,keepalive,keepcnt=5,keepidle=1,keepintvl=1 \
+ unix-l:$HOME/freenode.sock &
+while [ ! -e $HOME/freenode.sock ];
+do
+ sleep 0.5
+done
+
+# Connection
+ii -s chat.freenode.net -u $HOME/freenode.sock -n annna \
+ -f "Annna Robert-Houdin" &
+
+## Waiting for the socket to be available.
+while [ ! -e $HOME/irc/chat.freenode.net/out ];
+do
+ sleep 0.5
+done
+
+## Waiting for the motd to have ended so we can send something.
+while [ -z "$(grep "End of /MOTD command" $HOME/irc/chat.freenode.net/out)" ];
+do
+ sleep 0.5
+done
+
+sleep 2
+
+# Nickserv
+ircuser="$(grep user $HOME/irc-credentials.txt | cut -d' ' -f 2)"
+ircpass="$(grep pass $HOME/irc-credentials.txt | cut -d' ' -f 2)"
+printf "/privmsg nickserv :identify %s %s\n" "${ircuser}" "${ircpass}" \
+ > chat.freenode.net/in
+
+## Check that nickserv is there.
+while [ ! -e $HOME/irc/chat.freenode.net/nickserv/out ];
+do
+ sleep 0.5
+done
+
+## Waiting for us to be authenticated.
+while [ -z "$(grep "You are now identified for" \
+ $HOME/irc/chat.freenode.net/nickserv/out)" ];
+do
+ sleep 0.5
+done
+
+
+# Channels
+for chan in ${channels};
+do
+ printf "/j %s\n" "${chan}" > chat.freenode.net/in
+
+ ## Make sure the pipes are there.
+ while [ ! -e "$HOME/irc/chat.freenode.net/${chan}/out" ];
+ do
+ sleep 0.5
+ done
+
+ ## Make sure we are in the channel, for the services.
+ while [ -z "$(grep "${chan} End of /NAMES list" \
+ $HOME/irc/chat.freenode.net/out)" ];
+ do
+ sleep 0.5
+ done
+done
+
+# Services
+annna-start-services
+
DIR diff --git a/annna-start-services b/annna-start-services
@@ -4,63 +4,53 @@
# If this gets too unmaintainable, consider adding some directory structure.
#
+# Bitreich members who are allowed to run certain commands.
brmembers="__20h__ Evil_Bob chripo posativ quinq stateless solene josuah"
botname="annna"
iibase="/home/annna/irc/chat.freenode.net"
-function annna_say {
- channel="$1"
- shift 1
-
- [ $# -lt 1 ] && exit 1
-
- if [ -e "${iibase}/${channel}/in" ];
- then
- printf "%s\n" "$@" > "${iibase}/${channel}/in"
- # No spamming allowed.
- sleep 0.5
- else
- # Might be a user.
- printf "/j %s %s\n" "$channel" "$@" > "${iibase}/in"
- fi
-}
-
-echo '12345 <unknown> a' > ${iibase}/#bitreich-radio/out
-sleep 1
+
+# Permissions
+chmod o+rx $HOME/irc
+chmod o+rx $HOME/irc/chat.freenode.net
+## EN Channel (this script)
+chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-en"
+chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-en/in"
+## SCM (/scm/post-receive)
+chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-scm"
+chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-scm/in"
+## Radio (/br/radio/playlist.sh + this script)
+chmod o+rx "$HOME/irc/chat.freenode.net/#bitreich-radio"
+chmod o+w "$HOME/irc/chat.freenode.net/#bitreich-radio/in"
+
# bitreich-radio
{
- #tail -f -n 1 "${iibase}/#bitreich-radio/out" \
ls "${iibase}/#bitreich-radio/out" | entr tail -n 1 "${iibase}/#bitreich-radio/out" \
| sed -u 's,[0-9]* <\([^ >]*\)> \(.*\)$,\1\n\2,' \
| {
while read user;
do
read text
- echo "$user"
- echo "$text"
-
[ "$user" = "${botname}" ] && continue
if [ "${text}" = "${botname}, next please" ];
then
/br/bin/bitreich-radio-playlist-next
- annna_say "#bitreich-radio" "You are very kind ${user}. To your command."
+ annna-say "#bitreich-radio" "You are very kind ${user}. To your command."
continue;
fi
if [ "${text}" = "${botname}, please help" ];
then
- annna_say "${user}" "»next please« gets the playlist further, ${user}."
- annna_say "${user}" "»please help« gives you this help listing, ${user}."
+ annna-say "${user}" "»next please« gets the playlist further, ${user}."
+ annna-say "${user}" "»please help« gives you this help listing, ${user}."
continue;
fi
done
}
} &
-echo '12345 <unknown> a' > ${iibase}/#bitreich-en/out
-sleep 1
# bitreich-en
{
ls "${iibase}/#bitreich-en/out" | entr tail -n 1 "${iibase}/#bitreich-en/out" \
@@ -80,31 +70,31 @@ sleep 1
if [ "${text}" = "${botname}, can you show me the uptime please?" ];
then
- annna_say "#bitreich-en" "$(hostname) uptime: $(uptime)"
+ annna-say "#bitreich-en" "$(hostname) uptime: $(uptime)"
continue;
fi
if [ "${text}" = "${botname}, please give me the count of online users." ];
then
- annna_say "#bitreich-en" "$(hostname): $(who -q | tail -n 1 | cut -d'=' -f 2)"
+ annna-say "#bitreich-en" "$(hostname): $(who -q | tail -n 1 | cut -d'=' -f 2)"
continue;
fi
if [ "${text}" = "${botname}, please roll a dice for me." ];
then
- annna_say "#bitreich-en" "$((($RANDOM % 6) + 1))"
+ annna-say "#bitreich-en" "$((($RANDOM % 6) + 1))"
continue;
fi
if [ "${text}" = "${botname}, please tell me your favourite flower." ];
then
- annna_say "#bitreich-en" "My favourite flower is the beer flower."
+ annna-say "#bitreich-en" "My favourite flower is the beer flower."
continue;
fi
if [ "${text}" = "${botname}, please tell me your favourite color." ];
then
- annna_say "#bitreich-en" "My favourite color is yellow."
+ annna-say "#bitreich-en" "My favourite color is yellow."
continue;
fi
@@ -112,9 +102,9 @@ sleep 1
then
if [ $(($RANDOM % 2)) -gt 0 ];
then
- annna_say "#bitreich-en" "I am not that kind of woman."
+ annna-say "#bitreich-en" "I am not that kind of woman."
else
- annna_say "#bitreich-en" "Thank you! Let us dance! :-D"
+ annna-say "#bitreich-en" "Thank you! Let us dance! :-D"
fi
continue;
@@ -122,42 +112,53 @@ sleep 1
if [ "${text}" = "${botname}, please tell me who is your favourite pleasure man." ];
then
- annna_say "#bitreich-en" "My favourite pleasure man is of course Gunther!"
+ annna-say "#bitreich-en" "My favourite pleasure man is of course Gunther!"
continue;
fi
if [ "${text}" = "${botname}, sudo make me a sandwich" ];
then
- annna_say "#bitreich-en" "Humans are no objects."
+ annna-say "#bitreich-en" "Humans are no objects."
continue;
fi
if [ "${text}" = "${botname}, sudo please make me a sandwich" ];
then
- annna_say "#bitreich-en" "Here is your sandwich."
+ annna-say "#bitreich-en" "Here is your sandwich."
continue;
fi
if [ "${text}" = "${botname}, please update the phlog index." ];
then
- /home/annna/bin/phlog-index.sh > /dev/null 2>&1
- annna_say "#bitreich-en" "Thank you for your kind request. I have done so."
+ phlog-index.sh > /dev/null 2>&1
+ annna-say "#bitreich-en" "Thank you for your kind request. I have done so."
continue;
fi
if [ "${text}" = "${botname}, please help" ];
then
- annna_say "${user}" "»can you show me the uptime please?« gets you the uptime, ${user}."
- annna_say "${user}" "»please give me the count of online users.«, ${user}."
- annna_say "${user}" "»please tell me your favourite flower.«, ${user}."
- annna_say "${user}" "»please tell me your favourite color.«, ${user}."
- annna_say "${user}" "»please dance with me.«, ${user}."
- annna_say "${user}" "»please update the phlog index.«, ${user}."
- annna_say "${user}" "»sudo make me a sandwich«, ${user}."
- annna_say "${user}" "»sudo please make me a sandwich«, ${user}."
- annna_say "${user}" "»please tell me who is your favourite pleasure man.«, ${user}."
- annna_say "${user}" "»please roll a dice for me.«, ${user}."
- annna_say "${user}" "»please help« gives you this help listing, ${user}."
+ # TODO: Add multiple line support to annna-say(1) with sleep.
+ annna-say "${user}" "»can you show me the uptime please?« gets you the uptime, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please give me the count of online users.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please tell me your favourite flower.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please tell me your favourite color.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please dance with me.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please update the phlog index.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»sudo make me a sandwich«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»sudo please make me a sandwich«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please tell me who is your favourite pleasure man.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please roll a dice for me.«, ${user}."
+ sleep 0.5
+ annna-say "${user}" "»please help« gives you this help listing, ${user}."
continue;
fi
done
DIR diff --git a/annna-stop b/annna-stop
@@ -1,6 +1,11 @@
#!/bin/sh
+#
+# Dare you to execute this script! You are cruel!
+#
export PATH="$PATH:/home/annna/bin"
-pkill -KILL -U annna -x socat
+annna-stop-checker
+annna-stop-services
+annna-stop-socat
DIR diff --git a/annna-stop-services b/annna-stop-services
@@ -1,4 +1,4 @@
#!/bin/sh
-pkill -f "tail -f -n 1 /home/annna/irc.*"
+pkill -f "entr tail -n 1 /home/annna/irc.*"
DIR diff --git a/annna-stop b/annna-stop-socat