astroport/g1sms/sms_REC.sh

175 lines
6.8 KiB
Bash
Raw Normal View History

2020-05-01 15:19:51 +02:00
#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
2020-05-07 02:27:11 +02:00
# Version: 1.0
2020-05-01 15:19:51 +02:00
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
2020-05-21 02:34:16 +02:00
# RECORD RECURRENT PAYEMENTS WITH "Cesium+ ID" Pubkey TODO cron /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS, Fusion avec sms_VIR.sh
2020-05-01 15:19:51 +02:00
################################################################################
2020-05-12 23:25:52 +02:00
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
source $MY_PATH/init.sh
source $MY_PATH/functions.sh
2020-05-01 15:19:51 +02:00
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"
2020-05-07 02:27:11 +02:00
log "__SUB:sms_VIR.sh: SEND TO MEMBER related wallet!!! $DESTRIB "
2020-05-01 15:19:51 +02:00
else
2020-05-07 02:27:11 +02:00
log "__SUB:sms_VIR.sh: SEARCH $VIRDEST related wallet!!! $DESTRIB "
2020-05-01 15:19:51 +02:00
DESTMEM="$VIRDEST"
# SEARCH IN WALLETS
2020-05-21 02:34:16 +02:00
LOCAL=$(grep -Rwl "$DESTMEM" /home/$YOU/.zen/g1sms/*/_uidname | tail -n 1 | cut -f 6 -d '/')
if [[ "$LOCAL" == "" ]]; then LOCAL=$(grep -Rwl "$DESTMEM" /home/$YOU/.zen/g1sms/*/_uidna | tail -n 1 | cut -f 6 -d '/'); fi
2020-05-01 15:19:51 +02:00
if [[ "$LOCAL" != "" ]]; then
# LOCAL G1sms account
2020-05-21 02:34:16 +02:00
DESTRIB=$(cat /home/$YOU/.zen/g1sms/$LOCAL/_pub)
2020-05-01 15:19:51 +02:00
DESTPHONE=$LOCAL
2020-05-07 02:27:11 +02:00
log "__SUB:sms_VIR.sh: FOUND LOCAL ($MASTERPHONE) G1sms+ wallet: $DESTRIB ($LOCAL)"
2020-05-01 15:19:51 +02:00
else
2020-05-07 02:27:11 +02:00
##################### REWRITE TODO
2020-05-01 15:19:51 +02:00
# SEARCH IN WALLETS SWARM (MEMBER THEN UIDNA)
2020-05-21 02:34:16 +02:00
# INSWARM=$(grep -Rwl "$DESTMEM" /home/$YOU/.zen/g1sms_swarm/*/*.uidname | tail -n 1 | cut -f 6 -d '/')
# if [[ "$INSWARM" == "" ]]; then INSWARM=$(grep -Rwl "$DESTMEM" /home/$YOU/.zen/g1sms_swarm/*/*.uidna | tail -n 1 | cut -f 6 -d '/'); fi
2020-05-07 02:27:11 +02:00
# if [[ "$INSWARM" != "" ]]; then
2020-05-21 02:34:16 +02:00
# REMOTENODE=$(cat /home/$YOU/.zen/g1sms_swarm/$INSWARM/MASTERPHONE.sms)
# DESTRIB=$(cat /home/$YOU/.zen/g1sms_swarm/$INSWARM/$INSWARM.pub)
2020-05-07 02:27:11 +02:00
# DESTPHONE=$INSWARM
# log "__SUB:sms_VIR.sh: FOUND SWARM ($REMOTENODE) G1sms+ wallet: $DESTRIB ($INSWARM)"
# else
##################### REWRITE TODO
2020-05-01 15:19:51 +02:00
# SEARCH WITH SILKAJ
DESTRIB=$(silkaj id "$DESTMEM" | grep -w "$DESTMEM" | awk '{print $2}')
2020-05-07 02:27:11 +02:00
log "__SUB:sms_VIR.sh: OUT OFF SWARM MEMBER wallet: $DESTRIB"
# fi
2020-05-01 15:19:51 +02:00
fi
fi
# ADD DATES TO VIREMENTS
if [[ "$DESTRIB" != "" ]]; then
2020-05-13 13:03:56 +02:00
duniter_server=$($MY_PATH/tools/duniter_getnode.sh)
2020-05-01 15:19:51 +02:00
log "__SUB:sms_REC.sh: TRAITEMENT DU VIREMENT DE $AMOUNT $UNIT = 1ER VIREMENT de $VIR G1 vers $VIRDEST ($DESTRIB) $TIME FOIS. OK..."
2020-05-21 02:34:16 +02:00
PAY=$(silkaj -p "$duniter_server" -af --file /home/$YOU/.zen/g1sms/$PHONE/secret.dunikey tx --output "$DESTRIB" --amount "$VIR" --comment "[G1sms+] REC 1/$TIME ($PERIOD)" -y)
2020-05-07 02:27:11 +02:00
if [[ "$(echo $PAY | grep 'successful')" == "" || "$PAY" == "" ]]; then
sms_ERROR "$PHONE" "Problème de payement $PAY";
2020-05-01 15:19:51 +02:00
log "__SUB:sms_REC.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
2020-05-07 02:27:11 +02:00
exit 1
2020-05-01 15:19:51 +02:00
fi
2020-05-21 02:34:16 +02:00
log "__SUB:sms_REC.sh: UPDATE /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS with FUTURE PAYEMENTS? $PAY"
2020-05-01 15:19:51 +02:00
2020-05-21 02:34:16 +02:00
########### MAKE /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS for NEXT ONES
if [[ -f "/home/$YOU/.zen/g1sms/$PHONE/VIREMENTS.gpg" ]]; then
2020-05-01 15:19:51 +02:00
# DECYPHER FILE FOR CHANGE
2020-05-21 02:34:16 +02:00
echo "${IPFSPrivKey}" | gpg -d -q --output "/home/$YOU/.zen/g1sms/$PHONE/VIREMENTS" --yes --pinentry-mode loopback --passphrase-fd 0 "/home/$YOU/.zen/g1sms/$PHONE/VIREMENTS.gpg"
2020-05-01 15:19:51 +02:00
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
2020-05-21 02:34:16 +02:00
echo "$NEXT $MEMBER $PHONE $VIR $DESTRIB $DESTMEM $MEMBER#$TIME#$AMOUNT#$DESTMEM $boucle " >> /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS
2020-05-01 15:19:51 +02:00
done
# CYPHER FILE
2020-05-21 02:34:16 +02:00
echo "${IPFSPrivKey}" | gpg -q --output "/home/$YOU/.zen/g1sms/$PHONE/VIREMENTS.gpg" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "/home/$YOU/.zen/g1sms/$PHONE/VIREMENTS"
2020-05-01 15:19:51 +02:00
# EMPTY CLEAR FILE
2020-05-21 02:34:16 +02:00
log "__SUB:sms_REC.sh: $(cat /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS)"
echo "" > /home/$YOU/.zen/g1sms/$PHONE/VIREMENTS
2020-05-01 15:19:51 +02:00
###########
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"
# 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
2020-05-21 02:34:16 +02:00
[[ -d /home/$YOU/.zen/g1sms/$PHONE ]] && [[ $(whoami) == "root" ]] && chown -R $YOU /home/$YOU/.zen/g1sms/$PHONE
2020-05-01 15:19:51 +02:00
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"