From fff6de58c57287f7af064e701059242b2d46352b Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Thu, 30 Apr 2020 06:36:33 +0000
Subject: [PATCH] Detect the charset used in the mesage body (ASCII, CP437, or
 UTF-8) and set the FIDOCHARSET header field appropriately. Detect UTF-8 in
 any of the viewable header fields and set the appropriate aux attribute flag.

---
 src/sbbs3/smbutil.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/sbbs3/smbutil.c b/src/sbbs3/smbutil.c
index ce96a805bd..38f3fd1e89 100644
--- a/src/sbbs3/smbutil.c
+++ b/src/sbbs3/smbutil.c
@@ -64,6 +64,7 @@ const char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
 #include <string.h>		/* strrchr */
 #include <ctype.h>		/* toupper */
 
+#include "utf8.h"
 #include "sbbs.h"
 #include "conwrap.h"
 
@@ -207,6 +208,7 @@ void postmsg(char type, char* to, char* to_number, char* to_address,
 	char		buf[1024];
 	uchar*		msgtxt=NULL;
 	uchar*		newtxt;
+	const char* charset = NULL;
 	long		msgtxtlen;
 	int 		i;
 	ushort		agent=AGENT_SMBUTIL;
@@ -240,6 +242,13 @@ void postmsg(char type, char* to, char* to_number, char* to_address,
 		msgtxtlen=lf_expand(msgtxt, newtxt);
 		free(msgtxt);
 		msgtxt=newtxt;
+
+		if(!str_is_ascii(msgtxt) && utf8_str_is_valid(msgtxt))
+			charset = FIDO_CHARSET_UTF8;
+		else if(str_is_ascii(msgtxt))
+			charset = FIDO_CHARSET_ASCII;
+		else
+			charset = FIDO_CHARSET_CP437;
 	}
 
 	memset(&msg,0,sizeof(smbmsg_t));
@@ -292,7 +301,7 @@ void postmsg(char type, char* to, char* to_number, char* to_address,
 					,beep,RECIPIENTNETADDR,i,smb.last_error);
 				bail(1); 
 			}
-		} 
+		}
 	}
 
 	if(from==NULL) {
@@ -351,6 +360,13 @@ void postmsg(char type, char* to, char* to_number, char* to_address,
 	}
 
 	smb_hfield_str(&msg, RFC822MSGID, gen_msgid(&smb, &msg, str, sizeof(str)-1));
+	if(charset != NULL)
+		smb_hfield_str(&msg, FIDOCHARSET, charset);
+
+	if((msg.to != NULL && !str_is_ascii(msg.to) && utf8_str_is_valid(msg.to))
+		|| (msg.from != NULL && !str_is_ascii(msg.from) && utf8_str_is_valid(msg.from))
+		|| (msg.subj != NULL && !str_is_ascii(msg.subj) && utf8_str_is_valid(msg.subj)))
+		msg.hdr.auxattr |= MSG_HFIELDS_UTF8;
 
 	if(mode&NOCRC || smb.status.max_crcs==0)	/* no CRC checking means no body text dupe checking */
 		dupechk_hashes&=~(1<<SMB_HASH_SOURCE_BODY);
-- 
GitLab