enhanceNews

returns a random line from the file
This commit is contained in:
SpudGunMan
2024-12-18 10:53:44 -08:00
parent ad11f787de
commit ca033f024e
4 changed files with 18 additions and 6 deletions

View File

@@ -185,6 +185,8 @@ file_path = alert.txt
broadcastCh = 2
enable_read_news = False
news_file_path = news.txt
# only return a single random line from the news file
news_random_line = False
[smtp]
# enable or disable the SMTP module

View File

@@ -3,22 +3,31 @@
from modules.log import *
import asyncio
import random
import os
trap_list_filemon = ("readnews",)
def read_file(file_monitor_file_path):
def read_file(file_monitor_file_path, random_line_only=False):
try:
with open(file_monitor_file_path, 'r') as f:
content = f.read()
return content
if random_line_only:
# read a random line from the file
with open(file_monitor_file_path, 'r') as f:
lines = f.readlines()
return random.choice(lines)
else:
# read the whole file
with open(file_monitor_file_path, 'r') as f:
content = f.read()
return content
except Exception as e:
logger.warning(f"FileMon: Error reading file: {file_monitor_file_path}")
return None
def read_news():
# read the news file on demand
return read_file(news_file_path)
return read_file(news_file_path, read_news_enabled)
def write_news(content, append=False):
# write the news file on demand

View File

@@ -224,6 +224,7 @@ try:
file_monitor_broadcastCh = config['fileMon'].getint('broadcastCh', 2) # default 2
read_news_enabled = config['fileMon'].getboolean('enable_read_news', False) # default disabled
news_file_path = config['fileMon'].get('news_file_path', 'news.txt') # default news.txt
news_random_line_only = config['fileMon'].getboolean('news_random_line', False) # default False
# games
game_hop_limit = config['messagingSettings'].getint('game_hop_limit', 5) # default 3 hops

View File

@@ -190,7 +190,7 @@ if file_monitor_enabled or read_news_enabled:
from modules.filemon import * # from the spudgunman/meshing-around repo
if read_news_enabled:
trap_list = trap_list + trap_list_filemon # items readnews
help_message = help_message + ", readmail"
help_message = help_message + ", readnews"
# clean up the help message
help_message = help_message.split(", ")