Skip to content

Commit

Permalink
reorganized config.toml and updated other code. Also fixed a bug in p…
Browse files Browse the repository at this point in the history
…agination code
  • Loading branch information
rickoooooo committed Dec 22, 2024
1 parent d142acb commit 948f901
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 30 deletions.
22 changes: 15 additions & 7 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

# This is a TOML document

[config]
[main]
activate_keyword = "bbs"
radio_ip = "192.168.70.105"
radio_channel = 1
guestbook_file = "data/guestbook.txt"
log_level = "DEBUG"

[interface_mesh_tcp]
radio_ip = "192.168.70.105"

[interface_mesh_serial]
#serial_device = "/dev/ttyACM0"

[interface_tcp_server]
tcp_server_port = 5050
tcp_server_ip = "127.0.0.1"
#serial_device = "/dev/ttyACM0"

[auth]
database = "user.db"
username_min_length = 5
username_max_length = 30

[database]
filename = "user.db"

[bbs]
database = "bbs.db"

Expand All @@ -27,6 +32,9 @@ border_bottom = "===================="
[sessions]
timeout = 300

[guestbook]
guestbook_file = "data/guestbook.txt"

[[menus]]
name ="Login"
description = "[L]ogin"
Expand Down
3 changes: 2 additions & 1 deletion contexts/cmd_guestbook_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class CmdGuestbookRead(Context):
def __init__(self, session: "UserSession", command: str, description: str):
super().__init__(session, command, description)
self.guestbook_file = config["config"]["guestbook_file"]
self.guestbook_file = config["guestbook"]["guestbook_file"]
self.message.header = "Guestbook - Read"
self.state = STATE_BEGIN

Expand All @@ -29,6 +29,7 @@ def start(self) -> None:
self.send_error("Error: Unable to read guestbook file: " + str(e))
else:
self.message.body = text
self.message.footer = "[q] to quit"
self.session.send_message(self.message)

elif self.state == STATE_READING:
Expand Down
2 changes: 1 addition & 1 deletion contexts/cmd_guestbook_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CmdGuestbookSign(Context):
def __init__(self, session: "UserSession", command: str, description: str):
super().__init__(session, command, description)
self.message.header = "Guestbook - Sign"
self.guestbook_file = config["config"]["guestbook_file"]
self.guestbook_file = config["guestbook"]["guestbook_file"]

'''
Invoked when this Context starts. Send welcome message.
Expand Down
11 changes: 6 additions & 5 deletions contexts/message_pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_current_page(self) -> str:
User wants to see the next page
'''
def next_page(self) -> None:
if self.current_page <= self.total_pages:
if self.current_page < self.total_pages - 1:
self.current_page += 1
page_text = self.messages[self.current_page]
self.message.body = self.get_current_page()
Expand All @@ -79,6 +79,9 @@ def next_page(self) -> None:
#self.session.revert_context()
else:
self.session.send_message(self.message)
else:
self.send_error("No more pages.")
return
'''
Invoked whenever a packet comes into this context. Handles user input.
'''
Expand Down Expand Up @@ -111,12 +114,10 @@ def receive_handler(self, packet: dict) -> None:
return
return
else:
self.message.body = "Error: Invalid page number."
self.session.send_message(self.message)
self.send_error("Error: Invalid page number.")
return

# User entered something that isn't a valid command
else:
self.message.body = "Invalid option!"
self.session.send_message(self.message)
self.send_error("Invalid option!")
return
4 changes: 2 additions & 2 deletions contexts/user_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def receive_handler(self, packet: dict) -> None:
self.send_error("Invalid characters in username! Use [a-z,A-Z,0-9,-,_,.]")
return

username_min_length = config["config"]["username_min_length"]
username_max_length = config["config"]["username_max_length"]
username_min_length = config["auth"]["username_min_length"]
username_max_length = config["auth"]["username_max_length"]
if len(text) < username_min_length or len(text) > 30:
self.send_error(f"Username must be between {username_min_length} and {username_max_length} characters long.")
return
Expand Down
14 changes: 8 additions & 6 deletions interfaces/comm_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
instantiate the new interface.
'''

ACTIVATE_KEYWORD = config["config"]["activate_keyword"]
ACTIVATE_KEYWORD = config["main"]["activate_keyword"]
AUTHORIZED_PORTNUMS = [
"TEXT_MESSAGE_APP", # Meshtastic chat app
"COMM_INTERFACE_MESHTASTIC_TCP" # Custom PORTNUM created in the CommInterfaceTCP object.
Expand Down Expand Up @@ -42,11 +42,13 @@ def on_receive(self, packet: dict, interface) -> None:
if session_db.check_session(user_id):
logger.info("User has session")

# Ensure the incoming packet is addressed to us. If not, just return
bbs_node_id = interface.getMyNodeInfo()["num"]
if packet["to"] != bbs_node_id:
return

# If the interface is None, it's because the connection is not from the Mesh but from TCP or something else
if interface != None:
# Ensure the incoming packet is addressed to us. If not, just return
bbs_node_id = interface.getMyNodeInfo()["num"]
if packet["to"] != bbs_node_id:
return

if "text" in packet["decoded"]:
session = session_db.get_session(user_id)
if session:
Expand Down
4 changes: 2 additions & 2 deletions interfaces/comm_interface_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def tcp_server_start(addr: set) -> None:
server.bind(addr)

server.listen()
logger.info(f"[LISTENING] TCP server is listening on {config['config']['tcp_server_ip']}:{config['config']['tcp_server_port']}")
logger.info(f"[LISTENING] TCP server is listening on {config['interface_tcp_server']['tcp_server_ip']}:{config['interface_tcp_server']['tcp_server_port']}")

while True:
conn, addr = server.accept()
Expand All @@ -93,7 +93,7 @@ def tcp_server_start(addr: set) -> None:

# Get TCP server settings from config.toml.
try:
addr = (config["config"]["tcp_server_ip"], config["config"]["tcp_server_port"])
addr = (config["interface_tcp_server"]["tcp_server_ip"], config["interface_tcp_server"]["tcp_server_port"])
except:
addr = ("", "")
logger.error("Unable to read TCP server settings from config.toml. Continuing without it.")
Expand Down
10 changes: 6 additions & 4 deletions mbbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
interface = None
serial_interface = None

# Meshtastic radio TCP interface
try:
radio_ip = config["config"]["radio_ip"]
radio_channel = config["config"]["radio_channel"]
radio_ip = config["interface_mesh_tcp"]["radio_ip"]
radio_channel = config["main"]["radio_channel"]
except:
logger.error("Unable to read radio's TCP config options from config.toml. Continuing without it.")
else:
interface = CommInterfaceMeshtasticTCP(radio_ip, radio_channel)

# Meshtastic radio serial interface
try:
radio_device = config["config"]["serial_device"]
radio_channel = config["config"]["radio_channel"]
radio_device = config["interface_mesh_serial"]["serial_device"]
radio_channel = config["main"]["radio_channel"]
except:
logger.error("Unable to read serial device path from config.toml. Continuing without it.")
else:
Expand Down
2 changes: 1 addition & 1 deletion utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Global var which can be imported and used to write formatted log messages
logger = logging.getLogger(__name__)

log_level = config["config"]["log_level"]
log_level = config["main"]["log_level"]

match log_level:
case "NOTSET":
Expand Down
2 changes: 1 addition & 1 deletion utils/user_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class UserDB():
def __init__(self):
self.db_file = f"data/{config["database"]["filename"]}"
self.db_file = f"data/{config["auth"]["database"]}"
self.con = sqlite3.connect(self.db_file)
self.cursor = self.con.cursor()
self.initialize_database()
Expand Down

0 comments on commit 948f901

Please sign in to comment.