diff --git a/.gitignore b/.gitignore index e989ec7..fb60d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,8 @@ etc/*.service # Python cache __pycache__/ -# rag data -data/rag/* +# data files +data/*.json # qrz db data/qrz.db @@ -31,6 +31,7 @@ data/inventory.db # fileMonitor test file bee.txt +*news.txt # .csv files *.csv diff --git a/config.template b/config.template index 6ef9c88..5b3aa58 100644 --- a/config.template +++ b/config.template @@ -392,8 +392,10 @@ broadcastCh = 2 # news command will return the contents of a text file enable_read_news = False news_file_path = ../data/news.txt -# only return a single random line from the news file +# only return a single random (head)line from the news file news_random_line = False +# only return news 'block' (seprated by two newlines) randomly (precidence over news_random_line) +news_block_mode = True # enable the use of exernal shell commands, this enables some data in `sysinfo` enable_runShellCmd = False diff --git a/data/README.md b/data/README.md index a83a02e..7c65010 100644 --- a/data/README.md +++ b/data/README.md @@ -1 +1,3 @@ -database admin tool is in [./etc/db_admin.py](../etc/db_admin.py) \ No newline at end of file +database admin tool is in [./etc/db_admin.py](../etc/db_admin.py) +this folder is populated with install.sh +to manually populate ` cp etc/data/* data/. ` \ No newline at end of file diff --git a/data/mesh_news.txt b/data/mesh_news.txt deleted file mode 100644 index 1017712..0000000 --- a/data/mesh_news.txt +++ /dev/null @@ -1 +0,0 @@ -Today in meshtastic you are looking at the coolest bot on the block. \ No newline at end of file diff --git a/data/news.txt b/data/news.txt deleted file mode 100644 index 6f3fc71..0000000 --- a/data/news.txt +++ /dev/null @@ -1 +0,0 @@ -no new news is good news! \ No newline at end of file diff --git a/data/hamradio/extra.json b/etc/data/hamradio/extra.json similarity index 100% rename from data/hamradio/extra.json rename to etc/data/hamradio/extra.json diff --git a/data/hamradio/general.json b/etc/data/hamradio/general.json similarity index 100% rename from data/hamradio/general.json rename to etc/data/hamradio/general.json diff --git a/data/hamradio/technician.json b/etc/data/hamradio/technician.json similarity index 100% rename from data/hamradio/technician.json rename to etc/data/hamradio/technician.json diff --git a/data/surveys/example_survey.json b/etc/data/surveys/example_survey.json similarity index 100% rename from data/surveys/example_survey.json rename to etc/data/surveys/example_survey.json diff --git a/data/surveys/snow_survey.json b/etc/data/surveys/snow_survey.json similarity index 100% rename from data/surveys/snow_survey.json rename to etc/data/surveys/snow_survey.json diff --git a/data/quiz_questions.json b/etc/quiz_questions.json similarity index 100% rename from data/quiz_questions.json rename to etc/quiz_questions.json diff --git a/install.sh b/install.sh index 975167a..a7d1e3e 100755 --- a/install.sh +++ b/install.sh @@ -157,6 +157,13 @@ if [[ ! -f modules/custom_scheduler.py ]]; then printf "\nCustom scheduler template copied to modules/custom_scheduler.py\n" fi +# copy contents of etc/data to data/ +if [[ -d data ]]; then + printf "\nCopying data templates to data/ directory\n" + mkdir -p data + cp etc/data/* data/ +fi + # generate config file, check if it exists if [[ -f config.ini ]]; then printf "\nConfig file already exists, moving to backup config.old\n" @@ -514,4 +521,4 @@ exit 0 # after install shenannigans # add 'bee = True' to config.ini General section. -# wget https://gist.github.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a -O bee.txt +# wget https://gist.githubusercontent.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a/raw/2411e31293a35f3e565f61e7490a806d4720ea7e/bee%2520movie%2520script -O bee.txt diff --git a/mesh_bot.py b/mesh_bot.py index 880cf06..a2260bf 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -483,15 +483,26 @@ def handle_wxalert(message_from_id, deviceID, message): def handleNews(message_from_id, deviceID, message, isDM): news = '' - # if news source is provided pass that to read_news() if "?" in message.lower(): return "returns the news. Add a source e.g. 📰readnews mesh" elif "readnews" in message.lower(): source = message.lower().replace("readnews", "").strip() if source: - news = read_news(source) + # if news source is provided pass that to read_news() + if my_settings.news_block_mode: + news = read_news(source=source, news_block_mode=True) + elif my_settings.news_random_line_only: + news = read_news(source=source, random_line_only=True) + else: + news = read_news(source=source) else: - news = read_news() + # no source provided, use news.txt + if my_settings.news_block_mode: + news = read_news(news_block_mode=True) + elif my_settings.news_random_line_only: + news = read_news(random_line_only=True) + else: + news = read_news() if news: # if not a DM add the username to the beginning of msg diff --git a/modules/filemon.py b/modules/filemon.py index ebd00f0..0c36cac 100644 --- a/modules/filemon.py +++ b/modules/filemon.py @@ -6,6 +6,7 @@ from modules.settings import ( file_monitor_file_path, news_file_path, news_random_line_only, + news_block_mode, allowXcmd, bbs_admin_list, xCmd2factorEnabled, @@ -23,16 +24,34 @@ trap_list_filemon = ("readnews",) NEWS_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data') newsSourcesList = [] -def read_file(file_monitor_file_path, random_line_only=False): +def read_file(file_monitor_file_path, random_line_only=False, news_block_mode=False): + logger.debug(f"FileMon: Reading file: {file_monitor_file_path} options - random_line_only: {random_line_only}, news_block_mode: {news_block_mode}") try: if not os.path.exists(file_monitor_file_path): if file_monitor_file_path == "bee.txt": return "🐝buzz 💐buzz buzz🍯" - if random_line_only: + if news_block_mode: + # read a random block (separated by 2+ blank lines, robust to line endings) + with open(file_monitor_file_path, 'r', encoding='utf-8') as f: + content = f.read().replace('\r\n', '\n').replace('\r', '\n') + blocks = [] + block = [] + for line in content.split('\n'): + if line.strip() == '': + if block: + blocks.append('\n'.join(block).strip()) + block = [] + else: + block.append(line) + if block: + blocks.append('\n'.join(block).strip()) + blocks = [b for b in blocks if b] + return random.choice(blocks) if blocks else None + elif random_line_only: # read a random line from the file with open(file_monitor_file_path, 'r', encoding='utf-8') as f: - lines = f.readlines() - return random.choice(lines) + lines = [line.strip() for line in f if line.strip()] + return random.choice(lines) if lines else None else: # read the whole file with open(file_monitor_file_path, 'r', encoding='utf-8') as f: @@ -42,13 +61,19 @@ def read_file(file_monitor_file_path, random_line_only=False): logger.warning(f"FileMon: Error reading file: {file_monitor_file_path}") return None -def read_news(source=None): +def read_news(source=None, random_line_only=False, news_block_mode=False): # Reads the news file. If a source is provided, reads {source}_news.txt. if source: file_path = os.path.join(NEWS_DATA_DIR, f"{source}_news.txt") else: file_path = os.path.join(NEWS_DATA_DIR, news_file_path) - return read_file(file_path, news_random_line_only) + # Block mode takes precedence over line mode + if news_block_mode: + return read_file(file_path, random_line_only=False, news_block_mode=True) + elif random_line_only: + return read_file(file_path, random_line_only=True, news_block_mode=False) + else: + return read_file(file_path) def write_news(content, append=False): # write the news file on demand diff --git a/modules/settings.py b/modules/settings.py index b78c406..2c1f6cf 100644 --- a/modules/settings.py +++ b/modules/settings.py @@ -457,6 +457,9 @@ try: read_news_enabled = config['fileMon'].getboolean('enable_read_news', False) # default disabled news_file_path = config['fileMon'].get('news_file_path', '../data/news.txt') # default ../data/news.txt news_random_line_only = config['fileMon'].getboolean('news_random_line', False) # default False + news_block_mode = config['fileMon'].getboolean('news_block_mode', False) # default False + if news_random_line_only and news_block_mode: + news_random_line_only = False enable_runShellCmd = config['fileMon'].getboolean('enable_runShellCmd', False) # default False allowXcmd = config['fileMon'].getboolean('allowXcmd', False) # default False xCmd2factorEnabled = config['fileMon'].getboolean('twoFactor_enabled', True) # default True