You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
6.5 KiB

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.1
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
# RECORD RECURRENT PAYEMENTS WITH "Cesium+ ID" Pubkey TODO cron ./wallets/$PHONE/VIREMENTS, Fusion avec sms_VIR.sh
################################################################################
source ./shell/init.sh
source ./shell/functions.sh
log "__SUB:sms_REC.sh: START ($1=PHONE, $2=VIRDEST, $3=AMOUNT, $4=PERIOD, $5=TIME)"
PHONE="$1"
VIRDEST="$2"
AMOUNT=$3
PERIOD="$4"
TIME=$5
case "$PERIOD" in
J|D)
PERIOD="jour";
NEXTDATE="days";
;;
S|W)
PERIOD="semaine";
NEXTDATE="weeks";
;;
M)
PERIOD="mois";
NEXTDATE="months";
;;
A|Y)
PERIOD="année";
NEXTDATE="years";
;;
*)
sms_ERROR "$PHONE" "Erreur sur la période de votre virement! Jour, Semaine, Mois, Année? Valeur: J,S,M,A (ex: REC 10 Fred J 3)"
exit;
;;
esac
###########################################################
# Initialise PHONE, PIN, PUBKEY, UNIT
sms_INIT_ACCOUNT "$PHONE" "NOSMS"
if [[ $MEMBER == "" ]]; then MEMBER=$UIDNA; fi
###########################################################
# CHECK FOR ERRORS
if [[ $UNKNOWN == "unknown" ]]; then
sms_ERROR "$PHONE" "Porte-monnaie inconnu. Envoyez N (suivi de votre Pseudo membre) pour le créer."
exit
fi
if [[ "$MEMBER" == "" ]]; then
sms_ERROR "$PHONE" "Aucun membre associé à votre Portefeuille!? Envoyer N suivi de votre Pseudo membre pour activer les virements récurrents..."
exit
fi
###########################################################
# CALCULATE if wallet have enough for VIR+PERCENT
AMOUNT=$(bc -l <<< "$3 * $TIME")
accounting=($(make_accounting))
log "__SUB:sms_REC.sh: $(declare -p accounting)"
testmin=${accounting[0]}
VIR=$(bc -l <<< "scale=2; ${accounting[1]} / $TIME")
PERCENT=${accounting[2]}
CHARGE=${accounting[3]}
if [[ $testmin -eq 0 ]]; then
sms_ERROR "$PHONE" "Votre Porte-monnaie G1sms doit comporter un solde suffisant! Au minimum $CHARGE G1! Rechargez par Cesium (ou contactez https://g1sms.fr)."
exit
fi
###########################################################
# TODO Unify REC and VIR
# FIND VIRDEST into wallets, wallest-swarm OR duniter (silkaj)
if [[ "$VIRDEST" == "" || "$VIRDEST" == "$MEMBER" ]]; then
# VIR TO MEMBER PHONE OWNER
DESTMEM="$MEMBER"
DESTRIB="$MEMRIB"
log "__SUB:sms_REC.sh: SEND TO MEMBER related wallet!!! $DESTRIB "
else
log "__SUB:sms_REC.sh: SEARCH $VIRDEST related wallet!!! $DESTRIB "
DESTMEM="$VIRDEST"
# SEARCH IN WALLETS
LOCAL=$(grep -Rwl "$DESTMEM" ./wallets/*/*.uidname | tail -n 1 | cut -f 3 -d '/')
if [[ "$LOCAL" == "" ]]; then LOCAL=$(grep -Rwl "$DESTMEM" ./wallets/*/*.uidna | tail -n 1 | cut -f 3 -d '/'); fi
if [[ "$LOCAL" != "" ]]; then
# LOCAL G1sms account
DESTRIB=$(cat ./wallets/$LOCAL/$LOCAL.pub)
DESTPHONE=$LOCAL
log "__SUB:sms_REC.sh: FOUND LOCAL ($MASTERPHONE) G1sms+ wallet: $DESTRIB ($LOCAL)"
else
# SEARCH IN WALLETS SWARM (MEMBER THEN UIDNA)
INSWARM=$(grep -Rwl "$DESTMEM" ./wallets_swarm/*/*.uidname | tail -n 1 | cut -f 3 -d '/')
if [[ "$INSWARM" == "" ]]; then INSWARM=$(grep -Rwl "$DESTMEM" ./wallets_swarm/*/*.uidna | tail -n 1 | cut -f 3 -d '/'); fi
if [[ "$INSWARM" != "" ]]; then
REMOTENODE=$(cat ./wallets_swarm/$INSWARM/MASTERPHONE.sms)
DESTRIB=$(cat ./wallets_swarm/$INSWARM/$INSWARM.pub)
DESTPHONE=$INSWARM
log "__SUB:sms_REC.sh: FOUND SWARM ($REMOTENODE) G1sms+ wallet: $DESTRIB ($INSWARM)"
else
# SEARCH WITH SILKAJ
DESTRIB=$(./silkaj/silkaj id "$DESTMEM" | grep -w "$DESTMEM" | awk '{print $2}')
log "__SUB:sms_REC.sh: OUT OFF SWARM MEMBER wallet: $DESTRIB"
fi
fi
fi
# ADD DATES TO VIREMENTS
if [[ "$DESTRIB" != "" ]]; then
log "__SUB:sms_REC.sh: TRAITEMENT DU VIREMENT DE $AMOUNT $UNIT = 1ER VIREMENT de $VIR G1 vers $VIRDEST ($DESTRIB) $TIME FOIS. OK..."
PAY=$(./shell/timeout.sh -t 20 ./silkaj/silkaj transaction --auth-scrypt -salt="$PHONE" -password="$PIN" --amount="$VIR" --output="$DESTRIB" --comment="[G1sms+] REC 1/$TIME ($PERIOD)" -y)
if [[ "$(echo $PAY | cut -d '|' -f 1)" == "KO" || "$PAY" == "" ]]; then
new=$(./shell/checknodes.sh "BAN")
sms_ERROR "$PHONE" "Problème de payement avec silkaj (changement de serveur $new): $PAY";
log "__SUB:sms_REC.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
exit
else
cents=$(echo $(bc -l <<< "scale=0; $VIR * 100") | cut -d '.' -f 1)
move_g1cents "$PHONE" "$DESTRIB" "$cents"
fi
log "__SUB:sms_REC.sh: UPDATE ./wallets/$PHONE/VIREMENTS with FUTURE PAYEMENTS? $PAY"
########### MAKE ./wallets/$PHONE/VIREMENTS for NEXT ONES
if [[ -f "./wallets/$PHONE/VIREMENTS.gpg" ]]; then
# DECYPHER FILE FOR CHANGE
echo "${GPGPASS}" | gpg -d -q --output "./wallets/$PHONE/VIREMENTS" --yes --pinentry-mode loopback --passphrase-fd 0 "./wallets/$PHONE/VIREMENTS.gpg"
fi
# WRITE NEW LINES INTO AN ARRAY FILE: VIREMENTS
boucle=$((TIME - 1))
num=0
while [[ boucle -gt 0 ]]; do
$((boucle --))
$((num ++))
NEXT=$(date --date="$num $NEXTDATE" '+%Y%m%d')
# ADD NEW LINE IN VIREMENTS
echo "$NEXT $MEMBER $PHONE $VIR $DESTRIB $DESTMEM $MEMBER#$TIME#$AMOUNT#$DESTMEM $boucle " >> ./wallets/$PHONE/VIREMENTS
done
# CYPHER FILE
echo "${GPGPASS}" | gpg -q --output "./wallets/$PHONE/VIREMENTS.gpg" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "./wallets/$PHONE/VIREMENTS"
# EMPTY CLEAR FILE
log "__SUB:sms_REC.sh: $(cat ./wallets/$PHONE/VIREMENTS)"
echo "" > ./wallets/$PHONE/VIREMENTS
###########
log "__SUB:sms_REC.sh: .....................................OK!!!"
# SEND ACK SMS
log "__SUB:sms_REC.sh: VIREMENTS RECURRENTS $MEMBER#$TIME#$AMOUNT#$DESTMEM => $VIR G1 pour $DESTMEM (pendant $TIME $PERIOD) + AJOUT"
mess="[G1sms+]
Virement de $VIR G1 pour $DESTMEM (pendant $TIME $PERIOD)
Total: $AMOUNT $UNIT
$MEMBER#$TIME#$AMOUNT#$DESTMEM
$DESTRIB"
sms_SEND "$PHONE" "$mess"
log_history "$PHONE" "REC, $VIR, $DESTRIB, $DESTMEM, $PERIOD, $TIME"
# Send DESTPHONE sms
if [[ "$DESTPHONE" != "" ]]; then
sms_SEND "$DESTPHONE" "[G1sms+] Bonjour $DESTMEM. Vous recev(r)ez un payement de $VIR G1 (pendant $TIME $PERIOD) de la part du portefeuille: $MEMBER $UIDNA."
fi
else
log "__SUB:sms_REC.sh: ERROR $MEMBER: $DESTMEM DESTRIB NOT FOUND"
mess="[G1sms+] Virement annulé...
Destinataire: $DESTMEM inconnu! Vérifiez son existence."
sms_SEND "$PHONE" "$mess"
fi
log "__SUB:sms_REC.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"