diff --git a/status/status.go b/status/status.go index 71f08c3..2e6fa89 100644 --- a/status/status.go +++ b/status/status.go @@ -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] @@ -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() {