tojam/tojam

237 lines
7.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Dialog accessibility
export DIALOGOPTS='--no-lines --visit-items'
# Functions
help() {
echo "${0##*/}"
echo -e "Released under the terms of the WTFPL License: http://www.wtfpl.net\n"
echo -e "Usage:\n"
echo "With no arguments, open recent servers list."
for i in "${!command[@]}" ; do
echo "-${i/:/ <parameter>}: ${command[${i}]}"
done | sort
exit 0
}
# Array of command line arguments
declare -A command=(
[c]="Configure ${0##*/}."
[D]="Delete a server."
[h]="This help screen."
[T]="Dedication."
)
add_server() {
# Have to validate the form
local errorMessage
local valid="false"
while [[ "${valid}" != "true" ]]; do
valid="true"
serverInfo="$(dialog --backtitle "Tojam" --title "Add Server" \
--form "\nEnter Server Information:\n\nUse up and down to move between fields, enter to accept, or escape to cancel." 25 60 16 \
"Server Name:" 1 1 "" 1 25 25 50 \
"Server Address:" 2 1 "" 2 25 25 50 \
"Server Port:" 4 1 "2049" 4 25 6 6 --stdout)"
mapfile -t serverInfo < <(echo "${serverInfo//^$/}")
if [[ "${#serverInfo[0]}" -le 1 ]]; then
errorMessage+="Server name was missing.\n"
valid="false"
fi
if [[ "${#serverInfo[1]}" -le 1 ]]; then
errorMessage+="Server address was missing.\n"
valid="false"
fi
if ! [[ "${serverInfo[2]}" =~ ^[1-9][0-9]*$ ]]; then
errorMessage+="Port must be numeric.\n"
valid="false"
fi
for i in "${!servers[@]}" ; do
if [[ "$i" == "${serverInfo[0]}" ]]; then
errorMessage+="Server name \"$i\" is already in use.\n"
valid="false"
fi
if [[ "${servers[${i}]}" == "${serverInfo[1]}" ]]; then
errorMessage+="Server address \"$i\" is already added.\n"
valid="false"
fi
done
if [[ "${valid}" == "false" ]]; then
dialog --msgbox "${errorMessage}" -1 -1
errorMessage=""
fi
done
echo "[${serverInfo[0]}]=\"${serverInfo[1]}:${serverInfo[2]}\"" >> "${config}/servers.conf" || exit 2
dialog --msgbox "Server \"${serverInfo[0]}\" added. Please relaunch ${0##*/} to use the updated server list." -1 -1
exit 0
}
delete_server() {
# Get servers into an array.
mapfile -t lines < "${config}/servers.conf"
for i in "${lines[@]}" ; do
j="${i#\[}"
j="${j/\]=\"/: }"
j="${j%\"}"
menuList+=("$i" "${j}")
done
server="$(dialog --clear \
--backtitle "Tojam" \
--no-tags \
--ok-label "Delete" \
--menu "Select a Ninjam server to be deleted" -1 -1 -1 "${menuList[@]}" --stdout)"
menuCode=$?
if [ ${menuCode} -eq 1 -o ${menuCode} -eq 255 ]; then
exit 0
fi
tmpFile="$(mktemp -t XXXXXX)"
grep -vF "$server" "${config}/servers.conf" > "${tmpFile}" || exit 3
mv "${tmpFile}" "${config}/servers.conf" || exit 4
dialog --msgbox "Server deleted." -1 -1
exit 0
}
configure_ninjam() {
local menuCode=0
while [[ $menuCode -ne 1 ]]; do
settings=(
"anonymous" "Login in anonymously: ($anonymous)"
"save source files" "Save Source Files: (${saveSourceFiles/-nosavesourcefiles/false recommended})"
"sound driver" "Sound Driver: (${soundDriver%% *})"
"username" "Set User Name: (${userName#*anonymous:})"
)
setting="$(dialog --backtitle "Tojam" \
--no-tags \
--menu "Select a setting" -1 -1 -1 "${settings[@]}" --stdout)"
menuCode=$?
if [ ${menuCode} -eq 1 -o ${menuCode} -eq 255 ]; then
exit 0
fi
case "${setting}" in
"sound driver")
soundDriver="$(dialog --yes-label "alsa" \
--no-label "jack" \
--yesno "Select a sound driver" -1 -1 --stdout)"
soundDriver=$?
[[ $? -eq 255 ]] && continue
soundDriver="${soundDriver/0/alsa}"
soundDriver="${soundDriver/1/jack}"
if [[ "${soundDriver}" == "jack" ]]; then
sed -i 's/^soundDriver=.*/soundDriver="jack"/' "${config}/config" &&
dialog --msgbox "Sound driver set to jack." -1 -1
continue
fi
;;
"username")
userName="$(dialog --backtitle "Tojam" \
--inputbox "Enter User Name (letters, numbers, dash, and underscore accepted):" -1 -1 "${userName#*anonymous:}" --stdout)"
menuCode=$?
[[ ${menuCode} -ne 0 ]] && continue
[[ "${anonymous}" == "true" ]] && userName="anonymous:${userName}"
sed -i "s/^userName=.*/userName='${userName}'/" "${config}/config" &&
dialog --msgbox "Username set to \"${userName#*anonymous:}\"." -1 -1
;;
esac
done
exit 0
}
# Configuration stuff
config="${XDG_CONFIG_HOME:-${HOME}/.config}/tojam"
sessiondir="${HOME}/tojam-sessions"
mkdir -p "${config}"
if ! [[ -r "${config}/config" ]]; then
# Write initial config file
{ echo 'anonymous="true"';
echo 'saveSourceFiles="false"';
echo 'soundDriver="jack"';
echo "userName='Tojam$(date '+%S')'"; } >> "${config}/config"
fi
source "${config}/config"
# Fix up username if anonymous is true
if [[ "${anonymous}" == "true" ]]; then
userName="anonymous:${userName}"
fi
# Fix up the source files setting
if [[ "${saveSourceFiles}" == "false" ]]; then
saveSourceFiles="-nosavesourcefiles"
else
saveSourceFiles=" "
fi
# Convert the keys of the associative array to a format usable by getopts
args="${!command[*]}"
args="${args//[[:space:]]/}"
while getopts "${args}" i ; do
case "$i" in
c) echo "Coming soon" ;;
D) delete_server ;;
h) help ;;
T)
echo "For Tony, because he's awesome!"
exit 0
;;
esac
done
declare -A servers=(
[Ninjamer 1]="ninjamer.com"
[Ninjamer 2]="ninjamer.com:2050"
[Ninjamer 3]="ninjamer.com:2051"
[Ninjamer 4]="ninjamer.com:2052"
)
if [[ -r "${config}/servers.conf" ]]; then
while IFS="=" read -r key value; do
if [[ "$key" =~ ^\[.*\]$ && "$value" =~ ^\".*\"$ ]]; then
key="${key#[}"
key="${key%]}"
value="${value#\"}"
value="${value%\"}"
servers["$key"]="$value"
else
echo "Error: invalid format in ${config}/servers.conf" >&2
exit 1
fi
done < <(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' "${config}/servers.conf")
fi
# Server menu
declare -a menuList
for i in "${!servers[@]}" ; do
menuList+=("${servers[$i]}" "${i}")
done
server="$(dialog --clear \
--backtitle "Tojam" \
--no-tags \
--extra-button \
--extra-label "Add Server" \
--ok-label "Connect" \
--cancel-label "Exit" \
--help-button \
--help-label "Settings" \
--menu "Select a Ninjam server" -1 -1 -1 "${menuList[@]}" --stdout)"
menuCode=$?
case ${menuCode} in
1|255) exit 0;;
2) configure_ninjam;;
3) add_server;;
esac
ninjam="$(command -v cninjam 2> /dev/null)"
[[ ${#ninjam} -le 3 ]] && { echo "Could not find cninjam."; exit 1; }
$ninjam "${server}" -user "${userName}" -${soundDriver} -sessiondir "${sessiondir}" ${saveSourceFiles}