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.
 
 
 
 
 
 

174 lines
8.5 KiB

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.1
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
################################################################################
# CE FICHIER cron_MINUTE.sh EST EXECUTE TOUTES LES MINUTES
# IL CONSTITUE LE BATEMENT DE COEUR DU SYSTEME
################################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
if [[ -f $MY_PATH/.install/.GPATH ]]; then source $MY_PATH/.install/.GPATH; fi
if [[ -f $MY_PATH/../.install/.GPATH ]]; then source $MY_PATH/../.install/.GPATH; fi
YOU=$(ps aux --sort=+utime | grep ipfs | tail -n 1 | cut -d " " -f 1)
#if [[ "$USER" != "root" ]]; then echo "Hey, $USER you must at least be admin of your system (sudo $0)"; exit; fi # BUG because cron USER=""
if [[ "$YOU" == "" || "$YOU" == "root" ]]; then echo "BAD IPFS. Aucune installation IPFS satisfaisante ici... Ciao $YOU !"; exit; fi
IPFSNODEID=$(su $YOU -c "ipfs id -f='<id>\n'")
cd $GPATH
source ./shell/init.sh
source ./shell/functions.sh
timebar=$(date +%H:%M)
minut=$(date +%M | sed 's/^0*//')
[[ $minut == "" ]] && minut=0
# log "__CRON:cron_MINUTE.sh - $minut - It is $timebar ... $1"
##################################################################
##################################################################
##################################################################
##################################################################
# EVERY EVERY MINUTES
# START CHECK FOR EMAIL CMD
# START CHECK FOR TX CMD
EVERY=1
##################################################################
if [[ $(($minut%$EVERY)) == 0 ]]; then
##################################################################
# CHECK FOR TX COMMAND
TIMEBEFORE=$(date --date="-$EVERY minutes" +"%s")
[[ $NODE_G1PUBKEY == "" ]] && NODE_G1PUBKEY="5sdcypPRDrzEirtTqXwzvNnMwGpjXK6EhNdti8qsdS4h"
TIMESTAMP=$(date +"%s")
#log "GETTING last $EVERY mn TX : https://duniter-g1.p2p.legal/tx/history/$NODE_G1PUBKEY/times/$TIMEBEFORE/$TIMESTAMP"
curl -s https://duniter-g1.p2p.legal/tx/history/$NODE_G1PUBKEY/times/$TIMEBEFORE/$TIMESTAMP | jq '.history.pending' | jq '.[].comment' > /tmp/zenfontain.g1tx.txt
while read SMS_1_TEXT
do
export $SMS_1_TEXT
log "$NODE_G1PUBKEY TX PENDING $timebar !! COMMENT = " $SMS_1_TEXT
# TODO: VERIFY SOURCE PUBKEY IS RELATED TO A WALLET
# PUBKEY=
# WALLET=$(grep -Rwl "$PUBKEY" ./wallets/*/*.pub | head -n 1)
# export SMS_1_NUMBER=$(echo $WALLET | cut -d '/' -f 3) #TODO CHANGE ON NEXT SHA RELEASE
# TODO: GET PHONE G1SMS (OPTION: SEND SMS FOR AUTH CHALENGE?)
# TODO: EXTRACT CMD FROM TX AND RELAY TO SMS_RECEIVED API
# TODO: MEMORIZE CMD TO MAKE IT ONLY ONE TIME!!!!!!!
done < /tmp/zenfontain.g1tx.txt
# CHECK FOR EMAIL COMMAND
FROM="None"
SUBJECT="None"
# LOOP ON EVERY mn received emails
while [[ $FROM != "" && $SUBJECT != "" ]]; do
# GET POP3 From Address (TODO: CREATE != EMAILs FOR EACH NODE = UIDNA@g1sms.fr )
FROM=$(curl -s --insecure --url "pop3://pop.bookmyname.com/1" --user "vir@g1sms.fr:Z1cG1V1r!" | grep From | head -n 1 | grep -i -o '[A-Z0-9._%+-]\+@[A-Z0-9.-]\+\.[A-Z]\{2,4\}' | tr -d '\r')
# GET POP3 Email Subject (remove carriage return | tr -d '\r' )
export SMS_1_TEXT=$(curl -s --insecure --url "pop3://pop.bookmyname.com/1" --user "vir@g1sms.fr:Z1cG1V1r!" | grep Subject | head -n 1 | cut -d ':' -f 2 | sed -e 's/^[[:space:]]*//' | tr -d '\r')
# SHA256(FROM) as Search ./wallets
[[ $FROM != "" ]] && HMAIL=$(echo -n $FROM | sha256sum | cut -d ' ' -f 1)
# SEARCH for FROM corresponding WALLET
[[ $HMAIL != "" ]] && WALLET=$(grep -Rwl "$HMAIL" ./wallets/*/_email.sha | head -n 1)
# HAVE WALLET SOURCE + CMD: OK
if [[ $WALLET ]]; then
# DELETE EMAIL 1
curl -s --user "vir@g1sms.fr:Z1cG1V1r!" pop3://pop.bookmyname.com/1 --request dele --list-only
# GET G1SMS PHONE NUMBER
export SMS_1_NUMBER=$(echo -n $WALLET | cut -d '/' -f 3) #TODO CHANGE ON NEXT SHA RELEASE
# $SMS_1_NUMBER && $SMS_1_TEXT => RUN ./sms_received.sh
[[ $SMS_1_TEXT != "" && $SMS_1_NUMBER ]] && log "EMAIL CMD = $SMS_1_NUMBER $SMS_1_TEXT" && ./sms_received.sh
cat ./email.txt | sed s/_EMAIL/"${FROM}"/g | sed s/_SUBJECT/"G1sms+ EMAIL ACK"/g | sed s/_MESSAGE/"$SMS_1_TEXT OK"/g | ssmtp ${FROM}
else
if [[ $FROM != "" ]]; then
log "MAIL RECEIVED! NO GOOD SENDER: $FROM"
curl -s --user "vir@g1sms.fr:Z1cG1V1r!" pop3://pop.bookmyname.com/1 --request dele --list-only
cat ./email.txt | sed s/_EMAIL/"${FROM}"/g | sed s/_SUBJECT/"G1sms+ INCONNU"/g | sed s/_MESSAGE/"Portefeuille G1sms inconnu"/g | ssmtp ${FROM}
fi
#log "NO MAIL"
fi
done
##################################################################
fi # END CHECK FOR EMAIL CMD
##################################################################
##################################################################
if [[ "$1" == "PRINT" ]]; then
##################################################################
##################################################################
# Refresh All peers SWARM Wallets.
##################################################################
ipfs_swarm_wallets_refresh "SIMPLE"
################################################################################
# PRINT G1Tag PRINTED FROM ANOTHER NODE (TODO: Correct BAD Cypher KEY + BUG why last image png layers are not printed???)
# CREATE and REMOVE rr.bin -> done.rr
if [[ -d "./wallets_swarm/.$IPFSNODEID/PRINT/" ]]; then
log "__SUB:cron_MINUTE.sh: SEARCH NEW PRINT FOR ME .$IPFSNODEID"
mkdir -p ./wallets/.$IPFSNODEID/PRINT/
for qrrtag in ./wallets_swarm/.$IPFSNODEID/PRINT/*.bin; do
rr=$(echo $qrrtag | cut -d '/' -f 5 | cut -d '.' -f 1)
if [[ ! -f "./wallets/.$IPFSNODEID/PRINT/done.$rr" ]]; then
log "__SUB:cron_MINUTE.sh: PRINT G1Tag $rr - START: $(date)"
./shell/natools.py decrypt -k "$NODE_G1AUTHFILE" -i "${qrrtag}" -o "/tmp/G1Tag.png"
log "__SUB:cron_MINUTE.sh: natools.py decrypt - $(date)"
brother_ql_create --model QL-700 "/tmp/G1Tag.png" --label-size 62 > "/tmp/G1Tag.bin"
log "__SUB:cron_MINUTE.sh: brother_ql_create - $(date)"
brother_ql_print "/tmp/G1Tag.bin" /dev/usb/lp0
log "__SUB:cron_MINUTE.sh: brother_ql_print - $(date)"
# INFORM PRINT DONE
echo "OK" > ./wallets/.$IPFSNODEID/PRINT/done.$rr
I=$(ipfs_node_wallets_add)
else
lqrrtag=$(echo "${qrrtag}" | sed s/_swarm//g )
log "__SUB:cron_MINUTE.sh: REMOVE OLD DONE PRINT ${lqrrtag} + ./wallets/.$IPFSNODEID/PRINT/done.$rr"
rm -f "./wallets/.$IPFSNODEID/PRINT/done.$rr"
rm -f "${lqrrtag}"
I=$(ipfs_node_wallets_add)
fi
done
log "__SUB:cron_MINUTE.sh: REMOVE OLD PRINT MARKED AS DONE"
for scan in ./wallets_swarm/.Qm*/PRINT/done.*; do
lscan=$(echo $scan | sed s/_swarm//g )
lid=$(echo $scan | cut -d '/' -f 3 | cut -d '.' -f 2 )
lrr=$(echo $scan | cut -d '/' -f 5 | cut -d '.' -f 2 )
log "__SUB:cron_MINUTE.sh: REMOVE ./wallets/.$lid/PRINT/$lrr.bin OLD PRINT ${lscan} SENT to $lid ($lrr.bin)"
rm -f ./wallets/.$lid/PRINT/$lrr.bin
rm -f "${lscan}"
I=$(ipfs_node_wallets_add)
done
rm "/tmp/G1Tag.bin"
rm "/tmp/G1Tag.png"
fi
fi
##################################################################
##################################################################
if [[ "$1" == "KALKUN" ]]; then
##################################################################
for number in ./wallets/*; do
member=""
phone=$(echo $number | cut -d '/' -f 3);
if [[ -f $number/$phone.uidname ]]; then member=$(cat $number/$phone.uidname); fi
if [[ $phone && $member ]]; then
rm_contact $phone $member;
sleep 2
add_contact $phone $member;
log "__SUB:cron_MINUTE.sh: KALKUN refresh contact $phone $member";
fi
done
fi
##################################################################
##################################################################
if [[ "$1" == "TAG" ]]; then
##################################################################
for tag in ./TAG/*; do
echo $tag
done
fi
##################################################################