bin: extend broadcast to use any jsonDB user db

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-09 22:04:14 +01:00
parent 168584d693
commit 2a3ab2a145

View File

@ -1,15 +1,18 @@
#!/bin/bash
#===============================================================================
# shellcheck disable=SC2059
#
# FILE: bin/broadcast_message.sh
#
# USAGE: broadcast_message.sh [-h|--help] [--doit] [--groups|--both] [format] "message ...." [debug]
USAGE='broadcast_message.sh [-h|--help] [--doit] [--groups|--both|--db=file] [format] "message ...." [debug]'
#
# DESCRIPTION: send a message to all users the bot have seen (listet in count.jssh)
# DESCRIPTION: send a message to all users listed in a jsonDB (default count db)
#
# OPTIONS: --doit - broadcast is dangerous, simulate run without --doit
# --groups - send to groups instead of users
# --both - send to users and groups
# --db name - send to all user/groups in jsonDB database (e.g. blocked)
# db file: name.jssh, db keys are user/chat id, values are ignored
#
# format - normal, markdown, html (optional)
# message - message to send in specified format
@ -24,13 +27,13 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 16.12.2020 16:14
#
#### $$VERSION$$ v1.25-dev-34-gda214ab
#### $$VERSION$$ v1.25-dev-44-g168584d
#===============================================================================
# shellcheck disable=SC2059
####
# minimum messages seen in a chat before send a broadcast to it
MINCOUNT=2
USERDB=""
####
# broadcast is dangerous, without --doit we do a dry run ...
@ -49,6 +52,11 @@ elif [ "$1" = "--groups" ]; then
SENDTO="groups"
GROUPSALSO=" only"
shift
elif [ "$1" = "--db" ]; then
USERDB="${2%.jssh}"
MINCOUNT=""
GROUPSALSO=" and groups"
shift 2
fi
####
@ -71,11 +79,11 @@ case "$1" in
printf "missing missing arguments\n"
;&
"-h"*)
printf 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]\n'
printf 'usage: %s\n' "${USAGE}"
exit 1
;;
'--h'*)
sed -n '3,/###/p' <"$0"
sed -n -e '/# shellcheck /d' -e '3,/###/p' <"$0"
exit 1
;;
esac
@ -85,15 +93,16 @@ esac
source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug
# read in users
# read in users from given DB or count.jssh
database="${USERDB:-${COUNTFILE}}"
declare -A SENDALL
jssh_readDB_async "SENDALL" "${COUNTFILE}"
jssh_readDB_async "SENDALL" "${database}"
if [ -z "${SENDALL[*]}" ]; then
printf "${ORANGE}Countfile not found or empty,${NC}\n"
printf "${ORANGE}User database not found or empty: ${NC}${database}\n"
fi
# loop over users
printf "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}"
printf "${GREEN}Sending broadcast message to user of ${BOT_NAME} using database:${NC}${GREY} ${database##*/}"
{ # dry run
[ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n"
@ -106,7 +115,7 @@ fi
# ignore everything not a user or group
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
# ignore chats with no count or lower MINCOUNT
[[ ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ]] && continue
[[ -n "${MINCOUNT}" && ( ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ) ]] && continue
(( COUNT++ ))
if [ -z "${DOIT}" ]; then
printf "${SEND} ${USER} $1 $2\n"