diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 361c3555..a0496644 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -840,6 +840,8 @@ func (m *Mattermost) addParentMsg(parentID string, msg string, newLen int, uncou return strings.TrimRight(msg, "\n") + replyMessage, nil } +var validIRCNickRegExp = regexp.MustCompile("^[a-zA-Z0-9_]*$") + //nolint:funlen,gocognit,gocyclo,cyclop,forcetypeassert func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { var data model.Post @@ -890,8 +892,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { if overrideUsername != "" { logger.Debugf("found override username %s", overrideUsername) // only allow valid irc nicks - re := regexp.MustCompile("^[a-zA-Z0-9_]*$") - if re.MatchString(overrideUsername) { + if validIRCNickRegExp.MatchString(overrideUsername) { ghost.Nick = overrideUsername ghost.Me = false } diff --git a/mm-go-irckit/server_commands.go b/mm-go-irckit/server_commands.go index 2f0c69be..c3b30794 100644 --- a/mm-go-irckit/server_commands.go +++ b/mm-go-irckit/server_commands.go @@ -460,9 +460,10 @@ func CmdPrivMsg(s Server, u *User, msg *irc.Message) error { return s.EncodeMessage(u, irc.ERR_NOSUCHNICK, msg.Params, "No such nick/channel") } +var parseReactionToMsgRegExp = regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`) + func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool { - re := regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseReactionToMsgRegExp.FindStringSubmatch(msg.Trailing) if len(matches) != 4 { return false } @@ -503,9 +504,10 @@ func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool { return true } +var parseModifyMsgRegExp = regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`) + func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool { - re := regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseModifyMsgRegExp.FindStringSubmatch(msg.Trailing) text := msg.Trailing // only two so s/xxx/ which means a delete @@ -576,9 +578,10 @@ func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool { return true } +var parseThreadIDRegExp = regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`) + func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string) { - re := regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`) - matches := re.FindStringSubmatch(msg.Trailing) + matches := parseThreadIDRegExp.FindStringSubmatch(msg.Trailing) if len(matches) == 0 { return "", "" } diff --git a/mm-go-irckit/user.go b/mm-go-irckit/user.go index 239123b7..e834d5ad 100644 --- a/mm-go-irckit/user.go +++ b/mm-go-irckit/user.go @@ -153,6 +153,9 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) { return nil } +var replyRegExp = regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`) +var modifyRegExp = regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`) + // Decode will receive and return a decoded message, or an error. // nolint:funlen,gocognit,gocyclo func (u *User) Decode() { @@ -181,9 +184,7 @@ func (u *User) Decode() { // start timer now t.Reset(time.Duration(bufferTimeout) * time.Millisecond) } else { - replyRe := regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`) - modifyRe := regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`) - if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRe.MatchString(msg.Trailing) || modifyRe.MatchString(msg.Trailing) { + if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRegExp.MatchString(msg.Trailing) || modifyRegExp.MatchString(msg.Trailing) { // flush buffer logger.Debug("flushing buffer because of /me, replies to threads, and message modifications") u.BufferedMsg.Trailing = strings.TrimSpace(u.BufferedMsg.Trailing)