prologic-saltyim/hooks/execbot.sh

48 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
# A Salty IM Command Execution Bot written as a POSIX Shell script
# using salty-chat read' --post-hook mechanism.
#
# Supported commands: date time uptime whoami
#
# Setup:
#
# $ salty-chat -i ~/.config/salty/execbot.key -u exec@yourdomain.tld make-user
# $ salty-chat -i ~/.config/salty/execbot.key -u exec@yourdomain.tld read --post-hook ./execbot.sh
set -e
# XXX: Set this to the execbot's key
identity=
# XXX: Set this to the execbot's addr
user=
tmpfile="$(mktemp -t "echobot-XXXXXX")"
trap 'rm $tmpfile' EXIT
cat > "$tmpfile"
sender="$(head -n 1 < "$tmpfile" | awk '{ print $2 }')"
sender="$(echo "$sender" | sed 's/[)(]//g')"
message="$(head -n 1 < "$tmpfile" | awk '{ $1 = ""; $2 = ""; print $0; }')"
cmd="$(echo "$message" | awk '{$1=$1};1')"
cmds="time date uptime whoami"
case $cmd in
"time" | "date")
result="$(date)"
;;
"uptime")
result="$(uptime)"
;;
"whoami")
result="$(whoami)"
;;
*)
result="$(printf "Invalid command '%s' Available commands are: $cmds" "$cmd")"
;;
esac
echo "$result" | salty-chat -d -i "$identity" -u "$user" send "$sender"