mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 00:00:27 +00:00
Refactor modifyMessage
This commit is contained in:
parent
95fac548bb
commit
c3a8b7a997
@ -25,9 +25,9 @@ func init() {
|
||||
flog = log.WithFields(log.Fields{"module": protocol})
|
||||
}
|
||||
|
||||
func New(config config.Protocol, origin string, c chan config.Message) *bdiscord {
|
||||
func New(cfg config.Protocol, origin string, c chan config.Message) *bdiscord {
|
||||
b := &bdiscord{}
|
||||
b.Config = &config
|
||||
b.Config = &cfg
|
||||
b.Remote = c
|
||||
b.protocol = protocol
|
||||
b.origin = origin
|
||||
|
@ -23,9 +23,9 @@ func init() {
|
||||
flog = log.WithFields(log.Fields{"module": protocol})
|
||||
}
|
||||
|
||||
func New(config config.Protocol, origin string, c chan config.Message) *Bgitter {
|
||||
func New(cfg config.Protocol, origin string, c chan config.Message) *Bgitter {
|
||||
b := &Bgitter{}
|
||||
b.Config = &config
|
||||
b.Config = &cfg
|
||||
b.Remote = c
|
||||
b.protocol = protocol
|
||||
b.origin = origin
|
||||
|
@ -14,6 +14,7 @@ type MMMessage struct {
|
||||
Text string
|
||||
Channel string
|
||||
Username string
|
||||
Raw *slack.MessageEvent
|
||||
}
|
||||
|
||||
type Bslack struct {
|
||||
@ -25,6 +26,7 @@ type Bslack struct {
|
||||
Remote chan config.Message
|
||||
protocol string
|
||||
origin string
|
||||
si *slack.Info
|
||||
channels []slack.Channel
|
||||
}
|
||||
|
||||
@ -35,13 +37,12 @@ func init() {
|
||||
flog = log.WithFields(log.Fields{"module": protocol})
|
||||
}
|
||||
|
||||
func New(config config.Protocol, origin string, c chan config.Message) *Bslack {
|
||||
func New(cfg config.Protocol, origin string, c chan config.Message) *Bslack {
|
||||
b := &Bslack{}
|
||||
b.Config = &config
|
||||
b.Config = &cfg
|
||||
b.Remote = c
|
||||
b.protocol = protocol
|
||||
b.origin = origin
|
||||
b.Config.UseAPI = config.UseAPI
|
||||
return b
|
||||
}
|
||||
|
||||
@ -148,6 +149,10 @@ func (b *Bslack) handleSlack() {
|
||||
time.Sleep(time.Second)
|
||||
flog.Debug("Start listening for Slack messages")
|
||||
for message := range mchan {
|
||||
// do not send messages from ourself
|
||||
if message.Username == b.si.User.Name {
|
||||
continue
|
||||
}
|
||||
texts := strings.Split(message.Text, "\n")
|
||||
for _, text := range texts {
|
||||
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.FullOrigin())
|
||||
@ -177,6 +182,7 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
|
||||
m.Username = user.Name
|
||||
m.Channel = channel.Name
|
||||
m.Text = ev.Text
|
||||
m.Raw = ev
|
||||
mchan <- m
|
||||
}
|
||||
count++
|
||||
@ -184,6 +190,7 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
|
||||
flog.Debugf("%#v", ev.Error())
|
||||
case *slack.ConnectedEvent:
|
||||
b.channels = ev.Info.Channels
|
||||
b.si = ev.Info
|
||||
case *slack.InvalidAuthEvent:
|
||||
flog.Fatalf("Invalid Token %#v", ev)
|
||||
default:
|
||||
|
@ -25,10 +25,10 @@ func init() {
|
||||
flog = log.WithFields(log.Fields{"module": protocol})
|
||||
}
|
||||
|
||||
func New(config config.Protocol, origin string, c chan config.Message) *Bxmpp {
|
||||
func New(cfg config.Protocol, origin string, c chan config.Message) *Bxmpp {
|
||||
b := &Bxmpp{}
|
||||
b.xmppMap = make(map[string]string)
|
||||
b.Config = &config
|
||||
b.Config = &cfg
|
||||
b.protocol = protocol
|
||||
b.origin = origin
|
||||
b.Remote = c
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/42wim/matterbridge/bridge"
|
||||
"github.com/42wim/matterbridge/bridge/config"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -34,7 +35,8 @@ func New(cfg *config.Config, gateway *config.Gateway) error {
|
||||
exists[br.Account] = true
|
||||
}
|
||||
gw.mapChannels()
|
||||
gw.mapIgnores()
|
||||
//TODO fix mapIgnores
|
||||
//gw.mapIgnores()
|
||||
exists = make(map[string]bool)
|
||||
for _, br := range gw.Bridges {
|
||||
err := br.Connect()
|
||||
@ -134,29 +136,24 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func setNickFormat(msg *config.Message, format string) {
|
||||
if format == "" {
|
||||
msg.Username = msg.Protocol + "." + msg.Origin + "-" + msg.Username + ": "
|
||||
return
|
||||
func (gw *Gateway) modifyMessage(msg *config.Message, dest bridge.Bridge) {
|
||||
val := reflect.ValueOf(gw.Config).Elem()
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
typeField := val.Type().Field(i)
|
||||
// look for the protocol map (both lowercase)
|
||||
if strings.ToLower(typeField.Name) == dest.Protocol() {
|
||||
// get the Protocol struct from the map
|
||||
protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Origin()))
|
||||
setNickFormat(msg, protoCfg.Interface().(config.Protocol))
|
||||
val.Field(i).SetMapIndex(reflect.ValueOf(dest.Origin()), protoCfg)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setNickFormat(msg *config.Message, cfg config.Protocol) {
|
||||
format := cfg.RemoteNickFormat
|
||||
msg.Username = strings.Replace(format, "{NICK}", msg.Username, -1)
|
||||
msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1)
|
||||
msg.Username = strings.Replace(msg.Username, "{PROTOCOL}", msg.Protocol, -1)
|
||||
}
|
||||
|
||||
func (gw *Gateway) modifyMessage(msg *config.Message, dest bridge.Bridge) {
|
||||
switch dest.Protocol() {
|
||||
case "irc":
|
||||
setNickFormat(msg, gw.Config.IRC[dest.Origin()].RemoteNickFormat)
|
||||
case "gitter":
|
||||
setNickFormat(msg, gw.Config.Gitter[dest.Origin()].RemoteNickFormat)
|
||||
case "xmpp":
|
||||
setNickFormat(msg, gw.Config.Xmpp[dest.Origin()].RemoteNickFormat)
|
||||
case "mattermost":
|
||||
setNickFormat(msg, gw.Config.Mattermost[dest.Origin()].RemoteNickFormat)
|
||||
case "slack":
|
||||
setNickFormat(msg, gw.Config.Slack[dest.Origin()].RemoteNickFormat)
|
||||
case "discord":
|
||||
setNickFormat(msg, gw.Config.Discord[dest.Origin()].RemoteNickFormat)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user