From e0379ca5af0658f59dbee78b730ebd87c279886d Mon Sep 17 00:00:00 2001 From: Fredrik de Vibe Date: Fri, 18 Mar 2016 15:12:58 -0400 Subject: Enable !users command and add a table formatter * Listen to wildcarded events, currently handle RPL_NAMREPLY. * Add a formatter to present nick lists from IRC as a table. --- README.md | 4 ++++ config.go | 2 ++ matterbridge.conf.sample | 2 ++ matterbridge.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a17f1b5c..688b8b7c 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ channel="#matterbridge" UseSlackCircumfix=false #whether to prefix messages from IRC to mattermost with the sender's nick. Useful if username overrides for incoming webhooks isn't enabled on the mattermost server PrefixMessagesWithNick=false +#how to format the list of IRC nicks when displayed in mattermost. Possible options are "table" and "plain" +NickFormatter=plain +#how many nicks to list per row for formatters that support this +NicksPerRow=4 [mattermost] #url is your incoming webhook url (account settings - integrations - incoming webhooks) diff --git a/config.go b/config.go index d0bf8044..5732d051 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,8 @@ type Config struct { Channel string UseSlackCircumfix bool PrefixMessagesWithNick bool + NicksPerRow int + NickFormatter string } Mattermost struct { URL string diff --git a/matterbridge.conf.sample b/matterbridge.conf.sample index 843402e0..4caf6c86 100644 --- a/matterbridge.conf.sample +++ b/matterbridge.conf.sample @@ -7,6 +7,8 @@ nick="matterbot" channel="#matterbridge" UseSlackCircumfix=false PrefixMessagesWithNick=false +NickFormatter=plain +NicksPerRow=4 [mattermost] url="http://yourdomain/hooks/yourhookkey" diff --git a/matterbridge.go b/matterbridge.go index 4302cf17..d2b996a4 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -58,7 +58,7 @@ func (b *Bridge) createIRC(name string) *irc.Connection { i.AddCallback("JOIN", b.handleJoinPart) i.AddCallback("PART", b.handleJoinPart) } - //i.AddCallback("353", b.handleOther) + i.AddCallback("*", b.handleOther) return i } @@ -76,11 +76,54 @@ func (b *Bridge) handleJoinPart(event *irc.Event) { //b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave") } +func tableformatter (nicks_s string, nicksPerRow int) string { + nicks := strings.Split(nicks_s, " ") + result := "|IRC users" + if nicksPerRow < 1 { + nicksPerRow = 4 + } + for i := 0; i < 2; i++ { + for j := 1; j <= nicksPerRow; j++ { + if i == 0 { + result += "|" + } else { + result += ":-|" + } + } + result += "\r\n|" + } + result += nicks[0] + "|" + for i := 1; i < len(nicks); i++ { + if i % nicksPerRow == 0 { + result += "\r\n|" + nicks[i] + "|" + } else { + result += nicks[i] + "|" + } + } + return result +} + +func plainformatter (nicks string, nicksPerRow int) string { + return nicks + " currently on IRC" +} + +func (b *Bridge) formatnicks (nicks string) string { + switch (b.Config.IRC.NickFormatter) { + case "table": + return tableformatter(nicks, b.Config.IRC.NicksPerRow) + default: + return plainformatter(nicks, b.Config.IRC.NicksPerRow) + } +} + func (b *Bridge) handleOther(event *irc.Event) { switch event.Code { case "353": log.Println("handleOther", b.getMMChannel(event.Arguments[0])) - b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC", b.getMMChannel(event.Arguments[0])) + b.Send(b.Config.IRC.Nick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0])) + break + default: + log.Printf("got unknown event: %+v\n", event); } } @@ -119,9 +162,11 @@ func (b *Bridge) handleMatter() { case "!users": log.Println("received !users from", message.UserName) b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token)) + return case "!gif": message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1))) b.Send(b.Config.IRC.Nick, message.Text, b.getIRCChannel(message.Token)) + return } texts := strings.Split(message.Text, "\n") for _, text := range texts { -- cgit v1.2.3 From a63433e41bad06a9ef8132082b8ef40d91d07aec Mon Sep 17 00:00:00 2001 From: Fredrik de Vibe Date: Fri, 18 Mar 2016 15:54:14 -0400 Subject: Move new config parameters from IRC to Mattermost. PrefixMessagesWithNick, NickFormatter and NicksPerRow. --- README.md | 12 ++++++------ config.go | 32 ++++++++++++++++---------------- matterbridge.conf.sample | 6 +++--- matterbridge.go | 8 ++++---- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 688b8b7c..5875f1ff 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,6 @@ SkipTLSVerify=true nick="matterbot" channel="#matterbridge" UseSlackCircumfix=false -#whether to prefix messages from IRC to mattermost with the sender's nick. Useful if username overrides for incoming webhooks isn't enabled on the mattermost server -PrefixMessagesWithNick=false -#how to format the list of IRC nicks when displayed in mattermost. Possible options are "table" and "plain" -NickFormatter=plain -#how many nicks to list per row for formatters that support this -NicksPerRow=4 [mattermost] #url is your incoming webhook url (account settings - integrations - incoming webhooks) @@ -76,6 +70,12 @@ showjoinpart=true #show irc users joining and parting token=yourtokenfrommattermost #disable certificate checking (selfsigned certificates) #SkipTLSVerify=true +#whether to prefix messages from IRC to mattermost with the sender's nick. Useful if username overrides for incoming webhooks isn't enabled on the mattermost server +PrefixMessagesWithNick=false +#how to format the list of IRC nicks when displayed in mattermost. Possible options are "table" and "plain" +NickFormatter=plain +#how many nicks to list per row for formatters that support this +NicksPerRow=4 #multiple channel config #token you can find in your outgoing webhook diff --git a/config.go b/config.go index 5732d051..e3fba6c9 100644 --- a/config.go +++ b/config.go @@ -8,28 +8,28 @@ import ( type Config struct { IRC struct { - UseTLS bool - SkipTLSVerify bool - Server string + UseTLS bool + SkipTLSVerify bool + Server string + Port int + Nick string + Password string + Channel string + UseSlackCircumfix bool + } + Mattermost struct { + URL string Port int - Nick string - Password string + ShowJoinPart bool + Token string + IconURL string + SkipTLSVerify bool + BindAddress string Channel string - UseSlackCircumfix bool PrefixMessagesWithNick bool NicksPerRow int NickFormatter string } - Mattermost struct { - URL string - Port int - ShowJoinPart bool - Token string - IconURL string - SkipTLSVerify bool - BindAddress string - Channel string - } Token map[string]*struct { IRCChannel string MMChannel string diff --git a/matterbridge.conf.sample b/matterbridge.conf.sample index 4caf6c86..0420a1c7 100644 --- a/matterbridge.conf.sample +++ b/matterbridge.conf.sample @@ -6,9 +6,6 @@ SkipTLSVerify=true nick="matterbot" channel="#matterbridge" UseSlackCircumfix=false -PrefixMessagesWithNick=false -NickFormatter=plain -NicksPerRow=4 [mattermost] url="http://yourdomain/hooks/yourhookkey" @@ -19,6 +16,9 @@ token=yourtokenfrommattermost IconURL="http://youricon.png" #SkipTLSVerify=true #BindAddress="0.0.0.0" +PrefixMessagesWithNick=false +NickFormatter=plain +NicksPerRow=4 [general] GiphyAPIKey=dc6zaTOxFJmzC diff --git a/matterbridge.go b/matterbridge.go index d2b996a4..6638c2c1 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -108,11 +108,11 @@ func plainformatter (nicks string, nicksPerRow int) string { } func (b *Bridge) formatnicks (nicks string) string { - switch (b.Config.IRC.NickFormatter) { + switch (b.Config.Mattermost.NickFormatter) { case "table": - return tableformatter(nicks, b.Config.IRC.NicksPerRow) + return tableformatter(nicks, b.Config.Mattermost.NicksPerRow) default: - return plainformatter(nicks, b.Config.IRC.NicksPerRow) + return plainformatter(nicks, b.Config.Mattermost.NicksPerRow) } } @@ -136,7 +136,7 @@ func (b *Bridge) SendType(nick string, message string, channel string, mtype str matterMessage.Channel = channel matterMessage.UserName = nick matterMessage.Type = mtype - if (b.Config.IRC.PrefixMessagesWithNick) { + if (b.Config.Mattermost.PrefixMessagesWithNick) { matterMessage.Text = nick + ": " + message } else { matterMessage.Text = message -- cgit v1.2.3 From f29822db029ab5838210e00e8b701525c34e8177 Mon Sep 17 00:00:00 2001 From: Fredrik de Vibe Date: Fri, 18 Mar 2016 18:03:15 -0400 Subject: Add double newline if the message is markup and prefixed. If the message is prefixed with the sender nick, it will break markup formatting on the same line. This commit introduces a very rudimentary markup checker, and if the message is deemed to be markup in those cases, the space between sender nick and message is replaced by a double newline. --- matterbridge.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/matterbridge.go b/matterbridge.go index 6638c2c1..7c586b16 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -131,13 +131,32 @@ func (b *Bridge) Send(nick string, message string, channel string) error { return b.SendType(nick, message, channel, "") } +func IsMarkup(message string) bool { + switch (message[0]) { + case '|': fallthrough + case '#': fallthrough + case '_': fallthrough + case '*': fallthrough + case '~': fallthrough + case '-': fallthrough + case ':': fallthrough + case '>': fallthrough + case '=': return true + } + return false +} + func (b *Bridge) SendType(nick string, message string, channel string, mtype string) error { matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL} matterMessage.Channel = channel matterMessage.UserName = nick matterMessage.Type = mtype - if (b.Config.Mattermost.PrefixMessagesWithNick) { - matterMessage.Text = nick + ": " + message + if b.Config.Mattermost.PrefixMessagesWithNick { + if IsMarkup(message) { + matterMessage.Text = nick + ":\n\n" + message + } else { + matterMessage.Text = nick + ": " + message + } } else { matterMessage.Text = message } -- cgit v1.2.3 From a53e6991128f790576a4b5cda519691df1a7c6cc Mon Sep 17 00:00:00 2001 From: Fredrik de Vibe Date: Fri, 18 Mar 2016 18:09:55 -0400 Subject: Remove unnecessary break in switch block. --- matterbridge.go | 1 - 1 file changed, 1 deletion(-) diff --git a/matterbridge.go b/matterbridge.go index 7c586b16..8a0dc766 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -121,7 +121,6 @@ func (b *Bridge) handleOther(event *irc.Event) { case "353": log.Println("handleOther", b.getMMChannel(event.Arguments[0])) b.Send(b.Config.IRC.Nick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0])) - break default: log.Printf("got unknown event: %+v\n", event); } -- cgit v1.2.3 From 3fcce2d8a05d45da9b13c85702ec6b9069585214 Mon Sep 17 00:00:00 2001 From: Fredrik de Vibe Date: Fri, 18 Mar 2016 18:13:17 -0400 Subject: Limit number of columns to number of IRC nicks. Don't add more columns to the IRC nicks table than the total number of nicks in the IRC channel. --- matterbridge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matterbridge.go b/matterbridge.go index 8a0dc766..4ca90dcf 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -83,7 +83,7 @@ func tableformatter (nicks_s string, nicksPerRow int) string { nicksPerRow = 4 } for i := 0; i < 2; i++ { - for j := 1; j <= nicksPerRow; j++ { + for j := 1; j <= nicksPerRow && j <= len(nicks); j++ { if i == 0 { result += "|" } else { -- cgit v1.2.3