diff --git a/modules/bbstools.py b/modules/bbstools.py index 60a917f..fe62afa 100644 --- a/modules/bbstools.py +++ b/modules/bbstools.py @@ -5,12 +5,19 @@ import pickle # pip install pickle from modules.log import * import time +useSynchCompression = False + +if useSynchCompression: + import zlib + from modules.system import send_raw_bytes + trap_list_bbs = ("bbslist", "bbspost", "bbsread", "bbsdelete", "bbshelp", "bbsinfo", "bbslink", "bbsack") # global message list, later we will use a pickle on disk bbs_messages = [] bbs_dm = [] + def load_bbsdb(): global bbs_messages # load the bbs messages from the database file @@ -201,6 +208,32 @@ def bbs_delete_dm(toNode, message): return "System: cleared mail for" + str(toNode) return "System: No DM found for node " + str(toNode) +def compress_data(data_to_compress): + # Prepare message as bytes + compressed = zlib.compress(data_to_compress.encode('utf-8')) + return compressed + +def decompress_data(data_bytes): + try: + decompressed = zlib.decompress(data_bytes) + msg = decompressed.decode('utf-8') + return msg + except Exception as e: + logger.warning(f"Error decompressing data: {e}") + return False + +def bbs_receive_compressed(data_bytes, fromNode, RxNode): + try: + decompressed = zlib.decompress(data_bytes) + msg = decompressed.decode('utf-8') + + bbs_sync_posts(msg, fromNode, RxNode) + + return msg + except Exception as e: + logger.error(f"Error decompressing BBS message: {e}") + return None + def bbs_sync_posts(input, peerNode, RxNode): messageID = 0 @@ -245,7 +278,13 @@ def bbs_sync_posts(input, peerNode, RxNode): if messageID % 5 == 0: time.sleep(10 + responseDelay) logger.debug(f"System: Sending bbslink message {messageID} of {len(bbs_messages)} to peer " + str(peerNode)) - return f"bbslink {messageID} ${bbs_messages[messageID][1]} #{bbs_messages[messageID][2]} @{fromNodeHex}" + msg = f"bbslink {messageID} ${bbs_messages[messageID][1]} #{bbs_messages[messageID][2]} @{fromNodeHex}" + if useSynchCompression: + compressed = compress_data(msg) + send_raw_bytes(peerNode, compressed) + logger.debug("System: Sent compressed bbslink message to peer " + str(peerNode)) + else: + return msg else: logger.debug("System: bbslink sync complete with peer " + str(peerNode)) diff --git a/modules/system.py b/modules/system.py index a297621..5b4c015 100644 --- a/modules/system.py +++ b/modules/system.py @@ -826,6 +826,16 @@ def send_raw_bytes(nodeid, raw_bytes, nodeInt=1, channel=0, portnum=256, want_a logger.error(f"System: Error sending raw bytes to {nodeid} via Device{nodeInt}: {e} bytes: {raw_bytes}") return False +def decode_raw_bytes(raw_bytes): + # Decode raw bytes received from a Meshtastic device. + try: + decoded_message = raw_bytes.decode('utf-8', errors='ignore') + logger.debug(f"Decoded raw bytes: {decoded_message}") + return decoded_message + except Exception as e: + logger.debug(f"System: Error decoding raw bytes: {e} bytes: {raw_bytes}") + return "" + def messageTrap(msg): # Check if the message contains a trap word, this is the first filter for listning to messages # after this the message is passed to the command_handler in the bot.py which is switch case filter for applying word to function