Changed the command line syntax. This changes the API. But luckily this is a really young project without many users. ;-)

This commit is contained in:
Fabian Schlenz 2019-04-30 17:30:11 +02:00
parent f7d852bd6a
commit 7ebe2f6cfd
2 changed files with 105 additions and 80 deletions

View file

@ -23,7 +23,7 @@ wrapped in <pre> tags.
## Usage ## Usage
### Logging in ### Logging in
Use `-l <homeserver>`. 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 `/.well-known/matrix/server` path. If that doesn't work, you'll get an error
message. 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. 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 Username on the server (just the local part, so e.g. 'bob'): bob
bob's password: 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. one of the latter will also accept the invitation and join that room.
``` ```
$ ./matrix.sh -s $ ./matrix.sh select-default-room
Getting Rooms... Getting Rooms...
Joined rooms: Joined rooms:
!GCHxYlasvdh778dsOx:matrix.org - Me and my server !GCHxYlasvdh778dsOx:matrix.org - Me and my server
@ -68,37 +69,40 @@ Saved default room to ~/.matrix.sh
### Sending messages ### Sending messages
#### Sending a normal text message: #### Sending a normal text message:
``` ```
$ ./matrix.sh "Hello World" $ ./matrix.sh send "Hello World"
``` ```
#### Sending a text message with markup: #### Sending a text message with markup:
``` ```
$ ./matrix.sh -H "This is <strong>very important</strong>." $ ./matrix.sh send --html "This is <strong>very important</strong>."
``` ```
#### Piping command output: #### Piping command output:
``` ```
$ echo "Hello" | ./matrix.sh $ echo "Hello" | ./matrix.sh send
``` ```
#### Code formatting: #### 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. HTML tags.
``` ```
$ ls -l | ./matrix.sh -P $ ls -l | ./matrix.sh send --pre
``` ```
#### Sending files: #### 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: #### Providing a room:
You can use `-r` 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 -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 (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.)

153
matrix.sh
View file

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#set -x #set -x
shopt -s extglob shopt -s extglob
VERSION="0.3" VERSION="1.0"
LOG="true" LOG="false"
AUTHORIZATION="X-Dummy: 1" AUTHORIZATION="X-Dummy: 1"
@ -15,31 +15,30 @@ help() {
version version
echo echo
echo "Usage:" echo "Usage:"
echo "$0 <options> <message>" echo "$0 <action> [<options>] [<message>]"
echo
echo "ACTIONS" echo "ACTIONS"
echo " -l <server> Login to a server." echo " login Login to a server."
echo " -L List rooms the matrix user joined or is invited to." echo " list-rooms List rooms the matrix user joined or is invited to."
echo " -s Select a default room." echo " select-default-room Select a default room."
echo " -h This help." echo " help Show this help."
echo echo
echo "OPTIONS" echo "OPTIONS"
echo " -r <room_id> Which room to send the message to." echo " --room=<room_id> Which room to send the message to."
echo " -H Enable HTML tags in message." echo " --html Enable HTML tags in message."
echo " -P 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."
echo echo " --file=<file> Send <file> to the room."
echo "FILES (message will be ignored)" echo " --image Send the file as image."
echo " -f <file> Send <file>." echo " --audio Send the file as audio."
echo " -a <file> Send <file> as audio." echo " --video Send the file as video."
echo " -i <file> Send <file> as image."
echo " -v <file> Send <file> as video."
echo 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 -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 echo
} }
_curl() { _curl() {
curl -s --fail -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" $* curl -s --fail -H "$AUTHORIZATION" -H "User-Agent: matrix.sh/$VERSION" "$@"
} }
die() { die() {
@ -63,7 +62,7 @@ query() {
type="$3" type="$3"
log "POST $url" log "POST $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"`"
@ -100,6 +99,7 @@ hash curl >/dev/null 2>&1 || die "curl is required, but not installed."
############## Logic ############## Logic
login() { login() {
read -p "Address of the homeserver the account lives on: " MATRIX_HOMESERVER
MATRIX_HOMESERVER="https://${MATRIX_HOMESERVER#https://}" MATRIX_HOMESERVER="https://${MATRIX_HOMESERVER#https://}"
identifier="`whoami`@`hostname` using matrix.sh" identifier="`whoami`@`hostname` using matrix.sh"
identifier=`escape "$identifier"` identifier=`escape "$identifier"`
@ -165,7 +165,7 @@ _send_message() {
send_message() { send_message() {
# Get the text. Try the last variable # Get the text. Try the last variable
text="$1" text="$1"
[ "$text" = "-" ] || [ "$text" = "" ] && text=$(</dev/stdin) [ "$text" = "-" ] && text=$(</dev/stdin)
if $PRE; then if $PRE; then
text="${text//</&lt;}" text="${text//</&lt;}"
text="${text//>/&gt;}" text="${text//>/&gt;}"
@ -197,13 +197,12 @@ send_file() {
fi fi
filename=`basename "$FILE"` filename=`basename "$FILE"`
log "filename: $filename" log "filename: $filename"
filename=`jq -s -R . <<<"$filename"`
content_type=`file --brief --mime-type "$FILE"` content_type=`file --brief --mime-type "$FILE"`
log "content-type: $content_type" log "content-type: $content_type"
upload_file "$FILE" "$content_type" "$filename" upload_file "$FILE" "$content_type" "$filename"
uri=`jq -r .content_uri <<<"$response"` uri=`jq -r .content_uri <<<"$response"`
data="{\"body\":$filename, \"msgtype\":\"$FILETYPE\", \"filename\":$filename, \"url\":\"$uri\"}" data="{\"body\":`escape "$filename"`, \"msgtype\":\"$FILE_TYPE\", \"filename\":`escape "$filename"`, \"url\":\"$uri\"}"
_send_message "$data" _send_message "$data"
} }
@ -211,63 +210,82 @@ send_file() {
######## Program flow stuff ######## Program flow stuff
[ -r ~/.matrix.sh ] && source ~/.matrix.sh [ -r ~/.matrix.sh ] && source ~/.matrix.sh
ACTION="send_message" ACTION="send"
HTML="false" HTML="false"
PRE="false" PRE="false"
while getopts "l:shr:a:f:i:v:HPL" opt; do FILE=""
case $opt in FILE_TYPE="m.file"
l)
ACTION="login" for i in "$@"; do
MATRIX_HOMESERVER="$OPTARG" case $i in
# Options
--room=*)
MATRIX_ROOM_ID="${i#*=}"
shift
;; ;;
L) --html)
ACTION="list_rooms"
;;
s)
ACTION="select_room"
;;
h)
ACTION="help"
;;
H)
HTML="true" HTML="true"
shift
;; ;;
P) --pre)
PRE="true" PRE="true"
shift
;; ;;
r) --file=*)
MATRIX_ROOM_ID="$OPTARG" FILE="${i#*=}"
ACTION="send"
shift
;; ;;
f) --image)
ACTION="send_file" FILE_TYPE="m.image"
FILETYPE="m.file" shift
FILE="$OPTARG"
;; ;;
v) --audio)
ACTION="send_file" FILE_TYPE="m.audio"
FILETYPE="m.video" shift
FILE="$OPTARG"
;; ;;
i) --video)
ACTION="send_file" FILE_TYPE="m.video"
FILETYPE="m.image" shift
FILE="$OPTARG"
;; ;;
a)
ACTION="send_file" # Actions
FILETYPE="m.audio" login)
FILE="$OPTARG" ACTION="login"
shift
;; ;;
\?) list-rooms)
die "Invalid option -$OPTARG" ACTION="list_rooms"
shift
;; ;;
:) select-default-room)
die "Option -$OPTARG requires an argument" ACTION="select_room"
shift
;;
send-message|send)
ACTION="send"
shift
;;
help|--help|-h)
ACTION="help"
shift
;;
--*)
die "Unknown option $i"
;;
*)
TEXT="$i"
shift
;; ;;
esac esac
done done
shift $((OPTIND - 1)) if [ "$ACTION" = "" ]; then
help
exit 1
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 -l <homeserver> to log into an account on the given server and persist those settings."
@ -287,10 +305,13 @@ 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" = "send_file" ]; then elif [ "$ACTION" = "send" ]; then
send_file if [ "$FILE" = "" ]; then
elif [ "$ACTION" = "send_message" ]; then [ -z "$TEXT" ] && die "No message to send given."
send_message "$1" send_message "$TEXT"
else
send_file
fi
fi fi