Compare commits
No commits in common. "efd6d5ddbb260381901a95cc6679dede03009e69" and "2e98292b989e5ae4d2941ab4b1e172cfb9c89d5d" have entirely different histories.
efd6d5ddbb
...
2e98292b98
|
@ -4,16 +4,6 @@ print_err() {
|
||||||
printf "%s\n" "$1" >&2
|
printf "%s\n" "$1" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() {
|
|
||||||
print_err "Usage: $0 [option]..."
|
|
||||||
print_err ""
|
|
||||||
print_err " -h, --help"
|
|
||||||
print_err " print this usage screen and exit"
|
|
||||||
print_err " -t, --type"
|
|
||||||
print_err " which message type should be sent"
|
|
||||||
print_err " must be one of 'text' or 'notice'"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Blessed Stack Overflow
|
# Blessed Stack Overflow
|
||||||
rawurlencode() {
|
rawurlencode() {
|
||||||
(set +x
|
(set +x
|
||||||
|
@ -40,7 +30,6 @@ default_drone_message() {
|
||||||
printf '%s' "$msg"
|
printf '%s' "$msg"
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure_variables() {
|
|
||||||
if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then
|
if [ "$DRONE" == "true" ] && [ -z "$MESSAGE" ]; then
|
||||||
MESSAGE="$(default_drone_message)"
|
MESSAGE="$(default_drone_message)"
|
||||||
fi
|
fi
|
||||||
|
@ -55,12 +44,6 @@ ensure_variables() {
|
||||||
print_err "You must provide MESSAGE"
|
print_err "You must provide MESSAGE"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
send_message() {
|
|
||||||
local login_json
|
|
||||||
local token
|
|
||||||
local message_json
|
|
||||||
|
|
||||||
if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
if [ "$(curl -XGET "$ADDRESS/_matrix/client/r0/login" 2>/dev/null |
|
||||||
jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then
|
jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then
|
||||||
|
@ -69,54 +52,19 @@ send_message() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
login_json="$(printf '%s\n%s' "$USER" "$PASS" |
|
LOGIN_JSON="$(printf '%s\n%s' "$USER" "$PASS" |
|
||||||
jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null
|
jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null
|
||||||
token="$(curl -XPOST \
|
TOKEN="$(curl -XPOST \
|
||||||
-d "$login_json" \
|
-d "$LOGIN_JSON" \
|
||||||
"$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)"
|
"$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)"
|
||||||
|
|
||||||
if [ -z "$token" ] || [ "$token" == "null" ]; then
|
if [ -z "$TOKEN" ] || [ "$TOKEN" == "null" ]; then
|
||||||
print_err "Error during login"
|
print_err "Error during login"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
message_json="$(printf '%s' "$MESSAGE" |
|
MESSAGE_JSON="$(printf '%s' "$MESSAGE" |
|
||||||
jq --raw-input --slurp "{msgtype: \"$MSG_TYPE\", body: .}")"
|
jq --raw-input --slurp '{msgtype: "m.text", body: .}')"
|
||||||
curl -XPOST \
|
curl -XPOST \
|
||||||
-d "$message_json" \
|
-d "$MESSAGE_JSON" \
|
||||||
"$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null
|
"$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$TOKEN")" 2>/dev/null
|
||||||
}
|
|
||||||
|
|
||||||
MSG_TYPE='m.notice'
|
|
||||||
|
|
||||||
while [ $# -gt 0 ]; do
|
|
||||||
opt="$1"
|
|
||||||
shift
|
|
||||||
|
|
||||||
case "$opt" in
|
|
||||||
-t|--type)
|
|
||||||
arg="$1"
|
|
||||||
shift
|
|
||||||
if [ -z "$arg" ]; then
|
|
||||||
print_err "Must give a message type when using '-t|--type'"
|
|
||||||
exit 1
|
|
||||||
elif [ "$arg" != "text" ] && [ "$arg" != "notice" ]; then
|
|
||||||
print_err "Invalid message type '$arg'"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
MSG_TYPE="m.$arg"
|
|
||||||
;;
|
|
||||||
-h|--help)
|
|
||||||
usage
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
print_err "Unknown argument '$opt'"
|
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
ensure_variables
|
|
||||||
send_message
|
|
||||||
|
|
Loading…
Reference in a new issue