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 # SYNOPSIS
**matrix-send** **matrix-send**
\[**-t** *type*]
\[**-c**] \[**-c**]
\[**-h**] \[**-h**]
\[**-v**] \[**-v**]
@ -16,23 +17,34 @@ MATRIX-SEND(1) - General Commands Manual
**matrix-send** **matrix-send**
is a script to send a message to a Matrix room. is a script to send a message to a Matrix room.
It works by sending a JSON request to your Matrix server. The message It works by sending a JSON message to your Matrix server. The default message
type will always be m.text, but other options may be considered in the type is m.text, but you can specify a custom type with
future. *-t*.
The options are as follows: 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** **-c**
> Clear cached access tokens. > Clear cached access tokens.
**-h** **-h**
> Show a help menu, then exit. > Show the help menu, then exit.
**-V** **-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 To begin, start by making a configuration file. This always has the location of
*~/.config/matrix-send.conf*. *~/.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. 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: 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 That should send a message saying
*Hello world!* *Hello world!*
@ -117,4 +129,4 @@ There are currently no plans to add encryption to
matrix-send.conf(5) 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
*username* *username*
Your Matrix username. The username of your Matrix account.
* Password * Password
*password* *password*
@ -29,7 +29,7 @@ The following directives are available:
* AccessToken * AccessToken
*token* *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*. *token*.
* CacheLocation * CacheLocation
@ -37,6 +37,12 @@ The following directives are available:
Instead of caching access tokens to ~/.cache, cache them to Instead of caching access tokens to ~/.cache, cache them to
*location*. *location*.
* DefaultEvent
*type*
Use
*type*
as the default event type instead of m.text.
The following statements are available: The following statements are available:
* NoCache * NoCache
@ -57,4 +63,4 @@ The following statements are available:
matrix-send(1) matrix-send(1)
OpenBSD 7.1 - July 28, 2022 OpenBSD 7.1 - July 29, 2022

View file

@ -22,14 +22,15 @@ vargrep () {
} }
usage () { usage () {
printf "usage: $0 [-c] [-h] [-V] message room\n" printf "usage: matrix-send [-t type] [-c] [-h] [-V] message room\n"
exit 1 exit 1
} }
help () { help () {
cat <<EOF cat <<EOF
$0: send a message to a Matrix room matrix-send: send a message to a Matrix room
Options: Options:
-t type: change default event type
-c: clear cached access tokens -c: clear cached access tokens
-h: show this help menu -h: show this help menu
-V: show version and program information -V: show version and program information
@ -42,7 +43,7 @@ EOF
version () { version () {
cat <<EOF cat <<EOF
$0: send a message to a Matrix room matrix-send: send a message to a Matrix room
Version $version Version $version
matrix-send is licensed under the GNU General Public License v2. matrix-send is licensed under the GNU General Public License v2.
@ -82,7 +83,14 @@ 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 ~)"; 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 () { 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 "";
else token=$(curl -s -XPOST -d "{"'"'"type"'"'":"'"'"m.login.password"'"'", "'"'"user"'"'":"'"'"$username"'"'", "'"'"password"'"'":"'"'"$password"'"'"}" "https://$server/_matrix/client/r0/login" | grep -oE 'syt_.+_...................._......'); else token=$(curl -s -XPOST -d "{"'"'"type"'"'":"'"'"m.login.password"'"'", "'"'"user"'"'":"'"'"$username"'"'", "'"'"password"'"'":"'"'"$password"'"'"}" "https://$server/_matrix/client/r0/login" | grep -oE 'syt_.+_...................._......');
fi fi
} }
@ -121,7 +129,7 @@ ClearCache () {
} }
Send () { 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" [ -e /usr/local/bin/curl ] || [ -e /usr/bin/curl ] || error "curl not found"
[ -z "$1" ] && usage [ -z "$1" ] && usage
#[ "$1" = "-c" ] && ClearCache while getopts :t:chV opt
#[ "$1" = "-h" ] && help
#[ "$1" = "-V" ] && version
while getopts :chV opt
do do
case $opt in 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 ;; c) ClearCache ;;
h) help ;; h) help ;;
V) version ;; V) version ;;
@ -174,13 +185,14 @@ fi
CacheAccessToken CacheAccessToken
if [ -z "$2" ]; if [ -z "$2" ];
then error "Room ID not specified (Leave out the '!')."; then error "Room ID not specified.";
else else
shift $((OPTIND-1))
message="$1" message="$1"
roomid_input="$2" roomid_input="$2"
if [ -z "$mtype" ]; then mtype="$defaultevent"; fi;
if vargrep '!' "$roomid_input" -qo if vargrep '!' "$roomid_input" -qo
then roomid="$(printf "$roomid" | sed 's/!//g')" then roomid="$(printf "$roomid_input" | sed 's/!//g')"
else roomid="$roomid_input" else roomid="$roomid_input"; fi
fi
Send Send
fi fi

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: July 28 2022 $ .Dd $Mdocdate: July 29 2022 $
.Dt MATRIX-SEND 1 .Dt MATRIX-SEND 1
.Os .Os
.Sh NAME .Sh NAME
@ -7,6 +7,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm matrix-send .Nm matrix-send
.Bk -words .Bk -words
.Op Fl t Ar type
.Op Fl c .Op Fl c
.Op Fl h .Op Fl h
.Op Fl v .Op Fl v
@ -15,18 +16,27 @@
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
is a script to send a message to a Matrix room. is a script to send a message to a Matrix room.
It works by sending a JSON request to your Matrix server. The message It works by sending a JSON message to your Matrix server. The default message
type will always be m.text, but other options may be considered in the type is m.text, but you can specify a custom type with
future. .Em -t .
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width keyword .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 .It Fl c
Clear cached access tokens. Clear cached access tokens.
.It Fl h .It Fl h
Show a help menu, then exit. Show the help menu, then exit.
.It Fl V .It Fl V
Print version and program information, then exit Print version and program information, then exit.
.El .El
.Pp .Pp
To begin, start by making a configuration file. This always has the location of 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. 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: You may have to escape the exclamation mark with a backslash. For example:
.Bd -literal -offset indent .Bd -literal -offset indent
matrix-send "Hello world!" \!zyxwvutsrq:example.org matrix-send "Hello world!" \\!zyxwvutsrq:example.org
.Ed .Ed
.Pp .Pp
That should send a message saying 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 .Dt MATRIX-SEND.CONF 5
.Os .Os
.Sh NAME .Sh NAME
@ -22,7 +22,7 @@ Server
.It .It
Username Username
.Ar username .Ar username
Your Matrix username. The username of your Matrix account.
.It .It
Password Password
.Ar password .Ar password
@ -30,13 +30,19 @@ Password
.It .It
AccessToken AccessToken
.Ar token .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 . .Ar token .
.It .It
CacheLocation CacheLocation
.Ar location .Ar location
Instead of caching access tokens to ~/.cache, cache them to Instead of caching access tokens to ~/.cache, cache them to
.Ar location . .Ar location .
.It
DefaultEvent
.Ar type
Use
.Ar type
as the default event type instead of m.text.
.El .El
.Pp .Pp
The following statements are available: The following statements are available: