Added -t option

This commit is contained in:
Jeremy 2022-07-29 08:12:43 +12:00
parent 14a7466c1c
commit 6ca4fe2d3f
5 changed files with 82 additions and 36 deletions

View file

@ -22,14 +22,15 @@ vargrep () {
}
usage () {
printf "usage: $0 [-c] [-h] [-V] message room\n"
printf "usage: matrix-send [-t type] [-c] [-h] [-V] message room\n"
exit 1
}
help () {
cat <<EOF
$0: send a message to a Matrix room
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
@ -42,7 +43,7 @@ EOF
version () {
cat <<EOF
$0: send a message to a Matrix room
matrix-send: send a message to a Matrix room
Version $version
matrix-send is licensed under the GNU General Public License v2.
@ -82,7 +83,14 @@ 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
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
}
##################################
@ -100,7 +108,7 @@ NoCache () {
GetAccessToken () {
[ -z "$manualAuth" ] && printf "Getting access token...\n"
if [ "$manualAuth" = "true" ];
then printf ""; # printf "" basically means do nothing
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
}
@ -121,7 +129,7 @@ ClearCache () {
}
Send () {
curl -s -XPOST -d "{"'"'"msgtype"'"'":"'"'"m.text"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$server/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token"
curl -s -XPOST -d "{"'"'"msgtype"'"'":"'"'"$mtype"'"'", "'"'"body"'"'":"'"'"$message"'"'"}" "https://$server/_matrix/client/r0/rooms/%21$roomid/send/m.room.message?access_token=$token"
}
########################
@ -129,15 +137,18 @@ Send () {
########################
[ -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
while getopts :chV opt
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 ;;
@ -174,13 +185,14 @@ fi
CacheAccessToken
if [ -z "$2" ];
then error "Room ID not specified (Leave out the '!').";
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" | sed 's/!//g')"
else roomid="$roomid_input"
fi
then roomid="$(printf "$roomid_input" | sed 's/!//g')"
else roomid="$roomid_input"; fi
Send
fi