diff options
author | @42wim <wim@42.be> | 2016-03-18 23:48:05 +0100 |
---|---|---|
committer | @42wim <wim@42.be> | 2016-03-18 23:48:05 +0100 |
commit | 4984473c1bf48bbf1e530eaecba841baca2fe3e6 (patch) | |
tree | 30a1cab9fd4ca8fbe1035f31f4d6e336b22c086e | |
parent | 4759ee6132e1a8ec1274870b5cf20ae36d02996c (diff) | |
parent | 3fcce2d8a05d45da9b13c85702ec6b9069585214 (diff) | |
download | matterbridge-msglm-4984473c1bf48bbf1e530eaecba841baca2fe3e6.tar.gz matterbridge-msglm-4984473c1bf48bbf1e530eaecba841baca2fe3e6.tar.bz2 matterbridge-msglm-4984473c1bf48bbf1e530eaecba841baca2fe3e6.zip |
Merge pull request #14 from fdevibe/enable-users-2
Enable !users command and add a table formatter
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | config.go | 34 | ||||
-rw-r--r-- | matterbridge.conf.sample | 4 | ||||
-rw-r--r-- | matterbridge.go | 71 |
4 files changed, 94 insertions, 23 deletions
@@ -56,8 +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 [mattermost] #url is your incoming webhook url (account settings - integrations - incoming webhooks) @@ -72,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 @@ -8,25 +8,27 @@ 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 - } - Mattermost struct { - URL string - Port int - ShowJoinPart bool - Token string - IconURL string - SkipTLSVerify bool - BindAddress string - Channel string + NicksPerRow int + NickFormatter string } Token map[string]*struct { IRCChannel string diff --git a/matterbridge.conf.sample b/matterbridge.conf.sample index 843402e0..0420a1c7 100644 --- a/matterbridge.conf.sample +++ b/matterbridge.conf.sample @@ -6,7 +6,6 @@ SkipTLSVerify=true nick="matterbot" channel="#matterbridge" UseSlackCircumfix=false -PrefixMessagesWithNick=false [mattermost] url="http://yourdomain/hooks/yourhookkey" @@ -17,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 4302cf17..4ca90dcf 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,53 @@ 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 <= len(nicks); 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.Mattermost.NickFormatter) { + case "table": + return tableformatter(nicks, b.Config.Mattermost.NicksPerRow) + default: + return plainformatter(nicks, b.Config.Mattermost.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])) + default: + log.Printf("got unknown event: %+v\n", event); } } @@ -88,13 +130,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.IRC.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 } @@ -119,9 +180,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 { |