From 4b85f54e77c12669f00983c4311a6ce56c8705bf Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 11:33:19 -0500 Subject: [PATCH 1/6] Fixed shortened prefix formatting issue! --- .gitignore | 1 + main.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 5902cb1..4fb4462 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .vscode config.yaml meshtastic.sqlite +__pycache__ \ No newline at end of file diff --git a/main.py b/main.py index 9c116a1..3df1e3d 100644 --- a/main.py +++ b/main.py @@ -280,12 +280,15 @@ async def on_room_message( if longname and meshnet_name: 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}: " - logger.info(f"Processing message from remote meshnet: {text}") + text = re.sub(rf"^\[{full_display_name}\]: ", "", text) # Remove the original prefix from the text + text = truncate_message(text) + full_message = f"{prefix}{text}" else: - logger.info(f"Processing message from local meshnet: {text}") + logger.info(f"Ignoring message from local meshnet: {text}") return else: display_name_response = await matrix_client.get_displayname( @@ -294,12 +297,9 @@ async def on_room_message( full_display_name = display_name_response.displayname or event.sender short_display_name = full_display_name[:5] prefix = f"{short_display_name}[M]: " - logger.info( - f"Processing matrix message from [{full_display_name}]: {text}" - ) - - text = truncate_message(text) - full_message = f"{prefix}{text}" + logger.info(f"Processing matrix message from [{full_display_name}]: {text}") + text = truncate_message(text) + full_message = f"{prefix}{text}" room_config = None for config in matrix_rooms: @@ -314,8 +314,7 @@ async def on_room_message( logger.info( f"Sending radio message from {full_display_name} to radio broadcast" ) - meshtastic_interface.sendText( - text=full_message, channelIndex=meshtastic_channel + meshtastic_interface.sendText(text=full_message, channelIndex=meshtastic_channel ) else: logger.debug( From 6ad2a855cf54865bc0fd4c9b8e02a35b62690af6 Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 11:36:11 -0500 Subject: [PATCH 2/6] Bump AppVersion=0.3.4 for next release --- mmrelay.iss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmrelay.iss b/mmrelay.iss index 1641212..e3ccffd 100644 --- a/mmrelay.iss +++ b/mmrelay.iss @@ -4,7 +4,7 @@ //WizardSmallImageFile=smallwiz.bmp AppName=Matrix <> Meshtastic Relay -AppVersion=0.3.2 +AppVersion=0.3.4 DefaultDirName={userpf}\MM Relay DefaultGroupName=MM Relay UninstallFilesDir={app} From ee6731c7de99297730c16b9e00ff5918c0ba321d Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 11:38:20 -0500 Subject: [PATCH 3/6] Minor changes --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 3df1e3d..b9fbb33 100644 --- a/main.py +++ b/main.py @@ -288,7 +288,7 @@ async def on_room_message( text = truncate_message(text) full_message = f"{prefix}{text}" else: - logger.info(f"Ignoring message from local meshnet: {text}") + # This is a message from a local user, it should be ignored no log is needed return else: display_name_response = await matrix_client.get_displayname( From 81bd633886e3ab23a8135dd056a7c512fce4b20e Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 13:17:41 -0500 Subject: [PATCH 4/6] lower max_bytes in truncate_message --- main.py | 2 +- mmrelay.iss | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index b9fbb33..5e6f466 100644 --- a/main.py +++ b/main.py @@ -249,7 +249,7 @@ def on_meshtastic_message(packet, loop=None): def truncate_message( - text, max_bytes=234 + text, max_bytes=230 ): # 234 is the maximum that we can run without an error. Trying it for awhile, otherwise lower this to 230 or less. """ Truncate the given text to fit within the specified byte size. diff --git a/mmrelay.iss b/mmrelay.iss index e3ccffd..591b68c 100644 --- a/mmrelay.iss +++ b/mmrelay.iss @@ -4,7 +4,7 @@ //WizardSmallImageFile=smallwiz.bmp AppName=Matrix <> Meshtastic Relay -AppVersion=0.3.4 +AppVersion=0.3.5 DefaultDirName={userpf}\MM Relay DefaultGroupName=MM Relay UninstallFilesDir={app} From 5d078237937130dbbc598f937da86806dd1303be Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 20:03:03 -0500 Subject: [PATCH 5/6] Getting back to core before plugin system --- .gitignore | 2 +- main.py | 2 +- map.py | 41 ----------------------------------------- matrix.py | 36 ------------------------------------ requirements.txt | 3 +-- 5 files changed, 3 insertions(+), 81 deletions(-) delete mode 100644 map.py delete mode 100644 matrix.py diff --git a/.gitignore b/.gitignore index 4fb4462..77228e0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ .vscode config.yaml meshtastic.sqlite -__pycache__ \ No newline at end of file +./plugins/__pycache__ \ No newline at end of file diff --git a/main.py b/main.py index 6bf7922..1f0d104 100644 --- a/main.py +++ b/main.py @@ -429,4 +429,4 @@ async def main(): await asyncio.sleep(60) # Update longnames every 60 seconds -asyncio.run(main()) +asyncio.run(main()) \ No newline at end of file diff --git a/map.py b/map.py deleted file mode 100644 index 62e09f3..0000000 --- a/map.py +++ /dev/null @@ -1,41 +0,0 @@ -import staticmaps # pip install py-staticmaps -import math -import random - - -def anonymize_location(lat, lon, radius=1000): - # Generate random offsets for latitude and longitude - lat_offset = random.uniform(-radius / 111320, radius / 111320) - lon_offset = random.uniform( - -radius / (111320 * math.cos(lat)), radius / (111320 * math.cos(lat)) - ) - - # Apply the offsets to the location coordinates - new_lat = lat + lat_offset - new_lon = lon + lon_offset - - return new_lat, new_lon - - -def get_map(locations, zoom=None, image_size=None, radius=10000): - """ - Anonymize a location to 10km by default - """ - context = staticmaps.Context() - context.set_tile_provider(staticmaps.tile_provider_OSM) - context.set_zoom(zoom) - - for location in locations: - new_location = anonymize_location( - lat=float(location["lat"]), - lon=float(location["lon"]), - radius=radius, - ) - radio = staticmaps.create_latlng(new_location[0], new_location[1]) - context.add_object(staticmaps.Marker(radio, size=10)) - - # render non-anti-aliased png - if image_size: - return context.render_pillow(image_size[0], image_size[1]) - else: - return context.render_pillow(1000, 1000) diff --git a/matrix.py b/matrix.py deleted file mode 100644 index 71462d2..0000000 --- a/matrix.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import io -import aiofiles.os -from PIL import Image -from nio import AsyncClient, UploadResponse -import base64 - - -async def upload_image(client: AsyncClient, image: Image.Image) -> UploadResponse: - buffer = io.BytesIO() - image.save(buffer, format="PNG") - image_data = buffer.getvalue() - - response, maybe_keys = await client.upload( - io.BytesIO(image_data), - content_type="image/png", - filename="location.png", - filesize=len(image_data), - ) - - return response - - -async def send_room_image( - client: AsyncClient, room_id: str, upload_response: UploadResponse -): - response = await client.room_send( - room_id=room_id, - message_type="m.room.message", - content={"msgtype": "m.image", "url": upload_response.content_uri, "body": ""}, - ) - - -async def send_image(client: AsyncClient, room_id: str, image: Image.Image): - response = await upload_image(client=client, image=image) - await send_room_image(client, room_id, upload_response=response) diff --git a/requirements.txt b/requirements.txt index e82b69a..a59a84a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ meshtastic==2.1.6 -matrix-nio==0.20.2 -py-staticmaps==0.4.0 \ No newline at end of file +matrix-nio==0.20.2 \ No newline at end of file From 640aea16dbff7f19e0a29a0f4a7935f1932e6a76 Mon Sep 17 00:00:00 2001 From: Jeremiah K Date: Mon, 24 Apr 2023 20:07:48 -0500 Subject: [PATCH 6/6] Preparing for plugin system --- main.py | 55 ++----------------------------------------------------- 1 file changed, 2 insertions(+), 53 deletions(-) diff --git a/main.py b/main.py index 1f0d104..84650c1 100644 --- a/main.py +++ b/main.py @@ -264,61 +264,10 @@ def truncate_message( # Callback for new messages in Matrix room async def on_room_message( - room: MatrixRoom, event: Union[RoomMessageText, RoomMessageNotice] -) -> None: - if event.server_timestamp < bot_start_time: - return - - body = event.body.strip() - match = re.match(r"^.*: !ping$", body.strip()) - if match: - # Respond to the command - await matrix_client.room_send( - room.room_id, "m.room.message", {"msgtype": "m.text", "body": "pong!"} - ) - return - pattern = r"^.*:(?: !map(?: zoom=(\d+))?(?: size=(\d+),(\d+))?)?$" - match = re.match(pattern, body.strip()) - if match: - zoom = match.group(1) - image_size = match.group(2, 3) - - try: - zoom = int(zoom) - except: - zoom = 8 - - if zoom < 0 or zoom > 30: - zoom = 8 - - try: - image_size = (int(image_size[0]), int(image_size[1])) - except: - image_size = (1000, 1000) - - if image_size[0] > 1000 or image_size[1] > 1000: - image_size = (1000, 1000) - - from map import get_map - - locations = [] - for node, info in meshtastic_interface.nodes.items(): - if "position" in info and "latitude" in info["position"]: - locations.append( - { - "lat": info["position"]["latitude"], - "lon": info["position"]["longitude"], - } - ) - - pillow_image = get_map(locations=locations, zoom=zoom, image_size=image_size) - - from matrix import send_image - - await send_image(matrix_client, room.room_id, pillow_image) - return + room: MatrixRoom, event: Union[RoomMessageText, RoomMessageNotice]) -> None: full_display_name = "Unknown user" + if event.sender != bot_user_id: message_timestamp = event.server_timestamp