2020-05-20 07:13:12 +00:00
|
|
|
// Package mock contains a mock cchat backend.
|
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-29 07:06:01 +00:00
|
|
|
"errors"
|
2020-05-20 07:13:12 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat"
|
2020-05-23 02:44:50 +00:00
|
|
|
"github.com/diamondburned/cchat/services"
|
2020-06-04 04:36:46 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-05-20 07:13:12 +00:00
|
|
|
)
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func init() {
|
|
|
|
services.RegisterService(&Service{})
|
2020-05-20 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
// ErrInvalidSession is returned if SessionRestore is given a bad session.
|
|
|
|
var ErrInvalidSession = errors.New("invalid session")
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
type Service struct{}
|
|
|
|
|
2020-05-20 07:13:12 +00:00
|
|
|
var (
|
2020-05-29 07:06:01 +00:00
|
|
|
_ cchat.Service = (*Service)(nil)
|
|
|
|
_ cchat.Configurator = (*Service)(nil)
|
|
|
|
_ cchat.SessionRestorer = (*Service)(nil)
|
2020-05-20 07:13:12 +00:00
|
|
|
)
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func (s Service) Name() string {
|
|
|
|
return "Mock"
|
2020-05-20 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) {
|
|
|
|
username, ok := storage["username"]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrInvalidSession
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSession(username), nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:29:06 +00:00
|
|
|
func (s Service) Authenticate() cchat.Authenticator {
|
|
|
|
return Authenticator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Authenticator struct{}
|
|
|
|
|
|
|
|
var _ cchat.Authenticator = (*Authenticator)(nil)
|
|
|
|
|
|
|
|
func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
2020-05-29 18:41:40 +00:00
|
|
|
return []cchat.AuthenticateEntry{
|
|
|
|
{
|
|
|
|
Name: "Username",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Password (ignored)",
|
|
|
|
Secret: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Paragraph (ignored)",
|
|
|
|
Multiline: true,
|
|
|
|
},
|
|
|
|
}
|
2020-05-20 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:29:06 +00:00
|
|
|
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
2020-05-29 07:06:01 +00:00
|
|
|
return newSession(form[0]), nil
|
2020-05-20 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// channel.go @ emulateAustralianInternet
|
|
|
|
internetCanFail = true
|
|
|
|
// 500ms ~ 3s
|
|
|
|
internetMinLatency = 500
|
2020-05-29 18:41:40 +00:00
|
|
|
internetMaxLatency = 3000
|
2020-05-20 07:13:12 +00:00
|
|
|
)
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func (s Service) Configuration() (map[string]string, error) {
|
2020-05-20 07:13:12 +00:00
|
|
|
return map[string]string{
|
|
|
|
"internet.canFail": strconv.FormatBool(internetCanFail),
|
|
|
|
"internet.minLatency": strconv.Itoa(internetMinLatency),
|
|
|
|
"internet.maxLatency": strconv.Itoa(internetMaxLatency),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func (s Service) SetConfiguration(config map[string]string) error {
|
2020-05-20 07:13:12 +00:00
|
|
|
for _, err := range []error{
|
|
|
|
// shit code, would not recommend. It's only an ok-ish idea here because
|
|
|
|
// unmarshalConfig() returns ErrInvalidConfigAtField.
|
|
|
|
unmarshalConfig(config, "internet.canFail", &internetCanFail),
|
|
|
|
unmarshalConfig(config, "internet.minLatency", &internetMinLatency),
|
|
|
|
unmarshalConfig(config, "internet.maxLatency", &internetMaxLatency),
|
|
|
|
} {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalConfig(config map[string]string, key string, value interface{}) error {
|
|
|
|
if err := json.Unmarshal([]byte(config[key]), value); err != nil {
|
|
|
|
return &cchat.ErrInvalidConfigAtField{
|
2020-05-29 18:41:40 +00:00
|
|
|
Key: key,
|
|
|
|
Err: err,
|
2020-05-20 07:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-23 02:44:50 +00:00
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
username string
|
|
|
|
servers []cchat.Server
|
2020-05-29 07:06:01 +00:00
|
|
|
lastid uint32 // used for generation
|
2020-05-23 02:44:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
var (
|
|
|
|
_ cchat.Session = (*Session)(nil)
|
|
|
|
_ cchat.SessionSaver = (*Session)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func newSession(username string) *Session {
|
|
|
|
ses := &Session{username: username}
|
|
|
|
ses.servers = GenerateServers(ses)
|
|
|
|
return ses
|
|
|
|
}
|
2020-05-23 02:44:50 +00:00
|
|
|
|
2020-06-04 04:36:46 +00:00
|
|
|
func (s *Session) ID() string {
|
|
|
|
return s.username
|
2020-05-23 02:44:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 04:36:46 +00:00
|
|
|
func (s *Session) Name(labeler cchat.LabelContainer) error {
|
|
|
|
labeler.SetLabel(text.Rich{Content: s.username})
|
|
|
|
return nil
|
2020-06-03 23:24:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func (s *Session) Servers(container cchat.ServersContainer) error {
|
|
|
|
container.SetServers(s.servers)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-29 07:06:01 +00:00
|
|
|
|
|
|
|
func (s *Session) Save() (map[string]string, error) {
|
|
|
|
return map[string]string{
|
|
|
|
"username": s.username,
|
|
|
|
}, nil
|
|
|
|
}
|