#!/usr/bin/env zsh # Open log files from paste services in a terminal. If the URL doesn't seem to # be plain text, open in ${default_cmd} instead setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL # Remove temporary files on exit trap "rm -r ${$(mktemp --dry-run --suffix='.end')/.*.end/.*.readwwwlog}" EXIT zmodload zsh/zutil local -a o_help=() zparseopts -D -K -- h=o_help if [[ ${#o_help} -ne 0 || ! -v 1 ]]; then local ret=$(( ${#o_help} ^ 1 )) print -u $(( 1 + ${ret} )) "usage: ${0} [-h] …" return ${ret} fi local -a urls=(${@}) local default_cmd="firefox" local termopts="" for url in ${urls}; do [[ ${url[-1]} == "/" ]] && url=${url[1,-2]} # Remove trailing slash (safe?) local id=${url##*/} # Most paste services have the ID as the last element if [[ ${url} =~ 'dpaste\.com' && ! ${url[-4,-1]} == ".txt" ]]; then url+=".txt" elif [[ ${url} =~ '(//|\.)(pastebin\.|paste\.centos\.org)' ]] && \ [[ ! ${url} =~ '/raw/' ]]; then local host=${${(@s:/:)url}[2]} if [[ ${${(@s:/:)url}[3]} == "view" ]]; then url="https://${host}/view/raw/${id}" else url="https://${host}/raw/${id}" fi elif [[ ${url} =~ 'irccloud\.com' && ! ${url} =~ '/raw/' ]]; then id=${${(@s:/:)url}[4]} url="https://www.irccloud.com/pastebin/raw/${id}" elif [[ ${url} =~ 'bpa\.st' && ! ${url} =~ '/download-archive/' ]]; then url="https://bpa.st/download-archive/${id}" elif [[ ${url} =~ 'paste\.debian\.net' && ! ${url} =~ '/plain/' ]]; then url="https://paste.debian.net/plain/${id}" elif [[ ${url} =~ 'paste\.ee' && ! ${url} =~ '/r/' ]]; then # NOTE: will only show 1st paste url="https://paste.ee/r/${id}" fi if [[ ${TERMINAL} =~ "alacritty|xfce4-terminal" ]]; then termopts="--title=${url}" fi local curl="$(curl --silent --location --head ${url})" local -a headers=(${(ps:\r\n:)curl}) local -a cmd=(less +1 --LINE-NUMBERS --ignore-case \ --pattern='(error|fail):') if [[ -o NO_INTERACTIVE ]]; then cmd=(${TERMINAL} ${termopts} -e ${cmd}) fi if [[ ${headers[(I)*text/*]} -ne 0 ]] || \ [[ ${headers[(I)*HTTP/1.1 405*]} -ne 0 ]]; then # No HEAD allowed [[ ${url} =~ 'dpaste\.com' ]] && sleep 1 local file="$(mktemp --suffix='.readwwwlog')" curl --silent --location --output ${file} ${url} ${cmd} ${file} elif [[ ${headers[(I)*application/zip]} -ne 0 ]]; then local dir="$(mktemp --directory --suffix='.readwwwlog')" local file=${dir}/archive.zip curl --silent --location --output ${file} ${url} unzip -qd ${dir} ${file} for txt in ${dir}/*; do if [[ ${txt} != "${dir}/archive.zip" ]]; then ${cmd} ${txt} fi done else ${default_cmd} ${url} fi done