Added --join-room, --leave-room and --invite-user.
This commit is contained in:
parent
7ebe2f6cfd
commit
65d533f8e5
2 changed files with 95 additions and 36 deletions
26
README.md
26
README.md
|
|
@ -69,40 +69,52 @@ Saved default room to ~/.matrix.sh
|
||||||
### Sending messages
|
### Sending messages
|
||||||
#### Sending a normal text message:
|
#### Sending a normal text message:
|
||||||
```
|
```
|
||||||
$ ./matrix.sh send "Hello World"
|
$ ./matrix.sh --send "Hello World"
|
||||||
|
```
|
||||||
|
|
||||||
|
Since `--send` is the default action, you can simply omit it:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ ./matrix.sh "Hello World"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Sending a text message with markup:
|
#### Sending a text message with markup:
|
||||||
```
|
```
|
||||||
$ ./matrix.sh send --html "This is <strong>very important</strong>."
|
$ ./matrix.sh --html "This is <strong>very important</strong>."
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Piping command output:
|
#### Piping command output:
|
||||||
```
|
```
|
||||||
$ echo "Hello" | ./matrix.sh send
|
$ echo "Hello" | ./matrix.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Code formatting:
|
#### Code formatting:
|
||||||
You can use `--pre` to send messages formatted as code. This will also escape
|
You can use `--pre` to send messages formatted as code. This will also escape
|
||||||
HTML tags.
|
HTML tags.
|
||||||
```
|
```
|
||||||
$ ls -l | ./matrix.sh send --pre
|
$ ls -l | ./matrix.sh --pre
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Sending files:
|
#### Sending files:
|
||||||
```
|
```
|
||||||
$ ./matrix.sh send --file=upload.zip
|
$ ./matrix.sh --file=upload.zip
|
||||||
```
|
```
|
||||||
Additionally use `--audio`, `-image` or `--video` to send files as audio, images or
|
Additionally use `--audio`, `-image` or `--video` to send files as audio, images or
|
||||||
video, respectively:
|
video, respectively:
|
||||||
```
|
```
|
||||||
$ ./matrix.sh send --file=IMG1234.jpg --image
|
$ ./matrix.sh --file=IMG1234.jpg --image
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Providing a room:
|
#### Providing a room:
|
||||||
You can use `--room=<room_id>` to provide a room_id. This supersedes the default room.
|
You can use `--room=<room_id>` to provide a room_id. This supersedes the default room.
|
||||||
```
|
```
|
||||||
$ ./matrix.sh send --room_id='!OEassajhhkasLULVAa:matrix.org' "Hello World"
|
$ ./matrix.sh --room_id='!OEassajhhkasLULVAa:matrix.org' "Hello World"
|
||||||
```
|
```
|
||||||
(Note: bash doesn't like exclamation marks in double quoted strings. So we
|
(Note: bash doesn't like exclamation marks in double quoted strings. So we
|
||||||
use single quotes for the room id.)
|
use single quotes for the room id.)
|
||||||
|
|
||||||
|
#### Other actions are:
|
||||||
|
* `--help` shows all available commands and options.
|
||||||
|
* `--join-room` joins a room. You will be asked for the room id.
|
||||||
|
* `--invite-user` invites a user into the default room or the one given by `--room`. You will be asked for the user id.
|
||||||
|
* `--leave-room` leaves a room. You will be asked which room to leave.
|
||||||
|
|
|
||||||
91
matrix.sh
91
matrix.sh
|
|
@ -1,8 +1,10 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#set -x
|
#set -x
|
||||||
|
|
||||||
|
# Needed for the strip-HTML-from-string-Regexp-like stuff.
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
VERSION="1.0"
|
VERSION="1.1"
|
||||||
LOG="false"
|
LOG="true"
|
||||||
|
|
||||||
AUTHORIZATION="X-Dummy: 1"
|
AUTHORIZATION="X-Dummy: 1"
|
||||||
|
|
||||||
|
|
@ -18,10 +20,14 @@ help() {
|
||||||
echo "$0 <action> [<options>] [<message>]"
|
echo "$0 <action> [<options>] [<message>]"
|
||||||
echo
|
echo
|
||||||
echo "ACTIONS"
|
echo "ACTIONS"
|
||||||
echo " login Login to a server."
|
echo " --login [*] Login to a server."
|
||||||
echo " list-rooms List rooms the matrix user joined or is invited to."
|
echo " --list-rooms List rooms the matrix user joined or is invited to."
|
||||||
echo " select-default-room Select a default room."
|
echo " --select-default-room [*] Select a default room."
|
||||||
echo " help Show this help."
|
echo " --join-room [*] Joins a room."
|
||||||
|
echo " --leave-room [*] Leaves a room."
|
||||||
|
echo " --invite-user [*] Invites a user to a room."
|
||||||
|
echo " --send Send a message. [DEFAULT]"
|
||||||
|
echo " --help Show this help."
|
||||||
echo
|
echo
|
||||||
echo "OPTIONS"
|
echo "OPTIONS"
|
||||||
echo " --room=<room_id> Which room to send the message to."
|
echo " --room=<room_id> Which room to send the message to."
|
||||||
|
|
@ -32,13 +38,15 @@ help() {
|
||||||
echo " --audio Send the file as audio."
|
echo " --audio Send the file as audio."
|
||||||
echo " --video Send the file as video."
|
echo " --video Send the file as video."
|
||||||
echo
|
echo
|
||||||
|
echo "Actions marked with [*] are done interactively."
|
||||||
|
echo
|
||||||
echo "If <message> is \"-\", stdin is used."
|
echo "If <message> is \"-\", stdin is used."
|
||||||
echo "See https://matrix.org/docs/spec/client_server/latest.html#m-room-message-msgtypes for a list of valid HTML tags for use with --html."
|
echo "See https://matrix.org/docs/spec/client_server/latest.html#m-room-message-msgtypes for a list of valid HTML tags for use with --html."
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
_curl() {
|
_curl() {
|
||||||
curl -s --fail -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" "$@"
|
curl -s -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
|
|
@ -52,22 +60,23 @@ log() {
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
url="$1"
|
url="$1"
|
||||||
|
shift
|
||||||
log "GET $url"
|
log "GET $url"
|
||||||
response=`_curl "${MATRIX_HOMESERVER}${url}"`
|
response=`_curl "$@" "${MATRIX_HOMESERVER}${url}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
query() {
|
query() {
|
||||||
url="$1"
|
url="$1"
|
||||||
data="$2"
|
data="$2"
|
||||||
type="$3"
|
type="$3"
|
||||||
log "POST $url"
|
log "$type $url"
|
||||||
response=$( _curl -X$type -H "Content-Type: application/json" --data "$data" "${MATRIX_HOMESERVER}${url}" )
|
response=$( _curl -X$type -H "Content-Type: application/json" --data "$data" "${MATRIX_HOMESERVER}${url}" )
|
||||||
if [ ! `jq -r .errcode <<<"$response"` = "null" ]; then
|
if [ ! `jq -r .errcode <<<"$response"` = "null" ]; then
|
||||||
echo
|
echo
|
||||||
>&2 echo "An error occurred. The matrix server responded with:"
|
>&2 echo "An error occurred. The matrix server responded with:"
|
||||||
>&2 echo "`jq -r .errcode <<<"$response"` `jq -r .error <<<"$response"`"
|
>&2 echo "`jq -r .errcode <<<"$response"`: `jq -r .error <<<"$response"`"
|
||||||
>&2 echo "Following request was sent to ${url}:"
|
#>&2 echo "Following request was sent to ${url}:"
|
||||||
>&2 jq . <<<"$data"
|
#>&2 jq . <<<"$data"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
@ -104,8 +113,8 @@ login() {
|
||||||
identifier="`whoami`@`hostname` using matrix.sh"
|
identifier="`whoami`@`hostname` using matrix.sh"
|
||||||
identifier=`escape "$identifier"`
|
identifier=`escape "$identifier"`
|
||||||
log "Trying homeserver: $MATRIX_HOMESERVER"
|
log "Trying homeserver: $MATRIX_HOMESERVER"
|
||||||
if ! get "/_matrix/client/versions"; then
|
if ! get "/_matrix/client/versions" --fail ; then
|
||||||
if ! get "/.well-known/matrix/server"; then
|
if ! get "/.well-known/matrix/server" --fail ; then
|
||||||
die "$MATRIX_HOMESERVER does not appear to be a matrix homeserver. Trying /.well-known/matrix/server failed. Please ask your homeserver's administrator for the correct address of the homeserver."
|
die "$MATRIX_HOMESERVER does not appear to be a matrix homeserver. Trying /.well-known/matrix/server failed. Please ask your homeserver's administrator for the correct address of the homeserver."
|
||||||
fi
|
fi
|
||||||
MATRIX_HOMESERVER=`jq -r '.["m.server"]' <<<"$response"`
|
MATRIX_HOMESERVER=`jq -r '.["m.server"]' <<<"$response"`
|
||||||
|
|
@ -128,7 +137,7 @@ login() {
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Success. Access token saved to ~/.matrix.sh."
|
echo "Success. Access token saved to ~/.matrix.sh."
|
||||||
echo "You should now use $0 -s to select a default room."
|
echo "You should now use $0 --select-default-room to select a default room."
|
||||||
}
|
}
|
||||||
|
|
||||||
list_rooms() {
|
list_rooms() {
|
||||||
|
|
@ -156,6 +165,26 @@ select_room() {
|
||||||
echo "Saved default room to ~/.matrix.sh"
|
echo "Saved default room to ~/.matrix.sh"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
join_room() {
|
||||||
|
read -p "Enter the ID or address of the room you want me to join: " room
|
||||||
|
post "/_matrix/client/r0/rooms/$room/join"
|
||||||
|
echo "Success."
|
||||||
|
}
|
||||||
|
|
||||||
|
leave_room() {
|
||||||
|
list_rooms
|
||||||
|
read -p "Enter the ID of the room you want me to leave: " room
|
||||||
|
[ "$room" = "$MATRIX_ROOM_ID" ] && die "It appears you are trying to leave the room that is currently set as default room. I'm sorry Dave, but I can't allow you to do that."
|
||||||
|
post "/_matrix/client/r0/rooms/$room/leave"
|
||||||
|
echo "Success."
|
||||||
|
}
|
||||||
|
|
||||||
|
invite_user() {
|
||||||
|
read -p "Enter the user ID you want to invite: " user
|
||||||
|
post "/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/invite" "{\"user_id\":\"$user\"}"
|
||||||
|
echo "Success."
|
||||||
|
}
|
||||||
|
|
||||||
_send_message() {
|
_send_message() {
|
||||||
data="$1"
|
data="$1"
|
||||||
txn=`date +%s%N`
|
txn=`date +%s%N`
|
||||||
|
|
@ -250,23 +279,35 @@ for i in "$@"; do
|
||||||
;;
|
;;
|
||||||
|
|
||||||
# Actions
|
# Actions
|
||||||
login)
|
--login)
|
||||||
ACTION="login"
|
ACTION="login"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
list-rooms)
|
--list-rooms)
|
||||||
ACTION="list_rooms"
|
ACTION="list_rooms"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
select-default-room)
|
--select-default-room)
|
||||||
ACTION="select_room"
|
ACTION="select_room"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
send-message|send)
|
--join-room)
|
||||||
|
ACTION="join_room"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--leave-room)
|
||||||
|
ACTION="leave_room"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--invite-user)
|
||||||
|
ACTION="invite_user"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--send-message|send)
|
||||||
ACTION="send"
|
ACTION="send"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
help|--help|-h)
|
--help|-h)
|
||||||
ACTION="help"
|
ACTION="help"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
|
@ -287,7 +328,7 @@ if [ "$ACTION" = "" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -z $MATRIX_HOMESERVER ] && die "No homeserver set. Use -l <homeserver> to log into an account on the given server and persist those settings."
|
[ -z $MATRIX_HOMESERVER ] && die "No homeserver set. Use '$0 --login' to log into an account on the given server and persist those settings."
|
||||||
|
|
||||||
if [ "$ACTION" = "login" ]; then
|
if [ "$ACTION" = "login" ]; then
|
||||||
login
|
login
|
||||||
|
|
@ -297,7 +338,7 @@ elif [ "$ACTION" = "help" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -z $MATRIX_TOKEN ] && die "No matrix token set. Use -l to login."
|
[ -z $MATRIX_TOKEN ] && die "No matrix token set. Use '$0 --login' to login."
|
||||||
|
|
||||||
AUTHORIZATION="Authorization: Bearer $MATRIX_TOKEN"
|
AUTHORIZATION="Authorization: Bearer $MATRIX_TOKEN"
|
||||||
|
|
||||||
|
|
@ -305,6 +346,12 @@ if [ "$ACTION" = "select_room" ]; then
|
||||||
select_room
|
select_room
|
||||||
elif [ "$ACTION" = "list_rooms" ]; then
|
elif [ "$ACTION" = "list_rooms" ]; then
|
||||||
list_rooms
|
list_rooms
|
||||||
|
elif [ "$ACTION" = "join_room" ]; then
|
||||||
|
join_room
|
||||||
|
elif [ "$ACTION" = "leave_room" ]; then
|
||||||
|
leave_room
|
||||||
|
elif [ "$ACTION" = "invite_user" ]; then
|
||||||
|
invite_user
|
||||||
elif [ "$ACTION" = "send" ]; then
|
elif [ "$ACTION" = "send" ]; then
|
||||||
if [ "$FILE" = "" ]; then
|
if [ "$FILE" = "" ]; then
|
||||||
[ -z "$TEXT" ] && die "No message to send given."
|
[ -z "$TEXT" ] && die "No message to send given."
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue