From de93de09a2d9ac9972b5ea2c986fc1eed2452737 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 17:00:06 -0500 Subject: [PATCH 1/9] First work for shortnames --- db_utils.py | 30 +++++++++++++++++++++++++++++- matrix_utils.py | 10 +++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/db_utils.py b/db_utils.py index 4c475c2..3e2c74e 100644 --- a/db_utils.py +++ b/db_utils.py @@ -9,6 +9,9 @@ def initialize_database(): cursor.execute( "CREATE TABLE IF NOT EXISTS longnames (meshtastic_id TEXT PRIMARY KEY, longname TEXT)" ) + cursor.execute( + "CREATE TABLE IF NOT EXISTS shortnames (meshtastic_id TEXT PRIMARY KEY, shortname TEXT)" + ) cursor.execute( "CREATE TABLE IF NOT EXISTS plugin_data (plugin_name TEXT, meshtastic_id TEXT, data TEXT, PRIMARY KEY (plugin_name, meshtastic_id))" ) @@ -81,7 +84,6 @@ def save_longname(meshtastic_id, longname): ) conn.commit() - def update_longnames(nodes): if nodes: for node in nodes.values(): @@ -90,3 +92,29 @@ def update_longnames(nodes): meshtastic_id = user["id"] longname = user.get("longName", "N/A") save_longname(meshtastic_id, longname) + +def get_shortname(meshtastic_id): + with sqlite3.connect("meshtastic.sqlite") as conn: + cursor = conn.cursor() + cursor.execute( + "SELECT shortname FROM shortnames WHERE meshtastic_id=?", (meshtastic_id,)) + result = cursor.fetchone() + return result[0] if result else None + +def save_shortname(meshtastic_id, shortname): + with sqlite3.connect("meshtastic.sqlite") as conn: + cursor = conn.cursor() + cursor.execute( + "INSERT OR REPLACE INTO shortnames (meshtastic_id, shortname) VALUES (?, ?)", + (meshtastic_id, shortname), + ) + conn.commit() + +def update_shortnames(): + if meshtastic_interface.nodes: + for node in meshtastic_interface.nodes.values(): + user = node.get("user") + if user: + meshtastic_id = user["id"] + shortname = user.get("shortName", "N/A") + save_shortname(meshtastic_id, shortname) \ No newline at end of file diff --git a/matrix_utils.py b/matrix_utils.py index 52d77e6..524f403 100644 --- a/matrix_utils.py +++ b/matrix_utils.py @@ -93,13 +93,14 @@ async def join_matrix_room(matrix_client, room_id_or_alias: str) -> None: # Send message to the Matrix room -async def matrix_relay(room_id, message, longname, meshnet_name): +async def matrix_relay(room_id, message, longname, shortname, meshnet_name): matrix_client = await connect_matrix() try: content = { "msgtype": "m.text", "body": message, "meshtastic_longname": longname, + "meshtastic_shortname": shortname, "meshtastic_meshnet": meshnet_name, } await asyncio.wait_for( @@ -156,6 +157,7 @@ async def on_room_message( text = event.body.strip() longname = event.source["content"].get("meshtastic_longname") + shortname = event.source["content"].get("meshtastic_shortname", None) meshnet_name = event.source["content"].get("meshtastic_meshnet") suppress = event.source["content"].get("mmrelay_suppress") local_meshnet_name = relay_config["meshtastic"]["meshnet_name"] @@ -168,9 +170,11 @@ async def on_room_message( full_display_name = f"{longname}/{meshnet_name}" if meshnet_name != local_meshnet_name: logger.info(f"Processing message from remote meshnet: {text}") - short_longname = longname[:3] short_meshnet_name = meshnet_name[:4] - prefix = f"{short_longname}/{short_meshnet_name}: " + # If shortname is None, truncate the longname to 3 characters + if shortname is None: + shortname = longname[:3] + prefix = f"{shortname}/{short_meshnet_name}: " text = re.sub( rf"^\[{full_display_name}\]: ", "", text ) # Remove the original prefix from the text From 9ac5b6a2db4387a61ed0c26084f4659b7da2457c Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 17:18:12 -0500 Subject: [PATCH 2/9] More shortname work --- db_utils.py | 2 +- main.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/db_utils.py b/db_utils.py index 3e2c74e..1a8e532 100644 --- a/db_utils.py +++ b/db_utils.py @@ -110,7 +110,7 @@ def save_shortname(meshtastic_id, shortname): ) conn.commit() -def update_shortnames(): +def update_shortnames(nodes): if meshtastic_interface.nodes: for node in meshtastic_interface.nodes.values(): user = node.get("user") diff --git a/main.py b/main.py index a4d73a2..95c664a 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from nio import ( ) from pubsub import pub from typing import List -from db_utils import initialize_database, update_longnames +from db_utils import initialize_database, update_longnames, update_shortnames from matrix_utils import ( connect_matrix, join_matrix_room, @@ -72,6 +72,7 @@ async def main(): try: # Update longnames update_longnames(meshtastic_interface.nodes) + update_shortnames(meshtastic_interface.nodes) matrix_logger.info("Syncing with server...") await matrix_client.sync_forever(timeout=30000) From 871b1dca659dfa80ff4e5884b560791b7c71e4c9 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 17:26:32 -0500 Subject: [PATCH 3/9] Update Comments --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 95c664a..0519cb4 100644 --- a/main.py +++ b/main.py @@ -70,7 +70,7 @@ async def main(): # Start the Matrix client while True: try: - # Update longnames + # Update longnames & shortnames update_longnames(meshtastic_interface.nodes) update_shortnames(meshtastic_interface.nodes) @@ -80,7 +80,7 @@ async def main(): except Exception as e: matrix_logger.error(f"Error syncing with server: {e}") - await asyncio.sleep(60) # Update longnames every 60 seconds + await asyncio.sleep(60) # Update longnames & shortnames every 60 seconds asyncio.run(main()) From df0fc4eb7f546b8c55a287397c7aaca2304c0403 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 17:41:03 -0500 Subject: [PATCH 4/9] Shortname work --- db_utils.py | 2 +- main.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/db_utils.py b/db_utils.py index 1a8e532..fc0c236 100644 --- a/db_utils.py +++ b/db_utils.py @@ -110,7 +110,7 @@ def save_shortname(meshtastic_id, shortname): ) conn.commit() -def update_shortnames(nodes): +def update_shortname(nodes): if meshtastic_interface.nodes: for node in meshtastic_interface.nodes.values(): user = node.get("user") diff --git a/main.py b/main.py index 0519cb4..af1b2b7 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from nio import ( ) from pubsub import pub from typing import List -from db_utils import initialize_database, update_longnames, update_shortnames +from db_utils import initialize_database, update_longnames, update_shortname from matrix_utils import ( connect_matrix, join_matrix_room, @@ -28,6 +28,7 @@ from meshtastic_utils import ( logger = get_logger(name="M<>M Relay") meshtastic_interface = connect_meshtastic() +logger.debug(f"meshtastic_interface: {meshtastic_interface}") matrix_rooms: List[dict] = relay_config["matrix_rooms"] matrix_access_token = relay_config["matrix"]["access_token"] @@ -72,7 +73,7 @@ async def main(): try: # Update longnames & shortnames update_longnames(meshtastic_interface.nodes) - update_shortnames(meshtastic_interface.nodes) + update_shortname(meshtastic_interface.nodes) matrix_logger.info("Syncing with server...") await matrix_client.sync_forever(timeout=30000) From ddd0b166a1784f0c58dff76ec914728f66311d02 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 22:00:02 -0500 Subject: [PATCH 5/9] Shortname work --- meshtastic_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/meshtastic_utils.py b/meshtastic_utils.py index 2576fec..533565b 100644 --- a/meshtastic_utils.py +++ b/meshtastic_utils.py @@ -149,6 +149,7 @@ def on_meshtastic_message(packet, loop=None): room["id"], formatted_message, longname, + shortname, meshnet_name, ), loop=loop, From f5450e24f49f193f4b2114edddc5f805592a08f0 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 22:04:59 -0500 Subject: [PATCH 6/9] Getting closer! --- meshtastic_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meshtastic_utils.py b/meshtastic_utils.py index 533565b..afdacaa 100644 --- a/meshtastic_utils.py +++ b/meshtastic_utils.py @@ -6,7 +6,7 @@ from typing import List from config import relay_config from log_utils import get_logger -from db_utils import get_longname +from db_utils import get_longname, get_shortname from plugin_loader import load_plugins matrix_rooms: List[dict] = relay_config["matrix_rooms"] @@ -115,6 +115,7 @@ def on_meshtastic_message(packet, loop=None): ) longname = get_longname(sender) or sender + shortname = get_shortname(sender) or sender meshnet_name = relay_config["meshtastic"]["meshnet_name"] formatted_message = f"[{longname}/{meshnet_name}]: {text}" From 471e63c3f687a2208bbc3ffe9b411168d7002da7 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 22:09:37 -0500 Subject: [PATCH 7/9] Finishing shortname work --- db_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db_utils.py b/db_utils.py index fc0c236..ead6685 100644 --- a/db_utils.py +++ b/db_utils.py @@ -111,8 +111,8 @@ def save_shortname(meshtastic_id, shortname): conn.commit() def update_shortname(nodes): - if meshtastic_interface.nodes: - for node in meshtastic_interface.nodes.values(): + if nodes: + for node in nodes.values(): user = node.get("user") if user: meshtastic_id = user["id"] From ee1657a5b53e9ebf540d024dadc2bfb9c0c4b56a Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 22:37:55 -0500 Subject: [PATCH 8/9] Making new function names uniform --- db_utils.py | 2 +- main.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db_utils.py b/db_utils.py index ead6685..713a9e7 100644 --- a/db_utils.py +++ b/db_utils.py @@ -110,7 +110,7 @@ def save_shortname(meshtastic_id, shortname): ) conn.commit() -def update_shortname(nodes): +def update_shortnames(nodes): if nodes: for node in nodes.values(): user = node.get("user") diff --git a/main.py b/main.py index af1b2b7..6b976b5 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from nio import ( ) from pubsub import pub from typing import List -from db_utils import initialize_database, update_longnames, update_shortname +from db_utils import initialize_database, update_longnames, update_shortnames from matrix_utils import ( connect_matrix, join_matrix_room, @@ -73,7 +73,7 @@ async def main(): try: # Update longnames & shortnames update_longnames(meshtastic_interface.nodes) - update_shortname(meshtastic_interface.nodes) + update_shortnames(meshtastic_interface.nodes) matrix_logger.info("Syncing with server...") await matrix_client.sync_forever(timeout=30000) From 3f1e6a081ec6faa4b30bfa6e57114bc1d5fb80bf Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Fri, 9 Jun 2023 12:17:43 -0500 Subject: [PATCH 9/9] Cleanup --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index 6b976b5..0519cb4 100644 --- a/main.py +++ b/main.py @@ -28,7 +28,6 @@ from meshtastic_utils import ( logger = get_logger(name="M<>M Relay") meshtastic_interface = connect_meshtastic() -logger.debug(f"meshtastic_interface: {meshtastic_interface}") matrix_rooms: List[dict] = relay_config["matrix_rooms"] matrix_access_token = relay_config["matrix"]["access_token"]