examples: format and run shellcheck

~~~
shfmt -i 2 -ci -w examples
shellcheck examples/*
~~~
master
Michael Santos 4 years ago
parent 11ee1c92f4
commit 25d8272656

@ -9,7 +9,7 @@ trap cleanup 0
BOT_DEBUG=${BOT_DEBUG-""} BOT_DEBUG=${BOT_DEBUG-""}
if [ "$BOT_DEBUG" ]; then if [ "$BOT_DEBUG" ]; then
set -x set -x
fi fi
TMPDIR=$(mktemp -d) TMPDIR=$(mktemp -d)
@ -17,63 +17,64 @@ TMPDIR=$(mktemp -d)
in="$TMPDIR/stdin" in="$TMPDIR/stdin"
out="$TMPDIR/stdout" out="$TMPDIR/stdout"
mkfifo $in mkfifo "$in"
mkfifo $out mkfifo "$out"
cleanup() { cleanup() {
rm -rf $TMPDIR rm -rf "$TMPDIR"
} }
decode() { decode() {
printf '%b' "${1//%/\\x}" printf '%b' "${1//%/\\x}"
} }
bot() { bot() {
local DEBUG=0 local DEBUG=0
while IFS=: read stanza type from to body; do while IFS=: read stanza type from to body; do
case "$stanza" in case "$stanza" in
m) ;; m) ;;
p) decode "$stanza:$type:$from:$to" 1>&2 p)
echo 1>&2 decode "$stanza:$type:$from:$to" 1>&2
continue echo 1>&2
;; continue
*) continue ;; ;;
esac *) continue ;;
esac
USER="$(decode ${from#*/})" USER="$(decode "${from#*/}")"
MSG="$(decode ${body})" MSG="$(decode "$body")"
case $MSG in case $MSG in
*"has set the subject to:"*) ;; *"has set the subject to:"*) ;;
"sudo make me a sandwich") "sudo make me a sandwich")
echo "$USER: you're a sandwich" echo "$USER: you're a sandwich"
;; ;;
sudo*) sudo*)
echo "I'm sorry, $USER. I'm afraid I can't do that." echo "I'm sorry, $USER. I'm afraid I can't do that."
;; ;;
uptime) uptime)
uptime uptime
;; ;;
runtime) runtime)
LC_ALL=POSIX ps -o etime= $$ LC_ALL=POSIX ps -o etime= $$
;; ;;
exit) exit)
echo "exiting ..." echo "exiting ..."
exit 0 exit 0
;; ;;
debug) debug)
DEBUG=$(( DEBUG ? 0 : 1 )) DEBUG=$((DEBUG ? 0 : 1))
;; ;;
*) *)
if [ "$DEBUG" == "0" ]; then if [ "$DEBUG" == "0" ]; then
printf "%s: %s\n" "$USER" "$MSG" printf "%s: %s\n" "$USER" "$MSG"
else else
echo "$MSG" echo "$MSG"
fi fi
;; ;;
esac esac
done < $out done <"$out"
} }
bot > $in & bot >"$in" &
xmppipe "$@" <$in >$out xmppipe "$@" <"$in" >"$out"

@ -17,7 +17,7 @@ set -o nounset
set -o pipefail set -o pipefail
if [ "$DEBUG" ]; then if [ "$DEBUG" ]; then
set -x set -x
fi fi
PROGNAME=$0 PROGNAME=$0
@ -25,45 +25,50 @@ TMPDIR=$(mktemp -d)
out=$TMPDIR/out out=$TMPDIR/out
atexit() { atexit() {
rm -rf $TMPDIR rm -rf "$TMPDIR"
} }
decode() { decode() {
while IFS=: read stanza type from to body; do # shellcheck disable=SC2034
case "$stanza" in while IFS=: read stanza type from to body; do
m) printf '%b' "${body//%/\\x}" ;; case "$stanza" in
*) ;; m) printf '%b' "${body//%/\\x}" ;;
esac *) ;;
done esac
done
} }
server() { server() {
CONNECT=0 CONNECT=0
xmppipe -e -r server -o $1 -b 1024 -x < /dev/null | \ xmppipe -e -r server -o "$1" -b 1024 -x </dev/null |
while IFS=: read stanza rest; do while IFS=: read stanza rest; do
case "$stanza" in case "$stanza" in
p) p)
CONNECT=$((CONNECT + 1)) CONNECT=$((CONNECT + 1))
if [ "$CONNECT" -gt "1" ]; then if [ "$CONNECT" -gt "1" ]; then
exec "$PROGNAME" session $@ # shellcheck disable=SC2068
fi exec "$PROGNAME" session $@
;; fi
*) ;; ;;
esac *) ;;
done > /dev/null esac
done >/dev/null
} }
session(){ session() {
mkdir -p $TMPDIR mkdir -p "$TMPDIR"
trap atexit 0 trap atexit 0
mkfifo $out mkfifo "$out"
nc $2 $3 < $out | xmppipe -P 100 -r session -o $1 -x -s | decode > $out nc "$2" "$3" <"$out" |
xmppipe -P 100 -r session -o "$1" -x -s |
decode >"$out"
} }
client() { client() {
xmppipe -P 100 -r client -o $1 -x -s | decode xmppipe -P 100 -r client -o "$1" -x -s | decode
} }
# shellcheck disable=SC2068
$@ $@

Loading…
Cancel
Save