2024-06-25 05:50:52 -07:00
|
|
|
import logging
|
|
|
|
|
2024-07-02 05:11:16 -07:00
|
|
|
from meshtastic import BROADCAST_NUM
|
|
|
|
|
2024-06-25 05:50:52 -07:00
|
|
|
from command_handlers import (
|
2024-07-09 08:57:38 -07:00
|
|
|
handle_mail_command, handle_bulletin_command, handle_help_command, handle_stats_command, handle_fortune_command,
|
2024-06-25 05:50:52 -07:00
|
|
|
handle_bb_steps, handle_mail_steps, handle_stats_steps, handle_wall_of_shame_command,
|
2024-07-04 20:46:22 -07:00
|
|
|
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
|
2024-06-25 05:50:52 -07:00
|
|
|
)
|
2024-07-04 20:46:22 -07:00
|
|
|
from db_operations import add_bulletin, add_mail, delete_bulletin, delete_mail, get_db_connection, add_channel
|
2024-07-14 15:52:06 -07:00
|
|
|
from js8call_integration import handle_js8call_command, handle_js8call_steps, handle_group_message_selection
|
2024-06-25 05:50:52 -07:00
|
|
|
from utils import get_user_state, get_node_short_name, get_node_id_from_num, send_message
|
|
|
|
|
2024-07-09 08:57:38 -07:00
|
|
|
main_menu_handlers = {
|
|
|
|
"q": handle_quick_help_command,
|
|
|
|
"b": lambda sender_id, interface: handle_help_command(sender_id, interface, 'bbs'),
|
|
|
|
"u": lambda sender_id, interface: handle_help_command(sender_id, interface, 'utilities'),
|
|
|
|
"x": handle_help_command
|
|
|
|
}
|
|
|
|
|
|
|
|
bbs_menu_handlers = {
|
|
|
|
"m": handle_mail_command,
|
|
|
|
"b": handle_bulletin_command,
|
|
|
|
"c": handle_channel_directory_command,
|
2024-07-14 15:52:06 -07:00
|
|
|
"j": handle_js8call_command,
|
2024-07-09 08:57:38 -07:00
|
|
|
"x": handle_help_command
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utilities_menu_handlers = {
|
|
|
|
"s": handle_stats_command,
|
|
|
|
"f": handle_fortune_command,
|
|
|
|
"w": handle_wall_of_shame_command,
|
|
|
|
"x": handle_help_command
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bulletin_menu_handlers = {
|
|
|
|
"g": lambda sender_id, interface: handle_bb_steps(sender_id, '0', 1, {'board': 'General'}, interface, None),
|
|
|
|
"i": lambda sender_id, interface: handle_bb_steps(sender_id, '1', 1, {'board': 'Info'}, interface, None),
|
|
|
|
"n": lambda sender_id, interface: handle_bb_steps(sender_id, '2', 1, {'board': 'News'}, interface, None),
|
|
|
|
"u": lambda sender_id, interface: handle_bb_steps(sender_id, '3', 1, {'board': 'Urgent'}, interface, None),
|
|
|
|
"x": handle_help_command
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
board_action_handlers = {
|
|
|
|
"r": lambda sender_id, interface, state: handle_bb_steps(sender_id, 'r', 2, state, interface, None),
|
|
|
|
"p": lambda sender_id, interface, state: handle_bb_steps(sender_id, 'p', 2, state, interface, None),
|
|
|
|
"x": handle_help_command
|
2024-06-25 05:50:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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}"
|
2024-07-02 05:11:16 -07:00
|
|
|
send_message(notification_message, BROADCAST_NUM, interface)
|
2024-06-25 05:50:52 -07:00
|
|
|
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}")
|
2024-07-09 08:57:38 -07:00
|
|
|
recipient_id = get_recipient_id_by_mail(unique_id)
|
2024-07-04 07:30:34 -07:00
|
|
|
delete_mail(unique_id, recipient_id, [], interface)
|
2024-07-04 20:46:22 -07:00
|
|
|
elif message.startswith("CHANNEL|"):
|
|
|
|
parts = message.split("|")
|
|
|
|
channel_name, channel_url = parts[1], parts[2]
|
|
|
|
add_channel(channel_name, channel_url)
|
2024-06-25 05:50:52 -07:00
|
|
|
else:
|
2024-07-09 08:57:38 -07:00
|
|
|
if message_lower.startswith("sm,,"):
|
|
|
|
handle_send_mail_command(sender_id, message_lower, interface, bbs_nodes)
|
|
|
|
elif message_lower.startswith("cm"):
|
2024-07-04 20:46:22 -07:00
|
|
|
handle_check_mail_command(sender_id, interface)
|
2024-07-09 08:57:38 -07:00
|
|
|
elif message_lower.startswith("pb,,"):
|
|
|
|
handle_post_bulletin_command(sender_id, message_lower, interface, bbs_nodes)
|
|
|
|
elif message_lower.startswith("cb,,"):
|
|
|
|
handle_check_bulletin_command(sender_id, message_lower, interface)
|
|
|
|
elif message_lower.startswith("chp,,"):
|
|
|
|
handle_post_channel_command(sender_id, message_lower, interface)
|
|
|
|
elif message_lower.startswith("chl"):
|
2024-07-04 20:46:22 -07:00
|
|
|
handle_list_channels_command(sender_id, interface)
|
2024-06-25 05:50:52 -07:00
|
|
|
else:
|
2024-07-09 08:57:38 -07:00
|
|
|
if state and state['command'] == 'MENU':
|
|
|
|
menu_name = state['menu']
|
|
|
|
if menu_name == 'bbs':
|
|
|
|
handlers = bbs_menu_handlers
|
|
|
|
elif menu_name == 'utilities':
|
|
|
|
handlers = utilities_menu_handlers
|
|
|
|
else:
|
|
|
|
handlers = main_menu_handlers
|
|
|
|
elif state and state['command'] == 'BULLETIN_MENU':
|
|
|
|
handlers = bulletin_menu_handlers
|
|
|
|
elif state and state['command'] == 'BULLETIN_ACTION':
|
|
|
|
handlers = board_action_handlers
|
2024-07-14 15:52:06 -07:00
|
|
|
elif state and state['command'] == 'JS8CALL_MENU':
|
|
|
|
handle_js8call_steps(sender_id, message, state['step'], interface, state)
|
|
|
|
return
|
|
|
|
elif state and state['command'] == 'GROUP_MESSAGES':
|
|
|
|
handle_group_message_selection(sender_id, message, state['step'], state, interface)
|
|
|
|
return
|
2024-07-09 08:57:38 -07:00
|
|
|
else:
|
|
|
|
handlers = main_menu_handlers
|
|
|
|
|
|
|
|
if message_lower == 'x':
|
|
|
|
# Reset to main menu state
|
|
|
|
handle_help_command(sender_id, interface)
|
|
|
|
return
|
|
|
|
|
|
|
|
if message_lower in handlers:
|
|
|
|
if state and state['command'] in ['BULLETIN_ACTION', 'BULLETIN_READ', 'BULLETIN_POST', 'BULLETIN_POST_CONTENT']:
|
|
|
|
handlers[message_lower](sender_id, interface, state)
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
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 command == 'BULLETIN_POST':
|
|
|
|
handle_bb_steps(sender_id, message, 4, state, interface, bbs_nodes)
|
|
|
|
elif command == 'BULLETIN_POST_CONTENT':
|
|
|
|
handle_bb_steps(sender_id, message, 5, state, interface, bbs_nodes)
|
|
|
|
elif command == 'BULLETIN_READ':
|
|
|
|
handle_bb_steps(sender_id, message, 3, state, interface, bbs_nodes)
|
2024-07-14 15:52:06 -07:00
|
|
|
elif command == 'JS8CALL_MENU':
|
|
|
|
handle_js8call_steps(sender_id, message, step, interface, state)
|
|
|
|
elif command == 'GROUP_MESSAGES':
|
|
|
|
handle_group_message_selection(sender_id, message, step, state, interface)
|
2024-07-15 07:28:40 -07:00
|
|
|
else:
|
|
|
|
handle_help_command(sender_id, interface)
|
2024-07-09 08:57:38 -07:00
|
|
|
else:
|
|
|
|
handle_help_command(sender_id, interface)
|
2024-06-25 05:50:52 -07:00
|
|
|
|
|
|
|
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}")
|
2024-07-04 07:30:34 -07:00
|
|
|
|
|
|
|
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
|