Skip to content

Commit

Permalink
Split UpdateTopic into functions for IRC, Mattermost.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpatulea committed Jan 10, 2025
1 parent 8543168 commit ad85c2e
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,20 @@ OuterLoop:
// UpdateTopic modifies the topic (IRC, Mattermost) by matching `re` and replacing
// the subexpression by `new`. The regexp must have exactly one subexpression.
func (ss *SWITCHSTATE) UpdateTopic(irccon *irc.Connection, nc *http.Client, re *regexp.Regexp, new string) {
// IRC
err := ss.updateTopicIRC(irccon, re, new)
if err != nil {
log.Printf("updateTopicIRC error: %s\n", err)
}

if configuration.MattermostServer != "" {
err = ss.updateTopicMattermost(nc, re, new)
if err != nil {
log.Printf("updateTopicMattermost error: %s\n", err)
}
}
}

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]
Expand All @@ -160,41 +173,42 @@ func (ss *SWITCHSTATE) UpdateTopic(irccon *irc.Connection, nc *http.Client, re *
log.Printf("IRC topic unchanged")
}
} else {
log.Printf("IRC topic %q did not match regexp %q\n", ss.Topic, re)
return fmt.Errorf("IRC topic %q did not match regexp %q", ss.Topic, re)
}
return nil
}

// Mattermost
if configuration.MattermostServer != "" {
mm := model.NewAPIv4Client(configuration.MattermostServer)
mm.HttpClient = nc
mm.SetToken(configuration.MattermostToken)
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)

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 {
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 {
log.Printf("Patch channel error: %+v", resp)
}
} else {
log.Printf("Mattermost header unchanged\n")
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 {
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 %q did not match regexp: %q\n", channel.Header, re)
log.Printf("Mattermost header unchanged\n")
}
} else {
return fmt.Errorf("Mattermost header %q did not match regexp: %q", channel.Header, re)
}
}
return nil
}

func (ss SWITCHSTATE) CloseSwitchStatus() {
Expand Down

0 comments on commit ad85c2e

Please sign in to comment.