URI: 
       Big refactoring for automatic server restart, if needed. - 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 f3d26842d5e8b3695eb3cd2d0cf8c83ae2f40565
   DIR parent e519438ca2a0234f4f0c04fc41dae0160741411a
  HTML Author: Annna Robert-Houdin <annna@bitreich.org>
       Date:   Sat,  1 Jan 2022 21:59:13 +0100
       
       Big refactoring for automatic server restart, if needed.
       
       * Make everything configurable in annna base dir.
       * Have a global file, if annna is running, which is checked everywhere.
       
       Diffstat:
         M annna-channel-service               |       7 ++++---
         A annna-join-channels                 |      38 +++++++++++++++++++++++++++++++
         M annna-message-common                |       9 ++++-----
         M annna-message-gopherproject         |       6 ++++--
         M annna-message-radio                 |       6 ++++--
         M annna-say                           |       2 +-
         M annna-start                         |       6 ++++--
         M annna-start-main                    |     149 ++++++++++++++-----------------
         M annna-start-service                 |      19 ++++++++++---------
         M annna-stop                          |       3 +++
       
       10 files changed, 140 insertions(+), 105 deletions(-)
       ---
   DIR diff --git a/annna-channel-service b/annna-channel-service
       @@ -11,10 +11,11 @@ pgrep -f "$0 $*" | grep -vxq "$$" && exec echo "$0 $* already running"
        server="$1"
        channel="$2"
        
       -iiroot="/home/annna/irc"
       -iibase="/home/annna/irc/${server}"
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +serverbase="${ircbase}/${server}"
        
       -ls "${iibase}/${channel}/out" | entr tail -n 1 "${iibase}/${channel}/out" 2>/dev/null \
       +ls "${serverbase}/${channel}/out" | entr tail -n 1 "${serverbase}/${channel}/out" 2>/dev/null \
                | sed -nu 's,[0-9]* <\([^ >]*\)> \(.*\),\1 \2,p' \
                | {
        while read -r user text;
   DIR diff --git a/annna-join-channels b/annna-join-channels
       @@ -0,0 +1,38 @@
       +#!/bin/sh
       +
       +set -x
       +
       +export PATH="$PATH:/home/annna/bin"
       +
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +
       +server="$1"
       +channels="$2"
       +
       +# Channels
       +for chan in ${channels};
       +do
       +        printf "/j %s\n" "${chan}" > ${ircbase}/${server}/in
       +
       +        # Do not flood.
       +        sleep 1
       +
       +        ## Make sure the pipes are there.
       +        while [ ! -e "${ircbase}/${server}/${chan}/out" ];
       +        do
       +                [ ! -f ${cfgbase}/running ] && exit 1
       +                sleep 1
       +        done
       +
       +        ## Make sure we are in the channel, for the services.
       +        while [ -z "$(grep " JOIN :${chan}" \
       +                        ${ircbase}/${server}/out)" ];
       +        do
       +                [ ! -f ${cfgbase}/running ] && exit 1
       +                sleep 1
       +        done
       +
       +        annna-start-service "${server}" "${chan}"
       +done
       +
   DIR diff --git a/annna-message-common b/annna-message-common
       @@ -1,17 +1,16 @@
        #!/bin/sh
       -#
       -# If this gets too unmaintainable, consider adding some directory structure.
       -#
        
        set -x
        
        export PATH="/br/bin:$PATH"
        
       -modbase="/home/annna/bin/modules"
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +modbase="$(cat ${cfgbase}/modbase)"
       +botname="$(cat ${cfgbase}/botname)"
        hashtagfile="${modbase}/hashtags/hashtags.txt"
        idontcaredb="${modbase}/i-dont-care/i-dont-care.txt"
        printnhashtags=2
       -botname="annna"
        brmembers="__20h__ Evil_Bob chripo posativ quinq stateless solene josuah parazyd bin KatolaZ adc"
        
        server="$1"
   DIR diff --git a/annna-message-gopherproject b/annna-message-gopherproject
       @@ -1,7 +1,9 @@
        #!/bin/sh -e
        
       -modbase="/home/annna/bin/modules/"
       -botname="annna"
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +modbase="$(cat ${cfgbase}/modbase)"
       +botname="$(cat ${cfgbase}/botname)"
        
        server="$1"
        channel="$2"
   DIR diff --git a/annna-message-radio b/annna-message-radio
       @@ -1,7 +1,9 @@
        #!/bin/sh -e
        
       -modbase="/home/annna/bin/modules/"
       -botname="annna"
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +modbase="$(cat ${cfgbase}/modbase)"
       +botname="$(cat ${cfgbase}/botname)"
        
        server="$1"
        channel="$2"
   DIR diff --git a/annna-say b/annna-say
       @@ -60,7 +60,7 @@ do
                        printf "%s\n" "$@" > "${ircpath}/in"
                else
                        # Might be a user.
       -                printf "/j %s %s\n" "$c" "$@" > "${ircbase}/in"
       +                printf "/j %s %s\n" "$c" "$@" > "${ircpath}/in"
                fi
        
        done
   DIR diff --git a/annna-start b/annna-start
       @@ -4,9 +4,11 @@ set -x
        
        export PATH="$PATH:/home/annna/bin"
        
       -cfgbase="/home/annna/annna"
       -cd ${cfgbase}
       +#cfgbase="$(pwd)"
       +cfgbase="$HOME/annna"
       +touch ${cfgbase}/running
        
       +cd ${cfgbase}
        for server in $(find . -maxdepth 1 -type d | tail -n +2 | cut -c 3-);
        do
                [ ! -f ${server}/autoconnect ] && continue
   DIR diff --git a/annna-start-main b/annna-start-main
       @@ -4,94 +4,81 @@ set -x
        
        export PATH="$PATH:/home/annna/bin"
        
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +
        server="$1"
        usetls="$2"
       -[ $usetls -gt 0 ] && tlsparam=" -t"
       +[ $usetls -gt 0 ] && tlsparam=" -v -t"
        port="$3"
        channels="$4"
        
       -rm -rf $HOME/irc/${server}
       -mkdir -p $HOME/irc/${server}
       -cd $HOME/irc
       -rm -f "$HOME/irc/${server}/sock"
       -
       -# Connect
       -#socat openssl:${server}:6697,keepalive,keepcnt=5,keepidle=1,keepintvl=1 \
       -#        unix-l:$HOME/irc/${server}/sock &
       -#while [ ! -e $HOME/irc/${server}/sock ];
       -#do
       -#        [ -z "$(pgrep socat)" ] && exit 1
       -#        sleep 0.5
       -#done
       -
       -# Connection
       -export ircuser="$(sed -n 's/^user: //p' $HOME/annna/${server}/irc-credentials.txt 2>/dev/null)"
       -export ircpass="$(sed -n 's/^pass: //p' $HOME/annna/${server}/irc-credentials.txt 2>/dev/null)"
       -#ii -s "${server}" -t -p 6697 -n "$ircuser" -k ircpass \
       -#        -f "Annna Robert-Houdin" &
       -if [ -n "${ircuser}" -a -n "${ircpass}" ];
       -then
       -        ii -s "${server}" -v${tlsparam} -p 6697 -n "$ircuser" \
       -                -f "Annna Robert-Houdin" &
       -        iipid=$!
       -else
       -        ii -s "${server}" -v${tlsparam} -p 6697 \
       -                -f "Annna Robert-Houdin" &
       -        iipid=$!
       -fi
       -
        {
       -        # Watch for ii to exit. Then remove the used directory to have all
       -        # services disappear.
       -        while kill -0 $iipid >/dev/null 2>&1;
       +        while [ -f ${cfgbase}/running ];
                do
       -                sleep 10
       +                # Connect
       +                #socat openssl:${server}:6697,keepalive,keepcnt=5,keepidle=1,keepintvl=1 \
       +                #        unix-l:${ircbase}/${server}/sock &
       +                #while [ ! -e ${ircbase}/${server}/sock ];
       +                #do
       +                #        [ -z "$(pgrep socat)" ] && exit 1
       +                #        sleep 0.5
       +                #done
       +
       +                rm -rf ${ircbase}/${server}
       +                mkdir -p ${ircbase}/${server}
       +
       +                # Connection
       +                export ircuser="$(sed -n 's/^user: //p' ${cfgbase}/${server}/irc-credentials.txt 2>/dev/null)"
       +                ircuserparam=""
       +                [ -n "${ircuserparam}" ] && ircuserparam="-u ${ircuser}"
       +
       +                export ircpass="$(sed -n 's/^pass: //p' ${cfgbase}/${server}/irc-credentials.txt 2>/dev/null)"
       +                ircpassparam=""
       +                [ -n "${ircpassparam}" ] && ircpassparam="-k ircpass"
       +
       +                if [ -f ${cfgbase}/${server}/fullname ];
       +                then
       +                        fullname="$(cat ${cfgbase}/${server}/fullname)"
       +                else
       +                        fullname="Gode"
       +                fi
       +
       +                #ii -s "${server}" -t -p 6697 -n "$ircuser" -k ircpass \
       +                #        -f "Annna Robert-Houdin" &
       +
       +                ii -s "${server}"${ircpassparam}${ircuserparam}${tlsparam} \
       +                        -f "${fullname}" -p ${port} &
       +                iipid=$!
       +
       +                sleep 5
       +
       +                ## Waiting for the socket to be available.
       +                while [ ! -e ${ircbase}/${server}/out ];
       +                do
       +                        kill -0 $iipid >/dev/null 2>&1 || exit 1
       +                        [ ! -f ${cfgbase}/running ] && exit 1
       +                        sleep 2
       +                done
       +
       +                #while [ -z "$(grep " 376 " ${ircbase}/${server}/out)" ];
       +                #do
       +                #        [ ! -f ${cfgbase}/running ] && exit 1
       +                #        sleep 2
       +                #done
       +
       +                {
       +                        sleep 2
       +                        annna-join-channels "${server}" "${channels}"
       +                } &
       +
       +                # Watch for ii to exit. Then remove the used directory to have all
       +                # services disappear.
       +                while kill -0 $iipid >/dev/null 2>&1;
       +                do
       +                        sleep 10
       +                done
       +                rm -rf ${ircbase}/${server}
                done
       -        rm -rf $HOME/irc/${server}
       -
        } &
        
       -## Waiting for the socket to be available.
       -while [ ! -e $HOME/irc/${server}/out ];
       -do
       -        kill -0 $iipid >/dev/null 2>&1 || exit 1
       -        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/${server}/out)" ];
       -#do
       -#        if [ -n "$(grep "Nickname is already in use" $HOME/irc/${server}/out)" ];
       -#        then
       -#                exit 1
       -#        fi
       -#
       -#        sleep 0.5
       -#done
       -
       -sleep 2
       -
       -# Channels
       -for chan in ${channels};
       -do
       -        printf "/j %s\n" "${chan}" > ${server}/in
       -
       -        # Do not flood.
       -        sleep 1
       -
       -        ## Make sure the pipes are there.
       -        while [ ! -e "$HOME/irc/${server}/${chan}/out" ];
       -        do
       -                sleep 0.5
       -        done
       -
       -        ## Make sure we are in the channel, for the services.
       -        while [ -z "$(grep "has joined ${chan}" \
       -                        $HOME/irc/${server}/${chan}/out)" ];
       -        do
       -                sleep 0.5
       -        done
       -
       -        annna-start-service "${server}" "${chan}"
       -done
       -
   DIR diff --git a/annna-start-service b/annna-start-service
       @@ -15,20 +15,21 @@ export PATH="$PATH:/home/annna/bin"
        # plan9port
        export PATH="$PATH:/opt/plan9/bin"
        
       -iiroot="/home/annna/irc"
       -iibase="/home/annna/irc/${server}"
       +cfgbase="$(pwd)"
       +ircbase="$(cat ${cfgbase}/ircbase)"
       +serverbase="${ircbase}/${server}"
        
        # Permissions
       -[ -d ${iiroot} ] && chmod o+rx ${iiroot}
       -[ -d ${iibase} ] && chmod o+rx ${iibase}
       +[ -d ${ircbase} ] && chmod o+rx ${ircbase}
       +[ -d ${serverbase} ] && chmod o+rx ${serverbase}
        
        ## Channel handlers
       -[ -d "${iibase}/${channel}" ] \
       -        && chmod o+rx "${iibase}/${channel}"
       -[ -p "${iibase}/${channel}/in" ] \
       -        && chmod o+w "${iibase}/${channel}/in"
       +[ -d "${serverbase}/${channel}" ] \
       +        && chmod o+rx "${serverbase}/${channel}"
       +[ -p "${serverbase}/${channel}/in" ] \
       +        && chmod o+w "${serverbase}/${channel}/in"
        
       -if [ -f "${iibase}/${channel}/out" ];
       +if [ -f "${serverbase}/${channel}/out" ];
        then
                annna-channel-service "${server}" "${channel}" &
        fi
   DIR diff --git a/annna-stop b/annna-stop
       @@ -5,6 +5,9 @@
        
        export PATH="$PATH:/home/annna/bin"
        
       +cfgbase="$HOME/annna"
       +[ -f "${cfgbase}/running" ] && rm -f "${cfgbase}/running"
       +
        annna-stop-ii
        annna-stop-checker
        annna-stop-services