From e53aa8a23b61b0fb126c5bf45b6c2ad86b0a1c54 Mon Sep 17 00:00:00 2001 From: poka Date: Wed, 18 Nov 2020 06:38:35 +0100 Subject: [PATCH] Prepare SendToCesium class --- dialog.py | 9 ++-- lib/cesiumMessaging.py | 116 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 114 insertions(+), 11 deletions(-) diff --git a/dialog.py b/dialog.py index d7e6a2b..cf4c291 100755 --- a/dialog.py +++ b/dialog.py @@ -9,17 +9,16 @@ try: except: sys.stderr.write("Please fill the path to your private key (PubSec), and a Cesium ES address in userEnv.py\n") sys.exit(1) -from lib.cesiumMessaging import ReadCesium - +from lib.cesiumMessaging import ReadFromCesium +# Parse arguments parser = argparse.ArgumentParser() parser.add_argument('-n', '--number',type=int, default=3, help="Affiche les NUMBER derniers messages") parser.add_argument('-o', '--outbox', action='store_true', help="Lecture des messages envoyés") args = parser.parse_args() - -messages = ReadCesium(dunikey, pod) +# Build ReadFromCesium object +messages = ReadFromCesium(dunikey, pod) messages.read(args.number, args.outbox) - # print(messages.sendDocument(args.number, args.outbox)) # For debug, print complete JSON answer diff --git a/lib/cesiumMessaging.py b/lib/cesiumMessaging.py index 2a4b3da..84c0e42 100755 --- a/lib/cesiumMessaging.py +++ b/lib/cesiumMessaging.py @@ -6,7 +6,7 @@ from datetime import datetime from termcolor import colored -class ReadCesium: +class ReadFromCesium: def __init__(self, dunikey, pod): # Get my pubkey from my private key try: @@ -61,16 +61,16 @@ class ReadCesium: } # Send JSON document and get JSON result - brut = requests.post('{0}/message/{1}/_search'.format(self.pod, boxType), headers=headers, data=document).json()["hits"] - return brut + result = requests.post('{0}/message/{1}/_search'.format(self.pod, boxType), headers=headers, data=document).json()["hits"] + return result # Parse JSON result and display messages - def readMessages(self, msgJSON): + def readMessages(self, msgJSON, nbrMsg): # Get terminal size rows = int(os.popen('stty size', 'r').read().split()[1]) self.total = msgJSON["total"] - infoTotal = " Nombre de messages: " + str(self.total) + " " + infoTotal = " Nombre de messages: " + str(nbrMsg) + "/" + str(self.total) + " " print(colored(infoTotal.center(rows, '#'), "yellow")) for hits in msgJSON["hits"]: self.idMsg = hits["_id"] @@ -95,5 +95,109 @@ class ReadCesium: def read(self, nbrMsg, outbox): jsonMsg = self.sendDocument(nbrMsg, outbox) - self.readMessages(jsonMsg) + self.readMessages(jsonMsg, nbrMsg) + + + + + + +############################################# + + + + + + + +class SendToCesium: + def __init__(self, dunikey, pod): + # Get my pubkey from my private key + try: + self.dunikey = dunikey + except: + sys.stderr.write("Please fill the path to your private key (PubSec)\n") + sys.exit(1) + + self.issuer = get_privkey(dunikey, "pubsec").pubkey + self.pod = pod + + # Configure JSON document to send + def configDoc(self, nbrMsg, outbox): + if outbox: + boxType = "issuer" + else: + boxType = "recipient" + + return { + "sort": { "time": "desc" }, + "from": 0, + "size": nbrMsg, + "_source":[ + "issuer", + "recipient", + "title", + "content", + "time", + "nonce", + "read_signature" + ],"query":{ + "bool":{ + "filter":{ + "term":{ + boxType: self.issuer + } + } + } + } + } + + + def sendDocument(self, nbrMsg, outbox): + if outbox: + boxType = "outbox" + else: + boxType = "inbox" + + document = json.dumps(self.configDoc(nbrMsg, outbox)) + headers = { + 'Content-type': 'application/json', + } + + # Send JSON document and get JSON result + result = requests.post('{0}/message/{1}/_search'.format(self.pod, boxType), headers=headers, data=document).json()["hits"] + return result + + # Parse JSON result and display messages + def readMessages(self, msgJSON, nbrMsg): + # Get terminal size + rows = int(os.popen('stty size', 'r').read().split()[1]) + + self.total = msgJSON["total"] + infoTotal = " Nombre de messages: " + str(nbrMsg) + "/" + str(self.total) + " " + print(colored(infoTotal.center(rows, '#'), "yellow")) + for hits in msgJSON["hits"]: + self.idMsg = hits["_id"] + msgSrc = hits["_source"] + self.issuer = msgSrc["issuer"] + nonce = msgSrc["nonce"] + nonce = base58.b58decode(nonce) + self.title = base64.b64decode(msgSrc["title"]) + self.title = box_decrypt(self.title, get_privkey(self.dunikey, "pubsec"), self.issuer, nonce).decode() + self.content = base64.b64decode(msgSrc["content"]) + self.content = box_decrypt(self.content, get_privkey(self.dunikey, "pubsec"), self.issuer, nonce).decode() + self.dateS = msgSrc["time"] + date = datetime.fromtimestamp(self.dateS).strftime(", le %d/%m/%Y à %H:%M ") + headerMsg = " De " + self.issuer + date + "(ID: {})".format(self.idMsg) + " " + + print('-'.center(rows, '-')) + print(colored(headerMsg, "blue").center(rows+9, '-')) + print('-'.center(rows, '-')) + print("Objet: " + self.title) + print(self.content) + + + def read(self, nbrMsg, outbox): + jsonMsg = self.sendDocument(nbrMsg, outbox) + self.readMessages(jsonMsg, nbrMsg)