xref: /titanic_52/usr/src/cmd/ast/libshell/common/scripts/shnote.sh (revision 906afcb89d0412cc073b95c2d701a804a8cdb62c)
1*906afcb8SAndy Fiddaman#!/usr/bin/ksh93
2*906afcb8SAndy Fiddaman
3*906afcb8SAndy Fiddaman#
4*906afcb8SAndy Fiddaman# CDDL HEADER START
5*906afcb8SAndy Fiddaman#
6*906afcb8SAndy Fiddaman# The contents of this file are subject to the terms of the
7*906afcb8SAndy Fiddaman# Common Development and Distribution License (the "License").
8*906afcb8SAndy Fiddaman# You may not use this file except in compliance with the License.
9*906afcb8SAndy Fiddaman#
10*906afcb8SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*906afcb8SAndy Fiddaman# or http://www.opensolaris.org/os/licensing.
12*906afcb8SAndy Fiddaman# See the License for the specific language governing permissions
13*906afcb8SAndy Fiddaman# and limitations under the License.
14*906afcb8SAndy Fiddaman#
15*906afcb8SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each
16*906afcb8SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*906afcb8SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the
18*906afcb8SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying
19*906afcb8SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner]
20*906afcb8SAndy Fiddaman#
21*906afcb8SAndy Fiddaman# CDDL HEADER END
22*906afcb8SAndy Fiddaman#
23*906afcb8SAndy Fiddaman
24*906afcb8SAndy Fiddaman#
25*906afcb8SAndy Fiddaman# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
26*906afcb8SAndy Fiddaman#
27*906afcb8SAndy Fiddaman
28*906afcb8SAndy Fiddaman# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
29*906afcb8SAndy Fiddamanexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
30*906afcb8SAndy Fiddaman
31*906afcb8SAndy Fiddaman# Make sure all math stuff runs in the "C" locale to avoid problems
32*906afcb8SAndy Fiddaman# with alternative # radix point representations (e.g. ',' instead of
33*906afcb8SAndy Fiddaman# '.' in de_DE.*-locales). This needs to be set _before_ any
34*906afcb8SAndy Fiddaman# floating-point constants are defined in this script).
35*906afcb8SAndy Fiddamanif [[ "${LC_ALL}" != "" ]] ; then
36*906afcb8SAndy Fiddaman    export \
37*906afcb8SAndy Fiddaman        LC_MONETARY="${LC_ALL}" \
38*906afcb8SAndy Fiddaman        LC_MESSAGES="${LC_ALL}" \
39*906afcb8SAndy Fiddaman        LC_COLLATE="${LC_ALL}" \
40*906afcb8SAndy Fiddaman        LC_CTYPE="${LC_ALL}"
41*906afcb8SAndy Fiddaman        unset LC_ALL
42*906afcb8SAndy Fiddamanfi
43*906afcb8SAndy Fiddamanexport LC_NUMERIC=C
44*906afcb8SAndy Fiddaman
45*906afcb8SAndy Fiddamanfunction fatal_error
46*906afcb8SAndy Fiddaman{
47*906afcb8SAndy Fiddaman	print -u2 "${progname}: $*"
48*906afcb8SAndy Fiddaman	exit 1
49*906afcb8SAndy Fiddaman}
50*906afcb8SAndy Fiddaman
51*906afcb8SAndy Fiddamanfunction encode_multipart_form_data
52*906afcb8SAndy Fiddaman{
53*906afcb8SAndy Fiddaman	nameref formdata="$1"
54*906afcb8SAndy Fiddaman	nameref content="formdata.content"
55*906afcb8SAndy Fiddaman	integer numformelements=${#formdata.form[*]}
56*906afcb8SAndy Fiddaman	integer i
57*906afcb8SAndy Fiddaman	typeset tmp
58*906afcb8SAndy Fiddaman
59*906afcb8SAndy Fiddaman	content=""
60*906afcb8SAndy Fiddaman
61*906afcb8SAndy Fiddaman	# todo: add support to upload files
62*906afcb8SAndy Fiddaman	for (( i=0 ; i < numformelements ; i++ )) ; do
63*906afcb8SAndy Fiddaman		nameref element="formdata.form[${i}]"
64*906afcb8SAndy Fiddaman
65*906afcb8SAndy Fiddaman		content+="--${formdata.boundary}\n"
66*906afcb8SAndy Fiddaman		content+="Content-Disposition: form-data; name=\"${element.name}\"\n"
67*906afcb8SAndy Fiddaman		content+="\n"
68*906afcb8SAndy Fiddaman		# make sure we quote the '\' properly since we pass these data to one instance of
69*906afcb8SAndy Fiddaman		# "print" when putting the content on the wire.
70*906afcb8SAndy Fiddaman		content+="${element.data//\\/\\\\}\n" # fixme: may need encoding for non-ASCII data
71*906afcb8SAndy Fiddaman	done
72*906afcb8SAndy Fiddaman
73*906afcb8SAndy Fiddaman	# we have to de-quote the content before we can count the real numer of bytes in the payload
74*906afcb8SAndy Fiddaman	tmp="$(print -- "${content}")"
75*906afcb8SAndy Fiddaman	formdata.content_length=${#tmp}
76*906afcb8SAndy Fiddaman
77*906afcb8SAndy Fiddaman	# add content tail (which MUST not be added to the content length)
78*906afcb8SAndy Fiddaman	content+="--${formdata.boundary}--\n"
79*906afcb8SAndy Fiddaman
80*906afcb8SAndy Fiddaman	return 0
81*906afcb8SAndy Fiddaman}
82*906afcb8SAndy Fiddaman
83*906afcb8SAndy Fiddaman# parse HTTP return code, cookies etc.
84*906afcb8SAndy Fiddamanfunction parse_http_response
85*906afcb8SAndy Fiddaman{
86*906afcb8SAndy Fiddaman	nameref response="$1"
87*906afcb8SAndy Fiddaman	typeset h statuscode statusmsg i
88*906afcb8SAndy Fiddaman
89*906afcb8SAndy Fiddaman	# we use '\r' as additional IFS to filter the final '\r'
90*906afcb8SAndy Fiddaman	IFS=$' \t\r' read -r h statuscode statusmsg  # read HTTP/1.[01] <code>
91*906afcb8SAndy Fiddaman	[[ "$h" != ~(Eil)HTTP/.* ]]         && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
92*906afcb8SAndy Fiddaman	[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n"  "$0" ; return 1 ; }
93*906afcb8SAndy Fiddaman	response.statuscode="$statuscode"
94*906afcb8SAndy Fiddaman	response.statusmsg="$statusmsg"
95*906afcb8SAndy Fiddaman
96*906afcb8SAndy Fiddaman	# skip remaining headers
97*906afcb8SAndy Fiddaman	while IFS='' read -r i ; do
98*906afcb8SAndy Fiddaman		[[ "$i" == $'\r' ]] && break
99*906afcb8SAndy Fiddaman
100*906afcb8SAndy Fiddaman		# strip '\r' at the end
101*906afcb8SAndy Fiddaman		i="${i/~(Er)$'\r'/}"
102*906afcb8SAndy Fiddaman
103*906afcb8SAndy Fiddaman		case "$i" in
104*906afcb8SAndy Fiddaman			~(Eli)Content-Type:.*)
105*906afcb8SAndy Fiddaman				response.content_type="${i/~(El).*:[[:blank:]]*/}"
106*906afcb8SAndy Fiddaman				;;
107*906afcb8SAndy Fiddaman			~(Eli)Content-Length:[[:blank:]]*[0-9]*)
108*906afcb8SAndy Fiddaman				integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
109*906afcb8SAndy Fiddaman				;;
110*906afcb8SAndy Fiddaman			~(Eli)Transfer-Encoding:.*)
111*906afcb8SAndy Fiddaman				response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
112*906afcb8SAndy Fiddaman				;;
113*906afcb8SAndy Fiddaman		esac
114*906afcb8SAndy Fiddaman	done
115*906afcb8SAndy Fiddaman
116*906afcb8SAndy Fiddaman	return 0
117*906afcb8SAndy Fiddaman}
118*906afcb8SAndy Fiddaman
119*906afcb8SAndy Fiddamanfunction cat_http_body
120*906afcb8SAndy Fiddaman{
121*906afcb8SAndy Fiddaman	typeset emode="$1"
122*906afcb8SAndy Fiddaman	typeset hexchunksize="0"
123*906afcb8SAndy Fiddaman	integer chunksize=0
124*906afcb8SAndy Fiddaman
125*906afcb8SAndy Fiddaman	if [[ "${emode}" == "chunked" ]] ; then
126*906afcb8SAndy Fiddaman		while IFS=$'\r' read hexchunksize &&
127*906afcb8SAndy Fiddaman			[[ "${hexchunksize}" == ~(Elri)[0-9abcdef]+ ]] &&
128*906afcb8SAndy Fiddaman			(( chunksize=$( printf "16#%s\n" "${hexchunksize}" ) )) && (( chunksize > 0 )) ; do
129*906afcb8SAndy Fiddaman			dd bs=1 count="${chunksize}" 2>/dev/null
130*906afcb8SAndy Fiddaman		done
131*906afcb8SAndy Fiddaman	else
132*906afcb8SAndy Fiddaman		cat
133*906afcb8SAndy Fiddaman	fi
134*906afcb8SAndy Fiddaman
135*906afcb8SAndy Fiddaman	return 0
136*906afcb8SAndy Fiddaman}
137*906afcb8SAndy Fiddaman
138*906afcb8SAndy Fiddamanfunction history_write_record
139*906afcb8SAndy Fiddaman{
140*906afcb8SAndy Fiddaman	# rec: history record:
141*906afcb8SAndy Fiddaman	#     rec.title
142*906afcb8SAndy Fiddaman	#     rec.description
143*906afcb8SAndy Fiddaman	#     rec.provider
144*906afcb8SAndy Fiddaman	#     rec.providertoken
145*906afcb8SAndy Fiddaman	#     rec.url
146*906afcb8SAndy Fiddaman	nameref rec="$1"
147*906afcb8SAndy Fiddaman	integer histfd
148*906afcb8SAndy Fiddaman
149*906afcb8SAndy Fiddaman	mkdir -p "${HOME}/.shnote"
150*906afcb8SAndy Fiddaman
151*906afcb8SAndy Fiddaman	{
152*906afcb8SAndy Fiddaman		# write a single-line record which can be read
153*906afcb8SAndy Fiddaman		# as a compound variable back into the shell
154*906afcb8SAndy Fiddaman		printf "title=%q description=%q date=%q provider=%q providertoken=%q url=%q\n" \
155*906afcb8SAndy Fiddaman			"${rec.title}" \
156*906afcb8SAndy Fiddaman			"${rec.description}" \
157*906afcb8SAndy Fiddaman			"$(date)" \
158*906afcb8SAndy Fiddaman			"${rec.provider}" \
159*906afcb8SAndy Fiddaman			"${rec.providertoken}" \
160*906afcb8SAndy Fiddaman			"${rec.url}"
161*906afcb8SAndy Fiddaman	} >>"${history_file}"
162*906afcb8SAndy Fiddaman
163*906afcb8SAndy Fiddaman	return $?
164*906afcb8SAndy Fiddaman}
165*906afcb8SAndy Fiddaman
166*906afcb8SAndy Fiddamanfunction print_history
167*906afcb8SAndy Fiddaman{
168*906afcb8SAndy Fiddaman	integer histfd # http stream number
169*906afcb8SAndy Fiddaman	typeset line
170*906afcb8SAndy Fiddaman
171*906afcb8SAndy Fiddaman	(( $# != 0 && $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
172*906afcb8SAndy Fiddaman
173*906afcb8SAndy Fiddaman	# default output format is:
174*906afcb8SAndy Fiddaman	# <access url>/<title> <date> <access url>
175*906afcb8SAndy Fiddaman	[[ "$1" == "-l" ]] || printf "# %s\t\t\t\t\t%s\t%s\n" "<url>" "<title>" "<date>"
176*906afcb8SAndy Fiddaman
177*906afcb8SAndy Fiddaman	# no history file ?
178*906afcb8SAndy Fiddaman	if [[ ! -f "${history_file}" ]] ; then
179*906afcb8SAndy Fiddaman		return 0
180*906afcb8SAndy Fiddaman	fi
181*906afcb8SAndy Fiddaman
182*906afcb8SAndy Fiddaman	# open history file
183*906afcb8SAndy Fiddaman	redirect {histfd}<> "${history_file}"
184*906afcb8SAndy Fiddaman	(( $? != 0 )) && { print -u2 "Could not open history file." ;  return 1 ; }
185*906afcb8SAndy Fiddaman
186*906afcb8SAndy Fiddaman	while read -u${histfd} line ; do
187*906afcb8SAndy Fiddaman		compound rec
188*906afcb8SAndy Fiddaman
189*906afcb8SAndy Fiddaman		printf "( %s )\n" "${line}"  | read -C rec
190*906afcb8SAndy Fiddaman
191*906afcb8SAndy Fiddaman		if [[ "$1" == "-l" ]] ; then
192*906afcb8SAndy Fiddaman			print -- "${rec}"
193*906afcb8SAndy Fiddaman		else
194*906afcb8SAndy Fiddaman			printf "%q\t%q\t%q\n" "${rec.url}" "${rec.title}" "${rec.date}"
195*906afcb8SAndy Fiddaman		fi
196*906afcb8SAndy Fiddaman
197*906afcb8SAndy Fiddaman		unset rec
198*906afcb8SAndy Fiddaman	done
199*906afcb8SAndy Fiddaman
200*906afcb8SAndy Fiddaman	# close history file
201*906afcb8SAndy Fiddaman	redirect {histfd}<&-
202*906afcb8SAndy Fiddaman
203*906afcb8SAndy Fiddaman	return 0
204*906afcb8SAndy Fiddaman}
205*906afcb8SAndy Fiddaman
206*906afcb8SAndy Fiddamanfunction put_note_pastebin_ca
207*906afcb8SAndy Fiddaman{
208*906afcb8SAndy Fiddaman	# key to autheticate this script against pastebin.ca
209*906afcb8SAndy Fiddaman	typeset -r pastebin_ca_key="9CFXFyeNC3iga/vthok75kTBu5kSSLPD"
210*906afcb8SAndy Fiddaman	# site setup
211*906afcb8SAndy Fiddaman	typeset url_host="opensolaris.pastebin.ca"
212*906afcb8SAndy Fiddaman	typeset url_path="/quiet-paste.php?api=${pastebin_ca_key}"
213*906afcb8SAndy Fiddaman	typeset url="http://${url_host}${url_path}"
214*906afcb8SAndy Fiddaman	integer netfd # http stream number
215*906afcb8SAndy Fiddaman	compound httpresponse
216*906afcb8SAndy Fiddaman
217*906afcb8SAndy Fiddaman	(( $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
218*906afcb8SAndy Fiddaman	(( ${#1} == 0 )) && { print -u2 -f $"%s: No data.\n" "$0" ; return 1 ; }
219*906afcb8SAndy Fiddaman
220*906afcb8SAndy Fiddaman	# argument for "encode_multipart_form_data"
221*906afcb8SAndy Fiddaman	compound mimeform=(
222*906afcb8SAndy Fiddaman		# input
223*906afcb8SAndy Fiddaman		typeset boundary
224*906afcb8SAndy Fiddaman		typeset -a form
225*906afcb8SAndy Fiddaman		# output
226*906afcb8SAndy Fiddaman		typeset content
227*906afcb8SAndy Fiddaman		integer content_length
228*906afcb8SAndy Fiddaman	)
229*906afcb8SAndy Fiddaman
230*906afcb8SAndy Fiddaman	typeset request=""
231*906afcb8SAndy Fiddaman	typeset content=""
232*906afcb8SAndy Fiddaman
233*906afcb8SAndy Fiddaman	typeset -r boundary="--------shnote_${RANDOM}_Xfish_${RANDOM}_Yeats_${RANDOM}_Zchicken_${RANDOM}monster_--------"
234*906afcb8SAndy Fiddaman
235*906afcb8SAndy Fiddaman	mimeform.boundary="${boundary}"
236*906afcb8SAndy Fiddaman	mimeform.form=( # we use explicit index numbers since we rely on them below when filling the history
237*906afcb8SAndy Fiddaman		[0]=( name="name"		data="${LOGNAME}" )
238*906afcb8SAndy Fiddaman		[1]=( name="expiry"		data="Never" )
239*906afcb8SAndy Fiddaman		[2]=( name="type"		data="1" )
240*906afcb8SAndy Fiddaman		[3]=( name="description"	data="logname=${LOGNAME};hostname=$(hostname);date=$(date)" )
241*906afcb8SAndy Fiddaman		[4]=( name="content"		data="$1" )
242*906afcb8SAndy Fiddaman	)
243*906afcb8SAndy Fiddaman	encode_multipart_form_data mimeform
244*906afcb8SAndy Fiddaman
245*906afcb8SAndy Fiddaman	content="${mimeform.content}"
246*906afcb8SAndy Fiddaman
247*906afcb8SAndy Fiddaman	request="POST ${url_path} HTTP/1.1\r\n"
248*906afcb8SAndy Fiddaman	request+="Host: ${url_host}\r\n"
249*906afcb8SAndy Fiddaman	request+="User-Agent: ${http_user_agent}\r\n"
250*906afcb8SAndy Fiddaman	request+="Connection: close\r\n"
251*906afcb8SAndy Fiddaman	request+="Content-Type: multipart/form-data; boundary=${boundary}\r\n"
252*906afcb8SAndy Fiddaman	request+="Content-Length: $(( mimeform.content_length ))\r\n"
253*906afcb8SAndy Fiddaman
254*906afcb8SAndy Fiddaman	redirect {netfd}<> "/dev/tcp/${url_host}/80"
255*906afcb8SAndy Fiddaman	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
256*906afcb8SAndy Fiddaman
257*906afcb8SAndy Fiddaman	# send http post
258*906afcb8SAndy Fiddaman	{
259*906afcb8SAndy Fiddaman		print -n -- "${request}\r\n"
260*906afcb8SAndy Fiddaman		print -n -- "${content}\r\n"
261*906afcb8SAndy Fiddaman	}  >&${netfd}
262*906afcb8SAndy Fiddaman
263*906afcb8SAndy Fiddaman	# process reply
264*906afcb8SAndy Fiddaman	parse_http_response httpresponse <&${netfd}
265*906afcb8SAndy Fiddaman	response="$(cat_http_body "${httpresponse.transfer_encoding}" <&${netfd})"
266*906afcb8SAndy Fiddaman
267*906afcb8SAndy Fiddaman	# close connection
268*906afcb8SAndy Fiddaman	redirect {netfd}<&-
269*906afcb8SAndy Fiddaman
270*906afcb8SAndy Fiddaman	if [[ "${response}" == ~(E).*SUCCESS.* ]] ; then
271*906afcb8SAndy Fiddaman		typeset response_token="${response/~(E).*SUCCESS:/}"
272*906afcb8SAndy Fiddaman
273*906afcb8SAndy Fiddaman		printf "SUCCESS: http://opensolaris.pastebin.ca/%s\n" "${response_token}"
274*906afcb8SAndy Fiddaman
275*906afcb8SAndy Fiddaman		# write history entry
276*906afcb8SAndy Fiddaman		compound histrec=(
277*906afcb8SAndy Fiddaman			title="${mimeform.form[0].data}"
278*906afcb8SAndy Fiddaman			description="${mimeform.form[3].data}"
279*906afcb8SAndy Fiddaman			providertoken="${response_token}"
280*906afcb8SAndy Fiddaman			provider="opensolaris.pastebin.ca"
281*906afcb8SAndy Fiddaman			url="http://opensolaris.pastebin.ca/${response_token}"
282*906afcb8SAndy Fiddaman		)
283*906afcb8SAndy Fiddaman
284*906afcb8SAndy Fiddaman		history_write_record histrec
285*906afcb8SAndy Fiddaman		return 0
286*906afcb8SAndy Fiddaman	else
287*906afcb8SAndy Fiddaman		printf "ERROR: %s\n" "${response}"
288*906afcb8SAndy Fiddaman		return 1
289*906afcb8SAndy Fiddaman	fi
290*906afcb8SAndy Fiddaman
291*906afcb8SAndy Fiddaman	# not reached
292*906afcb8SAndy Fiddaman}
293*906afcb8SAndy Fiddaman
294*906afcb8SAndy Fiddamanfunction get_note_pastebin_ca
295*906afcb8SAndy Fiddaman{
296*906afcb8SAndy Fiddaman	typeset recordname="$1"
297*906afcb8SAndy Fiddaman	integer netfd # http stream number
298*906afcb8SAndy Fiddaman
299*906afcb8SAndy Fiddaman	(( $# != 1 )) && { print -u2 -f $"%s: No key or key URL.\n" "$0" ; return 1 ; }
300*906afcb8SAndy Fiddaman
301*906afcb8SAndy Fiddaman	case "${recordname}" in
302*906afcb8SAndy Fiddaman		~(Elr)[0-9][0-9]*)
303*906afcb8SAndy Fiddaman			# pass-through
304*906afcb8SAndy Fiddaman			;;
305*906afcb8SAndy Fiddaman		~(Elr)http://opensolaris.pastebin.ca/raw/[0-9]*)
306*906afcb8SAndy Fiddaman			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\/raw\//}"
307*906afcb8SAndy Fiddaman			;;
308*906afcb8SAndy Fiddaman		~(Elr)http://opensolaris.pastebin.ca/[0-9]*)
309*906afcb8SAndy Fiddaman			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\//}"
310*906afcb8SAndy Fiddaman			;;
311*906afcb8SAndy Fiddaman		*)
312*906afcb8SAndy Fiddaman			fatal_error $"Unsupported record name ${recordname}."
313*906afcb8SAndy Fiddaman	esac
314*906afcb8SAndy Fiddaman
315*906afcb8SAndy Fiddaman	print -u2 -f "# Record name is '%s'\n" "${recordname}"
316*906afcb8SAndy Fiddaman
317*906afcb8SAndy Fiddaman	typeset url_host="opensolaris.pastebin.ca"
318*906afcb8SAndy Fiddaman	typeset url_path="/raw/${recordname}"
319*906afcb8SAndy Fiddaman	typeset url="http://${url_host}${url_path}"
320*906afcb8SAndy Fiddaman	# I hereby curse Solaris for not having an entry for "http" in /etc/services
321*906afcb8SAndy Fiddaman
322*906afcb8SAndy Fiddaman	# open TCP channel
323*906afcb8SAndy Fiddaman	redirect {netfd}<> "/dev/tcp/${url_host}/80"
324*906afcb8SAndy Fiddaman	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
325*906afcb8SAndy Fiddaman
326*906afcb8SAndy Fiddaman	# send HTTP request
327*906afcb8SAndy Fiddaman	request="GET ${url_path} HTTP/1.1\r\n"
328*906afcb8SAndy Fiddaman	request+="Host: ${url_host}\r\n"
329*906afcb8SAndy Fiddaman	request+="User-Agent: ${http_user_agent}\r\n"
330*906afcb8SAndy Fiddaman	request+="Connection: close\r\n"
331*906afcb8SAndy Fiddaman	print -u${netfd} -- "${request}\r\n"
332*906afcb8SAndy Fiddaman
333*906afcb8SAndy Fiddaman	# collect response and send it to stdout
334*906afcb8SAndy Fiddaman	parse_http_response httpresponse <&${netfd}
335*906afcb8SAndy Fiddaman	cat_http_body "${httpresponse.transfer_encoding}" <&${netfd}
336*906afcb8SAndy Fiddaman
337*906afcb8SAndy Fiddaman	# close connection
338*906afcb8SAndy Fiddaman	redirect {netfd}<&-
339*906afcb8SAndy Fiddaman
340*906afcb8SAndy Fiddaman	print # add newline
341*906afcb8SAndy Fiddaman
342*906afcb8SAndy Fiddaman	return 0
343*906afcb8SAndy Fiddaman}
344*906afcb8SAndy Fiddaman
345*906afcb8SAndy Fiddamanfunction usage
346*906afcb8SAndy Fiddaman{
347*906afcb8SAndy Fiddaman	OPTIND=0
348*906afcb8SAndy Fiddaman	getopts -a "${progname}" "${USAGE}" OPT '-?'
349*906afcb8SAndy Fiddaman	exit 2
350*906afcb8SAndy Fiddaman}
351*906afcb8SAndy Fiddaman
352*906afcb8SAndy Fiddaman# program start
353*906afcb8SAndy Fiddamanbuiltin basename
354*906afcb8SAndy Fiddamanbuiltin cat
355*906afcb8SAndy Fiddamanbuiltin date
356*906afcb8SAndy Fiddamanbuiltin uname
357*906afcb8SAndy Fiddaman
358*906afcb8SAndy Fiddamantypeset progname="${ basename "${0}" ; }"
359*906afcb8SAndy Fiddaman
360*906afcb8SAndy Fiddaman# HTTP protocol client identifer
361*906afcb8SAndy Fiddamantypeset -r http_user_agent="shnote/ksh93 (2010-03-27; $(uname -s -r -p))"
362*906afcb8SAndy Fiddaman
363*906afcb8SAndy Fiddaman# name of history log (the number after "history" is some kind of version
364*906afcb8SAndy Fiddaman# counter to handle incompatible changes to the history file format)
365*906afcb8SAndy Fiddamantypeset -r history_file="${HOME}/.shnote/history0.txt"
366*906afcb8SAndy Fiddaman
367*906afcb8SAndy Fiddamantypeset -r shnote_usage=$'+
368*906afcb8SAndy Fiddaman[-?\n@(#)\$Id: shnote (Roland Mainz) 2010-03-27 \$\n]
369*906afcb8SAndy Fiddaman[-author?Roland Mainz <roland.mainz@nrubsig.org>]
370*906afcb8SAndy Fiddaman[+NAME?shnote - read/write text data to internet clipboards]
371*906afcb8SAndy Fiddaman[+DESCRIPTION?\bshnote\b is a small utilty which can read and write text
372*906afcb8SAndy Fiddaman	data to internet "clipboards" such as opensolaris.pastebin.ca.]
373*906afcb8SAndy Fiddaman[+?The first arg \bmethod\b describes one of the methods, "put" saves a string
374*906afcb8SAndy Fiddaman	to the internet clipboard, returning an identifer and the full URL
375*906afcb8SAndy Fiddaman	where the data are stored. The method "get" retrives the raw
376*906afcb8SAndy Fiddaman	information using the identifer from the previous "put" action.
377*906afcb8SAndy Fiddaman	The method "hist" prints a history of transactions created with the
378*906afcb8SAndy Fiddaman	"put" method and the keys to retrive them again using the "get" method.]
379*906afcb8SAndy Fiddaman[+?The second arg \bstring\b contains either the string data which should be
380*906afcb8SAndy Fiddaman	stored on the clipboard using the "put" method, the "get" method uses
381*906afcb8SAndy Fiddaman	this information as identifer to retrive the raw data from the
382*906afcb8SAndy Fiddaman	clipboard.]
383*906afcb8SAndy Fiddaman
384*906afcb8SAndy Fiddamanmethod [ string ]
385*906afcb8SAndy Fiddaman
386*906afcb8SAndy Fiddaman[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), \bshtinyurl\b(1), http://opensolaris.pastebin.ca]
387*906afcb8SAndy Fiddaman'
388*906afcb8SAndy Fiddaman
389*906afcb8SAndy Fiddamanwhile getopts -a "${progname}" "${shnote_usage}" OPT ; do
390*906afcb8SAndy Fiddaman#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
391*906afcb8SAndy Fiddaman	case ${OPT} in
392*906afcb8SAndy Fiddaman		*)	usage ;;
393*906afcb8SAndy Fiddaman	esac
394*906afcb8SAndy Fiddamandone
395*906afcb8SAndy Fiddamanshift $((OPTIND-1))
396*906afcb8SAndy Fiddaman
397*906afcb8SAndy Fiddaman# expecting at least one more argument, the single method below will do
398*906afcb8SAndy Fiddaman# the checks for more arguments if needed ("put" and "get" methods need
399*906afcb8SAndy Fiddaman# at least one extra argument, "hist" none).
400*906afcb8SAndy Fiddaman(($# >= 1)) || usage
401*906afcb8SAndy Fiddaman
402*906afcb8SAndy Fiddamantypeset method="$1"
403*906afcb8SAndy Fiddamanshift
404*906afcb8SAndy Fiddaman
405*906afcb8SAndy Fiddamancase "${method}" in
406*906afcb8SAndy Fiddaman	put)	put_note_pastebin_ca "$@" ; exit $? ;;
407*906afcb8SAndy Fiddaman	get)	get_note_pastebin_ca "$@" ; exit $? ;;
408*906afcb8SAndy Fiddaman	hist)	print_history "$@"        ; exit $? ;;
409*906afcb8SAndy Fiddaman	*)	usage ;;
410*906afcb8SAndy Fiddamanesac
411*906afcb8SAndy Fiddaman
412*906afcb8SAndy Fiddamanfatal_error $"not reached."
413*906afcb8SAndy Fiddaman# EOF.
414