From f5659d455d2c28a6f2fe4c40f4dc344419ff523a Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Mon, 26 Nov 2018 17:47:04 +0800 Subject: Sync channel topics between Slack bridges (#585) Added logic to allow for configurable synchronisation of topics and purposes of channels between Slack bridges. --- bridge/slack/helpers.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'bridge/slack/helpers.go') diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index b0fdaba1..39fbcea7 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -262,12 +262,28 @@ func (b *Bslack) populateMessageWithBotInfo(ev *slack.MessageEvent, rmsg *config } var ( - mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`) - channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`) - variableRE = regexp.MustCompile(``) - urlRE = regexp.MustCompile(`<(.*?)(\|.*?)?>`) + mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`) + channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`) + variableRE = regexp.MustCompile(``) + urlRE = regexp.MustCompile(`<(.*?)(\|.*?)?>`) + topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`) ) +func (b *Bslack) extractTopicOrPurpose(text string) (string, string) { + r := topicOrPurposeRE.FindStringSubmatch(text) + if len(r) == 5 { + action, updateType, extracted := r[2], r[3], r[4] + switch action { + case "set": + return updateType, extracted + case "cleared": + return updateType, "" + } + } + b.Log.Warnf("Encountered channel topic or purpose change message with unexpected format: %s", text) + return "unknown", "" +} + // @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users func (b *Bslack) replaceMention(text string) string { replaceFunc := func(match string) string { -- cgit v1.2.3