198 lines
4.6 KiB
Bash
Executable file
198 lines
4.6 KiB
Bash
Executable file
#!/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: matrix-send [-t type] [-c] [-h] [-V] 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: 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 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" -Eo
|
|
then defaultevent="$1"
|
|
else conf_error "Invalid default event type"; fi
|
|
}
|
|
|
|
##################################
|
|
#### Configuration Statements ####
|
|
##################################
|
|
|
|
NoCache () {
|
|
nocache="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 () {
|
|
curl -s -XPOST -d "{"'"'"msgtype"'"'":"'"'"$mtype"'"'", "'"'"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
|
|
while getopts :t:chV opt
|
|
do
|
|
case $opt in
|
|
t)
|
|
if vargrep "m\.(text|notice)" "$OPTARG" -Eo
|
|
then
|
|
mtype="$OPTARG"
|
|
optind="$OPTIND"
|
|
else error "Type not valid (-t)"
|
|
fi
|
|
;;
|
|
c) ClearCache ;;
|
|
h) help ;;
|
|
V) version ;;
|
|
esac
|
|
done
|
|
|
|
###############################
|
|
#### 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.";
|
|
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
|