You can now define token and homeserver via command line options. This removes the need for a config file.

This commit is contained in:
Fabian Schlenz 2019-05-29 12:41:55 +02:00
parent 49a75f5e08
commit a204e72896
2 changed files with 20 additions and 1 deletions

View file

@ -66,6 +66,13 @@ Enter the room_id (the thing at the beginning of the line):
Saved default room to ~/.matrix.sh Saved default room to ~/.matrix.sh
``` ```
### Without logging in
You can also use this script without logging in first. If you have an access
token, you can use it like this:
```
$ ./matrix.sh --homeserver=https://matrix.org --token=abcdefg --room=\!2o587thjlgjHUIUHni:matrix.org ...
```
### Sending messages ### Sending messages
#### Sending a normal text message: #### Sending a normal text message:
``` ```

View file

@ -30,6 +30,8 @@ help() {
echo " --help Show this help." echo " --help Show this help."
echo echo
echo "OPTIONS" echo "OPTIONS"
echo " --token=<token> Access token to use. Only useful if you don't want to use --login."
echo " --homeserver=<url> Homeserver address to use. Only useful if you don't want to use --login. Must start with \"https\". Must not have a trailing slash."
echo " --room=<room_id> Which room to send the message to." echo " --room=<room_id> Which room to send the message to."
echo " --html Enable HTML tags in message." echo " --html Enable HTML tags in message."
echo " --pre Wraps the given message into <pre> and escapes all other HTML special chars." echo " --pre Wraps the given message into <pre> and escapes all other HTML special chars."
@ -110,6 +112,7 @@ hash curl >/dev/null 2>&1 || die "curl is required, but not installed."
login() { login() {
read -p "Address of the homeserver the account lives on: " MATRIX_HOMESERVER read -p "Address of the homeserver the account lives on: " MATRIX_HOMESERVER
MATRIX_HOMESERVER="https://${MATRIX_HOMESERVER#https://}" MATRIX_HOMESERVER="https://${MATRIX_HOMESERVER#https://}"
MATRIX_HOMESERVER="${MATRIX_HOMESERVER%/}" # Strip trailing slash
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"
@ -130,7 +133,7 @@ login() {
echo echo
post "/_matrix/client/r0/login" "{\"type\":\"m.login.password\", \"identifier\":{\"type\":\"m.id.user\",\"user\":\"${username}\"},\"password\":\"${password}\",\"initial_device_display_name\":$identifier}" post "/_matrix/client/r0/login" "{\"type\":\"m.login.password\", \"identifier\":{\"type\":\"m.id.user\",\"user\":\"${username}\"},\"password\":\"${password}\",\"initial_device_display_name\":$identifier}"
data="MATRIX_TOKEN=\"`jq -r .access_token <<<"$response"`\"\nMATRIX_HOMESERVER=\"$MATRIX_HOMESERVER\"\nMATRIX_USER=\"`jq -r .user_id <<<"$response"`\"\n" data="MATRIX_TOKEN=\"`jq -r .access_token <<<"$response"`\"\nMATRIX_HOMESERVER=\"${MATRIX_HOMESERVER%/}\"\nMATRIX_USER=\"`jq -r .user_id <<<"$response"`\"\n"
echo -e "$data" > ~/.matrix.sh echo -e "$data" > ~/.matrix.sh
chmod 600 ~/.matrix.sh chmod 600 ~/.matrix.sh
source ~/.matrix.sh source ~/.matrix.sh
@ -244,14 +247,23 @@ HTML="false"
PRE="false" PRE="false"
FILE="" FILE=""
FILE_TYPE="m.file" FILE_TYPE="m.file"
MESSAGE_TYPE="m.text"
for i in "$@"; do for i in "$@"; do
case $i in case $i in
# Options # Options
--token=*)
MATRIX_TOKEN="${i#*=}"
shift
;;
--room=*) --room=*)
MATRIX_ROOM_ID="${i#*=}" MATRIX_ROOM_ID="${i#*=}"
shift shift
;; ;;
--homeserver=*)
MATRIX_HOMESERVER="${i#*=}"
shift
;;
--html) --html)
HTML="true" HTML="true"
shift shift