2020-12-18 13:53:44 +00:00
|
|
|
#!/bin/bash
|
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
|
|
|
#
|
|
|
|
# USAGE: broadcast_message.sh [-h|--help] [--doit] [--groups] [format] "message ...." [debug]
|
|
|
|
#
|
|
|
|
# DESCRIPTION: send a message to all user the bot have seen (listet in count.jssh)
|
|
|
|
#
|
|
|
|
# OPTIONS: --doit - broadcast is dangerous, simulate run without --doit
|
|
|
|
# --groups - send to groups also, by default we send only to users
|
|
|
|
#
|
|
|
|
# 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
|
|
|
#
|
2020-12-24 10:45:40 +00:00
|
|
|
#### $$VERSION$$ v1.2-dev2-62-gfa24673
|
2020-12-16 16:41:14 +00:00
|
|
|
#===============================================================================
|
|
|
|
|
2020-12-18 13:53:44 +00:00
|
|
|
# set bashbot environment
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
source "${0%/*}/bashbot_env.inc.sh"
|
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 ...
|
|
|
|
if [ "$1" == "--doit" ]; then
|
|
|
|
DOIT="yes"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2020-12-18 13:53:44 +00:00
|
|
|
####
|
2020-12-16 16:41:14 +00:00
|
|
|
# send to users by default, --group sends to groups also
|
|
|
|
if [ "$1" == "--groups" ]; then
|
|
|
|
GROUPSALSO=" and groups"
|
|
|
|
shift
|
|
|
|
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
|
|
|
|
"nor*"|"tex*")
|
|
|
|
SEND="send_normal_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"mark"*)
|
|
|
|
SEND="send_markdownv2_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"html")
|
|
|
|
SEND="send_html_message"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
'')
|
|
|
|
echo "missing missing arguments"
|
|
|
|
;&
|
|
|
|
"-h"*)
|
2020-12-23 16:03:17 +00:00
|
|
|
echo 'usage: send_message [-h|--help] [format] "message ...." [debug]'
|
2020-12-16 16:41:14 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
'--h'*)
|
|
|
|
sed -n '3,/###/p' <"$0"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# source bashbot
|
2020-12-17 07:43:30 +00:00
|
|
|
# shellcheck disable=SC1090
|
2020-12-16 16:41:14 +00:00
|
|
|
source "${BASHBOT_HOME}/bashbot.sh" source "$3"
|
|
|
|
|
|
|
|
# read in users
|
|
|
|
declare -A SENDALL
|
|
|
|
jssh_readDB_async "SENDALL" "${COUNTFILE}"
|
|
|
|
if [ -z "${SENDALL[*]}" ]; then
|
|
|
|
echo -e "${ORANGE}Countfile not found or empty,${NC} "
|
|
|
|
fi
|
|
|
|
|
|
|
|
# loop over users
|
|
|
|
echo -e "${GREEN}Sending broadcast message to all users of $(getConfigKey "botname")${NC}${GREY}\c"
|
|
|
|
|
|
|
|
{ # dry run
|
|
|
|
[ -z "${DOIT}" ] && echo -e "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}"
|
|
|
|
|
|
|
|
for USER in ${!SENDALL[*]}
|
|
|
|
do
|
|
|
|
# ignore everything not a user or group
|
|
|
|
[[ -z "${GROUPSALSO}" && "${USER}" == *"-"* ]] && continue
|
|
|
|
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
|
|
|
|
(( COUNT++ ))
|
|
|
|
if [ -z "${DOIT}" ]; then
|
|
|
|
echo "${SEND}" "${USER}" "$1" "$2"
|
|
|
|
else
|
|
|
|
"${SEND}" "${USER}" "$1" "$2"
|
|
|
|
echo -e ".\c" 1>&2
|
|
|
|
sleep 0.1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo -e "${NC}\nMessage \"$1\" sent to ${COUNT} users${GROUPSALSO}."
|
|
|
|
} | more
|