2020-12-18 13:53:44 +00:00
|
|
|
#!/bin/bash
|
2020-12-16 16:41:14 +00:00
|
|
|
#===============================================================================
|
2021-01-09 21:04:14 +00:00
|
|
|
# shellcheck disable=SC2059
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
2020-12-24 10:41:40 +00:00
|
|
|
# FILE: bin/broadcast_message.sh
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
2021-01-09 21:04:14 +00:00
|
|
|
USAGE='broadcast_message.sh [-h|--help] [--doit] [--groups|--both|--db=file] [format] "message ...." [debug]'
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
2021-01-09 21:04:14 +00:00
|
|
|
# DESCRIPTION: send a message to all users listed in a jsonDB (default count db)
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
|
|
|
# OPTIONS: --doit - broadcast is dangerous, simulate run without --doit
|
2020-12-24 17:20:39 +00:00
|
|
|
# --groups - send to groups instead of users
|
2021-01-10 16:16:14 +00:00
|
|
|
# --both - send to users and groups (default with --db)
|
2021-01-09 21:04:14 +00:00
|
|
|
# --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
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
|
|
|
# format - normal, markdown, html (optional)
|
|
|
|
# message - message to send in specified format
|
|
|
|
# if no format is givern send_message() format is used
|
|
|
|
#
|
|
|
|
# -h - display short help
|
|
|
|
# --help - this help
|
|
|
|
#
|
|
|
|
# Set BASHBOT_HOME to your installation directory
|
|
|
|
#
|
|
|
|
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
|
|
|
|
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
|
2020-12-18 13:53:44 +00:00
|
|
|
# CREATED: 16.12.2020 16:14
|
2020-12-16 16:41:14 +00:00
|
|
|
#
|
2021-01-17 08:57:08 +00:00
|
|
|
#### $$VERSION$$ v1.30-0-g3266427
|
2020-12-16 16:41:14 +00:00
|
|
|
#===============================================================================
|
2020-12-26 19:43:45 +00:00
|
|
|
|
|
|
|
####
|
|
|
|
# minimum messages seen in a chat before send a broadcast to it
|
|
|
|
MINCOUNT=2
|
2021-01-09 21:04:14 +00:00
|
|
|
USERDB=""
|
2020-12-16 16:41:14 +00:00
|
|
|
|
2020-12-18 13:53:44 +00:00
|
|
|
####
|
2020-12-16 16:41:14 +00:00
|
|
|
# broadcast is dangerous, without --doit we do a dry run ...
|
2020-12-24 17:20:39 +00:00
|
|
|
if [ "$1" = "--doit" ]; then
|
2020-12-16 16:41:14 +00:00
|
|
|
DOIT="yes"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2020-12-18 13:53:44 +00:00
|
|
|
####
|
2020-12-24 17:20:39 +00:00
|
|
|
# send to users by default, --group sends groups, --both to both
|
|
|
|
SENDTO="users"
|
|
|
|
if [ "$1" = "--both" ]; then
|
2020-12-16 16:41:14 +00:00
|
|
|
GROUPSALSO=" and groups"
|
|
|
|
shift
|
2020-12-24 17:20:39 +00:00
|
|
|
elif [ "$1" = "--groups" ]; then
|
|
|
|
SENDTO="groups"
|
|
|
|
GROUPSALSO=" only"
|
|
|
|
shift
|
2021-01-09 21:04:14 +00:00
|
|
|
elif [ "$1" = "--db" ]; then
|
|
|
|
USERDB="${2%.jssh}"
|
|
|
|
MINCOUNT=""
|
|
|
|
GROUPSALSO=" and groups"
|
|
|
|
shift 2
|
2020-12-16 16:41:14 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-18 13:53:44 +00:00
|
|
|
####
|
2020-12-16 16:41:14 +00:00
|
|
|
# parse args -----------------
|
|
|
|
SEND="send_message"
|
|
|
|
case "$1" in
|
2021-01-07 12:15:18 +00:00
|
|
|
"nor"*|"tex"*)
|
2020-12-16 16:41:14 +00:00
|
|
|
SEND="send_normal_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"mark"*)
|
|
|
|
SEND="send_markdownv2_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"html")
|
|
|
|
SEND="send_html_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
'')
|
2020-12-26 19:43:45 +00:00
|
|
|
printf "missing missing arguments\n"
|
2020-12-16 16:41:14 +00:00
|
|
|
;&
|
|
|
|
"-h"*)
|
2021-01-09 21:04:14 +00:00
|
|
|
printf 'usage: %s\n' "${USAGE}"
|
2020-12-16 16:41:14 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
'--h'*)
|
2021-01-09 21:04:14 +00:00
|
|
|
sed -n -e '/# shellcheck /d' -e '3,/###/p' <"$0"
|
2020-12-16 16:41:14 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-12-25 17:43:36 +00:00
|
|
|
# set bashbot environment
|
2020-12-17 07:43:30 +00:00
|
|
|
# shellcheck disable=SC1090
|
2020-12-25 17:43:36 +00:00
|
|
|
source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug
|
|
|
|
|
2020-12-16 16:41:14 +00:00
|
|
|
|
2021-01-09 21:04:14 +00:00
|
|
|
# read in users from given DB or count.jssh
|
|
|
|
database="${USERDB:-${COUNTFILE}}"
|
2020-12-16 16:41:14 +00:00
|
|
|
declare -A SENDALL
|
2021-01-09 21:04:14 +00:00
|
|
|
jssh_readDB_async "SENDALL" "${database}"
|
2020-12-16 16:41:14 +00:00
|
|
|
if [ -z "${SENDALL[*]}" ]; then
|
2021-01-09 21:04:14 +00:00
|
|
|
printf "${ORANGE}User database not found or empty: ${NC}${database}\n"
|
2020-12-16 16:41:14 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# loop over users
|
2021-01-10 16:16:14 +00:00
|
|
|
printf "${GREEN}Sending broadcast message to ${SENDTO}${GROUPSALSO} of ${BOT_NAME} using database:${NC}${GREY} ${database##*/}"
|
2020-12-16 16:41:14 +00:00
|
|
|
|
|
|
|
{ # dry run
|
2020-12-26 19:43:45 +00:00
|
|
|
[ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n"
|
2020-12-16 16:41:14 +00:00
|
|
|
|
|
|
|
for USER in ${!SENDALL[*]}
|
|
|
|
do
|
2020-12-24 17:20:39 +00:00
|
|
|
# send to users, groups or both ...
|
2020-12-16 16:41:14 +00:00
|
|
|
[[ -z "${GROUPSALSO}" && "${USER}" == *"-"* ]] && continue
|
2020-12-24 17:20:39 +00:00
|
|
|
[[ "${SENDTO}" != "users" && "${USER}" != *"-"* ]] && continue
|
|
|
|
# ignore everything not a user or group
|
2020-12-16 16:41:14 +00:00
|
|
|
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
|
2020-12-26 19:43:45 +00:00
|
|
|
# ignore chats with no count or lower MINCOUNT
|
2021-01-09 21:04:14 +00:00
|
|
|
[[ -n "${MINCOUNT}" && ( ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ) ]] && continue
|
2020-12-16 16:41:14 +00:00
|
|
|
(( COUNT++ ))
|
|
|
|
if [ -z "${DOIT}" ]; then
|
2020-12-26 19:43:45 +00:00
|
|
|
printf "${SEND} ${USER} $1 $2\n"
|
2020-12-16 16:41:14 +00:00
|
|
|
else
|
|
|
|
"${SEND}" "${USER}" "$1" "$2"
|
2020-12-26 19:43:45 +00:00
|
|
|
printf "." 1>&2
|
|
|
|
# ups, kicked or banned ...
|
|
|
|
if [ "${BOTSENT[ERROR]}" = "403" ]; then
|
|
|
|
# remove chat from future broadcasts
|
|
|
|
jssh_insertKeyDB "${USER}" "${SENDALL[${USER}]} banned" "${COUNTFILE}"
|
|
|
|
printf "${ORANGE}Warning: bot banned from chat${NC} %s ${ORANGE}after${NC} %s ${ORANGE}commands${NC}\n"\
|
|
|
|
"${USER}" "${SENDALL[${USER}]}"
|
|
|
|
fi
|
2020-12-16 16:41:14 +00:00
|
|
|
sleep 0.1
|
|
|
|
fi
|
|
|
|
done
|
2020-12-26 19:43:45 +00:00
|
|
|
# printout final stats message
|
|
|
|
printf "${NC}\n${GREEN}Message${NC} $1 ${GREEN}sent to${NC} ${COUNT} ${GREEN}${SENDTO}${GROUPSALSO}.${NC}\n"
|
2020-12-16 16:41:14 +00:00
|
|
|
} | more
|
2020-12-26 19:43:45 +00:00
|
|
|
|