Fixed a few issues

This commit is contained in:
Jeremy 2022-07-28 18:31:13 +12:00
parent e59bb405d6
commit bfe278eee5

89
matrix-send Executable file → Normal file
View file

@ -1,9 +1,7 @@
#!/bin/sh
# matrix-send: a super-simple command-line matrix client
#!/usr/bin/env sh
# matrix-send: send a message to a Matrix room
Version="dev_rolling"
[ -e /usr/bin/curl ] || printf "\033[31;1merror:\033[0m curl not found\n"
version="1.0"
###########################
#### Generic Functions ####
@ -14,44 +12,77 @@ error () {
exit 1
}
conf_error () { printf "\033[31;1mconfiguration error:\033[0m $1\n" exit 2
conf_error () {
printf "\033[31;1mconfiguration error:\033[0m $1\n"
exit 2
}
vargrep () {
printf "$2\n" | grep "$1" $3
}
usage () {
printf "usage: $0 [-c] [-h] [-V] message room\n"
exit 1
}
help () {
cat <<EOF
$0: send a message to a Matrix room
Options:
-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
$0: 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 ####
##################################
Homeserver () {
[ -z $1 ] && conf_error "Homeserver not specified in directive Homeserver"
homeserver="$1"
Server () {
[ -z $1 ] && conf_error "No argument for directive Server"
server="$1"
}
Username () {
[ -z $1 ] && conf_error "Username not specified in directive Username"
[ -z $1 ] && conf_error "No argument for directive Username"
username="$1"
}
Password () {
[ -z "$1" ] && conf_error "Password not specified in directive Password"
password="$*"
[ -z $1 ] && conf_error "No argument for directive Password"
password="$@"
}
AccessToken () {
[ -z $1 ] && conf_error "Token not specified in directive AccessToken"
[ -z $1 ] && conf_error "No argument for directive AccessToken"
manualAuth="true"
token="$1"
}
CacheLocation () {
[ -z $1 ] && conf_error "Cache Location not specified in directive 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 ~) in directive CacheLocation"; fi
else conf_error "Cache Location is not valid (does not begin with / or ~)"; fi
}
##################################
@ -70,7 +101,7 @@ GetAccessToken () {
[ -z "$manualAuth" ] && printf "Getting access token...\n"
if [ "$manualAuth" = "true" ];
then printf ""; # printf "" basically means do nothing
else token=$(curl -XPOST -d "{"'"'"type"'"'":"'"'"m.login.password"'"'", "'"'"user"'"'":"'"'"$username"'"'", "'"'"password"'"'":"'"'"$password"'"'"}" "https://$homeserver/_matrix/client/r0/login" | grep -oE 'syt_.+_...................._......');
else token=$(curl -XPOST -d "{"'"'"type"'"'":"'"'"m.login.password"'"'", "'"'"user"'"'":"'"'"$username"'"'", "'"'"password"'"'":"'"'"$password"'"'"}" "https://$server/_matrix/client/r0/login" | grep -oE 'syt_.+_...................._......');
fi
}
@ -90,9 +121,20 @@ ClearCache () {
}
Send () {
curl -XPOST -d "{"'"'"msgtype"'"'":"'"'"m.text"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$homeserver/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token"
curl -XPOST -d "{"'"'"msgtype"'"'":"'"'"m.text"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$server/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token"
}
########################
#### Initial checks ####
########################
[ -e /usr/local/bin/curl ] || [ -e /usr/bin/curl ] || error "curl not found"
[ -z "$1" ] && usage
[ "$1" = "-c" ] && ClearCache
[ "$1" = "-h" ] && help
[ "$1" = "-V" ] && version
###############################
#### Configuration loading ####
###############################
@ -109,7 +151,7 @@ else
fi
# Run checks for essential directives
[ -z $homeserver ] && conf_error "Homeserver directive is not present"
[ -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"
@ -117,9 +159,6 @@ fi
#### Main ####
##############
[ -z "$1" ] && error "Message not specified"
[ "$1" = "-c" ] && ClearCache
# 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
@ -129,6 +168,10 @@ if [ -z "$2" ];
then error "Room ID not specified (Leave out the '!').";
else
message="$1"
roomid="$2"
Send;
roomid_input="$2"
if vargrep '!' "$roomid_input" -qo
then roomid="$(printf "$roomid" | sed 's/!//g')"
else roomid="$roomid_input"
fi
Send
fi