From 9d897b888d170c24620063b0132c534d357c6a31 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows)" <rob@synchro.net>
Date: Thu, 10 Aug 2023 11:10:48 -0700
Subject: [PATCH] Allow a msg value of "0" to clear/unset the node/+/set/#
 topics/flags

e.g. publishing "0" to node/#/set/intr will clear the node-interrupt flag.
Any non-zero message value will "set" the flag. Same is true for the
lock, down, and rerun topics/flags.

These node attributes (misc) flags could be cleared previously
by setting the 'misc' topic (e.g. to 0), but since that's not an atomic
read/modify/write operation, other set misc flags could be lost clearing flags
in that manner.
---
 src/sbbs3/mqtt.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/sbbs3/mqtt.c b/src/sbbs3/mqtt.c
index 15e14b82b0..fe888f3e54 100644
--- a/src/sbbs3/mqtt.c
+++ b/src/sbbs3/mqtt.c
@@ -466,6 +466,14 @@ static int lprintf(int (*lputs)(int level, const char* str), int level, const ch
 }
 
 #ifdef USE_MOSQUITTO
+
+static ulong mqtt_message_value(const struct mosquitto_message* msg, ulong deflt)
+{
+	if(msg->payloadlen < 1)
+		return deflt;
+	return strtoul(msg->payload, NULL, 0);
+}
+
 static void mqtt_message_received(struct mosquitto* mosq, void* cbdata, const struct mosquitto_message* msg)
 {
 	char topic[128];
@@ -475,31 +483,31 @@ static void mqtt_message_received(struct mosquitto* mosq, void* cbdata, const st
 		bbs_startup_t* bbs_startup = (bbs_startup_t*)mqtt->startup;
 		for(int i = bbs_startup->first_node; i <= bbs_startup->last_node; i++) {
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/status", i)) == 0) {
-				set_node_status(mqtt->cfg, i, strtoul(msg->payload, NULL, 0));
+				set_node_status(mqtt->cfg, i, mqtt_message_value(msg, 0));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/errors", i)) == 0) {
-				set_node_errors(mqtt->cfg, i, strtoul(msg->payload, NULL, 0));
+				set_node_errors(mqtt->cfg, i, mqtt_message_value(msg, 0));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/misc", i)) == 0) {
-				set_node_misc(mqtt->cfg, i, strtoul(msg->payload, NULL, 0));
+				set_node_misc(mqtt->cfg, i, mqtt_message_value(msg, 0));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/lock", i)) == 0) {
-				set_node_lock(mqtt->cfg, i, TRUE);
+				set_node_lock(mqtt->cfg, i, mqtt_message_value(msg, TRUE));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/intr", i)) == 0) {
-				set_node_interrupt(mqtt->cfg, i, TRUE);
+				set_node_interrupt(mqtt->cfg, i, mqtt_message_value(msg, TRUE));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/down", i)) == 0) {
-				set_node_down(mqtt->cfg, i, TRUE);
+				set_node_down(mqtt->cfg, i, mqtt_message_value(msg, TRUE));
 				return;
 			}
 			if(strcmp(msg->topic, mqtt_topic(mqtt, TOPIC_BBS, topic, sizeof(topic), "node/%d/set/rerun", i)) == 0) {
-				set_node_rerun(mqtt->cfg, i, TRUE);
+				set_node_rerun(mqtt->cfg, i, mqtt_message_value(msg, TRUE));
 				return;
 			}
 		}
-- 
GitLab