2019-05-12 15:51:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# file: modules/chatMember.sh
|
|
|
|
# do not edit, this file will be overwritten on update
|
|
|
|
|
|
|
|
# This file is public domain in the USA and all free countries.
|
|
|
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
|
|
|
#
|
2021-02-10 19:50:04 +00:00
|
|
|
#### $$VERSION$$ v1.45-dev-23-g805a74e
|
2020-06-14 18:56:46 +00:00
|
|
|
|
|
|
|
# will be automatically sourced from bashbot
|
2020-05-14 17:47:37 +00:00
|
|
|
|
|
|
|
# source once magic, function named like file
|
|
|
|
eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
|
2019-05-12 15:51:52 +00:00
|
|
|
|
|
|
|
|
2021-01-10 19:30:56 +00:00
|
|
|
# manage chat functions -------
|
2021-01-14 17:06:24 +00:00
|
|
|
# $1 chat
|
|
|
|
new_chat_invite() {
|
|
|
|
sendJson "$1" "" "${URL}/exportChatInviteLink"
|
2021-01-14 21:56:43 +00:00
|
|
|
[ "${BOTSENT[OK]}" = "true" ] && printf "%s\n" "${BOTSENT[RESULT]}"
|
2021-01-14 17:06:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:30:56 +00:00
|
|
|
# $1 chat, $2 title
|
|
|
|
set_chat_title() {
|
|
|
|
sendJson "$1" '"title": "'"$2"'"' "${URL}/setChatTitle"
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat, $2 title
|
|
|
|
set_chat_description() {
|
|
|
|
sendJson "$1" '"description": "'"$2"'"' "${URL}/setChatDescription"
|
2019-05-12 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 19:50:04 +00:00
|
|
|
# $1 chat $2 file
|
|
|
|
set_chat_photo() {
|
|
|
|
local file=$2
|
|
|
|
#XXX factor out to checkFileLocation ??
|
|
|
|
[[ "${file}" = *'..'* || "${file}" = '.'* ]] && err=1 # no directory traversal
|
|
|
|
if [[ "${file}" = '/'* ]] ; then
|
|
|
|
[[ ! "${file}" =~ ${FILE_REGEX} ]] && err=2 # absolute must match REGEX
|
|
|
|
else
|
|
|
|
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # others must be in UPLOADDIR
|
|
|
|
fi
|
|
|
|
[ ! -r "${file}" ] && err=3 # and file must exits of course
|
|
|
|
# file path error, generate error response
|
|
|
|
if [ -n "${err}" ]; then
|
|
|
|
BOTSENT=(); BOTSENT[OK]="false"
|
|
|
|
case "${err}" in
|
|
|
|
1) BOTSENT[ERROR]="Path to file $2 contains to much '../' or starts with '.'";;
|
|
|
|
2) BOTSENT[ERROR]="Path to file $2 does not match regex: ${FILE_REGEX} ";;
|
|
|
|
3) if [[ "$2" == "/"* ]];then
|
|
|
|
BOTSENT[ERROR]="File not found: $2"
|
|
|
|
else
|
|
|
|
BOTSENT[ERROR]="File not found: ${UPLOADDIR}/$2"
|
|
|
|
fi;;
|
|
|
|
esac
|
|
|
|
[ -n "${BASHBOTDEBUG}" ] && log_debug "set_chat_photo: CHAT=$1 FILE=$2 MSG=${BOTSENT[DESCRIPTION]}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
sendUpload "$1" "photo" "${file}" "${URL}/setChatPhoto"
|
|
|
|
}
|
2021-01-14 17:06:24 +00:00
|
|
|
# $1 chat
|
|
|
|
delete_chat_photo() {
|
|
|
|
sendJson "$1" "" "${URL}/deleteChatPhoto"
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat, $2 message_id
|
|
|
|
pin_chat_message() {
|
|
|
|
sendJson "$1" '"message_id": "'"$2"'"' "${URL}/pinChatMessage"
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat, $2 message_id
|
|
|
|
unpin_chat_message() {
|
|
|
|
sendJson "$1" '"message_id": "'"$2"'"' "${URL}/unpinChatMessage"
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat
|
2021-01-14 20:02:35 +00:00
|
|
|
unpinall_chat_message() {
|
2021-01-14 21:56:43 +00:00
|
|
|
sendJson "$1" "" "${URL}/unpinAllChatMessages"
|
2021-01-14 17:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat
|
|
|
|
delete_chat_stickers() {
|
|
|
|
sendJson "$1" "" "${URL}/deleteChatStickerSet"
|
|
|
|
}
|
2021-01-10 19:30:56 +00:00
|
|
|
|
|
|
|
# manage chat member functions -------
|
2021-01-27 18:30:03 +00:00
|
|
|
# $1 chat
|
|
|
|
chat_member_count() {
|
|
|
|
sendJson "$1" "" "${URL}/getChatMembersCount"
|
|
|
|
[ "${BOTSENT[OK]}" = "true" ] && printf "%s\n" "${BOTSENT[RESULT]}"
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:51:52 +00:00
|
|
|
kick_chat_member() {
|
2021-01-10 22:17:35 +00:00
|
|
|
sendJson "$1" 'user_id: '"$2"'' "${URL}/kickChatMember"
|
2019-05-12 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unban_chat_member() {
|
2021-01-10 22:17:35 +00:00
|
|
|
sendJson "$1" 'user_id: '"$2"'' "${URL}/unbanChatMember"
|
2019-05-12 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_chat() {
|
2021-01-10 22:17:35 +00:00
|
|
|
sendJson "$1" "" "${URL}/leaveChat"
|
2019-05-12 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 18:44:45 +00:00
|
|
|
# $1 chat, $2 userid, $3 ... "right[:true]" default false
|
|
|
|
# right: is_anonymous change_info post_messages edit_messages delete_messages invite_users restrict_members pin_messages promote_member
|
|
|
|
promote_chat_member() {
|
|
|
|
local arg bool json chat="$1" user="$2; shift 2"
|
|
|
|
for arg in "$@"
|
|
|
|
do
|
|
|
|
# default false
|
|
|
|
bool=false; [ "${arg##*:}" = "true" ] && bool="true"
|
|
|
|
# expand args
|
|
|
|
case "${arg}" in
|
|
|
|
*"anon"*) arg="is_anonymous";;
|
|
|
|
*"change"*) arg="can_change_info";;
|
|
|
|
*"post"*) arg="can_post_messages";;
|
|
|
|
*"edit"*) arg="can_edit_messages";;
|
|
|
|
*"delete"*) arg="can_delete_messages";;
|
|
|
|
*"pin"*) arg="can_pin_messages";;
|
|
|
|
*"invite"*) arg="can_invite_users";;
|
|
|
|
*"restrict"*) arg="can_restrict_members";;
|
|
|
|
*"promote"*) arg="can_promote_members";;
|
2021-02-07 09:20:09 +00:00
|
|
|
*) [ -n "${BASHBOTDEBUG}" ] && log_debug "promote_chat_member: unknown promotion CHAT=${chat} USER=${user} PROM=${arg}"
|
2021-01-25 18:44:45 +00:00
|
|
|
continue;;
|
|
|
|
esac
|
|
|
|
# compose json
|
|
|
|
[ -n "${json}" ] && json+=","
|
|
|
|
json+='"'"${arg}"'": "'"${bool}"'"'
|
|
|
|
done
|
|
|
|
sendJson "${chat}" '"user_id":'"${user}"','"${json}"'' "${URL}/promoteChatMember"
|
|
|
|
}
|
2021-01-10 19:30:56 +00:00
|
|
|
|
|
|
|
# bashbot specific functions ---------
|
|
|
|
|
|
|
|
# usage: status="$(get_chat_member_status "chat" "user")"
|
|
|
|
# $1 chat # $2 user
|
|
|
|
get_chat_member_status() {
|
2021-01-16 09:10:16 +00:00
|
|
|
sendJson "$1" '"user_id":'"$2"'' "${URL}/getChatMember"
|
2021-01-10 19:30:56 +00:00
|
|
|
# shellcheck disable=SC2154
|
2021-01-19 16:24:53 +00:00
|
|
|
printf "%s\n" "${UPD["result,status"]}"
|
2021-01-10 19:30:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:51:52 +00:00
|
|
|
user_is_creator() {
|
2020-06-28 14:40:35 +00:00
|
|
|
# empty is false ...
|
|
|
|
[[ "${1:--}" == "${2:-+}" || "$(get_chat_member_status "$1" "$2")" == "creator" ]] && return 0
|
2019-05-12 15:51:52 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:07:39 +00:00
|
|
|
# $1 chat
|
|
|
|
bot_is_admin() {
|
2020-12-03 13:25:32 +00:00
|
|
|
user_is_admin "$1" "$(getConfigKey "botid")"
|
2020-12-03 13:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# $1 chat # $2 user
|
2019-05-12 15:51:52 +00:00
|
|
|
user_is_admin() {
|
2020-12-03 13:25:32 +00:00
|
|
|
[[ -z "$1" || -z "$2" ]] && return 1
|
2020-06-28 14:40:35 +00:00
|
|
|
[ "${1:--}" == "${2:-+}" ] && return 0
|
2020-05-16 06:49:36 +00:00
|
|
|
user_is_botadmin "$2" && return 0
|
2020-06-28 14:40:35 +00:00
|
|
|
local me; me="$(get_chat_member_status "$1" "$2")"
|
|
|
|
[[ "${me}" =~ ^creator$|^administrator$ ]] && return 0
|
2019-05-12 15:51:52 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:07:39 +00:00
|
|
|
# $1 user
|
2019-05-12 15:51:52 +00:00
|
|
|
user_is_botadmin() {
|
2020-12-03 13:25:32 +00:00
|
|
|
[ -z "$1" ] && return 1
|
2020-06-28 14:40:35 +00:00
|
|
|
local admin; admin="$(getConfigKey "botadmin")"; [ -z "${admin}" ] && return 1
|
2021-01-05 15:17:34 +00:00
|
|
|
[[ "${admin}" == "$1" || "${admin}" == "$2" ]] && return 0
|
|
|
|
#[[ "${admin}" = "@*" ]] && [[ "${admin}" = "$2" ]] && return 0
|
2020-06-27 16:32:50 +00:00
|
|
|
if [ "${admin}" = "?" ]; then setConfigKey "botadmin" "${1:-?}"; return 0; fi
|
2019-05-12 15:51:52 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:07:39 +00:00
|
|
|
# $1 user # $2 key # $3 chat
|
2019-05-12 15:51:52 +00:00
|
|
|
user_is_allowed() {
|
2020-05-14 18:33:30 +00:00
|
|
|
[ -z "$1" ] && return 1
|
2021-01-14 19:23:18 +00:00
|
|
|
user_is_admin "$1" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
# user can do everything
|
2021-01-16 11:59:11 +00:00
|
|
|
grep -F -xq "$1:*:*" "${BOTACL}" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
[ -z "$2" ] && return 1
|
|
|
|
# user is allowed todo one action in every chat
|
2021-01-16 11:59:11 +00:00
|
|
|
grep -F -xq "$1:$2:*" "${BOTACL}" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
# all users are allowed to do one action in every chat
|
2021-01-16 11:59:11 +00:00
|
|
|
grep -F -xq "ALL:$2:*" "${BOTACL}" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
[ -z "$3" ] && return 1
|
|
|
|
# user is allowed to do one action in one chat
|
2021-01-16 11:59:11 +00:00
|
|
|
grep -F -xq "$1:$2:$3" "${BOTACL}" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
# all users are allowed to do one action in one chat
|
2021-01-16 11:59:11 +00:00
|
|
|
grep -F -xq "ALL:$2:$3" "${BOTACL}" && return 0
|
2020-11-03 11:21:11 +00:00
|
|
|
return 1
|
2019-05-12 15:51:52 +00:00
|
|
|
}
|