#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative # radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
if [[ "${LC_ALL}" != "" ]] ; then
export \
LC_MONETARY="${LC_ALL}" \
LC_MESSAGES="${LC_ALL}" \
LC_COLLATE="${LC_ALL}" \
LC_CTYPE="${LC_ALL}"
unset LC_ALL
fi
export LC_NUMERIC=C
function fatal_error
{
print -u2 "${progname}: $*"
exit 1
}
function encode_x_www_form_urlencoded
{
nameref formdata=$1
nameref content="formdata.content"
integer numformelements=${#formdata.form[*]}
integer i j
content=""
nameref element="formdata.form[${i}]"
typeset data="${element.data}"
integer datalen="${#data}"
typeset c
content+="${element.name}="
c="${data:j:1}"
' ') c="+" ;;
'!') c="%21" ;;
'*') c="%2A" ;;
"'") c="%27" ;;
'(') c="%28" ;;
')') c="%29" ;;
';') c="%3B" ;;
':') c="%3A" ;;
'@') c="%40" ;;
'&') c="%26" ;;
'=') c="%3D" ;;
'+') c="%2B" ;;
'$') c="%24" ;;
',') c="%2C" ;;
'/') c="%2F" ;;
'?') c="%3F" ;;
'%') c="%25" ;;
'#') c="%23" ;;
'[') c="%5B" ;;
']') c="%5D" ;;
*) ;;
esac
content+="$c"
done
done
formdata.content_length=${#content}
return 0
}
# parse HTTP return code, cookies etc.
function parse_http_response
{
nameref response="$1"
typeset h statuscode statusmsg i
# we use '\r' as additional IFS to filter the final '\r'
[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n" "$0" ; return 1 ; }
# skip remaining headers
[[ "$i" == $'\r' ]] && break
# strip '\r' at the end
i="${i/~(Er)$'\r'/}"
response.content_type="${i/~(El).*:[[:blank:]]*/}"
;;
integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
;;
response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
;;
esac
done
return 0
}
function cat_http_body
{
typeset emode="$1"
typeset hexchunksize="0"
integer chunksize=0
if [[ "${emode}" == "chunked" ]] ; then
done
else
fi
return 0
}
function encode_http_basic_auth
{
typeset user="$1"
typeset passwd="$2"
typeset s
integer s_len
typeset -b base64var
# ksh93 binary variables use base64 encoding, the same as the
# HTTP basic authentification. We only have to read the
# plaintext user:passwd string into the binary variable "base64var"
# and then print this variable as ASCII.
s="${user}:${passwd}"
s_len="${#s}"
print -- "${base64var}" # print ASCII (base64) representation of binary var
return 0
}
function put_twitter_message
{
[[ "$SHTWITTER_USER" == "" ]] && { print -u2 -f $"%s: SHTWITTER_USER not set.\n" "$0" ; return 1 ; }
[[ "$SHTWITTER_PASSWD" == "" ]] && { print -u2 -f $"%s: SHTWITTER_PASSWD not set.\n" "$0" ; return 1 ; }
# site setup
typeset url_host="twitter.com"
typeset url_path="/statuses/update.xml"
typeset url="http://${url_host}${url_path}"
integer netfd # http stream number
typeset msgtext="$1"
compound httpresponse # http response
# argument for "encode_x_www_form_urlencoded"
compound urlform=(
# input
compound -a form=(
)
# output
typeset content
integer content_length
)
typeset request=""
typeset content=""
content="${urlform.content}"
request="POST ${url_path} HTTP/1.1\r\n"
request+="Host: ${url_host}\r\n"
request+="Authorization: Basic ${ encode_http_basic_auth "${SHTWITTER_USER}" "${SHTWITTER_PASSWD}" ; }\r\n"
request+="User-Agent: ${http_user_agent}\r\n"
request+="Connection: close\r\n"
request+="Content-Type: application/x-www-form-urlencoded\r\n"
(( $? != 0 )) && { print -u2 -f "%s: Could not open connection to %s\n." "$0" "${url_host}" ; return 1 ; }
# send http post
{
print -n -- "${request}\r\n"
print -n -- "${content}\r\n"
} >&${netfd}
# process reply
# close connection
redirect {netfd}<&-
printf $"twitter response was (%s,%s): %s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
return 0
else
return 1
fi
# not reached
}
function verify_twitter_credentials
{
[[ "$SHTWITTER_USER" == "" ]] && { print -u2 -f $"%s: SHTWITTER_USER not set.\n" "$0" ; return 1 ; }
[[ "$SHTWITTER_PASSWD" == "" ]] && { print -u2 -f $"%s: SHTWITTER_PASSWD not set.\n" "$0" ; return 1 ; }
# site setup
typeset url_host="twitter.com"
typeset url_path="/account/verify_credentials.xml"
typeset url="http://${url_host}${url_path}"
integer netfd # http stream number
compound httpresponse # http response
typeset request=""
request="POST ${url_path} HTTP/1.1\r\n"
request+="Host: ${url_host}\r\n"
request+="Authorization: Basic ${ encode_http_basic_auth "${SHTWITTER_USER}" "${SHTWITTER_PASSWD}" ; }\r\n"
request+="User-Agent: ${http_user_agent}\r\n"
request+="Connection: close\r\n"
request+="Content-Type: application/x-www-form-urlencoded\r\n"
(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
# send http post
{
print -n -- "${request}\r\n"
} >&${netfd}
# process reply
# close connection
redirect {netfd}<&-
printf $"twitter response was (%s,%s): %s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
return 0
else
return 1
fi
# not reached
}
function usage
{
OPTIND=0
exit 2
}
# program start
builtin basename
builtin cat
builtin date
builtin uname
# HTTP protocol client identifer
typeset -r shtwitter_usage=$'+
[-?\n@(#)\$Id: shtwitter (Roland Mainz) 2010-03-27 \$\n]
[-author?Roland Mainz <roland.mainz@nrubsig.org>]
[+DESCRIPTION?\bshtwitter\b is a small utility which can read and write text
to the twitter.com microblogging site.]
[+?The first arg \bmethod\b describes one of the methods, "update" posts a
text message to the users twitter blog, returning the raw response
message from the twitter server.]
[+?The second arg \bstring\b contains the string data which should be
stored on twitter.com.]
method [ string ]
[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtinyurl\b(1), http://www.twitter.com]
'
while getopts -a "${progname}" "${shtwitter_usage}" OPT ; do
# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
*) usage ;;
esac
done
# expecting at least one more argument
typeset method="$1"
shift
case "${method}" in
*) usage ;;
esac
fatal_error $"not reached."
# EOF.