POSIX_Scripts/plumber

90 lines
2.3 KiB
Bash
Executable File

#!/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 - &
}
case "${2}" in
0) set -- "${1}" "${2}" "Some other error." ;;
1) set -- "${1}" "${2}" "It doesn't exist on the local filesystem." ;;
esac
err "${@}"
}
local_handler()
{
cleanse_prefix()
{
printf "%s" "$(printf "%s" "${1}" | sed -ne 's&file://&&p')"
}
target="$(cleanse_prefix "${1}")"
if [ -e "${target}" ]
then
mime="$(file --mime-type -b "${target}")"
case ${mime} in
*html*) exec Links "${target}" & ;;
text*) exec xless "${target}" & ;;
video*) exec fpv "${target}" & ;;
audio*) exec fpm "${target}" & ;;
image*) exec iview "${target}" & ;;
*pdf) exec gv "${target}" & ;;
*) exec exit 1 ;; #Abort. This should never be reached.
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
edgecase "${@}"
fi
esac
exit