commit 3919c3495e68a78293f2783836ff502918f91e07
parent 3feee60a2aaaeb7253c59ea4abdee9a8e73921d4
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Fri, 21 Mar 2025 19:54:23 +0000
MQTT subscriptions are configurable
Diffstat:
M | main.go | | | 24 | +++++++++++++++--------- |
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/main.go b/main.go
@@ -48,6 +48,7 @@ type MqttConfig struct {
Password string
TLS bool
Keepalive int
+ Topics []string
}
type Config struct {
@@ -82,6 +83,9 @@ func readConfig(path string) Config {
SendMid: ": ",
ShowMid: ": ",
},
+ Mqtt: MqttConfig{
+ Topics: []string{"#"},
+ },
}
f, err := os.Open("mqttim.toml")
@@ -184,7 +188,7 @@ func main() {
if err != nil {
log.Fatal("irc.Connect:", err)
}
- go subscribeAll(m, ircQueue)
+ go subscribeAll(m, ircQueue, config.Mqtt.Topics)
go mqttReader(m, l, ircQueue, &config)
go ircSender(&config.Irc, i, ircQueue, cmdQueue)
go mqttKeepalive(m, ircQueue, config.Mqtt.Keepalive)
@@ -329,15 +333,17 @@ func ircSendFilters(config *IrcConfig, i *irc.Connection, f *mqttTopicFilter, bu
ircSendTopicList(config, i, "ignored", f.ignored, buf)
}
-func subscribeAll(m *mqtt.Client, ircQueue chan<- Msg) {
- for {
- err := m.Subscribe(nil, "#")
+func subscribeAll(m *mqtt.Client, ircQueue chan<- Msg, topics []string) {
+ for _, topic := range topics {
+ for {
+ err := m.Subscribe(nil, topic)
- if err != nil {
- ircQueue <- errMsg("Subscribe", err)
- time.Sleep(1 * time.Minute)
- } else {
- return
+ if err != nil {
+ ircQueue <- errMsg("Subscribe", err)
+ time.Sleep(1 * time.Minute)
+ } else {
+ break
+ }
}
}
}