From 59cce00bf8e90a8e19153bad78e5c1354e1780b6 Mon Sep 17 00:00:00 2001 From: Georg Date: Sat, 25 Sep 2021 16:55:29 +0200 Subject: [PATCH] Init pounceman.sh Signed-off-by: Georg --- pounceman.sh | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100755 pounceman.sh diff --git a/pounceman.sh b/pounceman.sh new file mode 100755 index 0000000..c64e4a1 --- /dev/null +++ b/pounceman.sh @@ -0,0 +1,225 @@ +#!/bin/bash + +DIALOG_CANCEL=1 +DIALOG_ESC=255 +DIALOG_EXTRA=3 +HEIGHT=0 +WIDTH=0 +pouncedir="/var/lib/pounce" + +display_result() { + dialog --title "$1" \ + --no-collapse \ + --msgbox "$result" 0 0 +} + +menu() { + exec 3>&1 + selection=$(dialog \ + --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \ + --title "Welcome!" \ + --clear \ + --cancel-label "Exit" \ + --menu "Please select:" $HEIGHT $WIDTH 4 \ + "1" "Display active networks" \ + "2" "Display disabled networks" \ + "3" "Add new network" \ + "4" "Enable network" \ + "5" "Disable network" \ + 2>&1 1>&3) + exit_status=$? + exec 3>&- +} + +get_networks() { + exec 3>&1 + if [ $pouncedir = "" ]; then + exit + fi + pouncedir="/var/lib/pounce" + if ! $(find $pouncedir/users/$USER/$1 -mindepth 0 -maxdepth 0 -empty | grep -q .); then + COUNTER=1 + RADIOLIST="" + user_choice="" + for i in $pouncedir/users/$USER/$1/*; do + FILENAME=$(basename "$i") + RADIOLIST="$RADIOLIST $FILENAME $FILENAME off" + let COUNTER=COUNTER+1 + done + user_choice=$(dialog --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \ + --radiolist "Networks" 0 0 $COUNTER \ + $RADIOLIST \ + 2>&1 1>&3) + exec 3>&- + echo $user_choice > /tmp/userchoice + else + dialog --title "No entries found" \ + --backtitle "LibertaCasa Pounce Configurator" \ + --msgbox "There seem to be no $1 networks in your account." \ + 10 60 + user_choice="" + fi +} + +edit_network() { + tmpfile=$(mktemp /tmp/$USER.XXXXXXXXXXXXXXXXXXXX) + cp $pouncedir/users/$USER/$1/$user_choice $tmpfile + nw_name=$user_choice + nw_hostname=$(egrep '(^|\s)host =' $tmpfile | sed 's/host = //' - ) + nw_nickname=$(grep 'nick' $tmpfile | sed 's/nick = //' - ) + nw_password=$(grep 'sasl-plain' $tmpfile | sed 's/sasl-plain = //' - ) + nw_channels=$(grep 'join' $tmpfile | sed 's/join = //' - ) + nw_away=$(grep 'away' $tmpfile | sed 's/away = //' -) + exec 3>&1 + user_input=$(dialog --ok-label "Submit" \ + --extra-button \ + --extra-label "Delete" \ + --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \ + --title "New IRC Network" \ + --form "Network specific details:" \ + 20 50 0 \ + "Arbitrary name:" 1 1 "$nw_name" 1 10 20 0 \ + "Hostname:" 2 1 "$nw_hostname" 2 10 20 0 \ + "Nickname:" 3 1 "$nw_nickname" 3 10 20 0 \ + "SASL Password:" 4 1 "$nw_password" 4 10 20 0 \ + "Channels:" 5 1 "$nw_channels" 5 10 20 0 \ + "Away message:" 6 1 "$nw_away" 6 10 20 0 \ + 2>&1 1>&3) + exit_status=$? + exec 3>&- + echo $user_input > /tmp/userinput + tmp_name=$(echo "$user_input" | sed -n 1p) + tmp_hostname=$(echo "$user_input" | sed -n 2p) + tmp_nickname=$(echo "$user_input" | sed -n 3p) + tmp_password=$(echo "$user_input" | sed -n 4p) + tmp_channels=$(echo "$user_input" | sed -n 5p) + tmp_away=$(echo "$user_input" | sed -n 6p) + sed -e "s/$(egrep '(^|\s)host =' $tmpfile)/host = $tmp_hostname/" -i $tmpfile +# sed -e "s/$nw_name/$tmp_name/" -i $tmpfile +# sed -e "s/$nw_pasword/$tmp_password/" -i $tmpfile +# sed -e "s/$nw_hostname/$tmp_hostname/" -i $tmpfile +# sed -e "s/$nw_channels/$tmp_channels/" -i $tmpfile +# sed -e "s/$nw_nickname/$tmp_nickname/" -i $tmpfile +# sed -e "s/$nw_away/$tmp_away/" -i $tmpfile + DIFF=$(diff $pouncedir/users/$USER/$1/$user_choice $tmpfile) + if [ ! "$DIFF" = "" ]; then + cp $tmpfile $pouncedir/users/$USER/$1/$user_choice + fi + #rm $tmpfile + nw_name="" + nw_hostname="" + nw_nickname="" + nw_password="" + nw_channels="" + nw_away="" +} + +set_network() { + if [ $1 = "disabled" ]; then + mv $pouncedir/users/$USER/disabled/$user_choice $pouncedir/users/$USER/enabled/$user_choice + fi + if [ $1 = "enabled" ]; then + mv $pouncedir/users/$USER/enabled/$user_choice $pouncedir/users/$USER/disabled/$user_choice + fi +} + +remove_network() { + dialog --title "Delete network" \ + --backtitle "LibertaCasa Pounce Configurator" \ + --yesno "This will permanently delete the network configuration $USER/$user_choice - Are you sure?" 7 60 + exit_status=$? + case $exit_status in + 0) rm -f $(find $pouncedir/users/$USER -type f -name "$user_choice");; + esac +} + +add_network() { + exec 3>&1 + user_input=$(dialog --ok-label "Submit" \ + --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \ + --title "New IRC Network" \ + --form "Network specific details:" \ + 20 50 0 \ + "Arbitrary name:" 1 1 "$nw_name" 1 10 20 0 \ + "Hostname:" 2 1 "$nw_hostname" 2 10 20 0 \ + "Nickname:" 3 1 "$nw_nickname" 3 10 20 0 \ + "SASL Password:" 4 1 "$nw_password" 4 10 20 0 \ + "Channels:" 5 1 "$nw_channels" 5 10 20 0 \ + "Away message:" 6 1 "$nw_away" 6 10 20 0 \ + 2>&1 1>&3) + #3>&1 1>&2 2>&3 3>&-) + exec 3>&- + echo $user_input > /tmp/userinput + tmp_name=$(echo "$user_input" | sed -n 1p) + tmp_hostname=$(echo "$user_input" | sed -n 2p) + tmp_nickname=$(echo "$user_input" | sed -n 3p) + tmp_password=$(echo "$user_input" | sed -n 4p) + tmp_channels=$(echo "$user_input" | sed -n 5p) + tmp_away=$(echo "$user_input" | sed -n 6p) + if [ ! $tmp_name = "TEMPLATE" ]; then + cp $pouncedir/TEMPLATE $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%POUNCEDIR%%/$pouncedir/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%USER%%/$USER/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%NAME%%/$tmp_name/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%PASSWORD%%/$tmp_password/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%HOSTNAME%%/$tmp_hostname/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%CHANNELS%%/$tmp_channels/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%USERNAME%%/$tmp_nickname/" -i $pouncedir/users/$USER/disabled/$tmp_name + sed -e "s/%%AWAY%%/$tmp_away/" -i $pouncedir/users/$USER/disabled/$tmp_name + fi +} + +while true; do + menu + case $exit_status in + $DIALOG_CANCEL) + clear + echo "Good bye!" + exit + ;; + $DIALOG_ESC) + clear + echo "See you!" >&2 + exit 1 + ;; + esac + case $selection in + 1 ) + #result=$(ls $pouncedir/users/$USER/enabled/) + #display_result "Enabled networks:" + get_networks "enabled" + if [ ! -z "$user_choice" ]; then + edit_network "enabled" + if [ $exit_status = $DIALOG_EXTRA ]; then + remove_network + fi + fi + ;; + 2 ) + #result=$(ls $pouncedir/users/$USER/disabled/) + #display_result "Disabled networks:" + get_networks "disabled" + if [ ! -z "$user_choice" ]; then + edit_network "disabled" + if [ $exit_status = $DIALOG_EXTRA ]; then + remove_network + fi + fi + ;; + 3 ) + add_network + ;; + 4 ) + get_networks "disabled" + if [ ! -z "$user_choice" ]; then + set_network "disabled" + fi + ;; + 5 ) + get_networks "enabled" + if [ ! -z "$user_choice" ]; then + set_network "enabled" + fi + ;; + esac +done