Files
meshcore-bot/modules/models.py
T
agessaman 0c1aa4fd15 fix(region): never warn the synthetic "Channel User" sender
A channel message with no "Name: " prefix falls back to a stand-in name.
That is not a node: every such message shares the identity, so they would
accumulate a run together and then get a DM addressed to a contact that
does not exist. The hook now passes no sender for those, so they are still
counted but can never earn a warning. The literal is a named constant and a
test pins the hook's guard to it.

Also folds the channel body-budget formula into models.channel_body_limit
rather than letting the web viewer keep a fourth copy of
"max(130, 160 - len(name) - 2)"; BaseCommand and CommandManager now call it
too, and 158 becomes models.DM_BODY_LIMIT.
2026-09-15 22:39:34 -07:00

100 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Data models for the MeshCore Bot
Contains shared data structures used across modules
"""
from dataclasses import dataclass
from typing import Any, Optional
# Firmware reserves extra bytes for regional (non-global) TC_FLOOD scope on channel text.
CHANNEL_REGIONAL_FLOOD_SCOPE_BODY_OVERHEAD = 10
# A DM carries no username prefix, so the whole cipher block is body.
DM_BODY_LIMIT = 158
def channel_body_limit(username: Optional[str]) -> int:
"""Global-scope body budget in UTF-8 bytes for a channel message from ``username``.
Channel messages go out as ``"<username>: <body>"``, so the budget is the
160-byte cipher block minus the name and the ``": "``. Regional scope costs
a further ``CHANNEL_REGIONAL_FLOOD_SCOPE_BODY_OVERHEAD``, which callers
subtract themselves once they know the outgoing scope.
Shared by the command layer and the web viewer, which computes the same
number in a process that has no bot object.
"""
name = str(username or "Bot")
return max(130, 160 - len(name.encode("utf-8")) - 2)
@dataclass
class MeshMessage:
"""Simplified message structure for our bot"""
content: str
sender_id: Optional[str] = None
sender_pubkey: Optional[str] = None
channel: Optional[str] = None
hops: Optional[int] = None
path: Optional[str] = None
is_dm: bool = False
timestamp: Optional[int] = None
snr: Optional[float] = None
rssi: Optional[int] = None
elapsed: Optional[str] = None
# When set from RF routing: path_nodes, path_hex, bytes_per_hop, path_length, route_type, etc.
routing_info: Optional[dict[str, Any]] = None
# Matched flood scope for the reply (e.g. "#west"), None means global flood
reply_scope: Optional[str] = None
# Lowercased content set by base_command.cleanup_message_for_matching
content_lower: str = ""
# Transient flag: True once CommandManager.check_keywords has stripped the
# configured command prefix (and legacy "!") from content. Prevents per-command
# cleanup_message_for_matching from re-stripping/re-rejecting an already-normalized
# message, which previously broke matching for all-but-the-first command.
prefix_normalized: bool = False
# Transient: when not None, CommandManager.send_response appends the reply here
# and transmits nothing. Set by CommandManager.render_command_output so a command
# can be run for its text alone (e.g. a {cmd:...} placeholder in a scheduled
# message) without spending airtime. A synthetic message only.
capture_sink: Optional[list[str]] = None
# On-air body at construction. Mention/prefix cleanup may rewrite ``content``
# for command matching; display and web-viewer capture must use this snapshot.
original_content: str = ""
def __post_init__(self) -> None:
if not self.original_content:
self.original_content = self.content
def effective_outgoing_flood_scope(self, bot: Any) -> str:
"""Resolve outbound flood scope the same way as ``CommandManager.send_channel_message``.
For channel replies: ``reply_scope`` when set, else per-channel
``[Channels] flood_scope.<channel>``, else ``[Channels] outgoing_flood_scope_override``.
Empty string means global flood. DMs return ``""`` (not applicable).
"""
if self.is_dm:
return ""
if self.reply_scope is not None:
return (self.reply_scope or "").strip()
if self.channel and bot.config.has_section("Channels"):
channel_key = self.channel.strip().removeprefix("#").lower()
for key, value in bot.config.items("Channels"):
if not key.startswith("flood_scope."):
continue
configured_channel = key[len("flood_scope."):].strip().removeprefix("#").lower()
if configured_channel == channel_key:
return (value or "").strip()
scope_cfg = ""
if bot.config.has_section("Channels") and bot.config.has_option(
"Channels", "outgoing_flood_scope_override"
):
scope_cfg = (bot.config.get("Channels", "outgoing_flood_scope_override") or "").strip()
return scope_cfg
@staticmethod
def is_global_flood_scope(scope: str) -> bool:
"""Match ``send_channel_message`` global markers (before ``_normalize_scope_name``)."""
return scope in ("", "*", "0", "None") or scope.lower() == "none"