mailfmtshort (823B)
1 #!/bin/sh 2 # use shortner running on lxc container urlshort 3 4 w=$(tput cols) 5 if [ -z "$w" ] || [ "$w" -gt 120 ] ; then 6 w=120 7 fi 8 9 tmp1=$(mktemp -p /tmp mailfmt.XXXXXXXX) 10 tmp2=$(mktemp -p /tmp mailfmt.XXXXXXXX) 11 trap "rm -f $tmp1 $tmp2" EXIT QUIT INT 12 13 # trim mutt [-- Attachment --] headers 14 grep -vE -- '\[-- .* --\]$' | fmt -s -w $w | tee $tmp1 > $tmp2 15 16 grep -oE '[hH][tT]{2}[pP][sS]?://([^/?#><"]+)([^?#><"]*)(\?([^#><"]*))?(#(.*))?' $tmp1 | sort | uniq | while read url; do 17 if [ "$(echo $url | wc -c)" -gt $w ]; then 18 # shorten url 19 short=$(curl -sS -X PUT -d "$url" http://url.short) 20 21 # escape special chars in url for passing it to sed 22 escape=$(printf '%s\n' "$url" | sed 's/[]\/$*.^[]/\\&/g') 23 24 # replace url with short link (using space as sed separator) 25 sed -i "s $escape $short g" $tmp2 26 fi 27 done 28 cat $tmp2