You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xmppipe/examples/bot.sh

65 lines
1.1 KiB
Bash

#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
if [ "$BOT_DEBUG" ]; then
set -x
fi
decode() {
printf '%b' "${1//%/\\x}"
}
bot() {
local DEBUG=0
while IFS=: read stanza type from to body; do
case "$stanza" in
m) ;;
p)
decode "$stanza:$type:$from:$to" 1>&2
echo 1>&2
continue
;;
*) continue ;;
esac
USER="$(decode "${from#*/}")"
MSG="$(decode "$body")"
case $MSG in
*"has set the subject to:"*) ;;
"sudo make me a sandwich")
echo "$USER: you're a sandwich"
;;
sudo*)
echo "I'm sorry, $USER. I'm afraid I can't do that."
;;
uptime)
uptime
;;
runtime)
LC_ALL=POSIX ps -o etime= $$
;;
exit)
echo "exiting ..."
exit 0
;;
debug)
DEBUG=$((DEBUG ? 0 : 1))
;;
*)
if [ "$DEBUG" == "0" ]; then
printf "%s: %s\n" "$USER" "$MSG"
else
echo "$MSG"
fi
;;
esac
done
}
coproc bot
xmppipe "$@" <&"${COPROC[0]}" >&"${COPROC[1]}"