5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 14:00:27 +00:00

Refactor handleReceive

This commit is contained in:
Wim 2016-08-20 17:21:40 +02:00
parent 3a6c655dfb
commit 3311c7f923

View File

@ -54,20 +54,8 @@ func (b *Bridge) handleReceive(c chan config.Message) {
for { for {
select { select {
case msg := <-c: case msg := <-c:
m := b.getChannel(msg.Origin, msg.Channel)
if m == nil {
continue
}
for _, br := range b.Bridges { for _, br := range b.Bridges {
if b.ignoreMessage(msg.Username, msg.Text, msg.Origin) { b.handleMessage(msg, br)
continue
}
// do not send to originated bridge
if br.Name() != msg.Origin {
msg.Channel = m[br.Name()]
msgmod := b.modifyMessage(msg, br.Name())
br.Send(msgmod)
}
} }
} }
} }
@ -92,19 +80,33 @@ func (b *Bridge) mapIgnores() {
b.ignoreNicks = m b.ignoreNicks = m
} }
func (b *Bridge) getChannel(src, name string) map[string]string { func (b *Bridge) getDestChannel(msg *config.Message, dest string) string {
for _, v := range b.Channels { for _, v := range b.Channels {
if v[src] == name { if v[msg.Origin] == msg.Channel {
return v return v[dest]
} }
} }
return nil return ""
} }
func (b *Bridge) ignoreMessage(nick string, message string, protocol string) bool { func (b *Bridge) handleMessage(msg config.Message, dest Bridger) {
if b.ignoreMessage(&msg) {
return
}
if dest.Name() != msg.Origin {
msg.Channel = b.getDestChannel(&msg, dest.Name())
if msg.Channel == "" {
return
}
b.modifyMessage(&msg, dest.Name())
dest.Send(msg)
}
}
func (b *Bridge) ignoreMessage(msg *config.Message) bool {
// should we discard messages ? // should we discard messages ?
for _, entry := range b.ignoreNicks[protocol] { for _, entry := range b.ignoreNicks[msg.Origin] {
if nick == entry { if msg.Username == entry {
return true return true
} }
} }
@ -120,14 +122,13 @@ func setNickFormat(msg *config.Message, format string) {
msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1) msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1)
} }
func (b *Bridge) modifyMessage(msg config.Message, dest string) config.Message { func (b *Bridge) modifyMessage(msg *config.Message, dest string) {
switch dest { switch dest {
case "irc": case "irc":
setNickFormat(&msg, b.Config.IRC.RemoteNickFormat) setNickFormat(msg, b.Config.IRC.RemoteNickFormat)
case "xmpp": case "xmpp":
setNickFormat(&msg, b.Config.Xmpp.RemoteNickFormat) setNickFormat(msg, b.Config.Xmpp.RemoteNickFormat)
case "mattermost": case "mattermost":
setNickFormat(&msg, b.Config.Mattermost.RemoteNickFormat) setNickFormat(msg, b.Config.Mattermost.RemoteNickFormat)
} }
return msg
} }