4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-07 22:54:04 +00:00

Refactor for more flexibility

* Move from gcfg to toml configuration because gcfg was too restrictive
* Implemented gateway which has support multiple in and out bridges.
* Allow for bridging the same bridges, which means eg you can now bridge between multiple mattermosts.
* Support multiple gateways
This commit is contained in:
Wim
2016-09-18 19:21:15 +02:00
parent 6e410b096e
commit 7baf386ede
40 changed files with 3816 additions and 2292 deletions

View File

@ -8,7 +8,6 @@ import (
"strings"
)
//type Bridge struct {
type MMhook struct {
mh *matterhook.Client
}
@ -28,15 +27,16 @@ type MMMessage struct {
type Bmattermost struct {
MMhook
MMapi
*config.Config
Plus bool
Remote chan config.Message
Config *config.Protocol
Plus bool
Remote chan config.Message
name string
origin string
protocol string
}
type FancyLog struct {
irc *log.Entry
mm *log.Entry
xmpp *log.Entry
mm *log.Entry
}
var flog FancyLog
@ -44,16 +44,17 @@ var flog FancyLog
const Legacy = "legacy"
func init() {
flog.irc = log.WithFields(log.Fields{"module": "irc"})
flog.mm = log.WithFields(log.Fields{"module": "mattermost"})
flog.xmpp = log.WithFields(log.Fields{"module": "xmpp"})
}
func New(cfg *config.Config, c chan config.Message) *Bmattermost {
func New(cfg config.Protocol, origin string, c chan config.Message) *Bmattermost {
b := &Bmattermost{}
b.Config = cfg
b.Config = &cfg
b.origin = origin
b.Remote = c
b.Plus = cfg.General.Plus
b.protocol = "mattermost"
b.name = cfg.Name
b.Plus = cfg.UseAPI
b.mmMap = make(map[string]string)
return b
}
@ -64,44 +65,62 @@ func (b *Bmattermost) Command(cmd string) string {
func (b *Bmattermost) Connect() error {
if !b.Plus {
b.mh = matterhook.New(b.Config.Mattermost.URL,
matterhook.Config{InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify,
BindAddress: b.Config.Mattermost.BindAddress})
b.mh = matterhook.New(b.Config.URL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.BindAddress})
} else {
b.mc = matterclient.New(b.Config.Mattermost.Login, b.Config.Mattermost.Password,
b.Config.Mattermost.Team, b.Config.Mattermost.Server)
b.mc.SkipTLSVerify = b.Config.Mattermost.SkipTLSVerify
b.mc.NoTLS = b.Config.Mattermost.NoTLS
flog.mm.Infof("Trying login %s (team: %s) on %s", b.Config.Mattermost.Login, b.Config.Mattermost.Team, b.Config.Mattermost.Server)
b.mc = matterclient.New(b.Config.Login, b.Config.Password,
b.Config.Team, b.Config.Server)
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
b.mc.NoTLS = b.Config.NoTLS
flog.mm.Infof("Trying login %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server)
err := b.mc.Login()
if err != nil {
return err
}
flog.mm.Info("Login ok")
b.mc.JoinChannel(b.Config.Mattermost.Channel)
for _, val := range b.Config.Channel {
b.mc.JoinChannel(val.Mattermost)
}
/*
b.mc.JoinChannel(b.Config.Channel)
for _, val := range b.Config.Channel {
b.mc.JoinChannel(val.Mattermost)
}
*/
go b.mc.WsReceiver()
}
go b.handleMatter()
return nil
}
func (b *Bmattermost) FullOrigin() string {
return b.protocol + "." + b.origin
}
func (b *Bmattermost) JoinChannel(channel string) error {
return b.mc.JoinChannel(channel)
}
func (b *Bmattermost) Name() string {
return "mattermost"
return b.protocol + "." + b.origin
}
func (b *Bmattermost) Origin() string {
return b.origin
}
func (b *Bmattermost) Protocol() string {
return b.protocol
}
func (b *Bmattermost) Send(msg config.Message) error {
flog.mm.Infof("mattermost send %#v", msg)
if msg.Origin != "mattermost" {
if msg.Origin != b.origin {
return b.SendType(msg.Username, msg.Text, msg.Channel, "")
}
return nil
}
func (b *Bmattermost) SendType(nick string, message string, channel string, mtype string) error {
if b.Config.Mattermost.PrefixMessagesWithNick {
if b.Config.PrefixMessagesWithNick {
/*if IsMarkup(message) {
message = nick + "\n\n" + message
} else {
@ -110,7 +129,7 @@ func (b *Bmattermost) SendType(nick string, message string, channel string, mtyp
//}
}
if !b.Plus {
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.Channel = channel
matterMessage.UserName = nick
matterMessage.Type = mtype
@ -141,7 +160,7 @@ func (b *Bmattermost) handleMatter() {
texts := strings.Split(message.Text, "\n")
for _, text := range texts {
flog.mm.Debug("Sending message from " + message.Username + " to " + message.Channel)
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: "mattermost"}
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()}
}
}
}