Fixed a few issues

This commit is contained in:
Jeremy Baxter 2022-07-28 18:31:13 +12:00
parent 867531d31b
commit 299ddafdb9

89
matrix-send Executable file → Normal file
View file

@ -1,9 +1,7 @@
#!/bin/sh #!/usr/bin/env sh
# matrix-send: a super-simple command-line matrix client # matrix-send: send a message to a Matrix room
Version="dev_rolling" version="1.0"
[ -e /usr/bin/curl ] || printf "\033[31;1merror:\033[0m curl not found\n"
########################### ###########################
#### Generic Functions #### #### Generic Functions ####
@ -14,44 +12,77 @@ error () {
exit 1 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 () { vargrep () {
printf "$2\n" | grep "$1" $3 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 #### #### Configuration Directives ####
################################## ##################################
Homeserver () { Server () {
[ -z $1 ] && conf_error "Homeserver not specified in directive Homeserver" [ -z $1 ] && conf_error "No argument for directive Server"
homeserver="$1" server="$1"
} }
Username () { Username () {
[ -z $1 ] && conf_error "Username not specified in directive Username" [ -z $1 ] && conf_error "No argument for directive Username"
username="$1" username="$1"
} }
Password () { Password () {
[ -z "$1" ] && conf_error "Password not specified in directive Password" [ -z $1 ] && conf_error "No argument for directive Password"
password="$*" password="$@"
} }
AccessToken () { AccessToken () {
[ -z $1 ] && conf_error "Token not specified in directive AccessToken" [ -z $1 ] && conf_error "No argument for directive AccessToken"
manualAuth="true" manualAuth="true"
token="$1" token="$1"
} }
CacheLocation () { 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 || \ if vargrep "^/.+$" "$1" -Eq || \
vargrep "^~.+$" "$1" -Eq; vargrep "^~.+$" "$1" -Eq;
then cacheloc="$1"; 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" [ -z "$manualAuth" ] && printf "Getting access token...\n"
if [ "$manualAuth" = "true" ]; if [ "$manualAuth" = "true" ];
then printf ""; # printf "" basically means do nothing 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 fi
} }
@ -90,9 +121,20 @@ ClearCache () {
} }
Send () { 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 #### #### Configuration loading ####
############################### ###############################
@ -109,7 +151,7 @@ else
fi fi
# Run checks for essential directives # 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 $username ] && conf_error "Username directive is not present"
[ -z "$password" ] && conf_error "Password directive is not present" [ -z "$password" ] && conf_error "Password directive is not present"
@ -117,9 +159,6 @@ fi
#### Main #### #### Main ####
############## ##############
[ -z "$1" ] && error "Message not specified"
[ "$1" = "-c" ] && ClearCache
# Get token and cache it (unless NoCache is set) # 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" ] && token=$(cat $cacheloc/matrix-send/access-token)
[ -e "$cacheloc/matrix-send/access-token" ] || GetAccessToken [ -e "$cacheloc/matrix-send/access-token" ] || GetAccessToken
@ -129,6 +168,10 @@ if [ -z "$2" ];
then error "Room ID not specified (Leave out the '!')."; then error "Room ID not specified (Leave out the '!').";
else else
message="$1" message="$1"
roomid="$2" roomid_input="$2"
Send; if vargrep '!' "$roomid_input" -qo
then roomid="$(printf "$roomid" | sed 's/!//g')"
else roomid="$roomid_input"
fi
Send
fi fi