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