#!/bin/bash #----------------------------------------------- # Fetches an I-D, puts it in the I-D directory, # and sets it to open using BBEdit. #----------------------------------------------- # Version 1.82 # June 14, 2011 # Randall Gellens #----------------------------------------------- ID_DIR="$HOME/IETF/Internet_Drafts" TMP_DIR="/tmp/IDs" SKIP_STR="A new Request for Comments"\n"This Internet-Draft has been deleted" #FTP_HOST="ftp.isi.edu" FTP_HOST="ftp.ietf.org" EDITOR_CREATOR_CODE="R*ch" SETFILE="/Developer/Tools/SetFile" ERR_NO_ERROR=0 # if at least one draft found ERR_FTP_ERROR=1 # if FTP fails ERR_BAD_PARAM=2 # if paramaters are not correct ERR_NO_MATCH=3 # if no matching drafts found ERR_NOTHING_NEW=4 # if all drafts are expired stubs or already exist # set up clean temp directory for drafts rm -fr ${TMP_DIR} > /dev/null 2>&1 mkdir ${TMP_DIR} cd ${TMP_DIR} # # ftp.ietf.org now works with IPv4 # if false then # # ftp.ietf.org is broken when using IPv4 (it works fine with IPv6) # if ping6 -c2 "$FTP_HOST" >/dev/null 2>&1 then # echo "can reach $FTP_HOST via IPv6"; IPv6="true" else # echo "unable to reach $FTP_HOST via IPv6"; FTP_HOST="ftp.isi.edu" FTP_DIR="in-notes" IPv6="false" fi fi #----------------------------------------------- # Show script usage #----------------------------------------------- function usage () { X=`basename $0` echo "usage: $X [options] "; echo " $X -n [options] "; echo ""; echo "Use \"--\" to end options when draft name starts with \"-\""; echo "-n turns off wilcarding"; echo ""; echo "Options:" echo " -v turns on verbose mode"; echo " -V turns on super-verbose mode"; echo " -m creates email with name(s) of new draft(s)"; echo " -t writes timestamp to stdout" echo ""; echo "Example:"; echo " $X -v -- -foo-"; echo "(verbosely fetches all drafts with \"-foo-\" in name)"; echo ""; echo "Return value: 0 (success) if at least one draft found." echo " 1 if FTP failed." echo " 2 if the paramaters are not correct." echo " 3 if no matching drafts found." echo " 4 if all matching drafts are expired stubs or already exist locally." echo ""; } #----------------------------------------------- # Verbose messages #----------------------------------------------- function say () { if [ "${opt_verbose}" = "y" ]; then echo "$1" fi } function vsay () { if [ "${opt_super_verbose}" = "y" ]; then echo "$1" fi } #----------------------------------------------- # Send mail. # Parameters: # 1: To # 2: Subject # 3: Body #----------------------------------------------- function send_mail () { vsay "::Sending mail::" vsay "To: $1" vsay "Subject: $2" vsay "Body: $3" if [ -x /usr/bin/osascript ]; then /usr/bin/osascript << EOI tell application "eudora v6.2.4b6.app" set aMsg to make message at (end of mailbox "Out" of mail folder "") set field "To" of aMsg to "$1" set field "Subject" of aMsg to "$2" set field "" of aMsg to "$3" queue aMsg end tell EOI say "Sent mail with Eudora to $1 with subject $2" elif [ -x /usr/bin/mail ]; then /usr/bin/mail -s "$2" "$1" << EOI2 $3 EOI2 say "Sent mail using /usr/bin/mail to $1 with subject $2" fi } #----------------------------------------------- # Set MacOS X creator code #----------------------------------------------- function set_creator_code () { if [ -x $SETFILE ]; then $SETFILE -c "$EDITOR_CREATOR_CODE" "$1" else vsay "$SETFILE doesn't exist or not executable" fi } #----------------------------------------------- # Exit and return a value # Parameters: # 1: Value to be returned. #----------------------------------------------- function EXIT () { X=`basename $0` vsay "$X returning $1" exit $1 } #----------------------------------------------- # Start of main script #----------------------------------------------- if [ $# -eq 0 ]; then usage; EXIT $ERR_BAD_PARAM; fi all_params="$*" opt_no_wildcards="n" opt_mail="n" opt_verbose="n" opt_super_verbose="n" while getopts ":tmnvV-" opt do case $opt in n) echo "will NOT use wildcards" opt_no_wildcards="y" ;; m) echo "will compose mail for new draft(s)" opt_mail="y" ;; v) echo "verbose mode" opt_verbose="y" ;; V) echo "super verbose mode" opt_super_verbose="y" opt_verbose="y" ;; t) DATE=`date` echo "------------------- $DATE -------------------" ;; -) #echo "--" ;; *) echo "Unrecognized option: $opt" EXIT $ERR_BAD_PARAM ;; esac done shift $(($OPTIND - 1)) # put strings in temp file for later search rm /tmp/TEMP_FTP_ID > /dev/null 2>&1 rm -fr /tmp/GOOD_STR > /dev/null 2>&1 cat >/tmp/GOOD_STR1 << "EOF" This\Wdocument\Wis\Wan\WInternet[- ]Draft By submitting this Internet[- ]Draft This\WInternet[- ]Draft\Wis\Wsubmitted Internet[- ]Drafts are working documents EOF cat >/tmp/GOOD_STR << "EOF" This\WInternet[- ]Draft\Wis\Wsubmitted This\Wdocument\Wis\Wan\WInternet[- ]Draft By submitting this Internet[- ]Draft Internet[- ]Drafts are working documents EOF DRAFTS_ADDED="" DRAFTS_COUNT=0 STUB_COUNT=0 DUP_COUNT=0 RSLT=${ERR_NO_ERROR} say "Request: $all_params" #loop through requested drafts (each of which may be a partial match) for idx do # make sure to use lowercase id=`echo "${idx}" | tr "[:upper:]" "[:lower:]"` vsay "Looking for ${idx}..." vsay "id: ${id}" if [ "$id" = "" ]; then echo "Bad parameter: empty string matches all I-Ds"; RSLT=${ERR_BAD_PARAM} continue; fi; if [ -f "$id" ]; then echo "I-D $id already exists"; let DUP_COUNT++ RSLT=${ERR_NOTHING_NEW} continue; fi; if [ "${opt_no_wildcards}" = "y" ]; then id_ftp=$id else id_ftp=*$id* fi # echo "will download ${id_ftp}" # download all matching drafts if ftp -v ${FTP_HOST}:/internet-drafts/${id_ftp} > /tmp/TEMP_FTP_ID 2>&1 ; then say "Fetched I-Ds matching ${id_ftp}" ID_LIST=`cat /tmp/TEMP_FTP_ID | fgrep "local: " | awk '{print $2}'`; if [ -z "$ID_LIST" ]; then vsay "****** FTP returned TRUE but unable to locate file name in 150 line of result:" MSGS=`cat /tmp/TEMP_FTP_ID` vsay "$MSGS" else vsay "$ID_LIST" fi # process each downloaded file # skip those that already exist or do not appear to be drafts for ID_FOUND in $ID_LIST do ID_FILE_NAME=${ID_DIR}/${ID_FOUND} # echo "processing ID ${ID_FOUND} as ${ID_FILE_NAME}" if [ -f "${ID_FILE_NAME}" ]; then say "ID already exists: ${ID_FOUND}" let DUP_COUNT++ RSLT=${ERR_NOTHING_NEW} elif [[ $ID_FOUND == *.xml ]]; then vsay "${ID_FOUND} is an .xml file" else # only process documents which appear to be IDs (not expired stubs # or notices that a draft was published as an RFC rslt=`grep -c -i -f /tmp/GOOD_STR "${ID_FOUND}"` vsay "grep result: ${rslt}" if [ "$rslt" != "0" ]; then set_creator_code ${ID_FOUND} # vsay $(ls -lh "${ID_FOUND}") ditto -rsrc ${TMP_DIR}/${ID_FOUND} "${ID_DIR}/" chmod a-w ${ID_DIR}/${ID_FOUND} # echo ### ls "${ID_FILE_NAME}" echo added ID: $(ls "${ID_FOUND}") DRAFTS_ADDED="${DRAFTS_ADDED} ${ID_FOUND}" let DRAFTS_COUNT++ else vsay "ls -lh $(/bin/pwd)/${ID_FOUND}" lsrslt=$(ls -lh "${ID_FOUND}") vsay "$lsrslt" say "${ID_FOUND} appears to be an expired stub or publication notice" let STUB_COUNT++ RSLT=${ERR_NOTHING_NEW} fi fi done rm /tmp/TEMP_FTP_ID; # rm -fr ${TMP_DIR} else echo "No such I-D: $id"; echo "FTP returned false"; MSGS=`cat /tmp/TEMP_FTP_ID` vsay "$MSGS" RSLT=${ERR_NO_MATCH} fi done if [ -n "$DRAFTS_ADDED" ]; then SEP=$'\r\t' PRE="file://${ID_DIR}" if [ $DRAFTS_COUNT > 1 ]; then say "Added $DRAFTS_COUNT drafts:$DRAFTS_ADDED" else say "Added 1 draft: $DRAFTS_ADDED" fi if [ "$opt_mail" = "y" ]; then SUBJECT="Added New I-D(s)" if [ $DRAFTS_COUNT -gt 1 ]; then BODY="The following ${DRAFTS_COUNT} drafts were added:" else BODY="The following draft was added:" fi for DRAFT in $DRAFTS_ADDED; do # BODY="${BODY}${SEP}<${PRE}${DRAFT}>" BODY="${BODY}${SEP}${DRAFT}" done send_mail "$USER" "$SUBJECT" "$BODY" fi RSLT=${ERR_NO_ERROR} else say "Added no drafts" RSLT=${ERR_NO_MATCH} fi vsay "returning $RSLT" EXIT $RSLT