diff --git a/README.md b/README.md index 398e2bc..8189f11 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ wrapped in <pre> tags. ## Usage ### Logging in -Use `-l `. The script will try to resolve delegation via the +Use `login`. The script will try to resolve delegation via the `/.well-known/matrix/server` path. If that doesn't work, you'll get an error message. @@ -33,7 +33,8 @@ file allow accessing your homeserver, you should keep it's contents secret. Therefore, it will be created with access mode 600. ``` -$ ./matrix.sh -l matrix.org +$ ./matrix.sh login +Address of the homeserver the account lives on: matrix.org Username on the server (just the local part, so e.g. 'bob'): bob bob's password: @@ -49,7 +50,7 @@ It will show all joined rooms as well as rooms you are invited to. Selecting one of the latter will also accept the invitation and join that room. ``` -$ ./matrix.sh -s +$ ./matrix.sh select-default-room Getting Rooms... Joined rooms: !GCHxYlasvdh778dsOx:matrix.org - Me and my server @@ -68,37 +69,40 @@ Saved default room to ~/.matrix.sh ### Sending messages #### Sending a normal text message: ``` -$ ./matrix.sh "Hello World" +$ ./matrix.sh send "Hello World" ``` #### Sending a text message with markup: ``` -$ ./matrix.sh -H "This is very important." +$ ./matrix.sh send --html "This is very important." ``` #### Piping command output: ``` -$ echo "Hello" | ./matrix.sh +$ echo "Hello" | ./matrix.sh send ``` #### Code formatting: -You can use `-P` 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. ``` -$ ls -l | ./matrix.sh -P +$ ls -l | ./matrix.sh send --pre ``` #### Sending files: ``` -$ ./matrix.sh -f upload.zip +$ ./matrix.sh send --file=upload.zip +``` +Additionally use `--audio`, `-image` or `--video` to send files as audio, images or +video, respectively: +``` +$ ./matrix.sh send --file=IMG1234.jpg --image ``` -Use `-a`, `-i`, `-v` instead of `-f` to send files as audio, images or -video, respectively. #### Providing a room: -You can use `-r` to provide a room_id. This supersedes the default room. +You can use `--room=` to provide a room_id. This supersedes the default room. ``` -$ ./matrix.sh -r '!OEassajhhkasLULVAa:matrix.org' "Hello World" +$ ./matrix.sh send --room_id='!OEassajhhkasLULVAa:matrix.org' "Hello World" ``` (Note: bash doesn't like exclamation marks in double quoted strings. So we use single quotes for the room id.) diff --git a/matrix.sh b/matrix.sh index d60f19b..6677a5b 100755 --- a/matrix.sh +++ b/matrix.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash #set -x shopt -s extglob -VERSION="0.3" -LOG="true" +VERSION="1.0" +LOG="false" AUTHORIZATION="X-Dummy: 1" @@ -15,31 +15,30 @@ help() { version echo echo "Usage:" - echo "$0 " + echo "$0 [] []" + echo echo "ACTIONS" - echo " -l Login to a server." - echo " -L List rooms the matrix user joined or is invited to." - echo " -s Select a default room." - echo " -h This help." + echo " login Login to a server." + echo " list-rooms List rooms the matrix user joined or is invited to." + echo " select-default-room Select a default room." + echo " help Show this help." echo echo "OPTIONS" - echo " -r Which room to send the message to." - echo " -H Enable HTML tags in message." - echo " -P Wraps the given message into
 and escapes all other HTML special chars."
-	echo
-	echo "FILES (message will be ignored)"
-	echo "  -f     Send ."
-	echo "  -a     Send  as audio."
-	echo "  -i     Send  as image."
-	echo "  -v     Send  as video."
+	echo "  --room=     Which room to send the message to."
+	echo "  --html               Enable HTML tags in message."
+	echo "  --pre                Wraps the given message into 
 and escapes all other HTML special chars."
+	echo "  --file=        Send  to the room."
+	echo "  --image              Send the file as image."
+	echo "  --audio              Send the file as audio."
+	echo "  --video              Send the file as video."
 	echo
 	echo "If  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 -H."
+	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
 }
 
 _curl() {
-	curl -s --fail -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" $*
+	curl -s --fail -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" "$@"
 }
 
 die() {
@@ -63,7 +62,7 @@ query() {
 	type="$3"
 	log "POST $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
 		>&2 echo "An error occurred. The matrix server responded with:"
 		>&2 echo "`jq -r .errcode <<<"$response"` `jq -r .error <<<"$response"`"
@@ -100,6 +99,7 @@ hash curl >/dev/null 2>&1 || die "curl is required, but not installed."
 
 ############## Logic
 login() {
+	read -p "Address of the homeserver the account lives on: " MATRIX_HOMESERVER
 	MATRIX_HOMESERVER="https://${MATRIX_HOMESERVER#https://}"
 	identifier="`whoami`@`hostname` using matrix.sh"
 	identifier=`escape "$identifier"`
@@ -165,7 +165,7 @@ _send_message() {
 send_message() {
 	# Get the text. Try the last variable
 	text="$1"
-	[ "$text" = "-" ] || [ "$text" = "" ] && text=$( to log into an account on the given server and persist those settings."
 
@@ -287,10 +305,13 @@ if [ "$ACTION" = "select_room" ]; then
 	select_room
 elif [ "$ACTION" = "list_rooms" ]; then
 	list_rooms
-elif [ "$ACTION" = "send_file" ]; then
-	send_file
-elif [ "$ACTION" = "send_message" ]; then
-	send_message "$1"
+elif [ "$ACTION" = "send" ]; then
+	if [ "$FILE" = "" ]; then
+		[ -z "$TEXT" ] && die "No message to send given."
+		send_message "$TEXT"
+	else
+		send_file
+	fi
 fi