commit ac9dfa999f28a483d0410f0358b3fe700fbdc4ef Author: tastytea Date: Fri Jun 15 02:26:03 2018 +0200 initial commit diff --git a/sudobot b/sudobot new file mode 100755 index 0000000..6eda366 --- /dev/null +++ b/sudobot @@ -0,0 +1,48 @@ +#!/bin/sh +# Toots a sudo insult +# Author: tastytea +# Licence: CC-0 / Public Domain + +download() +{ + local files="https://www.sudo.ws/repos/sudo/raw-file/tip/plugins/sudoers/ins_{2001,classic,csops,goons,python}.h" + curl -s ${files} | grep -Eho '^ +".*"' | sed -r 's/^ +"(.*)"/\1/' > "${1}" +} + +send() +{ + local token="${1}" + local db="${2}" + local lines=$(wc -l ${db} | cut -d' ' -f1) + local random=$(( ( RANDOM % ${lines} ) + 1 )) + local text="$(sed ${random}\!d ${db})" + + curl -sS --header "Authorization: Bearer ${token}" -X POST \ + --data "spoiler_text=insult&status=${text}" https://botsin.space/api/v1/statuses +} + +main() +{ + local cfg=~/.config/sudobot.cfg + local dir_db=~/.local/share/sudobot + local token="" + + if [[ -f "${cfg}" ]]; then + read token < "${cfg}" + token=$(echo $token | sed 's/^.*=//') + else + echo "ERROR: no config file found in ${cfg}" >&2 + exit 1 + fi + + if [[ ! -d "${dir_db}" ]]; then + mkdir -p "${dir_db}" + fi + if [[ ! -f "${dir_db}/insults.db" ]]; then + download "${dir_db}/insults.db" + fi + + send "${token}" "${dir_db}/insults.db" +} + +main