Merge pull request #14 from fdevibe/enable-users-2

Enable !users command and add a table formatter
This commit is contained in:
@42wim 2016-03-18 23:48:05 +01:00
commit 4984473c1b
4 changed files with 95 additions and 24 deletions

View File

@ -56,8 +56,6 @@ SkipTLSVerify=true
nick="matterbot" nick="matterbot"
channel="#matterbridge" channel="#matterbridge"
UseSlackCircumfix=false 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] [mattermost]
#url is your incoming webhook url (account settings - integrations - incoming webhooks) #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 token=yourtokenfrommattermost
#disable certificate checking (selfsigned certificates) #disable certificate checking (selfsigned certificates)
#SkipTLSVerify=true #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 #multiple channel config
#token you can find in your outgoing webhook #token you can find in your outgoing webhook

View File

@ -8,25 +8,27 @@ import (
type Config struct { type Config struct {
IRC struct { IRC struct {
UseTLS bool UseTLS bool
SkipTLSVerify bool SkipTLSVerify bool
Server string Server string
Port int Port int
Nick string Nick string
Password string Password string
Channel string Channel string
UseSlackCircumfix bool UseSlackCircumfix bool
PrefixMessagesWithNick bool
} }
Mattermost struct { Mattermost struct {
URL string URL string
Port int Port int
ShowJoinPart bool ShowJoinPart bool
Token string Token string
IconURL string IconURL string
SkipTLSVerify bool SkipTLSVerify bool
BindAddress string BindAddress string
Channel string Channel string
PrefixMessagesWithNick bool
NicksPerRow int
NickFormatter string
} }
Token map[string]*struct { Token map[string]*struct {
IRCChannel string IRCChannel string

View File

@ -6,7 +6,6 @@ SkipTLSVerify=true
nick="matterbot" nick="matterbot"
channel="#matterbridge" channel="#matterbridge"
UseSlackCircumfix=false UseSlackCircumfix=false
PrefixMessagesWithNick=false
[mattermost] [mattermost]
url="http://yourdomain/hooks/yourhookkey" url="http://yourdomain/hooks/yourhookkey"
@ -17,6 +16,9 @@ token=yourtokenfrommattermost
IconURL="http://youricon.png" IconURL="http://youricon.png"
#SkipTLSVerify=true #SkipTLSVerify=true
#BindAddress="0.0.0.0" #BindAddress="0.0.0.0"
PrefixMessagesWithNick=false
NickFormatter=plain
NicksPerRow=4
[general] [general]
GiphyAPIKey=dc6zaTOxFJmzC GiphyAPIKey=dc6zaTOxFJmzC

View File

@ -58,7 +58,7 @@ func (b *Bridge) createIRC(name string) *irc.Connection {
i.AddCallback("JOIN", b.handleJoinPart) i.AddCallback("JOIN", b.handleJoinPart)
i.AddCallback("PART", b.handleJoinPart) i.AddCallback("PART", b.handleJoinPart)
} }
//i.AddCallback("353", b.handleOther) i.AddCallback("*", b.handleOther)
return i 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") //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) { func (b *Bridge) handleOther(event *irc.Event) {
switch event.Code { switch event.Code {
case "353": case "353":
log.Println("handleOther", b.getMMChannel(event.Arguments[0])) 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, "") 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 { func (b *Bridge) SendType(nick string, message string, channel string, mtype string) error {
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL} matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
matterMessage.Channel = channel matterMessage.Channel = channel
matterMessage.UserName = nick matterMessage.UserName = nick
matterMessage.Type = mtype matterMessage.Type = mtype
if (b.Config.IRC.PrefixMessagesWithNick) { if b.Config.Mattermost.PrefixMessagesWithNick {
matterMessage.Text = nick + ": " + message if IsMarkup(message) {
matterMessage.Text = nick + ":\n\n" + message
} else {
matterMessage.Text = nick + ": " + message
}
} else { } else {
matterMessage.Text = message matterMessage.Text = message
} }
@ -119,9 +180,11 @@ func (b *Bridge) handleMatter() {
case "!users": case "!users":
log.Println("received !users from", message.UserName) log.Println("received !users from", message.UserName)
b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token)) b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token))
return
case "!gif": case "!gif":
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1))) message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
b.Send(b.Config.IRC.Nick, message.Text, b.getIRCChannel(message.Token)) b.Send(b.Config.IRC.Nick, message.Text, b.getIRCChannel(message.Token))
return
} }
texts := strings.Split(message.Text, "\n") texts := strings.Split(message.Text, "\n")
for _, text := range texts { for _, text := range texts {