feat: Add escape sequence decoding for greeting messages

- Implemented `_decode_escape_sequences` method in `GreeterCommand` to process escape sequences like `\\n`, `\\t`, and `\\r` in greeting messages.
- Updated `_load_config` to decode escape sequences in `greeting_message` and `mesh_info_format`, enhancing user flexibility in message formatting.
- Added decoding for per-channel greetings to ensure consistent message formatting across different channels.
This commit is contained in:
agessaman
2026-01-12 15:51:26 -08:00
parent 8bef8ed485
commit 01dc2679fd
+29
View File
@@ -119,16 +119,43 @@ class GreeterCommand(BaseCommand):
import traceback
self.logger.error(traceback.format_exc())
def _decode_escape_sequences(self, text: str) -> str:
"""Decode escape sequences in config strings.
Processes common escape sequences like \\n (newline), \\t (tab), \\\\ (backslash).
This allows users to add newlines in greeting messages using \\n.
Args:
text: The text string to process.
Returns:
str: The text with escape sequences decoded.
"""
# Replace escape sequences
# Order matters: \\ must be processed first to avoid double-processing
text = text.replace('\\\\', '\x00') # Temporary placeholder for backslash
text = text.replace('\\n', '\n') # Newline
text = text.replace('\\t', '\t') # Tab
text = text.replace('\\r', '\r') # Carriage return
text = text.replace('\x00', '\\') # Restore backslash
return text
def _load_config(self) -> None:
"""Load configuration for greeter command."""
self.enabled = self.get_config_value('Greeter_Command', 'enabled', fallback=False, value_type='bool')
self.greeting_message = self.get_config_value('Greeter_Command', 'greeting_message',
fallback='Welcome to the mesh, {sender}!')
# Decode escape sequences (e.g., \\n for newlines)
self.greeting_message = self._decode_escape_sequences(self.greeting_message)
self.rollout_days = self.get_config_value('Greeter_Command', 'rollout_days', fallback=7, value_type='int')
self.include_mesh_info = self.get_config_value('Greeter_Command', 'include_mesh_info',
fallback=True, value_type='bool')
self.mesh_info_format = self.get_config_value('Greeter_Command', 'mesh_info_format',
fallback='\n\nMesh Info: {total_contacts} contacts, {repeaters} repeaters')
# Decode escape sequences (e.g., \\n for newlines)
self.mesh_info_format = self._decode_escape_sequences(self.mesh_info_format)
self.per_channel_greetings = self.get_config_value('Greeter_Command', 'per_channel_greetings',
fallback=False, value_type='bool')
self.auto_backfill = self.get_config_value('Greeter_Command', 'auto_backfill',
@@ -163,6 +190,8 @@ class GreeterCommand(BaseCommand):
channel_name, greeting = entry.split(':', 1)
channel_name = channel_name.strip()
greeting = greeting.strip()
# Decode escape sequences (e.g., \\n for newlines)
greeting = self._decode_escape_sequences(greeting)
# Store both original and lowercase channel name for case-insensitive matching
self.channel_greetings[channel_name.lower()] = {
'channel': channel_name,