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