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

Allow reuse of api in different gateways. See #189

This commit is contained in:
Wim 2017-06-07 23:54:50 +02:00
parent ad3cb0386b
commit 359d0f2910
2 changed files with 20 additions and 5 deletions

View File

@ -22,6 +22,7 @@ type ApiMessage struct {
Text string `json:"text"` Text string `json:"text"`
Username string `json:"username"` Username string `json:"username"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
Gateway string `json:"gateway"`
} }
var flog *log.Entry var flog *log.Entry
@ -76,12 +77,15 @@ func (b *Api) handlePostMessage(c echo.Context) error {
if err := c.Bind(message); err != nil { if err := c.Bind(message); err != nil {
return err return err
} }
flog.Debugf("Sending message from %s on %s to gateway", message.Username, "api")
b.Remote <- config.Message{ b.Remote <- config.Message{
Text: message.Text, Text: message.Text,
Username: message.Username, Username: message.Username,
Channel: "api", Channel: "api",
Avatar: message.Avatar, Avatar: message.Avatar,
Account: b.Account, Account: b.Account,
Gateway: message.Gateway,
Protocol: "api",
} }
return c.JSON(http.StatusOK, message) return c.JSON(http.StatusOK, message)
} }

View File

@ -174,7 +174,7 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
if _, ok := gw.Channels[getChannelID(*msg)]; !ok { if _, ok := gw.Channels[getChannelID(*msg)]; !ok {
continue continue
} }
if channel.Direction == "out" && channel.Account == dest.Account && gw.validGatewayDest(*msg, channel) { if channel.Direction == "out" && channel.Account == dest.Account && gw.validGatewayDest(msg, channel) {
channels = append(channels, *channel) channels = append(channels, *channel)
} }
} }
@ -200,7 +200,6 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
} }
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name) log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
msg.Channel = channel.Name msg.Channel = channel.Name
msg.Gateway = gw.Name
gw.modifyAvatar(&msg, dest) gw.modifyAvatar(&msg, dest)
gw.modifyUsername(&msg, dest) gw.modifyUsername(&msg, dest)
// for api we need originchannel as channel // for api we need originchannel as channel
@ -235,7 +234,9 @@ func (gw *Gateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) {
if nick == "" { if nick == "" {
nick = dest.Config.RemoteNickFormat nick = dest.Config.RemoteNickFormat
} }
nick = strings.Replace(nick, "{NOPINGNICK}", msg.Username[:1]+""+msg.Username[1:], -1) if len(msg.Username) > 0 {
nick = strings.Replace(nick, "{NOPINGNICK}", msg.Username[:1]+""+msg.Username[1:], -1)
}
nick = strings.Replace(nick, "{NICK}", msg.Username, -1) nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1) nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1)
nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1) nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
@ -257,13 +258,21 @@ func getChannelID(msg config.Message) string {
return msg.Channel + msg.Account return msg.Channel + msg.Account
} }
func (gw *Gateway) validGatewayDest(msg config.Message, channel *config.ChannelInfo) bool { func (gw *Gateway) validGatewayDest(msg *config.Message, channel *config.ChannelInfo) bool {
GIDmap := gw.Channels[getChannelID(msg)].GID GIDmap := gw.Channels[getChannelID(*msg)].GID
// gateway is specified in message (probably from api)
if msg.Gateway != "" {
return channel.GID[msg.Gateway]
}
// check if we are running a samechannelgateway. // check if we are running a samechannelgateway.
// if it is and the channel name matches it's ok, otherwise we shouldn't use this channel. // if it is and the channel name matches it's ok, otherwise we shouldn't use this channel.
for k, _ := range GIDmap { for k, _ := range GIDmap {
if channel.SameChannel[k] == true { if channel.SameChannel[k] == true {
if msg.Channel == channel.Name { if msg.Channel == channel.Name {
// add the gateway to our message
msg.Gateway = k
return true return true
} else { } else {
return false return false
@ -273,6 +282,8 @@ func (gw *Gateway) validGatewayDest(msg config.Message, channel *config.ChannelI
// check if we are in the correct gateway // check if we are in the correct gateway
for k, _ := range GIDmap { for k, _ := range GIDmap {
if channel.GID[k] == true { if channel.GID[k] == true {
// add the gateway to our message
msg.Gateway = k
return true return true
} }
} }