diff --git a/dialog.py b/dialog.py new file mode 100755 index 0000000..836dbcc --- /dev/null +++ b/dialog.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import argparse, os, sys +from shutil import copyfile +if not os.path.isfile("userEnv.py"): + copyfile("userEnv.py.template", "userEnv.py") +try: + from userEnv import dunikey, pod +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 + + +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) +messages.read(args.number, args.outbox) + +# For debug, print complete JSON answer +# print(messages.sendDocument(args.number, args.outbox)) diff --git a/lib/cesiumMessaging.py b/lib/cesiumMessaging.py new file mode 100755 index 0000000..984739d --- /dev/null +++ b/lib/cesiumMessaging.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 + +import os, sys, requests, json, base58, base64 +from natools import fmt, sign, get_privkey, box_decrypt +from datetime import datetime + + +class ReadCesium: + 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.recipient = 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.recipient + } + } + } + } + } + + + 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 + brut = requests.post('{0}/message/{1}/_search'.format(self.pod, boxType), headers=headers, data=document).json()["hits"] + return brut + + # Parse JSON result and display messages + def readMessages(self, msgJSON): + # Get terminal size + rows = int(os.popen('stty size', 'r').read().split()[1]) + + self.total = msgJSON["total"] + infoTotal = " Nombre de messages: " + str(self.total) + " " + print(infoTotal.center(rows, '#')) + 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(headerMsg.center(rows, '-')) + print('-'.center(rows, '-')) + print("Objet: " + self.title) + print(self.content) + + + def read(self, nbrMsg, outbox): + jsonMsg = self.sendDocument(nbrMsg, outbox) + self.readMessages(jsonMsg) + diff --git a/readmsg.py b/readmsg.py deleted file mode 100755 index 6acaf83..0000000 --- a/readmsg.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python3 - -import os, sys, requests, json, base58, base64 -from userEnv import dunikey, pod -from natools import fmt, sign, get_privkey, box_decrypt -from datetime import datetime - -rows = int(os.popen('stty size', 'r').read().split()[1]) - -#recipient = sys.argv[1] -recipient = get_privkey(dunikey, "pubsec").pubkey -nbrMsg = 5 -boxType = "inbox" - -def configDoc(recipient, nbrMsg): - return { - "sort": { "time": "desc" }, - "from": 0, - "size": nbrMsg, - "_source":[ - "issuer", - "recipient", - "title", - "content", - "time", - "nonce", - "read_signature" - ],"query":{ - "bool":{ - "filter":{ - "term":{ - "recipient": recipient - } - } - } - } -} -document = json.dumps(configDoc(recipient, nbrMsg)) -# print(json.dumps(document)) - -headers = { - 'Content-type': 'application/json', -} -msgJSON = requests.post('{0}/message/{1}/_search'.format(pod, boxType), headers=headers, data=document).json()["hits"] - -total = msgJSON["total"] -for hits in msgJSON["hits"]: - isMsg = hits["_id"] - msgSrc = hits["_source"] - issuer = msgSrc["issuer"] - nonce = msgSrc["nonce"] - nonce = base58.b58decode(nonce) - title = base64.b64decode(msgSrc["title"]) - title = box_decrypt(title, get_privkey(dunikey, "pubsec"), issuer, nonce).decode() - content = base64.b64decode(msgSrc["content"]) - content = box_decrypt(content, get_privkey(dunikey, "pubsec"), issuer, nonce).decode() - - date = datetime.fromtimestamp(msgSrc["time"]).strftime(", le %d/%m/%Y à %H:%M ") - - headerMsg = " De " + issuer + date - print('-'.center(rows, '-')) - print(headerMsg.center(rows, '-')) - print('-'.center(rows, '-')) - print("Objet: " + title) - print(content) \ No newline at end of file