gmessages/libgm/pairing_handler.go

41 lines
1.1 KiB
Go
Raw Normal View History

package libgm
import (
2023-07-16 13:18:45 +00:00
"fmt"
"go.mau.fi/mautrix-gmessages/libgm/events"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
)
func (c *Client) handlePairingEvent(response *pblite.Response) {
2023-07-17 13:51:31 +00:00
pairEventData, ok := response.Data.Decrypted.(*gmproto.PairEvents)
if !ok {
2023-07-17 13:43:34 +00:00
c.Logger.Error().Type("decrypted_type", response.Data.Decrypted).Msg("Unexpected data type in pair event")
return
}
switch evt := pairEventData.Event.(type) {
2023-07-17 13:51:31 +00:00
case *gmproto.PairEvents_Paired:
2023-07-16 13:18:45 +00:00
c.completePairing(evt.Paired)
2023-07-17 13:51:31 +00:00
case *gmproto.PairEvents_Revoked:
2023-07-15 23:41:34 +00:00
c.triggerEvent(evt.Revoked)
default:
2023-07-17 13:43:34 +00:00
c.Logger.Debug().Any("evt", evt).Msg("Unknown pair event type")
}
}
2023-07-17 13:51:31 +00:00
func (c *Client) completePairing(data *gmproto.PairedData) {
2023-07-16 12:55:30 +00:00
c.updateTachyonAuthToken(data.GetTokenData().GetTachyonAuthToken(), data.GetTokenData().GetTTL())
c.AuthData.Mobile = data.Mobile
c.AuthData.Browser = data.Browser
2023-07-16 12:55:30 +00:00
c.triggerEvent(&events.PairSuccessful{PairedData: data})
2023-07-16 13:18:45 +00:00
err := c.Reconnect()
if err != nil {
c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
}
}