POSIX_Scripts/plumber

135 lines
3.0 KiB
Plaintext
Raw Normal View History

2021-06-29 23:22:57 -04:00
#!/bin/sh -e
2022-01-26 14:46:53 -05:00
web_handler()
2021-06-29 23:22:57 -04:00
{
2022-01-26 14:46:53 -05:00
# 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
}
2022-01-26 15:24:45 -05:00
edgecase()
2022-01-26 14:46:53 -05:00
{
2022-01-26 15:24:45 -05:00
err()
2022-01-26 14:46:53 -05:00
{
2022-01-26 15:24:45 -05:00
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
2022-01-26 14:46:53 -05:00
}
2022-01-26 15:24:45 -05:00
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."
2022-01-26 14:46:53 -05:00
esac
2021-06-29 23:22:57 -04:00
2022-01-26 15:24:45 -05:00
err "${@}"
}
2022-01-26 14:46:53 -05:00
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
}
2022-01-26 15:24:45 -05:00
local_handler()
2022-01-26 14:46:53 -05:00
{
set -- "$(cleanse_prefix "${1}")"
set -- "$(cleanse_leftparen "${1}")"
set -- "$(cleanse_rightparen "${1}")"
if [ -e "${1}" ]
2022-01-26 14:46:53 -05:00
then
mime="$(file --mime-type -b "${1}")"
2022-01-26 15:24:45 -05:00
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.
2022-01-26 15:24:45 -05:00
esac
2022-01-26 14:46:53 -05:00
else
2022-01-26 15:24:45 -05:00
# Set the error code and throw an error.
set -- "${1}" "1"
edgecase "${@}"
2022-01-26 14:46:53 -05:00
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 "${@}" ;;
2022-01-26 15:24:45 -05:00
*) firstchar=$(printf "%s" "${1}"|cut -b1)
if [ "${firstchar}" = '~' ] || [ "${firstchar}" = '/' ]
then
local_handler "${@}"
else
set -- "${1}" "2"
2022-01-26 15:24:45 -05:00
edgecase "${@}"
fi
2022-01-26 14:46:53 -05:00
esac
2021-06-29 23:22:57 -04:00
exit