Skip to content

Instantly share code, notes, and snippets.

@bortzmeyer
Created March 27, 2011 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bortzmeyer/889558 to your computer and use it in GitHub Desktop.
Save bortzmeyer/889558 to your computer and use it in GitHub Desktop.
Unix shell client for the SeenThis API. Runs the editor then post the message.
#!/bin/bash
ENDPOINT=https://seenthis.net/api/messages
TEMPLATE="<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'><summary></summary></entry>"
DEST_ENCODING=UTF-8
# TODO: it would be better to find it from the environment
MY_ENCODING=ISO-8859-1
if [ "$1" != "" ]; then
echo "Usage: $0" >&2
exit 1
fi
if [ ! -e $HOME/.seenthis/auth ]; then
echo "Cannot find the authentication data file $HOME/.seenthis/auth" >&2
exit 1
fi
USERNAME=$(head -n 1 $HOME/.seenthis/auth)
PASSWORD=$(head -n 2 $HOME/.seenthis/auth | tail -n 1)
if [ "$PASSWORD" = "" ]; then
echo "Invalid authentication data file $HOME/.seenthis/auth" >&2
exit 1
fi
if [ "$EDITOR" = "" ]; then
EDITOR=vi
fi
if ! which curl > /dev/null 2>&1 ; then
echo "Cannot find the HTTP client curl" >&2
exit 1
fi
if ! which iconv > /dev/null 2>&1 ; then
echo "Cannot find the character set converter iconv" >&2
exit 1
fi
# TODO: XML starlet can be installed as 'xml'
if ! which xmlstarlet > /dev/null 2>&1 ; then
echo "Cannot find the XML processing tool XML Startlet <http://xmlstar.sourceforge.net/>" >&2
exit 1
fi
# This script depends on the read builtin and its -t (timeout)
# option. It is not standard (hence the bash shebang), we have to test
# it.
# http://www.cyberciti.biz/tips/how-do-i-find-out-what-shell-im-using.html
shell=$(basename $(ps hp $$|awk '{print $5}'))
if [ $shell != "zsh" ] && [ $shell != "bash" ] && [ $shell != "ksh" ]; then
echo "Sorry, this script requires a shell with support of timeout in read. $shell is unknown to me." >&2
exit 1
fi
# OK, all preliminary tests done
TEMPORARY=$(mktemp)
if read -t 0 INPUT; then
echo $INPUT > $TEMPORARY
cat - >> $TEMPORARY
else
echo "Create the message for SeenThis..."
$EDITOR $TEMPORARY
if [ -z $TEMPORARY ]; then
echo "Empty file, cancelling..." >&2
exit 1
fi
fi
TEMPORARYXML=$(mktemp)
echo $TEMPLATE > $TEMPORARYXML
xmlstarlet ed --inplace --omit-decl \
-N atom=http://www.w3.org/2005/Atom \
--update '//atom:summary' \
-v "$(cat $TEMPORARY | iconv -f $MY_ENCODING -t $DEST_ENCODING | xmlstarlet esc | sed -e 's/$/\r/')" \
$TEMPORARYXML
# TODO: bad for security, other users can see the password with ps
curl --header "Content-Type: application/atom+xml;type=entry" \
--user $USERNAME:$PASSWORD \
--request POST \
--data @$TEMPORARYXML \
--insecure \
$ENDPOINT
rm -f $TEMPORARY $TEMPORARYXML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment