7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER START
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# The contents of this file are subject to the terms of the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Common Development and Distribution License (the "License").
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You may not use this file except in compliance with the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# See the License for the specific language governing permissions
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# and limitations under the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# When distributing Covered Code, include this CDDL HEADER in each
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# If applicable, add the following below this CDDL HEADER, with the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# fields enclosed by brackets "[]" replaced with your own identifying
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# information: Portions Copyright [yyyy] [name of copyright owner]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# CDDL HEADER END
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# Make sure all math stuff runs in the "C" locale to avoid problems
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# with alternative # radix point representations (e.g. ',' instead of
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# '.' in de_DE.*-locales). This needs to be set _before_ any
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# floating-point constants are defined in this script).
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin '\') c="%5C" ;; # we need this to avoid the '\'-quoting hell
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# parse HTTP return code, cookies etc.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # we use '\r' as additional IFS to filter the final '\r'
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin IFS=$' \t\r' read -r h statuscode statusmsg # read HTTP/1.[01] <code>
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$h" != ~(Eil)HTTP/.* ]] && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin response.content_type="${i/~(El).*:[[:blank:]]*/}"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner [[ "${hexchunksize}" == ~(Elri)[0-9abcdef]+ ]] &&
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner (( chunksize=$( printf "16#%s\n" "${hexchunksize}" ) )) && (( chunksize > 0 )) ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # ksh93 binary variables use base64 encoding, the same as the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # HTTP basic authentification. We only have to read the
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin # plaintext user:passwd string into the binary variable "base64var"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin print -- "${base64var}" # print ASCII (base64) representation of binary var
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$SHTWITTER_USER" == "" ]] && { print -u2 -f $"%s: SHTWITTER_USER not set.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$SHTWITTER_PASSWD" == "" ]] && { print -u2 -f $"%s: SHTWITTER_PASSWD not set.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (( $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin request+="Authorization: Basic ${ encode_http_basic_auth "${SHTWITTER_USER}" "${SHTWITTER_PASSWD}" ; }\r\n"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin request+="Content-Type: application/x-www-form-urlencoded\r\n"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin request+="Content-Length: $(( urlform.content_length ))\r\n"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (( $? != 0 )) && { print -u2 -f "%s: Could not open connection to %s\n." "$0" "${url_host}" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin redirect {netfd}<&-
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin printf $"twitter response was (%s,%s): %s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$SHTWITTER_USER" == "" ]] && { print -u2 -f $"%s: SHTWITTER_USER not set.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin [[ "$SHTWITTER_PASSWD" == "" ]] && { print -u2 -f $"%s: SHTWITTER_PASSWD not set.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (( $# != 0 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin typeset url_path="/account/verify_credentials.xml"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin request+="Authorization: Basic ${ encode_http_basic_auth "${SHTWITTER_USER}" "${SHTWITTER_PASSWD}" ; }\r\n"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin request+="Content-Type: application/x-www-form-urlencoded\r\n"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin redirect {netfd}<&-
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin printf $"twitter response was (%s,%s): %s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin getopts -a "${progname}" "${shtwitter_usage}" OPT '-?'
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# program start
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# HTTP protocol client identifer
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulknertypeset -r http_user_agent="shtwitter/ksh93 (2010-03-27; ${ uname -s -r -p ; })"
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner[-?\n@(#)\$Id: shtwitter (Roland Mainz) 2010-03-27 \$\n]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[-author?Roland Mainz <roland.mainz@nrubsig.org>]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+NAME?shtwitter - read/write text data to internet clipboards]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+DESCRIPTION?\bshtwitter\b is a small utility which can read and write text
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin to the twitter.com microblogging site.]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+?The first arg \bmethod\b describes one of the methods, "update" posts a
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin text message to the users twitter blog, returning the raw response
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin message from the twitter server.]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+?The second arg \bstring\b contains the string data which should be
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinmethod [ string ]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtinyurl\b(1), http://www.twitter.com]
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinwhile getopts -a "${progname}" "${shtwitter_usage}" OPT ; do
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin# expecting at least one more argument
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin update|blog) put_twitter_message "$@" ; exit $? ;;