ssb-g1like/process-likes-g1tx.sh

187 lines
6.8 KiB
Bash
Raw Normal View History

2020-03-13 17:52:08 +01:00
#!/bin/bash
################################################################################
# Authors:
# [@cel](@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519)
# [@Fred](@9BbJwPDjcyIqrOUPNn0nJZBduWdIrpMk3Cjz5MP361s=.ed25519)
2020-03-14 01:50:28 +01:00
# [@Boris](@l5nYExWYIgDLV6BYHOJPoI97jIUyTdSm8CTLpQ0XeOg=.ed25519)
2020-03-13 17:52:08 +01:00
# Version: 1.0
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
###########################################################################################
# PREVENT DOUBLE PAYEMENT
2020-03-14 23:10:42 +01:00
# ADD G1 Layer 10 LOVE to message writer you like !
2020-03-13 17:52:08 +01:00
############################################################################################
# Let's get G1 public and private keys
g1pub=$(cat ~/.ssb/secret.dunikey | grep "pub" | cut -d ' ' -f 2)
g1priv=$(cat ~/.ssb/secret.dunikey | grep "sec" | cut -d ' ' -f 2)
# SSB pubkey
ssbpub=$(cat ~/.ssb/secret | grep public\" | cut -d ' ' -f 4 | cut -d '.' -f 1 | sed s/\"//g)
############################################################################################
#### CHECK LIKE AND SEND LOVE
# Let's get G1 account balance
echo ""
echo "Welcome to the G1 SSB like microdonation system!"
2020-03-13 17:52:08 +01:00
echo "
MMMMMMMMMMMMMNk;'cdxxd:,c0WMMMMMMMMMMMMM
MMMMMMMMMMMMMNx,. .;kWMMMMMMMMMMMMM
MMMMMMMMMMMMMMMNOdlccld0NMMMMMMMMMMMMMMM
MMMMMMMMMMMWXko:,'....',:okXWMMMMMMMMMMM
MMMMMMMMMNk:. .cOWMMMMMMMMM
MMMMMMMW0: .c0MMMMMMMM
MMMMMMWk. 'lxkOOkdc' .cOWMMMMMMM
MMMMMMO' 'kNMMMMMMMMNxcoOXWMMMMMMMMM
MMMMMNl '0MMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMX; cNMMMMMMMNOkkkkkkkkkkONMMMMM
MMMMMNc ;XMMMMMMMNd' .OMMMMM
MMMMMWx. cKMMMMMMMWKc. .OMMMMM
MMMMMMNo. .lkKXNNXKkc. .OMMMMM
MMMMMMMNd. ...... .OMMMMM
MMMMMMMMWKl. 'c:. .OMMMMM
MMMMMMMMMMWXkc,.. ..,lkXWWO:;OMMMMM
MMMMMMMMMMMMMMWX0OxddxO0XWMMMMMMWXNMMMMM
MMMMMMMMMMMMMMMMNx;'',dNMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMK, '0MMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMNd. .oNMMMMMMMMMMMMMMMM
"
sleep 1
echo ""
echo "You know your SSB pubkey:"
echo $ssbpub
sleep 1
echo ""
echo "...but did you know it is also a valid G1 wallet?"
echo $g1pub
sleep 2
echo ""
echo "Let's check the current balance of your wallet!"
sleep 2
echo ""
echo "We are interrogating the G1 blockchain to check if anyone has already sent G1..."
sleep 1
echo ""
echo "A moment please..."
echo ""
sleep 1
duniter_server="duniter-g1.p2p.legal:443"
echo "Testing server $duniter_server..."
echo ""
silkaj -p "$duniter_server" balance $g1pub 2>/dev/null
if [ $? -eq 1 ]
then
duniter_server="g1.duniter.org:443"
echo "Testing server $duniter_server..."
echo ""
silkaj -p "$duniter_server" balance $g1pub 2>/dev/null
if [ $? -eq 1 ]
then
echo "The server did not respond well. Please try again."
exit 1
fi
fi
echo "Have any money? Then let's send G1love. Press ENTER to continue. " && read
###########################################################################################
self=$(sbotc whoami | jq -r .id) || exit 1
ssb_dir=~/.${ssb_appname:-ssb}
id_part=$(echo "$self" | sed 's/\//_/g' | tail -c +2 | head -c 9)
state_file=$ssb_dir/likes-g1-$id_part.ts
if [ -s "$state_file" ]
then last_ts=$(cat "$state_file") || exit 1
else
last_ts=$(date -u +%s%N | cut -b1-13)
last_ts=$((last_ts - 3600000)) # timestamp from 1h ago
#else last_ts=null
fi
process_msg() {
msg=$1
target_id=$(printf %s "$msg" | jq -r '.value?.content?.vote?.link') || return 1
target_msg=$(sbotc -e get "$target_id") || return 1
target_author=$(printf %s "$target_msg" | jq -r .author) || return 1
author_s_name=$(sbotc query.read '{"query":[{"$filter":{"value":{"author": "'"$target_author"'", "content":{"type":"about", "about": "'"$target_author"'"}}}}]}' | jq .value?.content?.name | grep -v null | tail -n 1)
2020-03-13 17:52:08 +01:00
root_id=$(printf %s "$target_msg" | jq -r .content?.root) || return 1
[[ $root_id == "null" ]] && root_id=$target_id
g1_author=$(echo $target_author | cut -d '.' -f 1 | cut -d '@' -f2 | base64 -d | base58)
msg_excerpt=$(printf %s "$target_msg" | jq -r .content?.text | head -n 10) || return 1
2020-03-13 17:52:08 +01:00
}
bold=$(tput bold)
normal=$(tput sgr0)
2020-03-13 17:52:08 +01:00
sbotc query.read '{"query":[{"$filter":{"value":{"author":"'"$self"'","content":{"type":"vote", "vote":{"expression":"Like"}},"timestamp":{"$gt":'"$last_ts"'}}}}]}' | while read -r msg
do
2020-03-16 01:15:07 +01:00
priv=$(printf %s "$msg" | jq .value.content.private)
if [[ $priv == true ]]
then
printf "Private message $priv, continue to next one\n" >&2
continue
fi
2020-03-13 17:52:08 +01:00
if ! ts=$(printf %s "$msg" | jq -r .value.timestamp)
then
printf 'Unable to get message timestamp\n' >&2
exit 1
fi
if ! echo "$ts" > "$state_file"~
then
printf 'Unable to write to backup state file.\n' >&2
exit 1
fi
if ! process_msg "$msg"
then
msg_id=$(printf %s "$msg" | jq -r .key)
printf 'Unable to process message %s\n' "$msg_id" >&2
exit 1
fi
echo ""
echo "================================================================"
printf "Let's thank ${bold}@%s ${normal}for their message: \n" "${author_s_name:1:-1}"
echo "================================================================"
printf "%s" "$msg_excerpt"
echo ""
echo "----------------------------------------------------------------"
2020-03-13 17:52:08 +01:00
if [[ $g1_author != $g1pub ]]; then
#printf '%s\n' "silkaj -af --file ~/.ssb/secret.dunikey tx --output $g1_author --amountUD 0.1 --comment _SSB:LIKE:$target_id"
2020-03-14 21:23:14 +01:00
#### SEND 10 LOVE = 0.1 DU to $g1_author wallet
silkaj -p "$duniter_server" -af --file ~/.ssb/secret.dunikey tx --output $g1_author --amountUD 0.1 --comment "_SSB:LIKE:$target_id" -y 2>/dev/null
2020-03-14 21:23:14 +01:00
#### WRITE RESPONSE MESSAGE
if [ ! -f ~/.ssb/db/g1likes ]; then
touch ~/.ssb/db/g1likes
fi
if ! grep -Fxq $target_author ~/.ssb/db/g1likes; then
echo $target_author >> ~/.ssb/db/g1likes
thank_you_msg=$(printf '[@%s](%s) > Thanks for this post ; I have just sent you G1 libre money. Use it with [Cesium](https://cesium.app). Get it from [https://git.p2p.legal/Axiom-Team/ssb-g1like](https://git.p2p.legal/Axiom-Team/ssb-g1like)' "${author_s_name:1:-1}" "$target_author")
sbotc publish '{"type":"post","text":"'"$thank_you_msg"'", "branch": "'"$target_id"'", "root": "'"$root_id"'"}' 2>&1>/dev/null
echo "----------------------------------------------------------------"
printf "Since it's the first time you sent G1 to %s, he was sent this message: \n\n%s \n" "$author_s_name" "$thank_you_msg"
echo "----------------------------------------------------------------"
fi
2020-03-15 03:01:24 +01:00
#printf "0.1 G1UD sent to %s!\n" "$author_s_name"
#echo "----------------------------------------------------------------"
echo ""
echo ""
sleep 20 # DO NOT OVER CHARGE DUNITER
2020-03-13 17:52:08 +01:00
else
echo "I LIKE MY MESSAGE $target_id"
fi
if ! mv "$state_file"~ "$state_file"
then
msg_id=$(printf %s "$msg" | jq -r .key)
printf 'Unable to write to state file. Update state file manually to prevent %s from being processed twice.\n' "$msg_id" >&2
exit 1
fi
done