From 1d54a50c31cb788bf786397ea5664bd02bfe36fb Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 4 May 2020 18:25:29 +0200 Subject: XMPP: autorejoin MUCs when reconnecting --- connector/xmpp/xmpp.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'connector') diff --git a/connector/xmpp/xmpp.go b/connector/xmpp/xmpp.go index 483c75e..a25a31b 100644 --- a/connector/xmpp/xmpp.go +++ b/connector/xmpp/xmpp.go @@ -35,7 +35,8 @@ type XMPP struct { conn *gxmpp.Client - isMUC map[string]bool + 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,12 +134,23 @@ func (xm *XMPP) connectLoop(num int) { } else { xm.connected = true xm.timeout = 10 - err = xm.handleXMPP() - if err != nil { - xm.connected = false - log.Debugf("XMPP disconnected: %s\n", err) - log.Debugf("Reconnecting.\n") + + 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") } } } @@ -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) { -- cgit v1.2.3