matrix-send/matrix-send
2022-08-04 17:47:59 +12:00

220 lines
5.2 KiB
Bash
Executable file

#!/usr/bin/env sh
# matrix-send: send a message to a Matrix room
version="1.1"
###########################
#### Generic Functions ####
###########################
error () {
printf "\033[31;1merror:\033[0m $1\n"
exit 1
}
conf_error () {
printf "\033[31;1mconfiguration error:\033[0m $1\n"
exit 2
}
warning () {
printf "\033[93;1mwarning:\033[0m $1\n"
}
vargrep () {
printf "$2\n" | grep "$1" $3
}
usage () {
printf "usage: matrix-send [-t type] [-C config] [-chV] message room\n"
exit 1
}
help () {
cat <<EOF
matrix-send: send a message to a Matrix room
Options:
-t type: change default event type
-C config: use a custom configuration file
-c: clear cached access tokens
-h: show this help menu
-V: show version and program information
For more information, type 'man matrix-send'.
https://codeberg.org/jtbx/matrix-send
EOF
exit 0
}
version () {
cat <<EOF
matrix-send: send a message to a Matrix room
Version $version
matrix-send is licensed under the GNU General Public License v2.
Made in New Zealand
https://codeberg.org/jtbx/matrix-send
EOF
exit 0
}
##################################
#### Configuration Directives ####
##################################
Server () {
[ -z $1 ] && conf_error "No argument for directive Server"
server="$1"
}
Username () {
[ -z $1 ] && conf_error "No argument for directive Username"
username="$1"
}
Password () {
[ -z $1 ] && conf_error "No argument(s) for directive Password"
password="$@"
}
AccessToken () {
[ -z $1 ] && conf_error "No argument for directive AccessToken"
manualAuth="true"
token="$1"
}
CacheLocation () {
[ -z $1 ] && conf_error "No argument for directive CacheLocation"
if vargrep "^/.+$" "$1" -Eq || \
vargrep "^~.+$" "$1" -Eq;
then cacheloc="$1";
else conf_error "Cache location is not valid (does not begin with / or ~)"; fi
}
DefaultEvent () {
[ -z $1 ] && conf_error "No argument for directive DefaultEvent"
if vargrep "m\.(text|notice)" "$1" -Eq
then defaultevent="$1"
else conf_error "Invalid default event type"; fi
}
##################################
#### Configuration Statements ####
##################################
NoCache () {
nocache="true"
}
PrettyPrint () {
prettyprint="true"
}
############################
#### Specific Functions ####
############################
GetAccessToken () {
[ -z "$manualAuth" ] && printf "Getting access token...\n"
if [ "$manualAuth" = "true" ];
then printf "";
else token=$(curl -s -XPOST -d "{"'"'"type"'"'":"'"'"m.login.password"'"'", "'"'"user"'"'":"'"'"$username"'"'", "'"'"password"'"'":"'"'"$password"'"'"}" "https://$server/_matrix/client/r0/login" | grep -oE 'syt_.+_...................._......');
fi
}
CacheAccessToken () {
if [ "$nocache" = "true" ];
then printf "";
else
mkdir -p "$cacheloc/matrix-send"
printf "$token\n" > "$cacheloc/matrix-send/access-token";
fi
}
ClearCache () {
[ -e "$cacheloc/matrix-send/access-token" ] || printf "There is no cache to be cleared.\n"
[ -e "$cacheloc/matrix-send/access-token" ] && rm -rf "$cacheloc/matrix-send/" && printf "Cleared cache\n"
exit 0
}
Send () {
if [ -z "$prettyprint" ]
then curl -s -XPOST -d "{"'"'"msgtype"'"'":"'"'"$mtype"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$server/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token"
else curl -s -XPOST -d "{"'"'"msgtype"'"'":"'"'"$mtype"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$server/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token" | jq
fi
}
########################
#### Initial checks ####
########################
[ -e /usr/local/bin/curl ] || [ -e /usr/bin/curl ] || error "curl not found"
[ -z "$1" ] && usage
CacheLocation "$HOME/.cache"
while getopts :t:C:chV opt
do
case $opt in
t)
if vargrep "m\.(text|notice)" "$OPTARG" -Eq
then
mtype="$OPTARG"
optind="$OPTIND"
else error "Type not valid (-t)"
fi
;;
C)
if vargrep "^~" "$OPTARG" -Eq
then config="$OPTARG"
elif vargrep "^/" "$OPTARG" -Eq
then config="$OPTARG"
else error "Configuration file location not valid (-C)"
fi
;;
c) ClearCache ;;
h) help ;;
V) version ;;
esac
done
unset OPTARG
unset OPTIND
###############################
#### Configuration loading ####
###############################
# Load configuration
if [ -e "$config" ];
then . "$config"
else
warning "~/.config/matrix-send.conf doesn't exist; creating it"
mkdir -p $HOME/.config
touch $HOME/.config/matrix-send.conf
fi
# Run checks for essential directives
[ -z $server ] && conf_error "Server directive is not present"
[ -z $username ] && conf_error "Username directive is not present"
[ -z "$password" ] && conf_error "Password directive is not present"
##############
#### Main ####
##############
# Get token and cache it (unless NoCache is set)
[ -e "$cacheloc/matrix-send/access-token" ] && token=$(cat $cacheloc/matrix-send/access-token)
[ -e "$cacheloc/matrix-send/access-token" ] || GetAccessToken
CacheAccessToken
if [ -z "$2" ];
then error "Room ID not specified.";
else
shift $((OPTIND-1))
message="$1"
roomid_input="$2"
if [ -z "$mtype" ]; then mtype="$defaultevent"; fi;
if vargrep '!' "$roomid_input" -qo
then roomid="$(printf "$roomid_input" | sed 's/!//g')"
else roomid="$roomid_input"; fi
Send
fi