diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | README | 45 | ||||
-rwxr-xr-x | choose | 6 | ||||
-rwxr-xr-x | copy_start_nevermind.sh | 35 | ||||
-rwxr-xr-x | search | 6 | ||||
-rw-r--r-- | sqesc.c | 18 | ||||
-rwxr-xr-x | ssh_hack | 26 | ||||
-rwxr-xr-x | urilaunch | 6 | ||||
-rw-r--r-- | urimatch.c | 75 | ||||
-rwxr-xr-x | uriqueryfilter | 101 | ||||
-rwxr-xr-x | uristart | 4 | ||||
-rw-r--r-- | uriunescape.c | 3 | ||||
-rwxr-xr-x | urnresolve | 14 | ||||
-rwxr-xr-x | xwindowURI | 35 |
14 files changed, 328 insertions, 50 deletions
@@ -24,6 +24,10 @@ clean: rm -f *.o install: all + install -t $(PREFIX)/bin sqesc + install -t $(PREFIX)/bin ssh_hack + install -t $(PREFIX)/bin xwindowURI + install -t $(PREFIX)/bin uriqueryfilter install -t $(PREFIX)/bin urimatch install -t $(PREFIX)/bin uricut install -t $(PREFIX)/bin urijoin @@ -1,16 +1,9 @@ -This is a handy little program to help you handle URIs that any program may want a handler for. -Just point those programs looking for a handler at uristart. +This repo has grown a lot of tools that I use on my own system. -Then write a matching pattern, and a handler pipeline. +There's a uri.h that can be included in other C programs -The matching patterns are a series of pairs of URI-piece and a string. - -Each piece has a single letter you use to select the piece. - -here's an example that uses all of the parts: -s://u:k@d:P/p?q#f - -where each piece is: +Most of the programs will take single letters to refer to parts of a URI. +each piece is: s: scheme u: username k: password (k for key) @@ -19,3 +12,33 @@ P: port p: path q: query_string f: fragment_id + +## uricut + +used for cutting portions of a URI out from the rest of it. + +to cut the domain out of all the input URIs and show one per line. +``` +urilist | uricut -d +``` + +## urimatch + +used for matching URIs +multiple arguments will print if any of the rules match. +so if you want to print only if all of the rules match, pipe urimatch into another urimatch + +to print all gemini or gopher URIs: +``` +urilist | urimatch s gemini s gopher +``` + +to print all the gemini URIs that are for the domain gemini.thebackupbox.net +``` +urilist | urimatch s gemini | urimatch d gemini.thebackupbox.net +``` + +to print all the non-http links using reverse match and globbing +``` +urilist | urimatch rs 'http*' +``` @@ -7,7 +7,9 @@ #shift #sed 's|,|\\,|g;s|:|\\:|g' | tr '\n' ',' | rev | cut -b2- | rev | tr '\n' '\0' | xargs -0 xmessage -nearmouse "$@" "$message" -print -buttons if [ $DISPLAY ];then - dmenu -l 10 -p "$1" +#usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor] +# [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid] + dmenu -l 10 -p "$1" -nb '#000000' -nf '#00aa00' -sb '#006600' -sf '#00ff00' else - printf "%s" "$1" ; ledit head -n1 + printf "%s" "$1" fi diff --git a/copy_start_nevermind.sh b/copy_start_nevermind.sh index 3f7f7af..66bf922 100755 --- a/copy_start_nevermind.sh +++ b/copy_start_nevermind.sh @@ -1,16 +1,35 @@ #!/bin/bash [ "$1" ] || exit 1 uri="$1" + +queryfiltereduri="$(uriqueryfilter "$uri")" +if [ "$uri" != "$queryfiltereduri" ];then + trackeruri="$uri" + uri="$queryfiltereduri" +fi + +### this clipping should be part of choose probably if [ "${uri}" != "${uri:0:64}" ];then prompt="${uri:0:64}..." +else + prompt="${uri}" fi + scheme="$(uricut -s <<< "$uri")" -selection="$(printf "start with: %s\ncopy\nshorten\nverbose\nforget\nQR\n%s\n" "$(urigetline "$uri")" "$(printf "%s\n" "$uri" | uricut | grep -v '^whole')" \ +selection="$(printf "start with: %s\nremote_csn\ncopy\nshorten\nunshorten\ntitle\nverbose\nforget\nQR\n%s\n" "$(urigetline "$uri")" "$(printf "%s\n" "$uri" | uricut | grep -v '^whole')" \ | choose "${prompt}")" # | choose "$(printf "uri: '%s'\nwill be ran with: %s\n" "$uri" "$(urigetline "$uri")" )" -default start )" echo "$selection" case "$selection" in +remote_csn) + remote_host="$(choose "remote host to run URI on> " < ~/.config/urirhosts.conf)" + ### TODO: include in the list our own display either to use -X or use our IP and display number? + display="$(ssh "${remote_host}" myXdisplays | choose "which display?")" + uristart "ssh://${remote_host}/copy_start_nevermind.sh%20${uri}?DISPLAY=$display" + exit 0 + ;; + verbose) x-terminal-emulator -e less -f <(printf "%s" "$uri") exit 0 @@ -21,6 +40,11 @@ copy) exit 0 ;; +title) + copy_start_nevermind.sh "$(urititle "$uri")" + exit 0 + ;; + forget) cp ~/.cache/uristart.log ~/.cache/uristart.log.tmp cat ~/.cache/uristart.log.tmp | grep -vFw "${uri}" > ~/.cache/uristart.log @@ -44,11 +68,18 @@ shorten) exit 0 ;; +unshorten) + copy_start_nevermind.sh "$(unshorten.sh "$uri")" + exit 0 + ;; + QR) x-terminal-emulator -hold -e qr "$uri" ;; *) - exec printf "%s" "$selection" | cut '-d ' -f2- | xclip -i + if [ "$selection" ];then + exec printf "%s" "$selection" | cut '-d ' -f2- | xclip -i + fi ;; esac @@ -1,2 +1,6 @@ #!/bin/sh -uristart "search:?q=$(echo | dmenu -p 'search>' | tr '\n' '\0' | xargs -0 uriescape | sed 's/&/%26/g;s/#/%23/g')" +q="$(printf "%s\n%s\n" "$(xclip -o | head -n1)" "$(xclip -o -selection clipboard | head -n1)" | choose 'search>' | tr '\n' '\0' | xargs -0 uriescape | sed 's/&/%26/g;s/#/%23/g')" +if [ ! "$q" ];then + exit 1 +fi +exec uristart "search:?q=$q" @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <string.h> + +int print_escaped(char *s) { + char *p=0; + for(p=strchr(s,'\'');p && s && *p && *s;p=strchr(s,'\'')) { + *p=0;//null out this single-quote + printf("%s",s); + printf("'\\''"); + s=p+1; + } + printf("%s",s); + return 0; +} + +int main(int argc,char *argv[]) { + return print_escaped(argv[1]); +} diff --git a/ssh_hack b/ssh_hack new file mode 100755 index 0000000..e882cd2 --- /dev/null +++ b/ssh_hack @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +### we need to export each of the query string variables +### then send them over ssh +q="$(printf "%s\n" "$1" | uricut -q)" +p="$(printf "%s\n" "$1" | uricut -p)" +d="$(printf "%s\n" "$1" | uricut -d)" +P="$(printf "%s\n" "$1" | uricut -P)" +u="$(printf "%s\n" "$1" | uricut -u)" + +eval "$(printf '%s\n' "$q" | tr '&' ' ' | grep '[^ ]*=' | sed 's/^/export /g')" +if [ "$p" ];then + p="$(uriunescape "$p")" +fi +if [ "$P" ]; then + HACK_PORT="-p $P" +fi +if [ "$u" ];then + HACK_USER="$u@" +fi + +tmpfile=$(mktemp); +printf '%s\n' "$q" | tr '&' '\n' | cut -d= -f1 | sed 's/^/SendEnv /g' > "$tmpfile" + +ssh -F "$tmpfile" -t ${HACK_PORT} ${HACK_USER}${d} ${p:1} +#ssh -F <(printf '%s\n' "$q" | tr '&' '\n' | cut -d= -f1 | sed 's/^/SendEnv /g') -t ${HACK_PORT} ${HACK_USER}${d} ${p:1} +#ssh -F <(printf '%s\n' "$q" | tr '&' '\n' | cut -d= -f1 | sed 's/^/SendEnv /g') -t ${HACK_PORT} ${HACK_USER}${d} ${p:1} @@ -3,10 +3,12 @@ #you'll probably want to change my gopher-get line to be something useful to you. exec copy_start_nevermind.sh \ "$(cat \ - <(printf "%s\n" $(xclip -o | head -n1)) \ + <(printf "%s\n" "$(xclip -o | head -n1)") \ + <(printf "%s\n" "$(xclip -o -selection clipboard | head -n1)") \ + <(xdotool getwindowfocus | xargs xwindowURI) \ <(printf "\n") \ <(gopher-get gopher://batou/0/links.txt) \ <(cat ~/.cache/uristart.log | sort | uniq) \ - | dmenu -l 10 -p urilaunch\> \ + | choose 'urilaunch>' \ | cut '-d ' -f1 )" @@ -6,29 +6,43 @@ #define LINE_LENGTH 1024 -int match(char negate,char *part,char *arg) { - if(negate) { - if(part == 0) return 1;//we found that the part isn't here! - } else { - if(part) { - if(!fnmatch(arg,part,FNM_NOESCAPE)) return 1; - } +#define MATCH_UNEXIST 0 +#define MATCH_PATTERN 1 +#define MATCH_REVERSE -1 + +// return 1 if the match and rule should have the main function print this URL. +int match(char rule,char *part,char *arg) { + switch(rule) { + case MATCH_UNEXIST: + if(part == 0) return 1; + break; + case MATCH_REVERSE: + if(part) { + if(!fnmatch(arg,part,FNM_NOESCAPE)) return 0; + else return 1; + } + break; + case MATCH_PATTERN: + if(part) if(!fnmatch(arg,part,FNM_NOESCAPE)) return 1; + break; + default: + fprintf(stderr,"oh god. what the hell happened to get here?\n"); + break; } return 0; } int main(int argc,char *argv[]) { - int i; + int i,j;; int ret=1; struct uri u; - char negate=0; + char rule=MATCH_PATTERN; char *line=malloc(LINE_LENGTH); char copy[LINE_LENGTH]; if(argc < 2) { - printf("usage: urimatch [-][n][s|u|k|d|D|P|p|q|f] [string]\n"); + printf("usage: urimatch [-][n][r][s|u|k|d|P|p|q|f] [string]\n"); printf("scheme://username:password@domain:port/path?query_string#fragment_id\n"); printf("s://u:k@d:P/p?q#f\n"); - printf("The D flag is special. it matches its argument against the last bytes of the input url's domain.\n"); printf("This allows matching of subdomains, like `echo epoch.ano | urimatch -D ano` would match.\n"); printf("the 'n' flag can be put before any of the other flags to check for a missing.\n"); return 1; @@ -40,30 +54,31 @@ int main(int argc,char *argv[]) { memset(&u,0,sizeof(u)); urifromline(&u,line); //use the character in argv[1] to match stdin against argv[2]. if match print whole line. - for(i=1;i<argc;i+=2) { - if(negate) {i--;}//we didn't really need to go that far. - negate=0; - if(argv[i][0] == '-') argv[i]++; - if(argv[i][0] == 'n') {argv[i]++; negate=1; }//heh. + for(i=1;i<argc;i+=2) {//for each line, we're reparsing the arguments... + rule=MATCH_PATTERN;//default + j=0; switch(argv[i][0]) { - case 's': if(match(negate,u.scheme,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'u': if(match(negate,u.username,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'k': if(match(negate,u.password,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'd': if(match(negate,u.domain,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'P': if(match(negate,u.port,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'p': if(match(negate,u.path,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'q': if(match(negate,u.query_string,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'f': if(match(negate,u.fragment_id,argv[i+1])) { printf("%s\n",copy); ret=0;} break; - case 'D': //not sure how to look for a missing one of these. it'd be like d. - if(u.domain && argv[i+1] && strlen(u.domain) >= strlen(argv[i+1]) && !strncmp(u.domain+strlen(u.domain)-strlen(argv[i+1]),argv[i+1],strlen(argv[i+1]))) { - printf("%s\n",copy); - ret=0; - } - break; + case '-': j=1; rule=MATCH_PATTERN; break;//so we can't be modifying these! how the fuck? + case 'n': j=1; rule=MATCH_UNEXIST; break; + case 'r': j=1; rule=MATCH_REVERSE; break; + default: break;//handled by the next switch + } + switch(argv[i][j]) { + case 's': if(match(rule,u.scheme,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'u': if(match(rule,u.username,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'k': if(match(rule,u.password,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'd': if(match(rule,u.domain,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'P': if(match(rule,u.port,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'p': if(match(rule,u.path,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'q': if(match(rule,u.query_string,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + case 'f': if(match(rule,u.fragment_id,argv[i+1])) { printf("%s\n",copy); ret=0;} break; + // TODO: after a while, get rid of 'D' completely. + case 'D': fprintf(stderr,"the 'D' flag has been removed.\ninstead of: D 'host'\nuse: d '*host'\n"); default: printf("unknown url part letter! '%c'\n",argv[i][0]); return ret; } + if(rule == MATCH_UNEXIST) {i--;}//UNEXIST doesn't consume the argument after it. so, go backwards 1. we're about to go ahead 2. } } return ret; diff --git a/uriqueryfilter b/uriqueryfilter new file mode 100755 index 0000000..72dca47 --- /dev/null +++ b/uriqueryfilter @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +## list grabbed from: +## https://github.com/newhouse/url-tracking-stripper/blob/master/assets/js/trackers.js +## referenced from: +## https://github.com/rknightuk/TrackerZapper/blob/3fe21dc2a59240dc2883569f8d435415010610d2/TrackerZapper/AppDelegate.swift#L162 +## more: +## https://maxchadwick.xyz/tracking-query-params-registry/ + +## usages: +## ./paramfilter URL1 URL2 URL3 ... +## ./paramfilter < list.uris +## if you use arguments it won't try to read from stdin. +## so if you want to use paramfilter as a filter don't use arguments. + +if [ "$1" ];then + while [ "$1" ];do + printf "%s\n" "$1" + shift + done +else + cat #useless cat? +fi | while read line;do + q="$(uricut -q "$line")" + if [ "$q" ];then + newq="$(printf "%s\n" "$q" | tr '&' '\n' | grep -vf <(grep -A 1000 "^__LIST__$" "$0" | tail -n+2 | sed 's/^/^/g;s/$/=/g') | tr '\n' '&' | sed 's/&$//')" + cat <(uricut "$line") <(printf "query_string: %s\n" "$newq") | urijoin 2>/dev/null + else + printf "%s\n" "$line" + fi +done + +exit 0 +#this was generated with: +# wget -qO- https://github.com/mpchadwick/tracking-query-params-registry/raw/master/_data/params.csv | tail -n+2 | cut -d, -f1 >> uriqueryfilter +# echo ref_src >> uriqueryfilter +__LIST__ +fbclid +gclid +gclsrc +utm_content +utm_term +utm_campaign +utm_medium +utm_source +utm_id +_ga +mc_cid +mc_eid +_bta_tid +_bta_c +trk_contact +trk_msg +trk_module +trk_sid +gdfms +gdftrk +gdffi +_ke +redirect_log_mongo_id +redirect_mongo_id +sb_referer_host +mkwid +pcrid +ef_id +s_kwcid +msclkid +dm_i +epik +pk_campaign +pk_kwd +pk_keyword +piwik_campaign +piwik_kwd +piwik_keyword +mtm_campaign +mtm_keyword +mtm_source +mtm_medium +mtm_content +mtm_cid +mtm_group +mtm_placement +matomo_campaign +matomo_keyword +matomo_source +matomo_medium +matomo_content +matomo_cid +matomo_group +matomo_placement +hsa_cam +hsa_grp +hsa_mt +hsa_src +hsa_ad +hsa_acc +hsa_net +hsa_kw +hsa_tgt +hsa_ver +ref_src @@ -10,6 +10,8 @@ ASKUSER="choose" check=dont +eval "$(printf "%s\n" "$1" | uricut | sed 's/^/export URI/;s/'\''/'\''\\'\'''\''/;s/: /='\''/g;s/$/'\''/')" + uristart_config=~/.config/uristart.conf usage() { @@ -43,7 +45,7 @@ if [ "$verbose" ];then echo "uristart: other:" "$@" >&2 fi -if [ ! "$@" ];then +if [ ! "$*" ];then usage >&2 #to stderr because it is an error to not have any extra args to process exit 1 fi diff --git a/uriunescape.c b/uriunescape.c index 23800a5..2261da9 100644 --- a/uriunescape.c +++ b/uriunescape.c @@ -11,7 +11,7 @@ int main(int argc,char *argv[]) { char buf2[BUFSIZE]; char *ab; int blen; - int hack; + int hack=0; if(argc > 1) { for(argv++,argc--;argc;argc--,argv++) { ab=malloc(strlen(*argv)); @@ -40,6 +40,7 @@ int main(int argc,char *argv[]) { } } } + if(blen < 0) perror("read"); } return 0; } @@ -25,6 +25,16 @@ if [ "$nid" = "ietf" ];then fi fi +if [ "$nid" = "phrack" ];then + if [ "$nss" ];then + if [ "$nss2" ];then + echo "gopher://thebackupbox.net:70/0/phrack/issues/${nss}/${nss2}.txt" + else + echo "gopher://thebackupbox.net:70/1/phrack/issues/${nss}/" + fi + fi +fi + if [ "$nid" = "xkcd" ];then echo "https://xkcd.com/${nss}/" fi @@ -40,3 +50,7 @@ if [ "$nid" = "fcc" ];then grep "^$nss2\s" $PREFIX/share/urn/fcc:frs | cut -f2 fi fi + +if [ "$nid" = "btih" ];then + btih2magnet "$nss" +fi diff --git a/xwindowURI b/xwindowURI new file mode 100755 index 0000000..6e9235a --- /dev/null +++ b/xwindowURI @@ -0,0 +1,35 @@ +#!/bin/sh +if [ ! "$1" ];then + id="$(xwinID)" +else + id="$1" +fi + +class=$(xclass -id "$id") +#echo "$class" + +case "$class" in +mupdf) + basename="$(xwintitle "$id" | rev | cut '-d ' -f5- | rev)" + echo "b: $basename" + page="$(xwintitle "$id" | tr ' ' '\n' | grep '[0-9][0-9]*/[0-9][0-9]*' | cut -d/ -f1)" + echo "$page" + for PID in $(pgrep mupdf);do + argv1="$(cat /proc/$PID/cmdline | tr '\0' '\n' | sed -n 2p)" + cd "$(readlink /proc/$PID/cwd)" + abspath="$(normalpath "$argv1")" + echo "a: $abspath" + echo "b: $basename" + if [ "$(basename "$abspath")" = "$basename" ];then + printf "file://%s#page=%s\n" "$(uriescape "$abspath")" "$page" + exit 0 + fi + done + ;; +Navigator) + xwintitle "$id" | rev | cut '-d ' -f4 | rev + ;; +*) + echo "class: $class" + ;; +esac |