2020-01-17 05:17:46 +00:00
|
|
|
// Package session abstracts around the REST API and the Gateway, managing both
|
|
|
|
// at once. It offers a handler interface similar to that in discordgo for
|
|
|
|
// Gateway events.
|
2020-01-15 04:43:34 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2020-03-01 02:13:58 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
"github.com/diamondburned/arikawa/api"
|
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-01-17 05:23:56 +00:00
|
|
|
"github.com/diamondburned/arikawa/handler"
|
2020-01-17 02:08:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-15 04:43:34 +00:00
|
|
|
)
|
|
|
|
|
2020-01-19 21:54:16 +00:00
|
|
|
var ErrMFA = errors.New("Account has 2FA enabled")
|
|
|
|
|
2020-01-17 05:23:56 +00:00
|
|
|
// Session manages both the API and Gateway. As such, Session inherits all of
|
|
|
|
// API's methods, as well has the Handler used for Gateway.
|
2020-01-15 04:43:34 +00:00
|
|
|
type Session struct {
|
2020-01-17 02:08:03 +00:00
|
|
|
*api.Client
|
2020-01-19 21:54:16 +00:00
|
|
|
Gateway *gateway.Gateway
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-01-17 05:23:56 +00:00
|
|
|
// Command handler with inherited methods.
|
2020-01-17 05:17:46 +00:00
|
|
|
*handler.Handler
|
2020-01-17 05:23:56 +00:00
|
|
|
|
2020-01-19 21:54:16 +00:00
|
|
|
// MFA only fields
|
|
|
|
MFA bool
|
|
|
|
Ticket string
|
|
|
|
|
2020-01-17 05:17:46 +00:00
|
|
|
hstop chan struct{}
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(token string) (*Session, error) {
|
|
|
|
// Initialize the session and the API interface
|
|
|
|
s := &Session{}
|
2020-01-17 05:17:46 +00:00
|
|
|
s.Handler = handler.New()
|
2020-01-17 02:08:03 +00:00
|
|
|
s.Client = api.NewClient(token)
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-01-17 02:08:03 +00:00
|
|
|
// Open a gateway
|
|
|
|
g, err := gateway.NewGateway(token)
|
2020-01-15 04:43:34 +00:00
|
|
|
if err != nil {
|
2020-01-17 02:08:03 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to connect to Gateway")
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
2020-01-19 21:54:16 +00:00
|
|
|
s.Gateway = g
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:54:16 +00:00
|
|
|
// Login tries to log in as a normal user account; MFA is optional.
|
|
|
|
func Login(email, password, mfa string) (*Session, error) {
|
|
|
|
// Make a scratch HTTP client without a token
|
|
|
|
client := api.NewClient("")
|
|
|
|
|
|
|
|
// Try to login without TOTP
|
|
|
|
l, err := client.Login(email, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to login")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Token != "" && !l.MFA {
|
|
|
|
// We got the token, return with a new Session.
|
|
|
|
return New(l.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discord requests MFA, so we need the MFA token.
|
|
|
|
if mfa == "" {
|
|
|
|
return nil, ErrMFA
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry logging in with a 2FA token
|
|
|
|
l, err = client.TOTP(mfa, l.Ticket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to login with 2FA")
|
|
|
|
}
|
|
|
|
|
|
|
|
return New(l.Token)
|
|
|
|
}
|
|
|
|
|
2020-01-17 02:08:03 +00:00
|
|
|
func NewWithGateway(gw *gateway.Gateway) *Session {
|
2020-02-16 05:29:25 +00:00
|
|
|
return &Session{
|
|
|
|
Gateway: gw,
|
2020-01-17 02:08:03 +00:00
|
|
|
// Nab off gateway's token
|
2020-02-16 05:29:25 +00:00
|
|
|
Client: api.NewClient(gw.Identifier.Token),
|
2020-01-17 05:17:46 +00:00
|
|
|
Handler: handler.New(),
|
2020-01-17 02:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Open() error {
|
2020-04-06 20:26:00 +00:00
|
|
|
// Start the handler beforehand so no events are missed.
|
2020-01-17 02:08:03 +00:00
|
|
|
stop := make(chan struct{})
|
|
|
|
s.hstop = stop
|
|
|
|
go s.startHandler(stop)
|
|
|
|
|
2020-04-06 20:26:00 +00:00
|
|
|
if err := s.Gateway.Open(); err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to start gateway")
|
|
|
|
}
|
|
|
|
|
2020-01-17 02:08:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) startHandler(stop <-chan struct{}) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
2020-01-19 21:54:16 +00:00
|
|
|
case ev := <-s.Gateway.Events:
|
2020-01-17 05:17:46 +00:00
|
|
|
s.Handler.Call(ev)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 02:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Close() error {
|
|
|
|
// Stop the event handler
|
2020-03-01 02:13:58 +00:00
|
|
|
s.close()
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-01-17 02:08:03 +00:00
|
|
|
// Close the websocket
|
2020-01-19 21:54:16 +00:00
|
|
|
return s.Gateway.Close()
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
2020-03-01 02:13:58 +00:00
|
|
|
|
|
|
|
// Wait blocks until either a SIGINT or a Gateway fatal error is received.
|
|
|
|
// Check the Gateway documentation for more information.
|
|
|
|
func (s *Session) Wait() error {
|
|
|
|
sigint := make(chan os.Signal)
|
|
|
|
signal.Notify(sigint, os.Interrupt)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sigint:
|
|
|
|
return s.Close()
|
|
|
|
case err := <-s.Gateway.FatalError:
|
|
|
|
s.close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) close() {
|
|
|
|
if s.hstop != nil {
|
|
|
|
close(s.hstop)
|
|
|
|
}
|
|
|
|
}
|