#!/bin/bash ### arguments: # $1: the URI we're starting # $2: the "referer" of the URI we're starting. ### requirements: # getsrv, urimatch, urijoin, uricut, some-way-to-ask-the-user-for-a-line (dmenu by default) # getsrv tries dig and host, you only need one or the other. ##might have to configure me. ASKUSER="dmenu -l 10 -p" if [ "$1" = "--check" ];then check=check shift else check=dont fi uri="${1}" scheme="$(printf "%s\n" "$uri" | uricut -s)" line="$(urigetline "$uri")" ### if we do not have a port, we are going to try to get it from their srv records. if printf "%s\n" "$uri" | urimatch nP >/dev/null;then proto="$(grep ^"$scheme"'\s' /etc/services | tr ' ' '\t' | tr -s '\t ' | cut -f2 | cut -d/ -f2 | head -n1)" if [ ! "$proto" ]; then proto=tcp fi uri="$(cat <(printf "%s\n" "$uri" | uricut) \ <(getsrv $(printf "%s\n" "$uri" | uriprintf '%s '"$proto"' %d' | cut -d+ -f2-) 2>&- | tr ':' ' ' | sed 's/ /_port: /' | sed 's/^/domain: /' | tr _ '\n') \ | urijoin 2>/dev/null)" printf "srv (%s) modified uri: %s\n" "$proto" "$uri" >&2 fi ### if they do not have srv records, fall-back to /etc/services if printf "%s\n" "$uri" | urimatch nP >/dev/null;then #if we *still* don't have a port uri="$(cat <(printf "%s\n" "$uri" | uricut) \ <(grep ^"$scheme"'\s' /etc/services | tr ' ' '\t' | tr -s '\t' | cut -f2 | cut -d/ -f1 | head -n1 | sed 's/^/port: /') \ | urijoin 2>/dev/null)" printf "getent modified uri: %s\n" "$uri" >&2 fi ### ask the user for a pipeline for starting these URIs if there isn't a pipeline already in the config. echo "the uri we're using: $uri" >&2 if [ "$line" = "" ];then line="$(printf "" | $ASKUSER "protocol scheme (${scheme}) not configured yet. enter new pipeline to use:")" if [ "$line" = "" ];then echo "user noped on entering new pipeline" >&2 exit 1 fi printf '%s:\t%s\n' "${scheme}" "${line}" >> ~/.config/uristart.conf fi ### log the uri if it isn't logged already. if ! cut '-d ' -f1 ~/.cache/uristart.log | grep -Fx "$uri" 2>&1 >/dev/null;then #only log URIs that aren't already listed if [ "$2" ];then printf "%s %s\n" "$uri" "$2" >> ~/.cache/uristart.log else printf "%s\n" "$uri" >> ~/.cache/uristart.log fi fi ### do the magic if [ "$check" = "check" ];then printf "%s\n" "$(printf "%s\n" "$uri" | sed 's/'\''/'\''\\'\'''\''/g' | uriprintf "$line")" else eval "$(printf "%s\n" "$uri" | sed 's/'\''/'\''\\'\'''\''/g' | uriprintf "$line")" fi