#!/bin/ksh # Jordan likes command-line interfaces... # # What is this nonsense? # - It lets me post updates to my homepage from the commandline. # - The $USER and $PASS variables are sourced from /u9/psionic/mysql_login, so # if you intend on playing with this script, you'll have to make the # necessary changes. # # Requirements: vim, ggrep, mysql (the client) # I'll eventually add support for $EDITOR, but ... eh.. I use vim. # # Updates # * [7/8/03] # - Added "active saving" support. It now runs another copy of post in the # background that watches the temp file and checks for changes. When new # changes are detected, it will automatically post to the website. # Yes, I am indeed pathetic. # # * [7/5/03] # - Added real image and file support. Thumbnails are autogenerated by # ImageMagick's convert. Simply prefix a line with File: or Image: and # the necessary actions will be taken. w00t I'm so pathetically sad. # {{{ Variables... . /u9/psionic/mysql_login ECHO=/usr/ucb/echo MYSQL_DB="psionic" MYSQL_SERVER="db.csh.rit.edu" MYSQL_COMMAND="mysql" MYSQL_ARGS="--user=${USER} --password=${PASS} --host=${MYSQL_SERVER} ${MYSQL_DB}" MYSQL_SILENT="-s" CMD="post" TMP="/tmp/postandstuff" WEBDIR="/u9/psionic/public_html" WEBLOC="http://www.csh.rit.edu/~psionic" # Convert! CONVERT=/usr/local/bin/convert CONVERT_ARGS="-size 127x82" CONVERT_ARGS2="-resize 127x82" WATCHFILE= ID= # }}} # {{{ Argument Processing while [ $# != 0 ]; do case $1 in --post|-p) CMD="post" ;; --edit|-e) CMD="edit" ;; --delete|-d) CMD="delete" ;; --force|-f) rm $TMP $TMP.foo $TMP.orig ;; -w) # You don't need to execute this, it's just jordan being sad. WATCHFILE=$2 shift ;; -i) # Don't use this either unless you really need to, eh? ID=$2 shift ;; esac shift done #}}} # {{{ If -w, watch the file... if [ "$WATCHFILE" ]; then TMP=${WATCHFILE} HUP=0 trap "HUP=1" HUP touch -f ${TMP} ${TMP}.watch while [ $HUP -le 3 ]; do if [ "${TMP}" -nt "${TMP}.watch" ]; then diff $TMP $TMP.orig > /dev/null 2>&1 if [ $? = 1 ]; then SUBJ=`head -n 1 $TMP | sed -e 's/^Subject: //'` LINES=`wc -l $TMP | awk '{print $1}'` LINES=`expr ${LINES} - 1` BODY=`tail -${LINES} ${TMP} | sed -e 's/\n/\\'` BODY=`${ECHO} "${BODY}" | sed -e 's/\"/\\\"/g'` SQL="UPDATE news SET subject=\"${SUBJ}\", content=\"${BODY}\" WHERE id=${ID};" ${ECHO} "${SQL}" | sh -c "${MYSQL_COMMAND} ${MYSQL_ARGS}" > /dev/null 2>&1 cp $TMP $TMP.orig fi touch -f "${TMP}" "${TMP}.watch" fi if [ $HUP -ge 1 ]; then HUP=$(($HUP + 1)) fi done echo "Done." exit; fi #}}} if [ $CMD = "post" ]; then # {{{ Post a new article ${ECHO} "Subject: " > $TMP ${ECHO} >> $TMP cp $TMP $TMP.orig vim -n $TMP head -n 1 $TMP | grep '^Subject: .*' > /dev/null 2>&1 while [ $? -ne 0 ]; do cp $TMP $TMP.foo vim -n -c "${ECHO} 'Invalid Subject line format!'" $TMP diff $TMP $TMP.foo > /dev/null 2>&1 if [ $? -eq 0 ]; then ${ECHO} "Looks like you want to quit..." exit fi head -n 1 $TMP | grep '^Subject: .*' > /dev/null 2>&1 done diff $TMP $TMP.orig > /dev/null 2>&1 if [ $? -eq 1 ]; then # We want to to post this onw. SUBJ=`head -n 1 $TMP | sed -e 's/^Subject: //'` IML= rm -f $TMP.file.* 2> /dev/null ggrep -E '^(File|Image): ' $TMP | sed -e 's/^File: //' | sed -e 's/^Image: //' | split -l 1 - $TMP.file. #for linkto in `ls $TMP.file.*`; do for linkto in `cat $TMP.file.* 2> /dev/null`; do # Verify that this file is indeed under ${WEBDIR} # < not implemented, jordan is lazy > if [ "${linkto%%.png}" != "${linkto}" ] || \ [ "${linkto%%.jpg}" != "${linkto}" ]; then # It's an image. Make a thumbnail and move on. img=`basename ${linkto}` dir=`echo $linkto | sed -e "s/${img}\$//"` thumb="${dir}thumb_${img}" ${CONVERT} ${CONVERT_ARGS} ${WEBDIR}/${linkto} ${CONVERT_ARGS2} ${WEBDIR}/${thumb} IML="${IML}
" else file=`basename ${linkto}` size=`ls -ld /u9/psionic/public_html/${linkto} | awk '{print $5}'` IML="${IML}
${file}
Size: ${size}B

" fi done IML=`${ECHO} "${IML}" | sed -e 's/\"/\\\"/g'` cp $TMP $TMP.orig ggrep -v -E '^(File|Image): ' $TMP.orig > $TMP rm $TMP.orig LINES=`wc -l $TMP | awk '{print $1}'` LINES=`expr ${LINES} - 1` BODY=`tail -${LINES} ${TMP} | sed -e 's/\n/\\'` BODY=`${ECHO} "${BODY}" | sed -e 's/\"/\\\"/g'` SQL="INSERT INTO news (subject,date,content,images) VALUES(\"${SUBJ}\",UNIX_TIMESTAMP(),\"${BODY}\",\"${IML}\");" ${ECHO} "${SQL}" | sh -c "${MYSQL_COMMAND} ${MYSQL_ARGS}" > /dev/null else ${ECHO} "You didn't write anything..." fi # }}} elif [ $CMD = "edit" ]; then # {{{ Edit an existing article rm $TMP $TMP.orig $TMP.foo > /dev/null 2>&1 # Print out all of our entries. DATA=`\ (${ECHO} "ID DATE SUBJECT" && \ ${ECHO} "SELECT id, date, subject FROM news ORDER BY date DESC;" \ | sh -c "${MYSQL_COMMAND} -s ${MYSQL_ARGS}" \ | perl -MPOSIX -ne 'chomp(); ($id,$date,$subj) = split(/\s+/,$_,3); print "$id\t" . strftime("%d-%b-%Y %H:%M:%S",localtime($date)) . "\t$subj\n";')` LINES=`${ECHO} -n ${DATA} | wc -l` LINES=`expr ${LINES} + 1` # Let's be cool and only use less if we need more lines than we have! ${ECHO} "${DATA}"| less -F ${ECHO} "Which entry do you want to edit? (Choose an ID)" read ID if [ -z "${ID}" ]; then ${ECHO} "Canceled..." exit fi rm $TMP > /dev/null 2>&1 ${ECHO} "SELECT subject, content FROM news WHERE id=${ID};" \ | sh -c "${MYSQL_COMMAND} -s ${MYSQL_ARGS}" \ | perl -e 'undef $/; $_ = <>; s/\t/\n/; s/\\n/\n/g; s/^(.)/Subject: $1/; print $_' \ > $TMP cp $TMP $TMP.orig # Here... post -w $TMP -i ${ID}& vim -n $TMP #Check if post -w is still running... wait for it to die. POSTWATCHER=`ps -ef -o 'pid user args' | grep psionic | grep "post -w $TMP -i ${ID}" | grep -v grep | awk '{print $1}'` kill -HUP ${POSTWATCHER} # }}} elif [ $CMD = "delete" ]; then # {{{ Delete an article ${ECHO} -n "Do you want to delete the most recent entry? [y/N] " read opt case $opt in [yY][eE][sS]|[yY]) ${ECHO} "Ok, then..." # Find the most recent entry. ID=`${ECHO} "SELECT id FROM news ORDER BY date DESC LIMIT 1;" | \ sh -c "${MYSQL_COMMAND} ${MYSQL_ARGS}" | grep -v id` ${ECHO} "DELETE FROM news WHERE id=${ID};" | \ sh -c "${MYSQL_COMMAND} ${MYSQL_ARGS}" > /dev/null ;; *) ${ECHO} "Canceled." esac #}}} fi