diff --git a/example-extensions/youtube-mime.toml b/example-extensions/youtube-mime.toml new file mode 100644 index 0000000..0b6ac6b --- /dev/null +++ b/example-extensions/youtube-mime.toml @@ -0,0 +1,7 @@ +# xdg-sanity mime extension + +[replace] # replaces these urls +urls = [ "youtube.com", "youtu.be" ] + +[with] # replaces the mime type of the above +mime = "video/vnd.youtube.yt" diff --git a/xdg-sanity b/xdg-sanity new file mode 100755 index 0000000..9fc0c99 --- /dev/null +++ b/xdg-sanity @@ -0,0 +1,116 @@ +#!/bin/sh + +set -e + +argv0="$0" + +# grabs configuration files +CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml +if ! test -e "$CONFIG"; then + touch "$CONFIG" + CONFIG=/etc/xdg-sanity.toml + if ! test -e "$CONFIG"; then + exit 66 # sysexits(3) EX_NOINPUT + fi +fi + + +# check if usage is valid +if ! test -n "$1"; then + printf "Usage: %s [resource...]\n" "$argv0" 1>&2 + exit 64 # sysexits(3) EX_USAGE +fi + +# check if we have curl(1) +if ! command -v curl >/dev/null 2>&1; then + printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +# check if we have tomcat(1) +if ! command -v tomcat >/dev/null 2>&1; then + printf "%s: Missing dependency: stoml(1)\n" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +# set the XDG_COMMAND +test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg $CONFIG)" +! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ + && XDG_COMMAND=false \ + || true + + +# check if we have a BROWSER +test -n "$BROWSER" || BROWSER="$(tomcat tools.browser $CONFIG)" +if ! test -n "$BROWSER"; then + printf "\ +%s: \$BROWSER not filled. + Please place the path to your preferred browser's executable in + $XDG_CONFIG_HOME/xdg-sanity.conf or /etc/xdg-sanity.conf +" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +while test -n "$1"; do + URL="$1" + + # use curl(1) to write out the request header's content_type, + # strip everything after the first semicolon, + # chop off any weird whitespace remnants + MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ + xargs echo)" + + # get the pattern for the extensions to MATCH + MATCH=$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ + sed -e 's/\/.*\+//g') + + # run through MIME extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ + /usr/share/xdg-sanity/*-mime.toml \ + /dev/null + do + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then + MIME=$(tomcat with.mime "$file") + fi + i=$(dc -e "$i 1 - p") + done + done + + # and the replace extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ + /usr/share/xdg-sanity/*-replace.toml \ + /dev/null + do + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then + URL=$(tomcat with.url "$file") + fi + i=$(dc -e "$i 1 - p") + done + done + + # these commands may fail; this is intentional + if [ "$MIME" = "text/html" ]; then + "$BROWSER" "$URL" + else + case "$(command -v $XDG_COMMAND)" in + */handlr ) + "XDG_COMMAND" "$MIME" -- "$URL" + ;; + */xdg-open ) + "$(xdg-mime query default $MIME)" "$URL" + ;; + false ) + exit 69 # sysexits(3) EX_UNAVAILABLE + ;; + esac + fi + shift +done + +exit 0 diff --git a/xdg-sanity/extensions/teddit-mime.sample b/xdg-sanity/extensions/teddit-mime.sample deleted file mode 100755 index 74908ee..0000000 --- a/xdg-sanity/extensions/teddit-mime.sample +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# EXT-TYPE=mime - -TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g') - -if [ "$TEST" = "teddit.net" ] -then -echo "text/html" -else -echo $1 -fi \ No newline at end of file diff --git a/xdg-sanity/extensions/youtube-replace b/xdg-sanity/extensions/youtube-replace deleted file mode 100755 index e0276d2..0000000 --- a/xdg-sanity/extensions/youtube-replace +++ /dev/null @@ -1,3 +0,0 @@ -# EXT-TYPE=replace - -replace youtube.com or youtu.be with piped.mint.lgbt \ No newline at end of file diff --git a/xdg-sanity/extensions/youtube-replace.sample b/xdg-sanity/extensions/youtube-replace.sample deleted file mode 100755 index a1fd473..0000000 --- a/xdg-sanity/extensions/youtube-replace.sample +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# EXT-TYPE=replace - -TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g') - -if [ "$TEST" = "youtube.com" ] -then -echo $1 | sed -ne 's/youtube.com/piped.mint.lgbt/p' -else -echo $1 -fi diff --git a/xdg-sanity/xdg-sanity.conf b/xdg-sanity/xdg-sanity.conf deleted file mode 100644 index 1acbb9a..0000000 --- a/xdg-sanity/xdg-sanity.conf +++ /dev/null @@ -1 +0,0 @@ -browser =