XMPP: autorejoin MUCs when reconnecting

This commit is contained in:
Alex 2020-05-04 18:25:29 +02:00
parent 473336f0eb
commit 1d54a50c31

View file

@ -36,6 +36,7 @@ type XMPP struct {
conn *gxmpp.Client
isMUC map[string]bool
joinedMUC map[string]bool
}
func (xm *XMPP) SetHandler(h Handler) {
@ -86,6 +87,7 @@ func (xm *XMPP) Configure(c Configuration) error {
if xm.isMUC == nil {
xm.isMUC = make(map[string]bool)
}
xm.joinedMUC = make(map[string]bool)
xm.connectorLoopNum += 1
go xm.connectLoop(xm.connectorLoopNum)
@ -106,7 +108,7 @@ func (xm *XMPP) connectLoop(num int) {
return
}
tc := &tls.Config{
ServerName: strings.Split(xm.jid, "@")[1],
ServerName: xm.server,
InsecureSkipVerify: true,
}
options := gxmpp.Options{
@ -132,15 +134,26 @@ func (xm *XMPP) connectLoop(num int) {
} else {
xm.connected = true
xm.timeout = 10
err = xm.handleXMPP()
for muc, joined := range xm.joinedMUC {
if joined {
_, err := xm.conn.JoinMUCNoHistory(muc, xm.nickname)
if err != nil {
log.Warnf("Could not rejoin MUC %s after reconnection: %s", muc, err)
xm.handler.Left(RoomID(muc))
delete(xm.joinedMUC, muc)
}
}
}
err = xm.handleXMPP()
xm.connected = false
log.Debugf("XMPP disconnected: %s\n", err)
log.Debugf("Reconnecting.\n")
}
}
}
}
func (xm *XMPP) xmppKeepAlive() chan bool {
done := make(chan bool)
@ -289,6 +302,11 @@ func (xm *XMPP) Join(roomId RoomID) error {
log.Tracef("Join %s with nick %s\n", roomId, xm.nickname)
_, err := xm.conn.JoinMUCNoHistory(string(roomId), xm.nickname)
if err == nil {
xm.joinedMUC[string(roomId)] = true
}
return err
}
@ -304,6 +322,7 @@ func (xm *XMPP) Invite(userId UserID, roomId RoomID) error {
func (xm *XMPP) Leave(roomId RoomID) {
xm.conn.LeaveMUC(string(roomId))
delete(xm.joinedMUC, string(roomId))
}
func (xm *XMPP) SearchForUsers(query string) ([]UserSearchResult, error) {