Added -t option

This commit is contained in:
Jeremy Baxter 2022-07-29 08:12:43 +12:00
parent b98624bc78
commit 9591bcdaa6
5 changed files with 82 additions and 36 deletions

View file

@ -7,6 +7,7 @@ MATRIX-SEND(1) - General Commands Manual
# SYNOPSIS
**matrix-send**
\[**-t** *type*]
\[**-c**]
\[**-h**]
\[**-v**]
@ -16,23 +17,34 @@ MATRIX-SEND(1) - General Commands Manual
**matrix-send**
is a script to send a message to a Matrix room.
It works by sending a JSON request to your Matrix server. The message
type will always be m.text, but other options may be considered in the
future.
It works by sending a JSON message to your Matrix server. The default message
type is m.text, but you can specify a custom type with
*-t*.
The options are as follows:
**-t** *type*
> Change the event type to
> *type*.
> Currently supported types are
> *m.text*
> and
> *m.notice*.
> Default is
> *m.text*.
**-c**
> Clear cached access tokens.
**-h**
> Show a help menu, then exit.
> Show the help menu, then exit.
**-V**
> Print version and program information, then exit
> Print version and program information, then exit.
To begin, start by making a configuration file. This always has the location of
*~/.config/matrix-send.conf*.
@ -85,7 +97,7 @@ This will be something like
Type the Room ID as the argument after the message you wish to send.
You may have to escape the exclamation mark with a backslash. For example:
matrix-send "Hello world!" zyxwvutsrq:example.org
matrix-send "Hello world!" \!zyxwvutsrq:example.org
That should send a message saying
*Hello world!*
@ -117,4 +129,4 @@ There are currently no plans to add encryption to
matrix-send.conf(5)
OpenBSD 7.1 - July 28, 2022
OpenBSD 7.1 - July 29, 2022

View file

@ -21,7 +21,7 @@ The following directives are available:
* Username
*username*
Your Matrix username.
The username of your Matrix account.
* Password
*password*
@ -29,7 +29,7 @@ The following directives are available:
* AccessToken
*token*
Instead of using a username and password to obtain an access token, just use
Instead of using a username and password to obtain an access token, use
*token*.
* CacheLocation
@ -37,6 +37,12 @@ The following directives are available:
Instead of caching access tokens to ~/.cache, cache them to
*location*.
* DefaultEvent
*type*
Use
*type*
as the default event type instead of m.text.
The following statements are available:
* NoCache
@ -57,4 +63,4 @@ The following statements are available:
matrix-send(1)
OpenBSD 7.1 - July 28, 2022
OpenBSD 7.1 - July 29, 2022

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

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: July 28 2022 $
.Dd $Mdocdate: July 29 2022 $
.Dt MATRIX-SEND 1
.Os
.Sh NAME
@ -7,6 +7,7 @@
.Sh SYNOPSIS
.Nm matrix-send
.Bk -words
.Op Fl t Ar type
.Op Fl c
.Op Fl h
.Op Fl v
@ -15,18 +16,27 @@
.Sh DESCRIPTION
.Nm
is a script to send a message to a Matrix room.
It works by sending a JSON request to your Matrix server. The message
type will always be m.text, but other options may be considered in the
future.
It works by sending a JSON message to your Matrix server. The default message
type is m.text, but you can specify a custom type with
.Em -t .
.Pp
The options are as follows:
.Bl -tag -width keyword
.It Fl t Ar type
Change the event type to
.Ar type .
Currently supported types are
.Em m.text
and
.Em m.notice .
Default is
.Em m.text .
.It Fl c
Clear cached access tokens.
.It Fl h
Show a help menu, then exit.
Show the help menu, then exit.
.It Fl V
Print version and program information, then exit
Print version and program information, then exit.
.El
.Pp
To begin, start by making a configuration file. This always has the location of
@ -82,7 +92,7 @@ This will be something like
Type the Room ID as the argument after the message you wish to send.
You may have to escape the exclamation mark with a backslash. For example:
.Bd -literal -offset indent
matrix-send "Hello world!" \!zyxwvutsrq:example.org
matrix-send "Hello world!" \\!zyxwvutsrq:example.org
.Ed
.Pp
That should send a message saying

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: July 28 2022 $
.Dd $Mdocdate: July 29 2022 $
.Dt MATRIX-SEND.CONF 5
.Os
.Sh NAME
@ -22,7 +22,7 @@ Server
.It
Username
.Ar username
Your Matrix username.
The username of your Matrix account.
.It
Password
.Ar password
@ -30,13 +30,19 @@ Password
.It
AccessToken
.Ar token
Instead of using a username and password to obtain an access token, just use
Instead of using a username and password to obtain an access token, use
.Ar token .
.It
CacheLocation
.Ar location
Instead of caching access tokens to ~/.cache, cache them to
.Ar location .
.It
DefaultEvent
.Ar type
Use
.Ar type
as the default event type instead of m.text.
.El
.Pp
The following statements are available: