iron-spmenu/iron-spmenu

95 lines
3.3 KiB
Bash
Executable file

#!/bin/sh
# iron-spmenu
# simple rautafarmi client using spmenu
# Licensed under GNU General Public License version 3.0
WDIR="${WDIR:-$HOME/.local/share/spmenu}"
INSTANCE="${INSTANCE:-https://donut.eu.org/rautafarmi}"
LINECOUNT="${LINECOUNT:-40}"
CKEY="${CKEY:-30}"
RUNLAUNCHER="${RUNLAUNCHER:-spmenu}"
RUNLAUNCHER_ARGS="${RUNLAUNCHER_ARGS:- -l $LINECOUNT -is 500 -g 1}"
# ansi escape codes, by default we use 6 colors
[ -z "$NOCOLOR" ] && \
COLOR_01="$(printf '\033[0m')" \
COLOR_02="$(printf '\033[0;33m')" \
COLOR_03="$(printf '\033[0;35m')" \
COLOR_04="$(printf '\033[0;36m')" \
COLOR_05="$(printf '\033[0;33m')" \
COLOR_06="$(printf '\033[0;32m')"
get_msg() {
curl -so "$WDIR/iron-spmenu-api" "$INSTANCE/api.php"
jq -e ".posts[] .username" "$WDIR/iron-spmenu-api" > "$WDIR/iron-spmenu-username"
jq -e ".posts[] .message" "$WDIR/iron-spmenu-api" > "$WDIR/iron-spmenu-message"
jq -e ".posts[] .time" "$WDIR/iron-spmenu-api" > "$WDIR/iron-spmenu-messagetime"
jq -e ".posts[] .postID" "$WDIR/iron-spmenu-api" > "$WDIR/iron-spmenu-messageid"
jq -e ".posts[] .imageURL" "$WDIR/iron-spmenu-api" > "$WDIR/iron-spmenu-messageimg"
for i in $(seq "$LINECOUNT"); do
username="$(head -n "$i" "$WDIR/iron-spmenu-username" | tail -n 1 | sed 's/""/Anonymous/g')"
message="$(head -n "$i" "$WDIR/iron-spmenu-message" | tail -n 1 | sed "s/%/%%/g; s/>>/DISREPLY/g; s/>/DISGTEXT/g; s/DISREPLY/${COLOR_04}>>/g; s/DISGTEXT/${COLOR_06}>/g; s/\" \"//g")"
time="$(head -n "$i" "$WDIR/iron-spmenu-messagetime" | tail -n 1)"
id="$(head -n "$i" "$WDIR/iron-spmenu-messageid" | tail -n 1)"
# download the image
curl -so "$WDIR/iron-spmenu-image-$id.png" "$(sed "${i}q;d" "$WDIR/iron-spmenu-messageimg" | sed 's/"//g')"
IMG="img://$WDIR/iron-spmenu-image-$id.png"
[ ! -f "$WDIR/iron-spmenu-image-$id.png" ] && IMG=""
# print it
if [ "$IMG" != "" ]; then
printf "$IMG\t"
printf "${COLOR_04}$id - ${COLOR_05}$username${COLOR_02} at ${COLOR_03}$time:${COLOR_01}\n" | sed 's/"//g' | sed "s/.*/${COLOR_01}&${COLOR_01}/"
printf "$IMG\t"
printf "$message${COLOR_01}\n" | sed 's/"//g' | sed "s/.*/${COLOR_01}&${COLOR_01}/"
else
printf "${COLOR_04}$id - ${COLOR_05}$username${COLOR_02} at ${COLOR_03}$time:${COLOR_01}\n$message${COLOR_01}\n" | sed 's/"//g' | sed "s/.*/${COLOR_01}&${COLOR_01}/"
fi
done
}
send_msg() {
[ -z "$1" ] && printf "You must specify a message to send.\n" && exit 1
curl -sX POST $INSTANCE/post.php -F "message=$1" -F "captcha=$CKEY" > /dev/null && STATUS=sent
if [ "$STATUS" = "sent" ]; then
printf "Message '%s' sent successfully!\n" "$1" && exit 0
else
printf "Message '%s' failed to send.\n" "$1" && exit 0
fi
}
help() {
cat << EOF
usage: iron-spmenu [-h] [-s message]
EOF
exit 0
}
cleanup() {
rm -f "$WDIR/iron-spmenu-*"
}
read_args() {
[ -z "$1" ] && get_msg | iconv -f utf-8 -t utf-8 | $RUNLAUNCHER $RUNLAUNCHER_ARGS && cleanup && exit 0
case "$1" in
"-s") send_msg "$2" ;;
"-h") help ;;
"") exit 0 ;;
*) printf "Unrecognized argument: $1\n" && exit 1 ;;
esac
}
main() {
[ ! -x "$(command -v jq)" ] && printf "jq not found in \$PATH\n" && exit 1
mkdir -p "$WDIR"
read_args "$@"
cleanup
}
main "$@"