#!/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 }' | awk -F '%' '{ print $1 }') idRoom=$(echo "$1" | awk -F '%' '{ print $2 }') else # chan=$(echo "$1" | awk -F '%' '{ print $1 }') 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]}_: args: $1" DEBUG echo "_${FUNCNAME[0]}_: Chan: $chan" DEBUG echo "_${FUNCNAME[0]}_: Tool: $tool" DEBUG echo "_${FUNCNAME[0]}_: Homedir: $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 elif [[ $tool == "telegram" ]]; then msg=$(echo "$msg" | sed "s/\\\n/%0a/g") curl -s "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage?chat_id=$idRoom&parse_mode=Markdown&text=$msg" > /dev/null 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* ..." soldeAccount=$($silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2-) echo "Votre solde est de *$soldeAccount*" 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 "idRoom=\"$idRoom\"\nsalt=\"$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\`\`\`" } link_account() { chanDB "$1" if ! [[ -f "$homedir"/_waitingLink ]]; then masterChan="$chan@$tool" chanDB "$2" [[ ! -d "$homedir" ]] && echo "Le compte $chan@$tool n'existe pas chez nous. $homedir" && return 2 local codeLink=$(i=0; while [[ $i -le 5 ]]; do echo $((0 + RANDOM % 9)); ((i++)); done | tr -d '\n') echo -e "masterAccount=$1\ncodeLink=$codeLink" > $homedir/_waitingLink sendmsg $2 "Une demande de fusion de compte a été émise de la part de $masterChan.\nSi vous êtes à l'origine de cette demande, veuillez répondre:\n\n*gbanque link $codeLink*\n\nà ce message.\nSinon, vous pouvez ignorer ce message." else source $homedir/_waitingLink if [[ "$codeLink" == "$2" ]]; then info_account $1 if [[ $soldeAccount == "0.0 Ğ1" ]]; then rm -r "$homedir" local slaveHomeDir="$homedir" local slaveChan="$chan@$tool" chanDB "$masterAccount" ln -sf "$homedir" "$slaveHomeDir" echo "$slaveChan" >> "$homedir/slaves" sendmsg "$chan@$tool" "Votre compte est désormais fusionné avec le compte $slaveChan" chanDB "$slaveChan" sendmsg "$chan@$tool" "Votre compte est désormais fusionné avec $chan" fi fi fi } #[[ $(type $1 | grep "est une fonction") ]] && $@ || echo "Veuillez préciser votre commande"