jaklis/jaklis.py

419 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import sys
import os
import string
import random
from os.path import join, dirname
from shutil import copyfile
from dotenv import load_dotenv
from duniterpy.key import SigningKey
__version__ = "0.0.5"
MY_PATH = os.path.realpath(sys.argv[0]).replace("jaklis.py", "")
# Get environment variables
if not os.path.isfile(MY_PATH + ".env"):
copyfile(MY_PATH + ".env.template", MY_PATH + ".env")
dotenv_path = join(dirname(__file__), MY_PATH + ".env")
load_dotenv(dotenv_path)
# Set global values (default parameters) regarding environment variables
node = (
os.getenv("DUNITER") + "/gva"
if os.getenv("DUNITER")
else "https://g1v1.p2p.legal/gva"
)
pod = os.getenv("ESNODE") if os.getenv("ESNODE") else "https://g1.data.e-is.pro"
destPubkey = False
# Parse arguments
parser = argparse.ArgumentParser(
description="CLI Client for Cesium+ and Ḡchange",
epilog="current node: '" + node + "', current pod: '" + pod + "'.",
)
parser.add_argument(
"-v",
"--version",
action="store_true",
help="Display the current program version",
)
parser.add_argument("-k", "--key", help="Path to the keyfile (PubSec)")
parser.add_argument(
"-n", "--node", help="Address of the Cesium+, Gchange, or Duniter node to use"
)
# Create subparsers for different commands
subparsers = parser.add_subparsers(title="jaklis Commands", dest="cmd")
read_cmd = subparsers.add_parser("read", help="Read messages")
send_cmd = subparsers.add_parser("send", help="Send a message")
delete_cmd = subparsers.add_parser("delete", help="Delete a message")
getProfile_cmd = subparsers.add_parser("get", help="View a Cesium+ profile")
getPage_cmd = subparsers.add_parser("page", help="View a Cesium+ page")
setProfile_cmd = subparsers.add_parser("set", help="Configure your Cesium+ profile")
eraseProfile_cmd = subparsers.add_parser("erase", help="Erase your Cesium+ profile")
stars_cmd = subparsers.add_parser(
"stars", help="View a profile's stars / Rate a profile (option -s RATING)"
)
unstars_cmd = subparsers.add_parser("unstars", help="Remove a star")
getoffer_cmd = subparsers.add_parser(
"getoffer", help="Get information about a Ḡchange listing"
)
setoffer_cmd = subparsers.add_parser("setoffer", help="Create a Ḡchange listing")
deleteoffer_cmd = subparsers.add_parser("deleteoffer", help="Delete a Ḡchange listing")
pay_cmd = subparsers.add_parser("pay", help="Pay in Ḡ1")
history_cmd = subparsers.add_parser(
"history", help="View Ḡ1 account transaction history"
)
balance_cmd = subparsers.add_parser("balance", help="View Ḡ1 account balance")
id_cmd = subparsers.add_parser("id", help="View public key/username identity")
id_balance_cmd = subparsers.add_parser(
"idBalance", help="View public key/username identity and balance"
)
currentUd = subparsers.add_parser(
"currentUd", help="Display the current Universal Dividend amount"
)
listWallets = subparsers.add_parser("listWallets", help="List all G1 wallets")
geolocProfiles = subparsers.add_parser(
"geolocProfiles", help="Get JSON of all geolocated accounts"
)
# Messaging management commands
read_cmd.add_argument(
"-n", "--number", type=int, default=3, help="Display the last NUMBER messages"
)
read_cmd.add_argument("-j", "--json", action="store_true", help="Output in JSON format")
read_cmd.add_argument("-o", "--outbox", action="store_true", help="Read sent messages")
send_cmd.add_argument(
"-d", "--destinataire", required=True, help="Recipient of the message"
)
send_cmd.add_argument("-t", "--titre", help="Title of the message to send")
send_cmd.add_argument("-m", "--message", help="Message to send")
send_cmd.add_argument("-f", "--fichier", help="Send the message from the 'FILE'")
send_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Send the message to the outbox"
)
delete_cmd.add_argument(
"-i",
"--id",
action="append",
nargs="+",
required=True,
help="ID(s) of the message(s) to delete",
)
delete_cmd.add_argument(
"-o", "--outbox", action="store_true", help="Delete a sent message"
)
# Profile management commands
setProfile_cmd.add_argument("-n", "--name", help="Profile name")
setProfile_cmd.add_argument("-d", "--description", help="Profile description")
setProfile_cmd.add_argument("-v", "--ville", help="Profile city")
setProfile_cmd.add_argument("-a", "--adresse", help="Profile address")
setProfile_cmd.add_argument(
"-pos", "--position", nargs=2, help="Geographical coordinates (lat + lon)"
)
setProfile_cmd.add_argument("-s", "--site", help="Profile website")
setProfile_cmd.add_argument("-A", "--avatar", help="Path to profile avatar in PNG")
getProfile_cmd.add_argument("-p", "--profile", help="Profile name")
getProfile_cmd.add_argument(
"-a",
"--avatar",
action="store_true",
help="Also retrieve the avatar in raw base64 format",
)
getPage_cmd.add_argument("-p", "--page", help="Page name")
getPage_cmd.add_argument(
"-a",
"--avatar",
action="store_true",
help="Also retrieve the page's avatar in raw base64 format",
)
# Likes management commands
stars_cmd.add_argument("-p", "--profile", help="Target profile")
stars_cmd.add_argument("-n", "--number", type=int, help="Number of stars")
unstars_cmd.add_argument("-p", "--profile", help="Profile to unstar")
# Offers management commands
getoffer_cmd.add_argument("-i", "--id", help="Target listing to retrieve")
setoffer_cmd.add_argument("-t", "--title", help="Title of the listing to create")
setoffer_cmd.add_argument(
"-d", "--description", help="Description of the listing to create"
)
setoffer_cmd.add_argument("-c", "--category", help="Category of the listing to create")
setoffer_cmd.add_argument(
"-l",
"--localisation",
nargs=2,
help="Location of the listing to create (lat + lon)",
)
setoffer_cmd.add_argument("-p", "--picture", help="Image of the listing to create")
setoffer_cmd.add_argument("-ci", "--city", help="City of the listing to create")
setoffer_cmd.add_argument("-pr", "--price", help="Price of the listing to create")
deleteoffer_cmd.add_argument("-i", "--id", help="Target listing to delete")
# GVA usage commands
pay_cmd.add_argument("-p", "--pubkey", help="Payment recipient")
pay_cmd.add_argument("-a", "--amount", type=float, help="Transaction amount")
pay_cmd.add_argument(
"-c", "--comment", default="", help="Transaction comment", nargs="*"
)
pay_cmd.add_argument("-m", "--mempool", action="store_true", help="Use mempool sources")
pay_cmd.add_argument(
"-v",
"--verbose",
action="store_true",
help="Display the JSON result of the transaction",
)
history_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
history_cmd.add_argument(
"-n",
"--number",
type=int,
default=10,
help="Display the last NUMBER transactions",
)
history_cmd.add_argument(
"-j", "--json", action="store_true", help="Display the result in JSON format"
)
history_cmd.add_argument(
"--nocolors", action="store_true", help="Display the result in black and white"
)
balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
balance_cmd.add_argument(
"-m", "--mempool", action="store_true", help="Use mempool sources"
)
id_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
id_cmd.add_argument("-u", "--username", help="Username of the target account")
id_balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
currentUd.add_argument("-p", "--pubkey", help="Public key of the target account")
listWallets.add_argument(
"-b", "--balance", action="store_true", help="Display balances"
)
listWallets.add_argument(
"--mbr", action="store_true", help="Display raw list of member pubkeys"
)
listWallets.add_argument(
"--non_mbr",
action="store_true",
help="Display raw list of non-member identity pubkeys",
)
listWallets.add_argument(
"--larf", action="store_true", help="Display raw list of non-member pubkeys"
)
listWallets.add_argument(
"--brut", action="store_true", help="Display raw list of all pubkeys"
)
listWallets.add_argument("-p", "--pubkey", help="Useless but needed")
args = parser.parse_args()
cmd = args.cmd
if args.version:
print(__version__)
sys.exit(0)
if not cmd:
parser.print_help()
sys.exit(1)
def createTmpDunikey():
# Generate a pseudo-random nonce
nonce = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(32)
)
keyPath = "/tmp/secret.dunikey-" + nonce
# Create a dummy key (replace with actual key creation logic)
key = SigningKey.from_credentials(
"sgse547yhd54xv6541srdh", "sfdgwdrhpkxdawsbszqpof1sdg65xc", None
)
key.save_pubsec_file(keyPath)
return keyPath
# Check if a dunikey is needed
try:
pubkey = args.pubkey
except:
pubkey = False
try:
profile = args.profile
except:
profile = False
if cmd in (
"history",
"balance",
"get",
"page",
"id",
"idBalance",
"listWallets",
) and (pubkey or profile):
noNeedDunikey = True
keyPath = False
try:
dunikey = args.pubkey
except:
dunikey = args.profile
else:
noNeedDunikey = False
if args.key:
dunikey = args.key
keyPath = False
else:
dunikey = os.getenv("DUNIKEY")
if not dunikey:
keyPath = createTmpDunikey()
dunikey = keyPath
else:
keyPath = False
if not os.path.isfile(dunikey):
HOME = os.getenv("HOME")
dunikey = HOME + dunikey
if not os.path.isfile(dunikey):
sys.stderr.write("The keyfile {0} is not found.\n".format(dunikey))
sys.exit(1)
# Construct the CesiumPlus object
if cmd in (
"read",
"send",
"delete",
"set",
"get",
"page",
"erase",
"stars",
"unstars",
"getoffer",
"setoffer",
"deleteoffer",
"geolocProfiles",
):
from lib.cesium import CesiumPlus
if args.node:
pod = args.node
cesium = CesiumPlus(dunikey, pod, noNeedDunikey)
# Messaging
if cmd == "read":
cesium.read(args.number, args.outbox, args.json)
elif cmd == "send":
if args.fichier:
with open(args.fichier, "r") as f:
msgT = f.read()
titre = msgT.splitlines(True)[0].replace("\n", "")
msg = "".join(msgT.splitlines(True)[1:])
if args.titre:
titre = args.titre
msg = msgT
elif args.titre and args.message:
titre = args.titre
msg = args.message
else:
titre = input("Enter the message title: ")
msg = input("Enter the message content: ")
cesium.send(titre, msg, args.destinataire, args.outbox)
elif cmd == "delete":
cesium.delete(args.id[0], args.outbox)
# Profiles
elif cmd == "set":
cesium.set(
args.name,
args.description,
args.ville,
args.adresse,
args.position,
args.site,
args.avatar,
)
elif cmd == "get":
cesium.get(args.profile, args.avatar)
elif cmd == "page":
cesium.getPage(args.page, args.avatar)
elif cmd == "erase":
cesium.erase()
elif cmd == "geolocProfiles":
cesium.geolocProfiles(node)
# Stars
elif cmd == "stars":
if args.number or args.number == 0:
cesium.like(args.number, args.profile)
else:
cesium.readLikes(args.profile)
elif cmd == "unstars":
cesium.unLike(args.profile)
# Offers
elif cmd == "getoffer":
cesium.getOffer(args.id)
elif cmd == "setoffer":
cesium.setOffer(
args.title,
args.description,
args.city,
args.localisation,
args.category,
args.price,
args.picture,
)
elif cmd == "deleteoffer":
cesium.deleteOffer(args.id)
# Construct the GvaApi object
elif cmd in (
"pay",
"history",
"balance",
"id",
"idBalance",
"currentUd",
"listWallets",
):
from lib.gva import GvaApi
if args.node:
node = args.node
if args.pubkey:
destPubkey = args.pubkey
gva = GvaApi(dunikey, node, destPubkey, noNeedDunikey)
if cmd == "pay":
gva.pay(args.amount, args.comment, args.mempool, args.verbose)
elif cmd == "history":
gva.history(args.json, args.nocolors, args.number)
elif cmd == "balance":
gva.balance(args.mempool)
elif cmd == "id":
gva.id(args.pubkey, args.username)
elif cmd == "idBalance":
gva.idBalance(args.pubkey)
elif cmd == "currentUd":
gva.currentUd()
elif cmd == "listWallets":
gva.listWallets(args.brut, args.mbr, args.non_mbr, args.larf)
if keyPath:
os.remove(keyPath)