jaklis/deletemsg.sh

71 lines
2.0 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# ###
# Supprimer un message Cesium+
# ###
[[ ! -f .env ]] && cp .env.template .env
source .env
REGEX_PUBKEYS="[a-zA-Z0-9]{42,44}"
2020-11-10 11:38:08 +01:00
# Help display
helpOpt() {
echo -e "Cesium+ messages deleting
Default: ID in interactive mode.
Advice: Fill your .env file for more fun.
Example: $0 <ID du message>
\rOptions:
-id,--id <ID du message>\tDelete the message with ID <id>.
-k,--key <key>\t\tPath <key> to the pubsec keychain file of the issuer.
-o,--outbox\t\t\tDelete outbox messages instead of inbox
-h,--help\t\t\tDisplay this help"
}
# Parse options
declare -a args=($@)
for ((i=0; i<${#args[*]}; ++i))
do
case ${args[$i]} in
-o|--outbox) type=outbox;;
-id|--id) id="${args[$i+1]}"
[[ -z $id ]] && echo "Veuillez préciser un ID de message." && exit 1;;
2020-11-22 03:31:15 +01:00
-k|--key) DUNIKEY="${args[$i+1]}"
[[ -z $DUNIKEY ]] && echo "Veuillez préciser un fichier de trousseau." && exit 1;;
2020-11-10 11:38:08 +01:00
-h|--help) helpOpt && exit 0;;
2020-11-05 04:32:22 +01:00
*) [[ "${args[$i]}" == "-"* ]] && echo "Option inconnue." && exit 1;;
esac
done
2020-11-22 03:31:15 +01:00
if [[ -z $DUNIKEY ]]; then
read -p "Fichier de trousseau: " DUNIKEY
fi
2020-11-22 03:31:15 +01:00
issuer=$(./natools.py pk -f pubsec -k $DUNIKEY)
if [[ -z $type ]]; then
type="inbox"
fi
2020-11-10 11:38:08 +01:00
[[ -z $id ]] && id=$1
if [[ -z $id ]]; then
read -p "ID de message: " ID
fi
[[ -z $(grep -Eo $REGEX_PUBKEYS <<<$issuer) ]] && echo "Le format de la clé publique de l'émetteur est invalide." && exit 1
times=$(date -u +'%s')
# Fabrication du hash
2020-11-05 13:46:07 +01:00
hashBrut="{\"version\":2,\"index\":\"message\",\"type\":\"$type\",\"id\":\"$id\",\"issuer\":\"$issuer\",\"time\":$times}"
hash=$(echo -n "$hashBrut" | sha256sum | cut -d ' ' -f1 | awk '{ print toupper($0) }')
# Fabrication de la signature
2020-11-22 03:31:15 +01:00
signature=$(echo -n "$hash" | ./natools.py sign -f pubsec -k $DUNIKEY --noinc -O 64)
2020-11-05 13:46:07 +01:00
document="{\"hash\":\"$hash\",\"signature\":\"$signature\",${hashBrut:1}"
jq . <<<$document
# Envoi du document
2020-11-22 03:31:15 +01:00
curl -s -X POST "$POD/history/delete" -d "$document"
2020-11-05 13:46:07 +01:00
echo