From 68c39a082dfc544dcd52d40b0f7b680a6223f235 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on ChromeOS)" <rob@synchro.net>
Date: Tue, 19 Nov 2024 22:52:37 -0800
Subject: [PATCH] Save and restore the Telnet BINARY_TX option state separate
 for each direction

It's possible the client requested binary transmit in only one of the two
directions and if so, restore just the direction that was previously *not*
in binary transmit mode to NVT mode.
---
 src/sbbs3/download.cpp | 21 ++++++++++-----------
 src/sbbs3/sbbs.h       |  9 ++-------
 2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/src/sbbs3/download.cpp b/src/sbbs3/download.cpp
index 9c7e4d0d63..9d0c3d9a4d 100644
--- a/src/sbbs3/download.cpp
+++ b/src/sbbs3/download.cpp
@@ -104,25 +104,23 @@ const char* sbbs_t::protcmdline(prot_t* prot, enum XFER_TYPE type)
 	return("invalid transfer type");
 }
 
-bool sbbs_t::data_transfer_begin()
+void sbbs_t::data_transfer_begin(uchar& local_binary_tx, uchar& remote_binary_tx)
 {
 	sys_status|=SS_FILEXFER;	/* disable spy during file xfer */
-	bool telnet_was_nvt = telnet_is_nvt();
+	local_binary_tx = telnet_local_option[TELNET_BINARY_TX];
+	remote_binary_tx = telnet_remote_option[TELNET_BINARY_TX];
 	/* enable telnet binary transmission in both directions */
 	request_telnet_opt(TELNET_DO,TELNET_BINARY_TX);
 	request_telnet_opt(TELNET_WILL,TELNET_BINARY_TX);
 	console |= CON_RAW_IN;
-	return telnet_was_nvt;
 }
 
-void sbbs_t::data_transfer_end(bool telnet_was_nvt)
+void sbbs_t::data_transfer_end(uchar local_binary_tx, uchar remote_binary_tx)
 {
 	sys_status&=~SS_FILEXFER;
-	if(telnet_was_nvt) {
-		/* Got back to Text/NVT mode */
-		request_telnet_opt(TELNET_DONT,TELNET_BINARY_TX);
-		request_telnet_opt(TELNET_WONT,TELNET_BINARY_TX);
-	}
+	/* Got back to Text/NVT mode */
+	request_telnet_opt(local_binary_tx, TELNET_BINARY_TX);
+	request_telnet_opt(remote_binary_tx, TELNET_BINARY_TX);
 	console &= ~CON_RAW_IN;
 }
 
@@ -168,11 +166,12 @@ int sbbs_t::protocol(prot_t* prot, enum XFER_TYPE type
 	cmdline=cmdstr(protcmdline(prot,type), fpath, fspec, NULL, ex_mode);
 	SAFEPRINTF(msg,"Transferring %s",cmdline);
 	spymsg(msg);
-	bool was_nvt_mode = data_transfer_begin();
+	uchar local_binary_tx, remote_binary_tx;
+	data_transfer_begin(local_binary_tx, remote_binary_tx);
 	time_t start = time(NULL);
 	i=external(cmdline,ex_mode,p);
 	time_t end = time(NULL);
-	data_transfer_end(was_nvt_mode);
+	data_transfer_end(local_binary_tx, remote_binary_tx);
 
 	// Save DSZLOG to logfile
 	if((stream=fnopen(NULL,protlog,O_RDONLY))!=NULL) {
diff --git a/src/sbbs3/sbbs.h b/src/sbbs3/sbbs.h
index 7e45978efa..9dfa9a92aa 100644
--- a/src/sbbs3/sbbs.h
+++ b/src/sbbs3/sbbs.h
@@ -539,11 +539,6 @@ public:
 	int 	telnet_rows = 0;
 	int		telnet_cols = 0;
 	int		telnet_speed = 0;
-	bool	telnet_is_nvt() {
-		return !(telnet_mode & TELNET_MODE_OFF)
-			&& telnet_local_option[TELNET_BINARY_TX] != TELNET_DO
-			&& telnet_remote_option[TELNET_BINARY_TX] != TELNET_WILL;
-	}
 
 	xpevent_t	telnet_ack_event;
 
@@ -1174,8 +1169,8 @@ public:
 	bool	bulkupload(int dirnum);
 
 	/* download.cpp */
-	bool	data_transfer_begin(void);
-	void	data_transfer_end(bool was_telnet_nvt_mode);
+	void	data_transfer_begin(uchar& local_binary_tx, uchar& remote_binary_tx);
+	void	data_transfer_end(uchar local_binary_tx, uchar remote_binary_tx);
 	void	downloadedfile(file_t* f);
 	void	notdownloaded(off_t size, time_t elapsed);
 	void	downloadedbytes(off_t size, time_t elapsed);
-- 
GitLab