#!/bin/bash MYVERSION=0.20080606 # If you improve this script, please read the comments at the # end of this file. Thanks. # usage/help message usage_message() { cat >&2 </dev/null; do case $ARGS in \?) printf "$ERRFMT" "I cannot make sense of those parameters" >&2 usage_message exit $E_ARGS ;; h) usage_message; exit 10; ;; b) if [[ $OPTARG == firefox ]]; then BROWSERCOMMAND=firefox elif [[ $OPTARG == konqueror ]]; then BROWSERCOMMAND=konqueror elif [[ $OPTARG == opera ]]; then BROWSERCOMMAND=opera\ -newpage else printf "$ERRFMT" "$OPTARG is not a supported browser" >&2 usage_message exit $E_ARGS fi ;; a) # if a bad arg was passed with -a, there's no error # the default browser specified above is used [[ $OPTARG == google ]] && ARCHIVE=google [[ $OPTARG == freenix ]] && ARCHIVE=freenix ;; o) OUTPUTFILE=$OPTARG; ;; c) APPEND=yes; ;; d) DUPEKILL=yes; ;; q) QUIET=yes; ;; f) FORCE=yes; ;; V) echo -e "\e[1;33m${0##*/}: version $MYVERSION\e[0m"; exit 10; ;; esac done # having caught the options, move to other parameters shift $((OPTIND - 1)) # some error checking on input files # not necessary, but prevents some errors sed would throw for INPUT in "$@"; do [[ ! -f $INPUT ]] && [[ ! -c $INPUT ]] && { printf "$ERRFMT" "$INPUT is not a file I can use as input" >&2 usage_message exit $E_INPUTFILE } [[ -r $INPUT ]] || { printf "$ERRFMT" "I have no read access to $INPUT" >&2 usage_message exit $E_INPUTFILE } done # some error checking on output file, if -o given # not necessary, but prevents errors later [[ -e $OUTPUTFILE ]] && { [[ ! -f $OUTPUTFILE ]] && [[ ! -c $OUTPUTFILE ]] && { printf "$ERRFMT" "$OUTPUTFILE is not a file I can use for output" >&2 usage_message exit $E_OUTPUTFILE } [[ -w $OUTPUTFILE ]] || { printf "$ERRFMT" "I have no write access to $OUTPUTFILE" >&2 usage_message exit $E_OUTPUTFILE } } # sed commands used to URL-encode the MID fields # I think these are all the characters which can be # in MIDs but must be encoded in URLs. # See RFC 2822, section 3.4.2. # Note that the < > and @ characters are encoded by # the last three commands. URLENC_SED='s~%~%25~g; s~!~%21~g; s~#~%23~g; s~\$~%24~g; s~'\''~%27~g; s~&~%26~g; s~\*~%2a~g; s~\+~%2b~g; s~-~%2d~g; s~/~%2f~g; s~\?~%3f~g; s~\^~%5e~g; s~_~%5f~g; s~`~%60~g; s~{~%7b~g; s~|~%7c~g; s~}~%7d~g; s/~/%7e/g; s~<~%3c~g; s~>~%3e~g; s~@~%40~g' # make the list of URLS [[ $ARCHIVE == google ]] && { URLLIST=$(sed -n "\~^Message-ID: <[^<>@]\+@[^<>@]\+>[[:space:]]\?\$~I{s~^Message-ID:~~I; s~[[:space:]]~~g; $URLENC_SED; s~^%3c\(.*\)%3e$~http://groups.google.com/groups?as_umsgid=\1~; p}" "$@" ) || { printf "$ERRFMT" "*** exiting due to a sed error ***" \ "*** please, please debug me ***" >&2 exit "$E_SED" } } [[ $ARCHIVE == freenix ]] && { URLLIST=$(sed -n "\~^Message-ID: <[^<>@]\+@[^<>@]\+>[[:space:]]\?\$~I{s~^Message-ID:~~I; s~[[:space:]]~~g; $URLENC_SED; s~.*~http://howardk.freenix.org/msgid.cgi\?STYPE=msgid\&MSGI=\0~; p}" "$@" ) || { printf "$ERRFMT" "*** exiting due to a sed error ***" \ "*** please, please debug me ***" >&2 exit "$E_SED" } } # quit now if nothing found [[ -z $URLLIST ]] && { printf "$WRNFMT" "no Message-ID headers were found" >&2 exit $E_EMPTYOUT } # destroy dupes if -d was given # sed commands modified from example at Eric Pement's archive # . # note: this scales poorly. for me, it's fine with a few # hundred MIDs, but takes few minutes of heavy cpu usage # with several thousand MIDs. [[ $DUPEKILL == yes ]] && { URLLIST=$(echo -e "$URLLIST" | sed -n 'G; s~\n~&&~; \~^\(.*\n\).*\n\1~d; s~\n~~; h; P' ) (( $? )) && { printf "$ERRFMT" "*** exiting due to a sed error ***" \ "*** please, please debug me ***" >&2 exit "$E_SED" } } # send the URLs to stdout, file, and/or browser [[ $QUIET == no ]] && echo -e "$URLLIST" [[ -n $OUTPUTFILE ]] && { [[ $APPEND == yes ]] && echo -e "$URLLIST" >> "$OUTPUTFILE" || echo -e "$URLLIST" > "$OUTPUTFILE" } [[ -n $BROWSERCOMMAND ]] && { URLCOUNT=$(echo "$URLLIST" | wc -l) [[ $URLCOUNT -gt $BROWSERLIMIT ]] && [[ $FORCE == no ]] && { printf "$WRNFMT" \ "there are $URLCOUNT URLs, too many to send to the browser" \ "running me with -f would send them anyway" >&2 exit $E_OVERLIMIT } echo -e "$URLLIST" | xargs $BROWSERCOMMAND &>/dev/null & } exit 0 ################################################################## ## ## ## This script is provided as-is, with no warranty. Use it ## ## however you want. No rights are reserved. ## ## ## ## If you improve the script, please mail your changes to ## ## boxcars at gmx dot net. ## ## ## ##################################################################