#!/usr/bin/env bash print_err() { 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 " -f, --format" print_err " should the message be formatted using 'pandoc'." print_err " Must be either 'true' or 'false'." print_err " -t, --type" 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 " 'CI_PIPELINE_STATUS'/'DRONE_BUILD_STATUS' indicates a failure." } # Blessed Stack Overflow rawurlencode() { (set +x local string="${1}" local strlen=${#string} local encoded="" local pos c o for ((pos=0 ; pos/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="$(make_message_json "$MESSAGE")" curl -XPOST \ -d "$message_json" \ "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$token")" 2>/dev/null } MSG_TYPE='auto' FORMAT='true' while [ $# -gt 0 ]; do opt="$1" shift case "$opt" in -f|--format) arg="$1" shift if [ "$arg" == "true" ] || [ "$arg" == "false" ]; then FORMAT="$arg" continue fi print_err "Must give value 'true' or 'false' with '-t|--type'" exit 1 ;; -t|--type) arg="$1" shift if [ -z "$arg" ]; then print_err "Must give a message type when using '-t|--type'" exit 1 fi for type in text notice auto; do if [ "$arg" == "$type" ]; then continue 2 # Go to next argument fi done print_err "Invalid message type '$arg'" exit 1 ;; -h|--help) usage exit ;; *) print_err "Unknown argument '$opt'" usage exit 1 ;; esac done ensure_variables send_message