1
0
mirror of https://github.com/km4ack/patmenu2.git synced 2025-05-15 22:30:11 -07:00
patmenu2/manage-pat-functions
2022-09-22 06:49:26 -05:00

604 lines
16 KiB
Plaintext

MYPATH=$HOME/patmenu2
MAIN=$MYPATH/./manage-pat
LOGO=$MYPATH/pmlogo.png
DELETEALIAS(){
PATCONFIG=$XDG_CONFIG_HOME/pat/config.json
OUTFILE=/run/user/$UID/currentlist.txt
TEMP=/run/user/$UID/patconfig.txt
WHO=$(whoami)
jq .connect_aliases $PATCONFIG | sed '/^}/d' | sed '/^{/d' | sed 's/^ *//g' > $OUTFILE
cat $OUTFILE
sed -i 's/"//g' $OUTFILE
sed -i 's/,//g' $OUTFILE
sed -i 's/://' $OUTFILE
echo;echo
cat $OUTFILE | awk 'OFS="\n" {print $1, $2}'
REMOVE=$(PARSER='OFS="\n" {print $1, $2}'
tail -50 $OUTFILE | awk "$PARSER" | \
yad --title="Current Alias List" --width=1100 --height=500 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --list --text="<b>Select Alias to Delete</b>" \
--column Call --column INFO \
--button="Delete Alias":2 \
--button="Cancel":1)
BUT=$?
if [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 252 ]; then
exit
fi
RCALL=$(echo $REMOVE | awk -F "|" '{print $1}')
RCALL1=$(echo "\"$RCALL\"")
#check something was selected
if [ -z "$RCALL" ]; then
yad --title="No Selection" --width=300 --height=100 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --form --text="No station was selected\r Please try again" \
--button=gtk-ok
$MAIN &
exit
fi
jq 'del(.connect_aliases.'$RCALL1')' $PATCONFIG > $TEMP
cp $TEMP $PATCONFIG
rm $TEMP $OUTFILE
sudo killall pat
sudo systemctl start pat@$WHO
yad --title="Alias Deleted" --width=400 --height=200 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --text="$RCALL1 Alias DELETED" \
--button=gtk-ok
$MAIN &
exit
}
LOGIN(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
WHO=$(whoami)
CURRENT_USER=$(grep mycall $CONFIG | head -1 | sed 's/"mycall": "//;s/ //g;s/",//')
USER=$(yad --form --width=420 --text-align=center --center --title="Pat Log In/Out" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Pat Log In/Out</b> by KM4ACK" \
--field="Call Sign" "" \
--field="Winlink Password" "" \
--field="Six Character Grid Square" "EM65TV" \
--field="Current User Logged In ---- <b>$CURRENT_USER</b>":LBL \
--button="Log In":2 \
--button="Log Out":3 \
--button="Cancel":1)
BUT=$?
if [ $BUT = 252 ]; then
exit
elif [ $BUT = 1 ]; then
$MAIN
elif [ $BUT = 2 ]; then
echo "Log IN"
CALLSIGN=$(echo $USER | awk -F "|" '{print $1}')
CALLSIGN=$(echo "${CALLSIGN^^}")
PASS=$(echo $USER | awk -F "|" '{print $2}')
GRID=$(echo $USER | awk -F "|" '{print $3}')
GRID=$(echo "${GRID^^}")
echo "$CALLSIGN $PASS $GRID"
#set callsign
sed -i "s/\"mycall\": \".*\",/\"mycall\": \"$CALLSIGN\",/" $CONFIG
#set password
sed -i "s/\"secure_login_password\": \".*\",/\"secure_login_password\": \"$PASS\",/" $CONFIG
#set locator
sed -i "s/\"locator\": \".*\",/\"locator\": \"$GRID\",/" $CONFIG
sudo systemctl restart pat@$WHO
$MAIN &
exit
elif [ $BUT = 3 ]; then
echo "Log OUT"
CALLSIGN=N0CALL
PASS=""
GRID=""
#set callsign
sed -i "s/\"mycall\": \".*\",/\"mycall\": \"$CALLSIGN\",/" $CONFIG
#set password
sed -i "s/\"secure_login_password\": \".*\",/\"secure_login_password\": \"$PASS\",/" $CONFIG
#set locator
sed -i "s/\"locator\": \".*\",/\"locator\": \"$GRID\",/" $CONFIG
sudo killall pat
$MAIN &
exit
fi
}
GPSGRID(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
RUBY_INSTALLED=$(which ruby)
if [ -z "$RUBY_INSTALLED" ]; then
yad --form --width=300 --text-align=center --center --title="Ruby not Found" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="Ruby is needed to read GPS data but not found installed on your system. Would you like to install it now?" \
--button="Install Ruby":2 \
--button="Cancel":1
BUT=$?
if [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 2 ]; then
INSTALLRUBY(){
sudo apt-get update
sudo apt-get install -y ruby2.5
sudo gem install gpsd_client
sudo gem install maidenhead
}
INSTALLRUBY | yad --center --progress --pulsate --auto-close --no-buttons --text-align=center \
--text="Installing Ruby. This will take several minutes\r<b>DO NOT CLOSE THIS WINDOW</b>\rDoing so will abort the install."
fi
fi
yad --center --timeout=5 --timeout-indicator=top \
--auto-close --no-buttons --text="Getting grid from GPS" &
NEWGRID=$($MYPATH/getgrid | cut -b 1-6)
if [ "$NEWGRID" = 'JJ00aa' ]; then
NEWGRID="GPS READ ERROR or NO LOCK"
yad --form --width=400 --text-align=center --center --title="GPS ERROR" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Update Pat Grid</b> by KM4ACK\r\rGPS ERROR" \
--field="Grid read from GPS":RO "$NEWGRID" \
--button=gtk-quit
$MAIN &
exit
fi
yad --form --width=200 --text-align=center --center --title="Update Grid" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Update Pat Grid</b> by KM4ACK" \
--field="Grid read from GPS":RO "$NEWGRID" \
--button="Update Pat":2 \
--button="Cancel":1
BUT=$?
if [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 2 ]; then
sed -i "s/\"locator\": \".*\",/\"locator\": \"$NEWGRID\",/" $CONFIG
yad --center --timeout=2 --timeout-indicator=top \
--auto-close --no-buttons --text="Grid Square Updated"
$MAIN &
exit
fi
}
RESTORECONFIG(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
OUTFILE=/run/user/$UID/config.list
WHO=$(whoami)
ls $XDG_CONFIG_HOME/pat/ | grep .bkup > $OUTFILE
INFO=$(PARSER='OFS="\n" {print $1}'
tail -10 $OUTFILE | awk "$PARSER" | \
yad --title="Restore Config" --width=1100 --height=500 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --list --text="Choose File to Restore" \
--column File-to-Restore \
--button="Cancel":1 \
--button="Restore File":2)
BUT=$?
if [ $BUT = 252 ]; then
exit
elif [ $BUT = 1 ]; then
$MAIN
exit
fi
FILE=$(echo $INFO | awk -F "|" '{print $1}')
cp $XDG_CONFIG_HOME/pat/$FILE $CONFIG
sudo killall pat
sudo systemctl start pat@$WHO
yad --form --width=420 --text-align=center --center --title="Config Restore" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Config Restore</b> by KM4ACK\r\r<b>RESTROE COMPLETE</b>" \
--button=gtk-ok
$MAIN
}
BKUPCONFIG(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
WHO=$(whoami)
DATE=$(date +%d%b%Y-%H%M)
yad --form --width=420 --text-align=center --center --title="Backup Config" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Backup Pat Config</b> by KM4ACK" \
--button="Backup Config":2 \
--button="Main Menu":1
BUT=$?
if [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 252 ]; then
exit
elif [ $BUT = 2 ]; then
echo "Backing up file"
BKUPCONFIG=config.$DATE.bkup
cp $CONFIG $XDG_CONFIG_HOME/pat/$BKUPCONFIG
yad --form --width=420 --text-align=center --center --title="Config Backup" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Config File Backup</b> by KM4ACK\r\r<b>Backup COMPLETE</b>" \
--button=gtk-ok
fi
$MAIN
}
SPEED(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
CURRENT=$(cat $CONFIG | grep -i max | sed 's/"Max":\ //' | tr -d "[:blank:]")
WHO=$(whoami)
NEW=$(yad --form --width=320 --text-align=center --center --title="Speed" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Current ARDOP Speed is $CURRENT</b>" \
--field="Set ARDOP Speed to":CB "200|500|1000|2000" \
--button="Cancel":1 \
--button="Set Speed":2)
BUT=$?
if [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 252 ]; then
exit
fi
NEW=$(echo $NEW | awk -F "|" '{print $1}')
sed -i "s/\"Max\":.*/ \"Max\": $NEW/" $CONFIG
sudo killall pat
sudo systemctl start pat@$WHO
yad --form --width=320 --text-align=center --center --title="Speed" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>ARDOP Speed set to $NEW</b>" \
--button=gtk-ok
$MAIN &
exit
}
SETPTT(){
CONFIG=$XDG_CONFIG_HOME/pat/config.json
WHO=$(whoami)
CURRENT=$(cat $CONFIG | grep ptt_ctrl | tail -1 | sed 's/"ptt_ctrl":\ //' | sed 's/,//' | tr -d "[:blank:]")
if [ $CURRENT = 'false' ]; then
CURRENT1="false|true"
elif [ $CURRENT = 'true' ]; then
CURRENT1="true|false"
fi
PTT=$(yad --form --width=400 --text="<b>Current Pat ARDOP PTT = $CURRENT</b>" --text-align=center --center --title="Set PTT" --text-align=center \
--separator="|" --item-separator="|" \
--image=$LOGO --window-icon=$LOGO --image-on-top \
--field="ARDOP PTT":CB "$CURRENT1" \
--button="Set PTT":2 \
--button="Cancel":1)
QUIT=$?
if [ $QUIT = 1 ]; then
$MAIN &
exit
elif [ $QUIT = 252 ]; then
exit
fi
ANS=$(echo $PTT | awk -F "|" '{print $1}')
CURRENT=$(cat $CONFIG | grep ptt_ctrl | tail -1 | sed 's/"ptt_ctrl":\ //' | sed 's/,//')
echo "Set $CURRENT to $ANS"
if [ $CURRENT = 'false' ]; then
sed -i "s/\"ptt_ctrl\":\ false,/\"ptt_ctrl\":\ $ANS,/" $CONFIG
elif [ $CURRENT = 'true' ]; then
sed -i "s/\"ptt_ctrl\":\ true,/\"ptt_ctrl\":\ $ANS,/" $CONFIG
fi
sudo killall pat
sudo systemctl start pat@$WHO
yad --form --width=400 --text-align=center --center --title="Set PTT" --text-align=center \
--separator="|" --item-separator="|" \
--image=$LOGO --window-icon=$LOGO --image-on-top \
--text="<b>Pat ARDOP PTT now set to $ANS</b>" \
--button=gtk-ok
$MAIN &
exit
}
MANUALGPS() {
WHO=$(whoami)
CONFIG=$XDG_CONFIG_HOME/pat/config.json
CURRENT_GRID=$(grep locator $CONFIG | sed 's/"locator": "//;s/ //g;s/",//')
GRID=$(yad --form --width=400 --text-align=center --center --title="Set Grid" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Update Pat Grid</b> by KM4ACK" \
--field="Six Character Grid": "$NEWGRID" \
--field="Current Grid -------- <b>$CURRENT_GRID</b>":LBL \
--button="Update Grid":2 \
--button="Cancel":1)
BUT=$?
if [ $BUT = 252 ]; then
exit
fi
if [ $BUT = 1 ]; then
$MAIN &
exit
fi
NEWGRID=$(echo $GRID | awk -F "|" '{print $1}')
NEWGRID=${NEWGRID^^}
sed -i "s/\"locator\": \".*\",/\"locator\": \"$NEWGRID\",/" $CONFIG
echo "GRID UPDATED"
sudo killall pat
sudo systemctl start pat@$WHO
yad --form --width=400 --text-align=center --center --title="Grid Update" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Grid Updated to $NEWGRID</b>" \
--button=gtk-ok
$MAIN
}
LISTEN(){
#This funtion sets the listen modes for Pat Winlink
WHO=$(whoami)
TEMP=/run/user/$UID/listen.txt
NOW=$(grep ExecStart /lib/systemd/system/pat@.service | sed 's|ExecStart=/usr/bin/pat -l||;s/http//')
if [ $NOW = 'ExecStart=/usr/bin/pat' ]; then
NOW="Not listening for incoming connections"
fi
LISTEN=$(yad --center --list --checklist --width=600 --height=300 --separator="|" \
--image $LOGO --column=Check --column=Mode --column=Description \
--print-column=2 --window-icon=$LOGO --image-on-top --text-align=center \
--text="<b>Pat Listen Modes</b>\rCurrently Listening on:\r$NOW" --title="Pat Menu" \
false "TELNET" "Connection over Internet or Mesh" \
false "AX25" "Packet Connections 2M-440" \
false "ARDOP" "HF Connections using ARDOP Modem" \
--button="Main Menu":1 \
--button="Set Listen Mode":2 \
--button="Stop Listening":3)
BUT=$?
if [ $BUT = 252 ]; then
exit
elif [ $BUT = 1 ]; then
$MAIN &
exit
elif [ $BUT = 2 ]; then
#Send results to temp file.
echo $LISTEN > $TEMP
#check temp file for what's there
TELNET=$(grep TELNET $TEMP)
ARDOP=$(grep ARDOP $TEMP)
AX25=$(grep AX25 $TEMP)
#Set telnet variable
if [ -n "$TELNET" ]; then
TELNET="telnet,"
fi
#set ardop variable
if [ -n "$ARDOP" ]; then
ARDOP="ardop,"
fi
#set ax25 variable
if [ -n "$AX25" ]; then
AX25="ax25,"
fi
cat <<EOF > $TEMP
[Unit]
Description=pat - Winlink client for %I
Documentation=https://github.com/la5nta/pat/wiki
After=ax25.service network.target
[Service]
User=%i
ExecStart=/usr/bin/pat http
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
#add listen modes set by user
sed -i "s|Exec.*|ExecStart=/usr/bin/pat -l $TELNET$ARDOP$AX25 http|" $TEMP
sudo cp $TEMP /lib/systemd/system/pat@.service
sudo systemctl daemon-reload
sudo systemctl restart pat@$WHO
yad --form --width=320 --text-align=center --center --title="Listening" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Listen mode set to $TELNET$ARDOP$AX25</b>" \
--button=gtk-ok
$MAIN &
exit
elif [ $BUT = 3 ]; then
cat <<EOF > $TEMP
[Unit]
Description=pat - Winlink client for %I
Documentation=https://github.com/la5nta/pat/wiki
After=ax25.service network.target
[Service]
User=%i
ExecStart=/usr/bin/pat http
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo cp $TEMP /lib/systemd/system/pat@.service
sudo systemctl daemon-reload
sudo systemctl restart pat@$WHO
fi
yad --form --width=320 --text-align=center --center --title="Listening" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>No longer listening for incoming connections</b>" \
--button=gtk-ok
$MAIN &
exit
}
REPAIRCONFIG(){
DIR=/run/user/$UID
WHO=$(whoami)
FILECHECK=$(ls $DIR | grep config.json)
if [ ! -z $FILECHECK ]; then
rm $DIR/config.json*
fi
#Verify Pat installed and set path variable if it is
if ! hash pat 2>/dev/null; then
echo "Pat Winlink not found on this system"
exit
else
PATV=$(pat version | awk '{print $2}' | sed 's/v0.//' | awk -F "." '{print $1}')
if [ $PATV -le 11 ]; then
PATPATH=$HOME/.wl2k/
else
PATPATH=$XDG_CONFIG_HOME/pat/
fi
fi
#user input
SETTINGS=$(yad --form --width=600 --text-align=center --center --title="Repair Config File" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>Pat Menu\rConfig File Repair\r</b> by KM4ACK" \
--field="Call Sign" "" \
--field="Winlink Password" "" \
--field="Six Character Grid Square" "" \
--field="Allow access to Pat across network?":CB "YES|NO" \
--button="Repair Config File":2 \
--button="Cancel":1)
BUT=$?
if [ $BUT = 1 ]; then
$MAIN
elif [ $BUT = 252 ]; then
exit
fi
#parse input into variables
CALL=$(echo $SETTINGS | awk -F "|" '{print $1}')
CALL=${CALL^^}
PASSWD=$(echo $SETTINGS | awk -F "|" '{print $2}')
GRID=$(echo $SETTINGS | awk -F "|" '{print $3}')
GRID=${GRID^^}
SECURE=$(echo $SETTINGS | awk -F "|" '{print $4}')
if [ "$SECURE" = 'NO' ]; then
SECURE="127.0.0.1"
else
SECURE="0.0.0.0"
fi
#check to see if EES is installed and set port accordingly
if [ -f /var/www/html/celladdy.php ]; then
PORT=5000
else
PORT=8080
fi
#download config.json file from km4ack github
cd $DIR
wget -q https://raw.githubusercontent.com/km4ack/pi-scripts/master/config.json
#verify download success
if [ $? != 0 ]; then
yad --title="Cannot Download!" --width=300 --height=100 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --text="<b>Cannot download new config file.\rCheck internet connection</b>" \
--button=gtk-ok
$MAIN & exit
fi
#update file with user settings
sed -i "s/N0CALL/$CALL/" $DIR/config.json
sed -i "s/PASSWORD/$PASSWD/" $DIR/config.json
sed -i "s/GRID/$GRID/" $DIR/config.json
sed -i "s/8080/$PORT/" $DIR/config.json
sed -i "s/127.0.0.1/$SECURE/" $DIR/config.json
#cp file and restart pat
cp $DIR/config.json $PATPATH/
sudo systemctl restart pat@$WHO
yad --title="Repair Complete" --width=300 --height=100 \
--image $LOGO --window-icon=$LOGO --image-on-top \
--center --text="<b>Config File Repaired</b>" \
--button=gtk-ok
$MAIN & exit
}
PEER(){
TEMP_DIR=/run/user/$UID
CONFIG=$XDG_CONFIG_HOME/pat/config.json
TEMP_CONFIG=$TEMP_DIR/temp-config.txt
WHO=`whoami`
#get data from user
PEER=$(yad --center --wrap --width=450 --title="Add P2P Alias" --text-align=center \
--text="Enter the Peer to Peer Details Below\rIf using a hostname, add \".local\" to the end.\rExample: <b>raspberrypi.local</b>" \
--image $LOGO --window-icon=$LOGO --image-on-top \
--button="Update:2" \
--button="Cancel:1" \
--form --separator="|" --item-separator="|" \
--field="Call Sign of Remote Station" \
--field="IP address <b>OR</b> hostname of Remote Station" \
)
BUT=$?
if [ $BUT = 1 ]; then
$MAIN & exit
elif [ $BUT = 252 ]; then
exit
elif [ $BUT = 2 ]; then
#parse info entered above
REMOTE_CALL=$(echo $PEER | awk -F "|" '{print $1}')
REMOTE_CALL=${REMOTE_CALL^^}
IP=$(echo $PEER | awk -F "|" '{print $2}')
#create shortcut
SC="telnet://{mycall}:CMSTelnet@$IP:8774/wl2k"
#mod config file
jq '.connect_aliases += {'\"P2P-"$REMOTE_CALL"-"$IP"\"' : '\"$SC\"'}' $CONFIG > $TEMP_CONFIG
cp $TEMP_CONFIG $CONFIG
sudo systemctl restart pat@$WHO
#give user notice
yad --form --width=420 --text-align=center --center --title="P2P Alias Add" --text-align=center \
--image $LOGO --window-icon=$LOGO --image-on-top --separator="|" --item-separator="|" \
--text="<b>P2P Alias Added</b>" \
--button=gtk-ok
$MAIN
fi
}