aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxbian <got.dacs@slmail.me>2023-10-08 22:02:22 +0200
committerOxbian <got.dacs@slmail.me>2023-10-08 22:02:22 +0200
commit7145d1818350af8d793bea68a354707248666fa3 (patch)
tree7d823eb848f75a1cde1cbb792cfdb2c108093a06
parent217660d0cfdcbe3233019a80eb2dbecbd7a963dc (diff)
downloadmatrix-sender-7145d1818350af8d793bea68a354707248666fa3.tar.gz
matrix-sender-7145d1818350af8d793bea68a354707248666fa3.zip
Updating project to be POSIX sh and creating a man page
-rw-r--r--README.md41
-rwxr-xr-xmatrix-sender128
-rw-r--r--matrix-sender.147
-rwxr-xr-xmatrix.sh135
4 files changed, 198 insertions, 153 deletions
diff --git a/README.md b/README.md
index efa3dce..add85d0 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
# Matrix Sender
-------
+---
This script allows you to send simple unencrypted message & html to matrix.
It can be usefull for logging / cronjobs error or monitoring.
@@ -9,8 +9,13 @@ It can be usefull for logging / cronjobs error or monitoring.
This script **is dependent of jq & curl**.
To install the script, just clone this repo:
-```bash
-git clone https://github.com/oxbian/matrix-sender.git
+```sh
+git clone https://git.arka.rocks/Oxbian/matrix-sender
+```
+
+And if you want you can add it to the PATH or link it to the `/bin` folder.
+```sh
+sudo ln -s $(pwd)/matrix-sender /bin/matrix-sender
```
## Usage
@@ -18,37 +23,37 @@ git clone https://github.com/oxbian/matrix-sender.git
First you need to edit the script with your homeserver url & the roomID
After this you need to get your token
-```bash
-./matrix.sh -t <username> <password>
+```sh
+./matrix-sender -t <username> <password>
```
Once all is setup, you can send messages:
- Simple message
-```bash
-./matrix.sh -s <message>
+```sh
+./matrix-sender -s <message>
```
-- HTML formatted message
-```bash
-./matrix.sh -html <message>
+- or an HTML formatted message
+```sh
+./matrix-sender -html <message>
```
For help you can use:
-```bash
-./matrix.sh -h
+```sh
+./matrix-sender -h
```
-
-```bash
-./matrix.sh --help
+or
+```sh
+./matrix-sender --help
```
-**Exemple**
+**Example**
```bash
-./matrix.sh -s 'Hello world!'
+./matrix-sender -s 'Hello world!'
```
```bash
-./matrix.sh -html '<h1 class="test"> t e s t </h1>'
+./matrix-sender -html '<h1 class="test"> t e s t </h1>'
```
## Contributing
diff --git a/matrix-sender b/matrix-sender
new file mode 100755
index 0000000..dca13b1
--- /dev/null
+++ b/matrix-sender
@@ -0,0 +1,128 @@
+#!/bin/sh
+# Script to send message to a matrix server
+
+#set -x
+
+# Matrix homeserver URL
+MATRIX_HOMESERVER=''
+
+# MATRIX room ID
+MATRIX_ROOM_ID=''
+
+# MATRIX token
+MATRIX_TOKEN=''
+
+# Check if variable are set
+check_var() {
+ [ -z "$MATRIX_HOMESERVER" ] && error "MATRIX_HOMESERVER variable not set"
+ [ -z "$MATRIX_ROOM_ID" ] && error "MATRIX_ROOM_ID variable not set"
+}
+
+# Check if token is set
+check_token() {
+ [ -z "$MATRIX_TOKEN" ] && error "MATRIX_TOKEN variable empty, use $0 -t <username> <password>"
+}
+
+# Display an error and leave
+error() {
+ printf "\033[0;31m%s\033[0m\n" "$1"
+ exit 1
+}
+
+# Clean an input
+clean() {
+ input="$1"
+ printf '%s' "$input" | jq -Rs '.'
+}
+
+# Make a request, $1 the CURL request
+request() {
+ request=$1
+ errorcode="$(printf "%s" "$request" | jq -r '.errcode')"
+
+ [ -n "$errorcode" ] && [ "$errorcode" != "null" ] && error "[x] Erreur: \"$(printf "%s" "$request" | jq -r '.error' )\""
+}
+
+# Get the token of the account
+# usage: ./matrix.sh -t <username> <password>
+# get_token <username> <password>
+get_token() {
+ check_var
+ token=$(curl -XPOST -H "Content-Type: application/json" --data "{\"type\": \"m.login.password\", \"identifier\": {\"user\": \"$1\", \"type\": \"m.id.user\"}, \"password\": \"$2\"}" \
+ "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/login")
+ request "$token"
+
+ token=$(echo "$token" | jq -r '.access_token')
+ sed -i -e "s#^MATRIX_TOKEN=.*#MATRIX_TOKEN=\'${token}\'#g" "$0"
+ printf "\033[1;32m[V] Token succesfully added\033[0m\n"
+ exit 0
+}
+
+# Send a message
+# usage: ./matrix.sh -s <message>
+# usage: ./matrix.sh -html <message>
+# send_message <message>
+send_message() {
+ check_var
+ check_token
+ id=$(date +%s%N) # Message id
+ message=$(clean "$1")
+
+ if $HTML; then
+ data="{\"msgtype\": \"m.text\", \"body\": $message, \"format\": \"org.matrix.custom.html\", \"formatted_body\": $message}"
+ else
+ data="{\"body\": $message, \"msgtype\": \"m.text\"}"
+ fi
+ request "$(curl -XPUT -H "Authorization: Bearer ${MATRIX_TOKEN}" -H "Content-Type: application/json" --data "$data" \
+ "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/send/m.room.message/$id")"
+ printf "\033[1;32m[v] Message sent\033[0m\n"
+ exit 0
+}
+
+# Show the help menu
+help() {
+ printf "Usage: %s <actions> <options>\n\n" "$0"
+ printf "Actions:\n"
+ printf " -h, --help show this menu\n"
+ printf " -t <username> <password> get the token of the account and save it\n"
+ printf " -s <message> send a message\n"
+ printf " -html <html> send an html message\n"
+}
+
+# Main
+hash curl >/dev/null 2>&1 || error "Curl is required!"
+hash jq >/dev/null 2>&1 || error "jq is required!"
+
+action=""
+case $1 in
+-t)
+ get_token "$2" "$3"
+ exit 0
+ ;;
+
+-s)
+ action="send"
+ shift
+ ;;
+
+-html)
+ action="html"
+ shift
+ ;;
+
+-h|--help|*)
+ help
+ exit 0
+ ;;
+esac
+
+MESSAGE=$*
+case $action in
+send)
+ send_message "$MESSAGE"
+ ;;
+html)
+ HTML="true"
+ send_message "$MESSAGE"
+ ;;
+esac
diff --git a/matrix-sender.1 b/matrix-sender.1
new file mode 100644
index 0000000..9a0b5ab
--- /dev/null
+++ b/matrix-sender.1
@@ -0,0 +1,47 @@
+.\" Manpage for matrix-sender.
+.\" Contact oxbian.noch@simplelogin.com to correct errors or typos.
+.TH man 8 "08 October 2023" "1.0" "matrix-sender man page"
+
+.SH NAME
+.B matrix-sender
+- send an unencrypted message to a matrix room
+
+.SH SYNOPSIS
+.B matrix-sender [OPTIONS]
+
+.SH DESCRIPTION
+Matrix sender is a POSIX shell script for sending unencrypted message to a Matrix room. It supports HTML formatting, usefull for log on servers
+There will be no update for supporting encrypted message, the simplicity of this script will be degraded by that, if you really need check
+.B nio-send
+
+.SH OPTIONS
+.IP "-h|--help"
+Prints help
+
+.IP "-t <username> <password>"
+Get token account token, needed for sending messages
+
+.IP "-s <message>"
+Send a message to a matrix room
+
+.IP "-html <html formatted message>"
+Send a HTML formatted message to a matrix room
+
+.SH EXAMPLES
+.IP "matrix-sender -s 'Hello World!'
+Send "Hello world!" to a matrix room
+
+.IP "matrix-sender -html '<h1>Hello World!</h1>'"
+Send a bold "Hello World!" to a matrix room
+
+.SH EXIT STATUS
+.TP
+.B 0
+Success
+
+.TP
+.B 1
+Error
+
+.SH AUTHOR
+Oxbian (oxbian.noch@simplelogin.com)
diff --git a/matrix.sh b/matrix.sh
deleted file mode 100755
index 91a6904..0000000
--- a/matrix.sh
+++ /dev/null
@@ -1,135 +0,0 @@
-#!/bin/bash
-# Script to send message to matrix
-
-#set -x
-
-# Matrix homeserver URL
-MATRIX_HOMESERVER=''
-
-# MATRIX room ID
-MATRIX_ROOM_ID=''
-
-# MATRIX token
-MATRIX_TOKEN=''
-
-# Check if variable are set
-check_var() {
- if [ "$MATRIX_HOMESERVER" == '' ]; then
- error "MATRIX_HOMESERVER variable not set"
- fi
- if [ "$MATRIX_ROOM_ID" == '' ]; then
- error "MATRIX_ROOM_ID variable not set"
- fi
-}
-
-# Check if token is set
-check_token() {
- if [ "$MATRIX_TOKEN" == '' ]; then
- error "MATRIX_TOKEN variable empty, use $0 -t <username> <password>"
- fi
-}
-
-# Display an error and leave
-error() {
- echo -e "\033[0;31m$@\033[0m"
- exit 1
-}
-
-# Clean an input
-clean() {
- local input=$@
- printf '%s' "$input" | jq -Rs '.'
-}
-
-# Make a request, $1 the CURL request
-request() {
- local request=$@
- local errorcode="$(jq -r '.errcode' <<< "$request")"
-
- if [[ "$errorcode" != "" && "$errorcode" != "null" ]]; then
- error "[x] Erreur: "$(jq -r '.error' <<< "$request")""
- fi
-}
-
-# Get the token of the account
-# usage: ./matrix.sh -t <username> <password>
-# get_token <username> <password>
-get_token() {
- check_var
- local token=$(curl -XPOST -H "Content-Type: application/json" --data "{\"type\": \"m.login.password\", \"identifier\": {\"user\": \"$1\", \"type\": \"m.id.user\"}, \"password\": \"$2\"}" \
- "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/login")
- request $token
-
- token=$(echo "$token" | jq -r '.access_token')
- sed -i -e "s#^MATRIX_TOKEN=.*#MATRIX_TOKEN=\'${token}\'#g" $0
- echo -e "\033[1;32m[V] Token succesfully added\033[0m"
-}
-
-# Send a message
-# usage: ./matrix.sh -s <message>
-# usage: ./matrix.sh -html <message>
-# send_message <message>
-send_message() {
- check_var
- check_token
- local id=$(date +%s%N) # Message id
- local message=$(clean "$@")
-
- if $HTML; then
- local data="{\"msgtype\": \"m.text\", \"body\": $message, \"format\": \"org.matrix.custom.html\", \"formatted_body\": $message}"
- else
- local data="{\"body\": $message, \"msgtype\": \"m.text\"}"
- fi
- request $(curl -XPUT -H "Authorization: Bearer ${MATRIX_TOKEN}" -H "Content-Type: application/json" --data "$data" \
- "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/send/m.room.message/$id")
- echo -e "\033[1;32m[v] Message sent\033[0m"
-}
-
-# Show the help menu
-help() {
- echo "Usage: $0 <actions> <options>"
- echo ""
- echo "Actions:"
- echo " -h, --help show this menu"
- echo " -t <username> <password> get the token of the account and save it"
- echo " -s <message> send a message"
- echo " -html <html> send an html message"
-}
-
-# Main
-hash curl >/dev/null 2>&1 || error "Curl is required!"
-hash jq >/dev/null 2>&1 || error "jq is required!"
-
-action=""
-case $1 in
--t)
- get_token $2 $3
- exit 0
- ;;
-
--s)
- action="send"
- shift
- ;;
-
--html)
- action="html"
- shift
- ;;
-
--h|--help|*)
- help
- exit 0
- ;;
-esac
-
-MESSAGE=$@
-case $action in
-send)
- send_message "$MESSAGE"
- ;;
-html)
- HTML="true"
- send_message "$MESSAGE"
- ;;
-esac
ArKa projects. All rights to me, and your next child right arm.