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/xmpp | |
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/xmpp')
-rw-r--r-- | bridge/xmpp/xmpp.go | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 5d602475..3de95450 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -10,64 +10,75 @@ import ( ) type Bxmpp struct { - xc *xmpp.Client - xmppMap map[string]string - *config.Config - Remote chan config.Message + xc *xmpp.Client + xmppMap map[string]string + Config *config.Protocol + origin string + protocol string + Remote chan config.Message } -type FancyLog struct { - xmpp *log.Entry -} - -type Message struct { - Text string - Channel string - Username string -} - -var flog FancyLog +var flog *log.Entry +var protocol = "xmpp" func init() { - flog.xmpp = log.WithFields(log.Fields{"module": "xmpp"}) + flog = log.WithFields(log.Fields{"module": protocol}) } -func New(config *config.Config, c chan config.Message) *Bxmpp { +func New(config config.Protocol, origin string, c chan config.Message) *Bxmpp { b := &Bxmpp{} b.xmppMap = make(map[string]string) - b.Config = config + b.Config = &config + b.protocol = protocol + b.origin = origin b.Remote = c return b } func (b *Bxmpp) Connect() error { var err error - flog.xmpp.Info("Trying XMPP connection") + flog.Info("Trying XMPP connection") b.xc, err = b.createXMPP() if err != nil { - flog.xmpp.Debugf("%#v", err) + flog.Debugf("%#v", err) return err } - flog.xmpp.Info("Connection succeeded") - b.setupChannels() + flog.Info("Connection succeeded") go b.handleXmpp() return nil } +func (b *Bxmpp) FullOrigin() string { + return b.protocol + "." + b.origin +} + +func (b *Bxmpp) JoinChannel(channel string) error { + b.xc.JoinMUCNoHistory(channel+"@"+b.Config.Muc, b.Config.Nick) + return nil +} + func (b *Bxmpp) Name() string { - return "xmpp" + return b.protocol + "." + b.origin +} + +func (b *Bxmpp) Protocol() string { + return b.protocol +} + +func (b *Bxmpp) Origin() string { + return b.origin } func (b *Bxmpp) Send(msg config.Message) error { - b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Xmpp.Muc, Text: msg.Username + msg.Text}) + b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text}) return nil } func (b *Bxmpp) createXMPP() (*xmpp.Client, error) { options := xmpp.Options{ - Host: b.Config.Xmpp.Server, - User: b.Config.Xmpp.Jid, - Password: b.Config.Xmpp.Password, + Host: b.Config.Server, + User: b.Config.Jid, + Password: b.Config.Password, NoTLS: true, StartTLS: true, //StartTLS: false, @@ -84,13 +95,6 @@ func (b *Bxmpp) createXMPP() (*xmpp.Client, error) { return b.xc, err } -func (b *Bxmpp) setupChannels() { - for _, val := range b.Config.Channel { - flog.xmpp.Infof("Joining %s as %s", val.Xmpp, b.Xmpp.Nick) - b.xc.JoinMUCNoHistory(val.Xmpp+"@"+b.Xmpp.Muc, b.Xmpp.Nick) - } -} - func (b *Bxmpp) xmppKeepAlive() { go func() { ticker := time.NewTicker(90 * time.Second) @@ -121,9 +125,9 @@ func (b *Bxmpp) handleXmpp() error { if len(s) == 2 { nick = s[1] } - if nick != b.Xmpp.Nick { - flog.xmpp.Infof("sending message to remote %s %s %s", nick, v.Text, channel) - b.Remote <- config.Message{Username: nick, Text: v.Text, Channel: channel, Origin: "xmpp"} + if nick != b.Config.Nick { + flog.Infof("sending message to remote %s %s %s", nick, v.Text, channel) + b.Remote <- config.Message{Username: nick, Text: v.Text, Channel: channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} } } case xmpp.Presence: |