177 lines
4.1 KiB
Bash
177 lines
4.1 KiB
Bash
#!/usr/bin/env sh
|
|
# matrix-send: send a message to a Matrix room
|
|
|
|
version="1.0"
|
|
|
|
###########################
|
|
#### 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
|
|
}
|
|
|
|
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 ####
|
|
##################################
|
|
|
|
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 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
|
|
}
|
|
|
|
##################################
|
|
#### Configuration Statements ####
|
|
##################################
|
|
|
|
NoCache () {
|
|
nocache="true"
|
|
}
|
|
|
|
############################
|
|
#### Specific Functions ####
|
|
############################
|
|
|
|
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://$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 () {
|
|
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 ####
|
|
###############################
|
|
|
|
# Set default config settings
|
|
CacheLocation "$HOME/.cache"
|
|
|
|
# Load configuration
|
|
if [ -e $HOME/.config/matrix-send.conf ];
|
|
then . $HOME/.config/matrix-send.conf;
|
|
else
|
|
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 (Leave out the '!').";
|
|
else
|
|
message="$1"
|
|
roomid_input="$2"
|
|
if vargrep '!' "$roomid_input" -qo
|
|
then roomid="$(printf "$roomid" | sed 's/!//g')"
|
|
else roomid="$roomid_input"
|
|
fi
|
|
Send
|
|
fi
|