diff options
author | Wim <wim@42.be> | 2016-09-18 19:21:15 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2016-09-18 19:21:15 +0200 |
commit | 7baf386edea4c71919b257d99ca7f7e07897c412 (patch) | |
tree | 5a6f7f0f506c3ab2d15d91548b1d0479bb8fb1ba /bridge/mattermost | |
parent | 6e410b096ef15e976f6a2d28f3412fe9e457f95a (diff) | |
download | matterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.tar.gz matterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.tar.bz2 matterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.zip |
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
Diffstat (limited to 'bridge/mattermost')
-rw-r--r-- | bridge/mattermost/mattermost.go | 77 |
1 files changed, 48 insertions, 29 deletions
diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 6482b91b..7f560594 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -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()} } } } |