TC2-BBS-mesh/message_processing.py
TC² aa6f70831f Large number of changes
- Changed menu structure to support Web Client
- Added Quick Commands
- Added Sync process for Channel Dir
2024-07-04 23:46:22 -04:00

141 lines
7 KiB
Python

import logging
from meshtastic import BROADCAST_NUM
from command_handlers import (
handle_mail_command, handle_bulletin_command, handle_exit_command,
handle_help_command, handle_stats_command, handle_fortune_command,
handle_bb_steps, handle_mail_steps, handle_stats_steps, handle_wall_of_shame_command,
handle_channel_directory_command, handle_channel_directory_steps, handle_send_mail_command,
handle_read_mail_command, handle_check_mail_command, handle_delete_mail_confirmation, handle_post_bulletin_command,
handle_check_bulletin_command, handle_read_bulletin_command, handle_read_channel_command,
handle_post_channel_command, handle_list_channels_command, handle_quick_help_command
)
from db_operations import add_bulletin, add_mail, delete_bulletin, delete_mail, get_db_connection, add_channel
from utils import get_user_state, get_node_short_name, get_node_id_from_num, send_message
command_handlers = {
"qch": handle_quick_help_command,
"st": handle_stats_command,
"fo": handle_fortune_command,
"ws": handle_wall_of_shame_command,
"exit": handle_exit_command,
"help": handle_help_command
}
def process_message(sender_id, message, interface, is_sync_message=False):
state = get_user_state(sender_id)
message_lower = message.lower()
bbs_nodes = interface.bbs_nodes
if is_sync_message:
if message.startswith("BULLETIN|"):
parts = message.split("|")
board, sender_short_name, subject, content, unique_id = parts[1], parts[2], parts[3], parts[4], parts[5]
add_bulletin(board, sender_short_name, subject, content, [], interface, unique_id=unique_id)
if board.lower() == "urgent":
notification_message = f"💥NEW URGENT BULLETIN💥\nFrom: {sender_short_name}\nTitle: {subject}"
send_message(notification_message, BROADCAST_NUM, interface)
elif message.startswith("MAIL|"):
parts = message.split("|")
sender_id, sender_short_name, recipient_id, subject, content, unique_id = parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]
add_mail(sender_id, sender_short_name, recipient_id, subject, content, [], interface, unique_id=unique_id)
elif message.startswith("DELETE_BULLETIN|"):
unique_id = message.split("|")[1]
delete_bulletin(unique_id, [], interface)
elif message.startswith("DELETE_MAIL|"):
unique_id = message.split("|")[1]
logging.info(f"Processing delete mail with unique_id: {unique_id}")
recipient_id = get_recipient_id_by_mail(unique_id) # Fetch the recipient_id using this helper function
delete_mail(unique_id, recipient_id, [], interface)
elif message.startswith("CHANNEL|"):
parts = message.split("|")
channel_name, channel_url = parts[1], parts[2]
add_channel(channel_name, channel_url)
else:
if message_lower in command_handlers:
command_handlers[message_lower](sender_id, interface)
elif state:
command = state['command']
step = state['step']
if command == 'MAIL':
handle_mail_steps(sender_id, message, step, state, interface, bbs_nodes)
elif command == 'BULLETIN':
handle_bb_steps(sender_id, message, step, state, interface, bbs_nodes)
elif command == 'STATS':
handle_stats_steps(sender_id, message, step, interface, bbs_nodes)
elif command == 'CHANNEL_DIRECTORY':
handle_channel_directory_steps(sender_id, message, step, state, interface)
elif command == 'CHECK_MAIL':
if step == 1:
handle_read_mail_command(sender_id, message, state, interface)
elif step == 2:
handle_delete_mail_confirmation(sender_id, message, state, interface, bbs_nodes)
elif command == 'CHECK_BULLETIN':
if step == 1:
handle_read_bulletin_command(sender_id, message, state, interface)
elif command == 'CHECK_CHANNEL':
if step == 1:
handle_read_channel_command(sender_id, message, state, interface)
elif command == 'LIST_CHANNELS':
if step == 1:
handle_read_channel_command(sender_id, message, state, interface)
elif message.startswith("SM|"):
handle_send_mail_command(sender_id, message, interface, bbs_nodes)
elif message.startswith("CM"):
handle_check_mail_command(sender_id, interface)
elif message.startswith("PB|"):
handle_post_bulletin_command(sender_id, message, interface, bbs_nodes)
elif message.startswith("CB|"):
handle_check_bulletin_command(sender_id, message, interface)
elif message.startswith("CHP|"):
handle_post_channel_command(sender_id, message, interface)
elif message.startswith("CHL"):
handle_list_channels_command(sender_id, interface)
else:
handle_help_command(sender_id, interface)
def on_receive(packet, interface):
try:
if 'decoded' in packet and packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP':
message_bytes = packet['decoded']['payload']
message_string = message_bytes.decode('utf-8')
sender_id = packet['from']
to_id = packet.get('to')
sender_node_id = packet['fromId']
sender_short_name = get_node_short_name(sender_node_id, interface)
receiver_short_name = get_node_short_name(get_node_id_from_num(to_id, interface),
interface) if to_id else "Group Chat"
logging.info(f"Received message from user '{sender_short_name}' to {receiver_short_name}: {message_string}")
bbs_nodes = interface.bbs_nodes
is_sync_message = any(message_string.startswith(prefix) for prefix in
["BULLETIN|", "MAIL|", "DELETE_BULLETIN|", "DELETE_MAIL|"])
if sender_node_id in bbs_nodes:
if is_sync_message:
process_message(sender_id, message_string, interface, is_sync_message=True)
else:
logging.info("Ignoring non-sync message from known BBS node")
elif to_id is not None and to_id != 0 and to_id != 255 and to_id == interface.myInfo.my_node_num:
process_message(sender_id, message_string, interface, is_sync_message=False)
else:
logging.info("Ignoring message sent to group chat or from unknown node")
except KeyError as e:
logging.error(f"Error processing packet: {e}")
def get_recipient_id_by_mail(unique_id):
# Fix for Mail Delete sync issue
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT recipient FROM mail WHERE unique_id = ?", (unique_id,))
result = c.fetchone()
if result:
return result[0]
return None