This repository has been archived on 2023-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
bog-bbot-archive/src/shell/functions.sh

179 lines
6.6 KiB
Bash
Executable File

#!/bin/bash -e
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
[[ -n $MY_PATH/.env ]] && source $MY_PATH/.env || (echo "Please create $MY_PATH/.env as $MY_PATH/.env.example" && exit 1)
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@
}
chanDB() {
if [[ "$1" =~ '@' ]]; then
chan=$(echo "$1" | awk -F '@' '{ print $1 }')
tool=$(echo "$1" | awk -F '@' '{ print $2 }')
else
chan="$1"
fi
[[ ! $chan ]] && echo "Please select user." && return 2
if [[ "$chan" =~ '#' ]]; then
homedir="$HOME/.bog/ids/$tool/$chan"
else
homedir="$HOME/.bog/ids/$tool/@$chan"
fi
DEBUG echo "_${FUNCNAME[0]}_: $chan"
DEBUG echo "_${FUNCNAME[0]}_: $tool"
DEBUG echo "_${FUNCNAME[0]}_: $homedir"
[[ -f $homedir/db ]] && source $homedir/db
}
# Send a message
sendmsg() {
chanDB "$1"
[[ ! "${chan::1}" =~ ^(#|@)$ ]] && chan="@$chan"
local msg="${@:2}"
[[ ! $chan || ! $msg ]] && echo "Please select user and message." && return 2
DEBUG echo "_${FUNCNAME[0]}_: $chan"
DEBUG echo "_${FUNCNAME[0]}_: $tool"
if [[ $tool == "axiomchat" ]]; then
curl -s -H "X-Auth-Token: $AC_API_TOKEN" \
-H "X-User-Id: $AC_API_ID" \
-H "Content-type:application/json" \
$AC_API_URL/api/v1/chat.postMessage \
-d "{ \"channel\": \"$chan\", \"text\": \"$msg\" }" > /dev/null 2>&1
else
echo -e "$msg"
fi
}
getPubkey() {
url="$node/wot/members"
# [[ -z $username || $username == "bot" ]] && username=$(echo $1 | tr -d '@')
# old way: local pubkey=$(curl -m 5 -s $url | grep -wiB 1 $username | grep "pubkey" | awk -F '"' '{ print $4 }')
[[ ! $usernameK ]] && usernameK="$1"
[[ ${usernameK::1} == '@' ]] && usernameK=${usernameK#?}
if [[ "$usernameK" =~ '@' ]]; then
chanDB "$usernameK"
else
local pubkey=$(curl -m 10 -s $url | jq -r --arg username "$usernameK" '.results[] | select(.uid==$username) | .pubkey')
fi
# DEBUG echo "_${FUNCNAME[0]}_: $pubkey"
# DEBUG echo "_${FUNCNAME[0]}_: $usernameK"
[[ $pubkey ]] && echo "$pubkey" || echo "none"
exit 0
}
getPseudo() {
url="$node/wot/members"
args=$(echo $@ | tr ' ' '\n' | grep -vEw 'biz|bog' | tr '\n' ' ')
[ $(echo $args | wc -w) -ge 2 ] && pubkey=$(echo $args | awk '{ print $2 }') || pubkey=$1
# pubkey="$1"
pubkey=$(echo $pubkey | tr -d '@')
local pseudo=$(curl -m 10 -s $url | jq -r --arg pubkey "$pubkey" '.results[] | select(.pubkey==$pubkey) | .uid')
[[ $pseudo ]] && echo "Le pseudo correspondant à la clé publique $pubkey est: @$pseudo" || echo "La clé publique $pubkey n'a pas de pseudo associé."
}
getPassword() {
chanDB "$1"
if [[ -d $homedir ]]; then
sendmsg $chan "Voici les identifiants de connexion de votre ḠCompte: \n \
Login: *$salt*\nMot de passe: *$pepper*\nClé publique: *$pubkey* \n \
Calcul du solde de votre compte en cours ..."
else
echo "Vous n'avez pas de compte chez nous."
fi
}
info_account() {
chanDB "$1"
if [[ -d $homedir ]]; then
sendmsg $chan "Récupération de votre solde en cours sur *$pubkey* ..."
local solde=$($silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2-)
echo "Votre solde est de *$solde*"
else
echo "Vous n'avez pas de compte chez nous."
fi
}
init_account() {
chanDB "$1"
if [[ -f $homedir/secret.dunikey ]]; then
pubkey=$(cat $homedir/secret.dunikey| grep "pub: " | awk '{ print $2 }')
echo "Vous disposez déjà d'un portefeuille Ḡ1: *$pubkey*"
return 2
else
sendmsg $dest "Création de votre portefeuille..."
fi
# LOGIN (=SALT)
local salt="$($MY_PATH/../dunikey/diceware.sh 6)"
# PASS (=PEPPER)
local pepper="$($MY_PATH/../dunikey/diceware.sh 4)"
# The pubkey for this salt/papper peer
local pubkey=$(python3 $MY_PATH/../dunikey/key_create_dunikey.py "$salt" "$pepper")
[[ ! -f /tmp/secret.dunikey ]] && echo "Arf, problem happens making your secret.dunikey" && return 2
[[ ! -d $homedir ]] && mkdir -p $homedir
[[ -f /tmp/secret.dunikey ]] && rm -f $homedir/secret.dunikey && mv /tmp/secret.dunikey $homedir/secret.dunikey
echo -e "salt=\"$salt\"\npepper=\"$pepper\"\npubkey=\"$pubkey\"" > $homedir/db
# info_account $chan
echo -e "Votre ḠCompte a bien été créé.\nPubkey: *$pubkey*"
}
rm_account() {
chanDB "$1"
if [[ -d $homedir ]]; then
local solde=$($silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2- | awk '{ print $1 }')
[[ "$solde" != "0.0" ]] && echo "Votre solde n'est pas nulle: $solde Ḡ1. Veuillez vider votre compte avant de pouvoir le supprimer." && return 2
echo "Suppression des données de votre portefeuille de votre ḠBanque."
echo -e "Login: *$salt*\nMot de passe: *$pepper*\nClé publique: *$pubkey*"
rm -r $homedir
echo "Suppression terminé, nous ne disposons plus d'aucune donnée vous concernant."
else
echo "Vous n'avez pas de compte chez nous."
fi
}
tx() {
local dest="$2"
local dest1="$dest"
local amount=$(echo "$3" | tr ',' '.')
local comment="${@:4}"
! [[ $dest && $amount ]] && echo "Des arguments sont manquants. Ex: pay Do99s6wQR2JLfhirPdpAERSjNbmjjECzGxHNJMiNKT3p 10 Merci pour la tarte" && return 2
#This shoudl works, but accents are ok and should not... [[ ! "$comment" =~ [^a-zA-Z0-9\ ] ]] && echo OK
[[ "$comment" =~ [^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:'('')'.+\ ] ]] && echo "Votre commentaire contient des charactères non autorisés." && return 2
[[ -z "$comment" ]] && local comment="Transaction effectuee avec BoG."
[[ $amount =~ '-' ]] && echo "La somme de votre transaction ne peut être négative: *$amount* Ḡ1" && return 2
[[ $(echo $dest | wc -c) -le 43 ]] && local dest=$(getPubkey $dest)
[[ $dest == "none" ]] && echo "Ce pseudo n'a pas de clé Ḡ1 associé." && return 2
toolDest=$(echo $dest1 | awk -F '@' '{ print $2 }')
chanDB "$1"
sender="$chan@$tool"
local result=$($silkaj --auth-file --file $homedir/secret.dunikey tx --amount $amount --output $dest --comment "$comment" -y)
resultCode=$?
[[ -n $result ]] && echo "\`\`\`$result\`\`\`" || (echo "Une erreur est survenu, votre transaction n'a pas été effecutée." && return 2)
[[ ${dest1#?} =~ '@' && $result && $resultCode == '0' && $toolDest != "telegram" ]] && sendmsg $dest1 "Vous avez reçus un paiement de *$amount Ḡ1* de la part de *$sender*\nCommentaire: *$comment*" || true
}
history_account() {
chanDB "$1"
sendmsg $chan "Récupération de l'historique de votre compte *$pubkey* ..."
local result=$($silkaj history $pubkey)
echo "\`\`\`$result\`\`\`"
}
#[[ $(type $1 | grep "est une fonction") ]] && $@ || echo "Veuillez préciser votre commande"