From 12857f64c4061cc80279b0b29b407ef828876249 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Debian Linux)" <rob@synchro.net>
Date: Fri, 20 Sep 2024 21:19:02 -0700
Subject: [PATCH] Add edit option when adding text files that don't already
 exist

Revisiting this script due to usability issues reported by W5jsn (TUCUMCAR).

Dynamically figure out the file path rather than storing another property
('path') in the list objects (which ends up in the .ini file). The absence of
the path property (when the file didn't exist) could result in script-ending
exceptions.

.ini files are supposed to be open for read/write access (even when just
writing), so open the file in "w+t" mode in write_list().

Clear console.aborted flag before menu listings: hitting Ctrl-C in some
point could display just header and prompt of lists with no contents.
---
 exec/text_sec.js | 48 ++++++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/exec/text_sec.js b/exec/text_sec.js
index fa99dd114f..7ea9da6eeb 100644
--- a/exec/text_sec.js
+++ b/exec/text_sec.js
@@ -20,6 +20,16 @@ function txtsec_data(sec)
 	return system.data_dir + "text/" + sec.code.toLowerCase();
 }
 
+function get_fpath(sec, fname)
+{
+	var path = file_getcase(txtsec_data(sec) + "/" + fname);
+	if(!path)
+		path = file_getcase(bbs.cmdstr(fname));
+	if(!path)
+		path = txtsec_data(sec) + "/" + file_getname(fname);
+	return path;
+}
+
 function read_list(sec)
 {
 	var f = new File(txtsec_data(sec) + ".ini");
@@ -27,21 +37,13 @@ function read_list(sec)
 		return [];
 	var list = f.iniGetAllObjects();
 	f.close();
-	for(var i = 0; i < list.length; i++) {
-		var fname = list[i].name;
-		var path = file_getcase(txtsec_data(sec) + "/" + fname);
-		if(!path)
-			path = file_getcase(bbs.cmdstr(fname));
-		if(path)
-			list[i].path = path;
-	}
 	return list;
 }
 
 function write_list(sec, list)
 {
 	var f = new File(txtsec_data(sec) + ".ini");
-	if(!f.open("wt"))
+	if(!f.open("w+t"))
 		return false;
 	f.iniSetAllObjects(list);
 	f.close();
@@ -61,6 +63,7 @@ if(!usrsec.length) {
 }
 bbs.node_action = NODE_RTXT;
 while(bbs.online) {
+	console.aborted = false;
 	if(bbs.menu_exists("text_sec"))
 		bbs.menu("text_sec");
 	else {
@@ -81,6 +84,7 @@ while(bbs.online) {
 		var prev;
 		var list = read_list(usrsec[cursec]);
 		var menu = "text" + (cursec + 1);
+		console.aborted = false;
 		if(bbs.menu_exists(menu))
 			bbs.menu(menu);
 		else {
@@ -119,7 +123,7 @@ while(bbs.online) {
 				for(var f = 0; f < files.length; f++) {
 					var match = false;
 					for(var j = 0; j < list.length && !match; j++) {
-						if(file_getname(list[j].path) == file_getname(files[f]))
+						if(file_getname(list[j].name) == file_getname(files[f]))
 							match = true;
 					}
 					if(!match) {
@@ -135,6 +139,12 @@ while(bbs.online) {
 				var path = fname;
 				if(!file_exists(path))
 					path = backslash(txtsec_data(usrsec[cursec])) + fname;
+				if(!file_exists(path)) {
+					console.print(format(bbs.text(FileDoesNotExist), path));
+					if(confirm("Create it"))
+						if(!(console.editfile(path)))
+							break;
+				}
 				console.printfile(path);
 				console.crlf();
 				console.print(bbs.text(AddTextFileDesc));
@@ -151,11 +161,11 @@ while(bbs.online) {
 				if(i < 1)
 					break;
 				i--;
-				var path = list[i].path;
+				var fpath = get_fpath(usrsec[cursec], list[i].name);
 				list.splice(i, 1);
 				if(write_list(usrsec[cursec], list) == true) {
-					if(file_exists(path) && !console.noyes(format(bbs.text(DeleteTextFileQ), path)))
-						file_remove(path);
+					if(file_exists(fpath) && !console.noyes(format(bbs.text(DeleteTextFileQ), fpath)))
+						file_remove(fpath);
 				}
 				break;
 			case 'E':
@@ -172,9 +182,10 @@ while(bbs.online) {
 						write_list(usrsec[cursec], list);
 					}
 				}
+				var fpath = get_fpath(usrsec[cursec], list[i].name);
 				if(!console.aborted
-					&& !console.noyes("Edit " + file_getname(list[i].path)))
-					console.editfile(list[i].path);
+					&& !console.noyes("Edit " + fpath))
+					console.editfile(fpath);
 				break;
 			default:
 				if(!cmd && typeof(prev) == "number")
@@ -194,11 +205,12 @@ while(bbs.online) {
 						console.putbyte(142);
 					if(console.term_supports(USER_RIP))
 						console.write("\x02|*\r\n");
+					var fpath = get_fpath(usrsec[cursec], list[cmd].name);
 					if(list[cmd].tail)
-						console.printtail(list[cmd].path, list[cmd].tail, mode);
+						console.printtail(fpath, list[cmd].tail, mode);
 					else
-						console.printfile(list[cmd].path, mode);
-					log(LOG_INFO, "read text file: " + list[cmd].path);
+						console.printfile(fpath, mode);
+					log(LOG_INFO, "read text file: " + fpath);
 					console.pause();
 					if(console.term_supports(USER_RIP))
 						console.write("\x02|*\r\n");
-- 
GitLab