2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import "go.mau.fi/mautrix-gmessages/libgm/util"
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
client *Client
|
|
|
|
|
|
|
|
prepareNewSession prepareNewSession
|
|
|
|
newSession newSession
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) SetActiveSession() (*util.SessionResponse, error) {
|
2023-06-30 13:26:46 +00:00
|
|
|
s.client.sessionHandler.ResetSessionID()
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
prepareResponses, prepareSessionErr := s.prepareNewSession.Execute()
|
|
|
|
if prepareSessionErr != nil {
|
|
|
|
return nil, prepareSessionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
newSessionResponses, newSessionErr := s.newSession.Execute()
|
|
|
|
if newSessionErr != nil {
|
|
|
|
return nil, newSessionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionResponse, processFail := s.client.processSessionResponse(prepareResponses, newSessionResponses)
|
|
|
|
if processFail != nil {
|
|
|
|
return nil, processFail
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessionResponse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type prepareNewSession struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *prepareNewSession) Execute() ([]*Response, error) {
|
|
|
|
instruction, _ := p.client.instructions.GetInstruction(PREPARE_NEW_SESSION_OPCODE)
|
2023-06-30 13:26:46 +00:00
|
|
|
sentRequestID, _ := p.client.createAndSendRequest(instruction.Opcode, p.client.ttl, false, nil)
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
responses, err := p.client.sessionHandler.WaitForResponse(sentRequestID, instruction.Opcode)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type newSession struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *newSession) Execute() ([]*Response, error) {
|
|
|
|
instruction, _ := n.client.instructions.GetInstruction(NEW_SESSION_OPCODE)
|
2023-06-30 13:26:46 +00:00
|
|
|
sentRequestID, _ := n.client.createAndSendRequest(instruction.Opcode, 0, true, nil)
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
responses, err := n.client.sessionHandler.WaitForResponse(sentRequestID, instruction.Opcode)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rest of the processing...
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|