matterbridge/bridge/xmpp/xmpp.go

134 lines
2.8 KiB
Go
Raw Normal View History

package bxmpp
import (
"github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus"
"github.com/mattn/go-xmpp"
"crypto/tls"
"strings"
"time"
)
type Bxmpp struct {
2016-11-13 23:06:37 +01:00
xc *xmpp.Client
xmppMap map[string]string
Config *config.Protocol
Remote chan config.Message
Account string
}
var flog *log.Entry
var protocol = "xmpp"
func init() {
flog = log.WithFields(log.Fields{"module": protocol})
}
2016-11-13 23:06:37 +01:00
func New(cfg config.Protocol, account string, c chan config.Message) *Bxmpp {
b := &Bxmpp{}
b.xmppMap = make(map[string]string)
2016-11-04 00:05:15 +01:00
b.Config = &cfg
2016-11-13 23:06:37 +01:00
b.Account = account
b.Remote = c
2016-08-15 23:16:07 +02:00
return b
}
func (b *Bxmpp) Connect() error {
var err error
flog.Infof("Connecting %s", b.Config.Server)
b.xc, err = b.createXMPP()
if err != nil {
flog.Debugf("%#v", err)
2016-08-15 23:16:07 +02:00
return err
}
flog.Info("Connection succeeded")
go b.handleXmpp()
2016-08-15 23:16:07 +02:00
return nil
}
func (b *Bxmpp) JoinChannel(channel string) error {
b.xc.JoinMUCNoHistory(channel+"@"+b.Config.Muc, b.Config.Nick)
return nil
}
func (b *Bxmpp) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg)
2016-11-13 23:06:37 +01:00
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) {
tc := new(tls.Config)
tc.InsecureSkipVerify = b.Config.SkipTLSVerify
options := xmpp.Options{
Host: b.Config.Server,
User: b.Config.Jid,
Password: b.Config.Password,
NoTLS: true,
StartTLS: true,
TLSConfig: tc,
//StartTLS: false,
Debug: true,
Session: true,
Status: "",
StatusMessage: "",
Resource: "",
InsecureAllowUnencryptedAuth: false,
//InsecureAllowUnencryptedAuth: true,
}
var err error
b.xc, err = options.NewClient()
return b.xc, err
}
2016-11-26 15:02:39 +01:00
func (b *Bxmpp) xmppKeepAlive() chan bool {
done := make(chan bool)
go func() {
ticker := time.NewTicker(90 * time.Second)
2016-11-26 15:02:39 +01:00
defer ticker.Stop()
for {
select {
case <-ticker.C:
2016-11-26 15:02:39 +01:00
b.xc.PingC2S("", "")
case <-done:
return
}
}
}()
2016-11-26 15:02:39 +01:00
return done
}
func (b *Bxmpp) handleXmpp() error {
2016-11-26 15:02:39 +01:00
done := b.xmppKeepAlive()
defer close(done)
nodelay := time.Time{}
for {
m, err := b.xc.Recv()
if err != nil {
return err
}
switch v := m.(type) {
case xmpp.Chat:
var channel, nick string
if v.Type == "groupchat" {
s := strings.Split(v.Remote, "@")
if len(s) == 2 {
channel = s[0]
}
s = strings.Split(s[1], "/")
if len(s) == 2 {
nick = s[1]
}
if nick != b.Config.Nick && v.Stamp == nodelay && v.Text != "" {
2016-11-13 23:06:37 +01:00
flog.Debugf("Sending message from %s on %s to gateway", nick, b.Account)
b.Remote <- config.Message{Username: nick, Text: v.Text, Channel: channel, Account: b.Account}
}
}
case xmpp.Presence:
// do nothing
}
}
}