2023-07-09 11:16:52 +00:00
|
|
|
package libgm
|
|
|
|
|
|
|
|
import (
|
2023-07-16 13:18:45 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/events"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/pblite"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/binary"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) handlePairingEvent(response *pblite.Response) {
|
|
|
|
pairEventData, ok := response.Data.Decrypted.(*binary.PairEvents)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.Logger.Error().Any("pairEventData", pairEventData).Msg("failed to assert response into PairEvents")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch evt := pairEventData.Event.(type) {
|
|
|
|
case *binary.PairEvents_Paired:
|
2023-07-16 13:18:45 +00:00
|
|
|
c.completePairing(evt.Paired)
|
2023-07-09 11:16:52 +00:00
|
|
|
case *binary.PairEvents_Revoked:
|
|
|
|
c.Logger.Debug().Any("data", evt).Msg("Revoked Device")
|
2023-07-15 23:41:34 +00:00
|
|
|
c.triggerEvent(evt.Revoked)
|
2023-07-09 11:16:52 +00:00
|
|
|
default:
|
|
|
|
c.Logger.Debug().Any("response", response).Any("evt", evt).Msg("Invalid PairEvents type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:18:45 +00:00
|
|
|
func (c *Client) completePairing(data *binary.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-09 11:16:52 +00:00
|
|
|
|
2023-07-16 12:55:30 +00:00
|
|
|
c.triggerEvent(&events.PairSuccessful{PairedData: data})
|
2023-07-09 11:16:52 +00:00
|
|
|
|
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)})
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
}
|