diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 9d9c4d7..f0d528e 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -7,7 +7,7 @@ cli: plugins: sources: - id: trunk - ref: v1.6.4 + ref: v1.6.5 uri: https://github.com/trunk-io/plugins # Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes) runtimes: @@ -17,17 +17,18 @@ runtimes: # This is the section where you manage your linters. (https://docs.trunk.io/check/configuration) lint: enabled: + - taplo@0.9.3 - actionlint@1.7.4 - - bandit@1.7.10 + - bandit@1.8.0 - black@24.10.0 - - checkov@3.2.287 + - checkov@3.2.317 - git-diff-check - isort@5.13.2 - - markdownlint@0.42.0 + - markdownlint@0.43.0 - osv-scanner@1.9.1 - - prettier@3.3.3 - - ruff@0.7.3 - - trufflehog@3.83.6 + - prettier@3.4.1 + - ruff@0.8.0 + - trufflehog@3.84.1 - yamllint@1.35.1 actions: disabled: diff --git a/log_utils.py b/log_utils.py index 4f50746..a66ba46 100644 --- a/log_utils.py +++ b/log_utils.py @@ -1,8 +1,10 @@ import logging import os from logging.handlers import RotatingFileHandler + from config import relay_config + def get_logger(name): logger = logging.getLogger(name=name) log_level = getattr(logging, relay_config["logging"]["level"].upper()) @@ -31,9 +33,15 @@ def get_logger(name): os.makedirs(log_dir, exist_ok=True) # Set up size-based log rotation - max_bytes = relay_config["logging"].get("max_log_size", 10 * 1024 * 1024) # Default 10 MB - backup_count = relay_config["logging"].get("backup_count", 1) # Default to 1 backup - file_handler = RotatingFileHandler(log_file, maxBytes=max_bytes, backupCount=backup_count) + max_bytes = relay_config["logging"].get( + "max_log_size", 10 * 1024 * 1024 + ) # Default 10 MB + backup_count = relay_config["logging"].get( + "backup_count", 1 + ) # Default to 1 backup + file_handler = RotatingFileHandler( + log_file, maxBytes=max_bytes, backupCount=backup_count + ) file_handler.setFormatter( logging.Formatter( diff --git a/meshtastic_utils.py b/meshtastic_utils.py index b742fa6..020857a 100644 --- a/meshtastic_utils.py +++ b/meshtastic_utils.py @@ -152,9 +152,7 @@ def connect_meshtastic(force_connect=False): ) time.sleep(wait_time) else: - logger.error( - f"Could not connect after {retry_limit} attempts: {e}" - ) + logger.error(f"Could not connect after {retry_limit} attempts: {e}") return None return meshtastic_client @@ -191,9 +189,7 @@ def on_lost_meshtastic_connection(interface=None): meshtastic_client = None if event_loop: - reconnect_task = asyncio.run_coroutine_threadsafe( - reconnect(), event_loop - ) + reconnect_task = asyncio.run_coroutine_threadsafe(reconnect(), event_loop) async def reconnect(): @@ -210,9 +206,7 @@ async def reconnect(): ) await asyncio.sleep(backoff_time) if shutting_down: - logger.info( - "Shutdown in progress. Aborting reconnection attempts." - ) + logger.info("Shutdown in progress. Aborting reconnection attempts.") break meshtastic_client = connect_meshtastic(force_connect=True) if meshtastic_client: @@ -247,7 +241,7 @@ def on_meshtastic_message(packet, interface): loop = event_loop - sender = packet.get('fromId') or packet.get('from') + sender = packet.get("fromId") or packet.get("from") decoded = packet.get("decoded", {}) text = decoded.get("text") diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index 4c2756c..c2950fd 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -6,14 +6,15 @@ import s2sphere import staticmaps -from log_utils import get_logger from nio import AsyncClient, UploadResponse from PIL import Image, ImageFont +from log_utils import get_logger from plugins.base_plugin import BasePlugin logger = get_logger(__name__) + class TextLabel(staticmaps.Object): def __init__(self, latlng: s2sphere.LatLng, text: str, fontSize: int = 12) -> None: staticmaps.Object.__init__(self) diff --git a/plugins/ping_plugin.py b/plugins/ping_plugin.py index e0c422e..cdf51ce 100644 --- a/plugins/ping_plugin.py +++ b/plugins/ping_plugin.py @@ -1,32 +1,62 @@ +import re +import string + from plugins.base_plugin import BasePlugin +def match_case(source, target): + return ''.join( + c.upper() if s.isupper() else c.lower() + for s, c in zip(source, target) + ) class Plugin(BasePlugin): plugin_name = "ping" + punctuation = string.punctuation @property def description(self): - return "Check connectivity with the relay" + return "Check connectivity with the relay or respond to pings over the mesh" async def handle_meshtastic_message( self, packet, formatted_message, longname, meshnet_name ): - if ( - "decoded" in packet - and "portnum" in packet["decoded"] - and packet["decoded"]["portnum"] == "TEXT_MESSAGE_APP" - and "text" in packet["decoded"] - ): - message = packet["decoded"]["text"] - message = message.strip() - if f"!{self.plugin_name}" not in message: - return - - from meshtastic_utils import connect_meshtastic - - meshtastic_client = connect_meshtastic() - meshtastic_client.sendText(text="pong!", destinationId=packet["fromId"]) - return True + if "decoded" in packet and "text" in packet["decoded"]: + message = packet["decoded"]["text"].strip() + + # Updated regex to match optional punctuation before and after "ping" + match = re.search( + r"(? 5: + reply_message = "Pong..." + else: + reply_message = pre_punc + base_response + post_punc + + # Send the reply back to the same channel + meshtastic_client.sendText(text=reply_message, channelIndex=channel) + return True def get_matrix_commands(self): return [self.plugin_name] diff --git a/sample_config.yaml b/sample_config.yaml index f3294eb..d2bda75 100644 --- a/sample_config.yaml +++ b/sample_config.yaml @@ -23,15 +23,14 @@ logging: #log_to_file: true # Default is false #filename: logs/mmrelay.log # Default location #max_log_size: 10485760 # 10 MB default if omitted - #backup_count: 3 # Keeps 1 backup as the default if omitted + #backup_count: 1 # Keeps 1 backup as the default if omitted -#Note: Some plugins are experimental and some need maintenance. +#These are core Plugins - Note: Some plugins are experimental and some need maintenance. plugins: health: active: true nodes: active: true - # Other core plugins.. #community-plugins: # Note: Community plugins are a new feature. Please report any issues. # sample_plugin: