From b169286142c3f3e34fa64eb66f03e7ae46bc5f91 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Tue, 6 Nov 2018 06:06:59 +0000
Subject: [PATCH] Don't generate FTN message-IDs for messages imported via FTN
 that are missing a message-ID (e.g. when exporting from SBBSecho). This
 addresses compliance with this [editorialized] clause in FTS-9:      No
 system      should ever add an MSGID and/or REPLY to,  or modify an existing 
     MSGID / REPLY contained in,  a message not originating on that [FTN]     
 system.

Messages gated from other networks (technically coming from another system,
but originating into an FTN from this system) will still have an FTN Message-ID
generated/added.

Since SBBSecho normally tosses to downlinks directly from packets, this adding
of generated Message-IDs would no normally occur. However, if a downlink
rescanned an area, any messages missing Message-IDs would get them generated
automatically and they would appear to have originating on the local system.
This was never the intention, so this is just a long standing but infrequently
observed (and never reported) bug.
---
 src/sbbs3/msg_id.c    | 17 +++++++++++++----
 src/sbbs3/postmsg.cpp | 10 ++++++----
 src/sbbs3/sbbsecho.c  |  6 +++---
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/sbbs3/msg_id.c b/src/sbbs3/msg_id.c
index 5eb9b9eb17..8d2923d8c1 100644
--- a/src/sbbs3/msg_id.c
+++ b/src/sbbs3/msg_id.c
@@ -70,13 +70,22 @@ static ulong msgid_serialno(smbmsg_t* msg)
 
 /****************************************************************************/
 /* Returns a FidoNet (FTS-9) message-ID										*/
+/* Returns NULL if the message is from FidoNet and doesn't have a MSGID		*/
+/* Pass NULL for msgid if (single-threaded) caller wishes to use static buf	*/
 /****************************************************************************/
 char* DLLCALL ftn_msgid(sub_t *sub, smbmsg_t* msg, char* msgid, size_t maxlen)
 {
-	if(msg->ftn_msgid!=NULL && *msg->ftn_msgid!=0) {
-		strncpy(msgid,msg->ftn_msgid,maxlen);
-		return(msg->ftn_msgid);
+	static char msgidbuf[256];
+	
+	if(msgid == NULL) {
+		msgid = msgidbuf;
+		maxlen = sizeof(msgidbuf);
 	}
+	if(msg->ftn_msgid!=NULL && *msg->ftn_msgid!=0)
+		return msg->ftn_msgid;
+
+	if(msg->from_net.type == NET_FIDO)	// Don't generate a message-ID for imported FTN messages
+		return NULL;
 
 	safe_snprintf(msgid,maxlen
 		,"%lu.%s@%s %08lx"
@@ -86,7 +95,7 @@ char* DLLCALL ftn_msgid(sub_t *sub, smbmsg_t* msg, char* msgid, size_t maxlen)
 		,msgid_serialno(msg)
 		);
 
-	return(msgid);
+	return msgid;
 }
 
 /****************************************************************************/
diff --git a/src/sbbs3/postmsg.cpp b/src/sbbs3/postmsg.cpp
index 99353c1079..acf87ee9ba 100644
--- a/src/sbbs3/postmsg.cpp
+++ b/src/sbbs3/postmsg.cpp
@@ -339,8 +339,9 @@ bool sbbs_t::postmsg(uint subnum, smbmsg_t *remsg, long wm_mode)
 
 	/* Generate FTN (FTS-9) MSGID */
 	if(cfg.sub[subnum]->misc&SUB_FIDO) {
-		ftn_msgid(cfg.sub[subnum],&msg,msg_id,sizeof(msg_id));
-		smb_hfield_str(&msg,FIDOMSGID,msg_id);
+		char* p;
+		if((p = ftn_msgid(cfg.sub[subnum],&msg,msg_id,sizeof(msg_id))) != NULL)
+			smb_hfield_str(&msg, FIDOMSGID, p);
 	}
 
 	/* Generate FidoNet Program Identifier */
@@ -512,8 +513,9 @@ extern "C" int DLLCALL savemsg(scfg_t* cfg, smb_t* smb, smbmsg_t* msg, client_t*
  	/* Generate FidoNet MSGID (for FidoNet sub-boards) */
  	if(smb->subnum!=INVALID_SUB && cfg->sub[smb->subnum]->misc&SUB_FIDO 
 		&& msg->ftn_msgid==NULL) {
- 		ftn_msgid(cfg->sub[smb->subnum],msg,msg_id,sizeof(msg_id));
- 		smb_hfield_str(msg,FIDOMSGID,msg_id);
+		char* p;
+ 		if((p = ftn_msgid(cfg->sub[smb->subnum],msg,msg_id,sizeof(msg_id))) != NULL)
+ 			smb_hfield_str(msg, FIDOMSGID, p);
  	}
 
 	/* Generate FidoNet Program Identifier */
diff --git a/src/sbbs3/sbbsecho.c b/src/sbbs3/sbbsecho.c
index fca70a70fe..7c3a123c3c 100644
--- a/src/sbbs3/sbbsecho.c
+++ b/src/sbbs3/sbbsecho.c
@@ -4593,7 +4593,6 @@ static void write_export_ptr(int subnum, uint32_t ptr, const char* tag)
 void export_echomail(const char* sub_code, const nodecfg_t* nodecfg, bool rescan)
 {
 	char	str[256],tear,cr;
-	char	msgid[256];
 	char*	buf=NULL;
 	char*	minus;
 	char*	fmsgbuf=NULL;
@@ -4796,8 +4795,9 @@ void export_echomail(const char* sub_code, const nodecfg_t* nodecfg, bool rescan
 			if(msg.ftn_flags!=NULL)
 				f+=sprintf(fmsgbuf+f,"\1FLAGS %.256s\r", msg.ftn_flags);
 
-			f+=sprintf(fmsgbuf+f,"\1MSGID: %.256s\r"
-				,ftn_msgid(scfg.sub[subnum],&msg,msgid,sizeof(msgid)));
+			char* p = ftn_msgid(scfg.sub[subnum], &msg, NULL, 0);
+			if(p != NULL)
+				f += sprintf(fmsgbuf+f,"\1MSGID: %.256s\r", p);
 
 			if(msg.ftn_reply!=NULL)			/* use original REPLYID */
 				f+=sprintf(fmsgbuf+f,"\1REPLY: %.256s\r", msg.ftn_reply);
-- 
GitLab