diff --git a/onionr/communicator2.py b/onionr/communicator2.py index e97cf242..76359e42 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -177,7 +177,7 @@ class OnionrCommunicatorDaemon: logger.info('Block passed proof, saving.') self._core.setData(content) self._core.addToBlockDB(blockHash, dataSaved=True) - self._core.utils.processBlockMetadata(blockHash) # caches block metadata values to block database + self._core._utils.processBlockMetadata(blockHash) # caches block metadata values to block database else: logger.warn('POW failed for block ' + blockHash) else: diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 651e88d1..077fe302 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' # Misc functions that do not fit in the main api, but are useful -import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math, json +import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math, json, re import nacl.signing, nacl.encoding from onionrblockapi import Block import onionrexceptions @@ -250,9 +250,17 @@ class OnionrUtils: ''' Read metadata from a block and cache it to the block database ''' - myBlock = Block(myBlock, self._core) + myBlock = Block(blockHash, self._core) self._core.updateBlockInfo(blockHash, 'dataType', myBlock.getType()) + def escapeAnsi(self, line): + ''' + Remove ANSI escape codes from a string with regex + + taken or adapted from: https://stackoverflow.com/a/38662876 + ''' + ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]') + return ansi_escape.sub('', line) def getBlockDBHash(self): ''' diff --git a/onionr/static-data/default-plugins/flow/main.py b/onionr/static-data/default-plugins/flow/main.py index 707952d0..acb3e261 100644 --- a/onionr/static-data/default-plugins/flow/main.py +++ b/onionr/static-data/default-plugins/flow/main.py @@ -19,17 +19,54 @@ ''' # Imports some useful libraries -import logger, config +import logger, config, threading, time from onionrblockapi import Block plugin_name = 'flow' class OnionrFlow: def __init__(self): - logger.info("HELLO") + self.myCore = pluginapi.get_core() + self.alreadyOutputed = [] + self.flowRunning = False return + def start(self): + message = "" + self.flowRunning = True + newThread = threading.Thread(target=self.showOutput) + newThread.start() + while self.flowRunning: + try: + message = logger.readline('\nInsert message into flow:').strip().replace('\n', '\\n').replace('\r', '\\r') + except EOFError: + pass + except KeyboardInterrupt: + self.flowRunning = False + if message == "q": + self.flowRunning = False + if len(message) > 0: + self.myCore.insertBlock(message) + + logger.info("Flow is exiting, goodbye") + return + + def showOutput(self): + while self.flowRunning: + for blockHash in self.myCore.getBlocksByType('txt'): + if blockHash in self.alreadyOutputed: + continue + if not self.flowRunning: + break + logger.info('\n------------------------') + block = Block(blockHash, self.myCore) + content = block.getContent() + # Escape new lines, remove trailing whitespace, and escape ansi sequences + content = self.myCore._utils.escapeAnsi(content.replace('\n', '\\n').replace('\r', '\\r').strip()) + logger.info("\n" + block.getDate().strftime("%m/%d %H:%M") + ' - ' + '\033[0;0m' + content) + self.alreadyOutputed.append(blockHash) + time.sleep(5) def on_init(api, data = None): ''' @@ -42,6 +79,7 @@ def on_init(api, data = None): # by simply referencing the variable `pluginapi`. global pluginapi pluginapi = api - api.commands.register(['flow'], OnionrFlow) + flow = OnionrFlow() + api.commands.register(['flow'], flow.start) api.commands.register_help('flow', 'Open the flow messaging interface') return \ No newline at end of file