#
# 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_multipart_form_data
{
nameref formdata="$1"
nameref content="formdata.content"
integer numformelements=${#formdata.form[*]}
integer i
typeset tmp
content=""
# todo: add support to upload files
nameref element="formdata.form[${i}]"
content+="--${formdata.boundary}\n"
content+="Content-Disposition: form-data; name=\"${element.name}\"\n"
content+="\n"
# make sure we quote the '\' properly since we pass these data to one instance of
# "print" when putting the content on the wire.
done
# we have to de-quote the content before we can count the real numer of bytes in the payload
formdata.content_length=${#tmp}
# add content tail (which MUST not be added to the content length)
content+="--${formdata.boundary}--\n"
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 history_write_record
{
# rec: history record:
# rec.url
nameref rec="$1"
integer histfd
mkdir -p "${HOME}/.shnote"
{
# write a single-line record which can be read
# as a compound variable back into the shell
} >>"${history_file}"
return $?
}
function print_history
{
integer histfd # http stream number
typeset line
# default output format is:
# <access url>/<title> <date> <access url>
# no history file ?
if [[ ! -f "${history_file}" ]] ; then
return 0
fi
# open history file
redirect {histfd}<> "${history_file}"
compound rec
if [[ "$1" == "-l" ]] ; then
print -- "${rec}"
else
fi
unset rec
done
# close history file
redirect {histfd}<&-
return 0
}
function put_note_pastebin_ca
{
# key to autheticate this script against pastebin.ca
typeset -r pastebin_ca_key="9CFXFyeNC3iga/vthok75kTBu5kSSLPD"
# site setup
typeset url_host="opensolaris.pastebin.ca"
typeset url_path="/quiet-paste.php?api=${pastebin_ca_key}"
typeset url="http://${url_host}${url_path}"
integer netfd # http stream number
compound httpresponse
(( ${#1} == 0 )) && { print -u2 -f $"%s: No data.\n" "$0" ; return 1 ; }
# argument for "encode_multipart_form_data"
compound mimeform=(
# input
typeset boundary
typeset -a form
# output
typeset content
integer content_length
)
typeset request=""
typeset content=""
typeset -r boundary="--------shnote_${RANDOM}_Xfish_${RANDOM}_Yeats_${RANDOM}_Zchicken_${RANDOM}monster_--------"
mimeform.boundary="${boundary}"
mimeform.form=( # we use explicit index numbers since we rely on them below when filling the history
)
content="${mimeform.content}"
request="POST ${url_path} HTTP/1.1\r\n"
request+="Host: ${url_host}\r\n"
request+="User-Agent: ${http_user_agent}\r\n"
request+="Connection: close\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}<&-
typeset response_token="${response/~(E).*SUCCESS:/}"
printf "SUCCESS: http://opensolaris.pastebin.ca/%s\n" "${response_token}"
# write history entry
compound histrec=(
title="${mimeform.form[0].data}"
description="${mimeform.form[3].data}"
providertoken="${response_token}"
url="http://opensolaris.pastebin.ca/${response_token}"
)
return 0
else
printf "ERROR: %s\n" "${response}"
return 1
fi
# not reached
}
function get_note_pastebin_ca
{
typeset recordname="$1"
integer netfd # http stream number
# pass-through
;;
recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\/raw\//}"
;;
recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\//}"
;;
*)
fatal_error $"Unsupported record name ${recordname}."
esac
print -u2 -f "# Record name is '%s'\n" "${recordname}"
typeset url_host="opensolaris.pastebin.ca"
typeset url_path="/raw/${recordname}"
typeset url="http://${url_host}${url_path}"
# open TCP channel
(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
# send HTTP request
request="GET ${url_path} HTTP/1.1\r\n"
request+="Host: ${url_host}\r\n"
request+="User-Agent: ${http_user_agent}\r\n"
request+="Connection: close\r\n"
print -u${netfd} -- "${request}\r\n"
# collect response and send it to stdout
# close connection
redirect {netfd}<&-
print # add newline
return 0
}
function usage
{
OPTIND=0
exit 2
}
# program start
builtin basename
builtin cat
builtin date
builtin uname
# HTTP protocol client identifer
# name of history log (the number after "history" is some kind of version
# counter to handle incompatible changes to the history file format)
typeset -r history_file="${HOME}/.shnote/history0.txt"
typeset -r shnote_usage=$'+
[-?\n@(#)\$Id: shnote (Roland Mainz) 2010-03-27 \$\n]
[-author?Roland Mainz <roland.mainz@nrubsig.org>]
[+DESCRIPTION?\bshnote\b is a small utilty which can read and write text
data to internet "clipboards" such as opensolaris.pastebin.ca.]
[+?The first arg \bmethod\b describes one of the methods, "put" saves a string
to the internet clipboard, returning an identifer and the full URL
where the data are stored. The method "get" retrives the raw
information using the identifer from the previous "put" action.
The method "hist" prints a history of transactions created with the
"put" method and the keys to retrive them again using the "get" method.]
[+?The second arg \bstring\b contains either the string data which should be
stored on the clipboard using the "put" method, the "get" method uses
this information as identifer to retrive the raw data from the
clipboard.]
method [ string ]
[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), \bshtinyurl\b(1), http://opensolaris.pastebin.ca]
'
# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
*) usage ;;
esac
done
# expecting at least one more argument, the single method below will do
# the checks for more arguments if needed ("put" and "get" methods need
# at least one extra argument, "hist" none).
typeset method="$1"
shift
*) usage ;;
esac
fatal_error $"not reached."
# EOF.