A set of POSIX* shell scripts designed for personal use. No guarantees when it comes to quality, as these are also used for various minor experiments of mine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.0 KiB

#!/bin/sh -e
web_handler()
{
# Choose a random invidious instance to distribute load.
# Good digital citizenship is important, you know.
rand_inst()
{
case $(($(rand)%6)) in
0) printf "vid.puffyan.us" ;;
1) printf "inv.riverside.rocks" ;;
2) printf "invidious.kavin.rocks" ;;
3) printf "invidious-us.kavin.rocks" ;;
4) printf "invidious.osi.kr" ;;
5) printf "invidio.xamh.de" ;;
esac
}
case "${1}" in
*youtu*) exec Links "$(printf "%s" "${1}"|sed -ne \
"s&\(http.*://\).*\(/.*\)&\1$(rand_inst)\2&p")" & ;;
*reddit*) exec Links "$(printf "%s" "${1}"|sed -ne \
"s&\(http.*://\).*\(/.*/\)&\1libredd.it\2&p")" & ;;
*4chan.org*) exec Links "$(printf "%s" "${1}"|sed -ne \
"s&4chan&4channel&p")" & ;;
*) exec Links "${1}" & ;;
esac
}
edgecase()
{
err()
{
printf "Plumber wasn't able to find a way to handle the following URI.\n%s\nError code: %s\nProbable reason: %s\n" "${1}" "${2}" "${3}"|xmessage -file - &
exit 1
}
case "${2}" in
0) set -- "${1}" "${2}" "Some other error." ;;
1) set -- "${1}" "${2}" "It doesn't exist on the local filesystem." ;;
2) set -- "${1}" "${2}" "There was a parsing error."
esac
err "${@}"
}
cleanse_prefix()
{
if [ "$(printf "%s" "${1}"|cut -b1)" = 'f' ]
then
printf "%s\n" "$(printf "%s" "${1}"|sed -ne 's&file://\(.*\)&\1&p')"
else
printf "%s" "${1}"
fi
}
cleanse_spaces()
{
if [ "$(echo "${1}"|grep -c '%20')" = '1' ]
then
printf "%s\n" "${1}"|sed -e 's&%20& *&g'
else
printf "%s\n" "${1}"
fi
}
cleanse_leftparen()
{
if [ "$(echo "${1}"|grep -c '%28')" = '1' ]
then
printf "%s\n" "${1}"|sed -e 's&%28&(&g'
else
printf "%s\n" "${1}"
fi
}
cleanse_rightparen()
{
if [ "$(echo "${1}"|grep -c '%29')" = '1' ]
then
printf "%s\n" "${1}"|sed -e 's&%29&)&g'
else
printf "%s\n" "${1}"
fi
}
local_handler()
{
set -- "$(cleanse_prefix "${1}")"
set -- "$(cleanse_leftparen "${1}")"
set -- "$(cleanse_rightparen "${1}")"
if [ -e "${1}" ]
then
mime="$(file --mime-type -b "${1}")"
case ${mime} in
*html*) exec Links "${1}" & ;;
text*) exec xless "${1}" & ;;
video*) exec fpv "${1}" & ;;
audio*) exec fpm "${1}" & ;;
image*) exec iview "${1}" & ;;
*pdf) exec gv "${1}" & ;;
*) set -- "${1}" "2" ;; #Abort.
esac
else
# Set the error code and throw an error.
set -- "${1}" "1"
edgecase "${@}"
fi
}
# Commented lines will have support in the future.
case "$(printf "%s" "${1}"|cut -b 1,2,3,4)" in
# gopher) err "${@}" ;;
http) web_handler "${@}" ;;
file) local_handler "${@}" ;;
# ftp) err "${@}" ;;
# ssh) err "${@}" ;;
*) firstchar=$(printf "%s" "${1}"|cut -b1)
if [ "${firstchar}" = '~' ] || [ "${firstchar}" = '/' ]
then
local_handler "${@}"
else
set -- "${1}" "2"
edgecase "${@}"
fi
esac
exit