Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor topic changes into a single place for IRC and Mattermost. #9

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 61 additions & 55 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"net/http"
"regexp"
"strings"
"sync"
"time"

Expand All @@ -22,8 +21,6 @@ import (
const StatusEndPoint = configuration.StatusEndPoint
const BotChannel = configuration.BotChannel

var GPIOMu sync.Mutex

type SWITCHSTATE struct {
ChStop chan struct{}
once sync.Once
Expand Down Expand Up @@ -71,20 +68,8 @@ OuterLoop:
cmnd = "off"
}

// IRC topic
match := regexp.MustCompile("\\|\\| LAB (OPEN|CLOSED) \\|\\|").MatchString(ss.Topic)
if match {
hold := strings.Split(ss.Topic, "||")
topic := fmt.Sprintf("%s|| LAB %s ||%s", hold[0], strStatus, hold[2])
if ss.Topic != topic {
if configuration.TopicUseChanserv {
irccon.Privmsg("ChanServ", fmt.Sprintf("TOPIC %s %s", configuration.BotChannel, topic))
} else {
irccon.SendRawf("TOPIC %s :%s", BotChannel, topic)
}
ss.Topic = topic
}
}
// IRC, Mattermost
ss.UpdateTopic(irccon, nc, regexp.MustCompile(`\|\| LAB (OPEN|CLOSED) \|\|`), strStatus)

// IRC announcement (but not at startup, to avoid spam)
if !first && configuration.TopicSendToChannel {
Expand Down Expand Up @@ -148,60 +133,81 @@ OuterLoop:
resp.Body.Close()
}
}

// Mattermost
if configuration.MattermostServer != "" {
if err := UpdateMattermost(nc, status); err != nil {
log.Printf("Mattermost error: %s\n", err)
} else {
log.Printf("Mattermost updated")
}
}
}
first = false
time.Sleep(time.Second)
}
}
}

func UpdateMattermost(nc *http.Client, status bool) error {
mm := model.NewAPIv4Client(configuration.MattermostServer)
mm.HttpClient = nc
mm.SetToken(configuration.MattermostToken)

channel, resp := mm.GetChannel(configuration.MattermostChannelId, "")
if channel == nil {
return fmt.Errorf("Get channel: %+v", resp)
// UpdateTopic modifies the topic (IRC, Mattermost) by matching `re` and replacing
// the subexpression by `new`. The regexp must have exactly one subexpression.
cpatulea marked this conversation as resolved.
Show resolved Hide resolved
func (ss *SWITCHSTATE) UpdateTopic(irccon *irc.Connection, nc *http.Client, re *regexp.Regexp, new string) {
err := ss.updateTopicIRC(irccon, re, new)
if err != nil {
log.Printf("updateTopicIRC error: %s\n", err)
}

const labOpen = "|| LAB OPEN ||"
const labClosed = "|| LAB CLOSED ||"

var strStatus string
if status {
strStatus = labOpen
} else {
strStatus = labClosed
if configuration.MattermostServer != "" {
cpatulea marked this conversation as resolved.
Show resolved Hide resolved
err = ss.updateTopicMattermost(nc, re, new)
if err != nil {
log.Printf("updateTopicMattermost error: %s\n", err)
}
}
}

var newHeader string
if strings.Contains(channel.Header, labOpen) {
newHeader = strings.ReplaceAll(channel.Header, labOpen, strStatus)
} else if strings.Contains(channel.Header, labClosed) {
newHeader = strings.ReplaceAll(channel.Header, labClosed, strStatus)
func (ss *SWITCHSTATE) updateTopicIRC(irccon *irc.Connection, re *regexp.Regexp, new string) error {
match := re.FindStringSubmatchIndex(ss.Topic)
if len(match) == 4 {
start, end := match[2], match[3]
topic := ss.Topic[:start] + new + ss.Topic[end:]
if ss.Topic != topic {
log.Printf("New IRC topic: %q\n", topic)
if configuration.TopicUseChanserv {
irccon.Privmsg("ChanServ", fmt.Sprintf("TOPIC %s %s", configuration.BotChannel, topic))
} else {
irccon.SendRawf("TOPIC %s :%s", BotChannel, topic)
}
ss.Topic = topic
} else {
log.Printf("IRC topic unchanged")
}
} else {
return fmt.Errorf("Channel header didn't have the key phrase: %q", channel.Header)
return fmt.Errorf("IRC topic %q did not match regexp %q", ss.Topic, re)
}
return nil
}

log.Printf("New Mattermost header: %q", newHeader)
func (ss *SWITCHSTATE) updateTopicMattermost(nc *http.Client, re *regexp.Regexp, new string) error {
mm := model.NewAPIv4Client(configuration.MattermostServer)
mm.HttpClient = nc
mm.SetToken(configuration.MattermostToken)

updated, resp := mm.PatchChannel(channel.Id, &model.ChannelPatch{
Header: &newHeader,
})
if updated == nil {
return fmt.Errorf("Update channel: %+v", resp)
channel, resp := mm.GetChannel(configuration.MattermostChannelId, "")
if channel == nil {
log.Printf("Mattermost error: Get channel: %+v\n", resp)
} else {
match := re.FindStringSubmatchIndex(channel.Header)
if len(match) == 4 {
cpatulea marked this conversation as resolved.
Show resolved Hide resolved
start, end := match[2], match[3]
header := channel.Header[:start] + new + channel.Header[end:]

if header != channel.Header {
log.Printf("New Mattermost header: %q\n", header)

updated, resp := mm.PatchChannel(channel.Id, &model.ChannelPatch{
Header: &header,
})
if updated == nil {
return fmt.Errorf("Patch channel error: %+v", resp)
}
} else {
log.Printf("Mattermost header unchanged\n")
}
} else {
return fmt.Errorf("Mattermost header %q did not match regexp: %q", channel.Header, re)
}
}

return nil
}

Expand Down