From ca77086526440a723dfd30e78a464c9287fbf500 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Mon, 3 Feb 2025 17:15:53 -0800
Subject: [PATCH] Fix random menu/display file selection when some files can't
 be displayed

When one or more files that match the menu file pattern can't be displayed
(e.g. the user's terminal is not wide enough or there's any other menu file
open/read error), pick another random menu file and try displaying that one,
until either a menu file has been displayed successfully or all files
(matching the pattern) have been attempted.

For example, you have text/menu/random1.c80.msg and random2.msg, if the first
file (random1.c80.msg) is selected at random but the user's terminal isn't
at least 80 columns wide, another file will be picked at random. Since only
random2.msg remains in the list (matching the pattern "random*"), that's the
only file that will always be displayed.
---
 src/sbbs3/prntfile.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/sbbs3/prntfile.cpp b/src/sbbs3/prntfile.cpp
index 93d339a4ff..caf470b4d5 100644
--- a/src/sbbs3/prntfile.cpp
+++ b/src/sbbs3/prntfile.cpp
@@ -388,11 +388,15 @@ bool sbbs_t::random_menu(const char *name, int mode, JSObject* obj)
 	strListDedupe(&names, /* case_sensitive: */ true);
 	bool   result = false;
 	size_t count = strListCount(names);
-	if (count > 0) {
+	while (count > 0) {
 		size_t i = sbbs_random(count);
 		if (menu_exists(names[i], NULL, path)) {
 			result = menu(names[i], mode, obj);
+			if (result == true)
+				break;
 		}
+		strListFastDelete(names, i, 1);
+		--count;
 	}
 	strListFree(&names);
 	return result;
-- 
GitLab