2021-06-12 01:11:43 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
print_err() {
|
|
|
|
printf "%s\n" "$1" >&2
|
|
|
|
}
|
|
|
|
|
2021-06-12 18:56:36 +02:00
|
|
|
usage() {
|
|
|
|
print_err "Usage: $0 [option]..."
|
|
|
|
print_err ""
|
|
|
|
print_err " -h, --help"
|
|
|
|
print_err " print this usage screen and exit"
|
2021-06-12 18:56:58 +02:00
|
|
|
print_err " -t, --type"
|
2021-06-12 19:04:17 +02:00
|
|
|
print_err " which message type should be sent must be one of"
|
|
|
|
print_err " 'text' or 'notice', or 'auto'."
|
|
|
|
print_err " The special value 'auto' defaults to 'notice', unless"
|
|
|
|
print_err " 'DRONE_BUILD_STATUS' indicates a failure."
|
2021-06-12 18:56:36 +02:00
|
|
|
}
|
|
|
|
|
2021-06-12 01:11:43 +02:00
|
|
|
# Blessed Stack Overflow
|
|
|
|
rawurlencode() {
|
|
|
|
(set +x
|
|
|
|
local string="${1}"
|
|
|
|
local strlen=${#string}
|
|
|
|
local encoded=""
|
|
|
|
local pos c o
|
|
|
|
|
|
|
|
for ((pos=0 ; pos<strlen; pos++)); do
|
|
|
|
c=${string:$pos:1}
|
|
|
|
case "$c" in
|
|
|
|
[-_.~a-zA-Z0-9] ) o="$c" ;;
|
|
|
|
* ) printf -v o '%%%02x' "'$c"
|
|
|
|
esac
|
|
|
|
encoded+="$o"
|
|
|
|
done
|
|
|
|
echo "$encoded")
|
|
|
|
}
|
|
|
|
|
2021-06-12 18:22:04 +02:00
|
|
|
default_drone_message() {
|
|
|
|
local msg="Build ${DRONE_BUILD_STATUS}"
|
|
|
|
msg="$msg ${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/${DRONE_BUILD_NUMBER}"
|
|
|
|
msg="$msg (${DRONE_BRANCH})"
|
|
|
|
printf '%s' "$msg"
|
|
|
|
}
|
|
|
|
|
2021-06-12 18:56:36 +02:00
|
|
|
ensure_variables() {
|
|
|
|
if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then
|
|
|
|
MESSAGE="$(default_drone_message)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$USER" ] || [ -z "$PASS" ]; then
|
|
|
|
print_err "You must provide USER and PASS"
|
|
|
|
exit 1
|
|
|
|
elif [ -z "$ADDRESS" ] || [ -z "$ROOM" ]; then
|
|
|
|
print_err "You must provide ADDRESS and ROOM"
|
|
|
|
exit 1
|
|
|
|
elif [ -z "$MESSAGE" ]; then
|
|
|
|
print_err "You must provide MESSAGE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
send_message() {
|
|
|
|
local login_json
|
|
|
|
local token
|
|
|
|
local message_json
|
|
|
|
|
|
|
|
if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
|
|
|
jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then
|
|
|
|
print_err "Login method not supported"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
login_json="$(printf '%s\n%s' "$USER" "$PASS" |
|
|
|
|
jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null
|
|
|
|
token="$(curl -XPOST \
|
|
|
|
-d "$login_json" \
|
|
|
|
"$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)"
|
|
|
|
|
|
|
|
if [ -z "$token" ] || [ "$token" == "null" ]; then
|
|
|
|
print_err "Error during login"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
message_json="$(printf '%s' "$MESSAGE" |
|
2021-06-12 18:56:58 +02:00
|
|
|
jq --raw-input --slurp "{msgtype: \"$MSG_TYPE\", body: .}")"
|
2021-06-12 18:56:36 +02:00
|
|
|
curl -XPOST \
|
|
|
|
-d "$message_json" \
|
|
|
|
"$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null
|
|
|
|
}
|
|
|
|
|
2021-06-12 18:57:27 +02:00
|
|
|
MSG_TYPE='m.notice'
|
2021-06-12 18:56:58 +02:00
|
|
|
|
2021-06-12 18:56:36 +02:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
opt="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$opt" in
|
2021-06-12 18:56:58 +02:00
|
|
|
-t|--type)
|
|
|
|
arg="$1"
|
|
|
|
shift
|
|
|
|
if [ -z "$arg" ]; then
|
|
|
|
print_err "Must give a message type when using '-t|--type'"
|
|
|
|
exit 1
|
2021-06-12 19:04:17 +02:00
|
|
|
elif [ "$arg" == "text" ] || [ "$arg" == "notice" ]; then
|
|
|
|
MSG_TYPE="m.$arg"
|
|
|
|
elif [ "$arg" == "auto" ]; then
|
|
|
|
MSG_TYPE="m.notice"
|
|
|
|
if [ "$DRONE_BUILD_STATUS" == "failure" ]; then
|
|
|
|
MSG_TYPE="m.text"
|
|
|
|
fi
|
|
|
|
else
|
2021-06-12 18:56:58 +02:00
|
|
|
print_err "Invalid message type '$arg'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2021-06-12 18:56:36 +02:00
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
print_err "Unknown argument '$opt'"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
ensure_variables
|
|
|
|
send_message
|