2020-05-20 07:13:12 +00:00
|
|
|
// Package mock contains a mock cchat backend.
|
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2020-06-15 01:57:02 +00:00
|
|
|
"context"
|
2020-05-20 07:13:12 +00:00
|
|
|
"encoding/json"
|
2020-06-30 02:58:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-05-20 07:13:12 +00:00
|
|
|
"strconv"
|
2020-06-30 02:58:21 +00:00
|
|
|
"strings"
|
2020-06-09 03:58:12 +00:00
|
|
|
"time"
|
2020-05-20 07:13:12 +00:00
|
|
|
|
2020-06-30 02:58:21 +00:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
2020-05-20 07:13:12 +00:00
|
|
|
"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-06-09 06:02:51 +00:00
|
|
|
"github.com/pkg/errors"
|
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-06-09 03:58:12 +00:00
|
|
|
func (s Service) Name() text.Rich {
|
|
|
|
return text.Rich{Content: "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) {
|
2020-06-09 06:02:51 +00:00
|
|
|
if err := simulateAustralianInternet(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Restore failed")
|
2020-06-07 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
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-06-07 17:45:14 +00:00
|
|
|
// SLOW IO TIME.
|
2020-06-09 06:02:51 +00:00
|
|
|
if err := simulateAustralianInternet(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Authentication failed")
|
2020-06-07 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
return newSession(form[0]), nil
|
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{
|
2020-07-03 23:53:46 +00:00
|
|
|
// refer to internet.go
|
2020-05-20 07:13:12 +00:00
|
|
|
"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 (
|
2020-06-30 02:58:21 +00:00
|
|
|
_ cchat.Icon = (*Session)(nil)
|
|
|
|
_ cchat.Session = (*Session)(nil)
|
|
|
|
_ cchat.ServerList = (*Session)(nil)
|
|
|
|
_ cchat.SessionSaver = (*Session)(nil)
|
|
|
|
_ cchat.Commander = (*Session)(nil)
|
|
|
|
_ cchat.CommandCompleter = (*Session)(nil)
|
2020-05-29 07:06:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09 03:58:12 +00:00
|
|
|
func (s *Session) Name() text.Rich {
|
|
|
|
return text.Rich{Content: s.username}
|
2020-06-03 23:24:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 23:39:49 +00:00
|
|
|
func (s *Session) Disconnect() error {
|
|
|
|
// Nothing to do here, but emulate errors.
|
|
|
|
return simulateAustralianInternet()
|
|
|
|
}
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
func (s *Session) Servers(container cchat.ServersContainer) error {
|
2020-06-09 03:58:12 +00:00
|
|
|
// Simulate slight IO.
|
|
|
|
<-time.After(time.Second)
|
|
|
|
|
2020-05-23 02:44:50 +00:00
|
|
|
container.SetServers(s.servers)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-29 07:06:01 +00:00
|
|
|
|
2020-06-29 21:01:21 +00:00
|
|
|
func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), error) {
|
2020-06-15 01:57:02 +00:00
|
|
|
// Simulate IO while ignoring the context.
|
2020-06-09 03:58:12 +00:00
|
|
|
simulateAustralianInternet()
|
|
|
|
|
2020-06-07 17:45:14 +00:00
|
|
|
iconer.SetIcon(avatarURL)
|
2020-06-29 21:01:21 +00:00
|
|
|
return func() {}, nil
|
2020-06-07 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 07:06:01 +00:00
|
|
|
func (s *Session) Save() (map[string]string, error) {
|
|
|
|
return map[string]string{
|
|
|
|
"username": s.username,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-06-30 02:58:21 +00:00
|
|
|
|
|
|
|
func (s *Session) RunCommand(cmds []string) (io.ReadCloser, error) {
|
|
|
|
var r, w = io.Pipe()
|
|
|
|
|
|
|
|
switch cmd := arg(cmds, 0); cmd {
|
|
|
|
case "ls":
|
|
|
|
go func() {
|
|
|
|
fmt.Fprintln(w, "Commands: ls, random")
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
case "random":
|
|
|
|
// callback used to generate stuff and stream into readcloser
|
|
|
|
var generator func() string
|
|
|
|
// number of times to generate the word
|
|
|
|
var times = 1
|
|
|
|
|
|
|
|
switch arg(cmds, 1) {
|
|
|
|
case "paragraph":
|
|
|
|
generator = randomdata.Paragraph
|
|
|
|
case "noun":
|
|
|
|
generator = randomdata.Noun
|
|
|
|
case "silly_name":
|
|
|
|
generator = randomdata.SillyName
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Usage: random <paragraph|noun|silly_name> [repeat]")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n := arg(cmds, 2); n != "" {
|
|
|
|
i, err := strconv.Atoi(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to parse repeat number")
|
|
|
|
}
|
|
|
|
times = i
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
for i := 0; i < times; i++ {
|
|
|
|
// Yes, we're simulating this even in something as trivial as a
|
|
|
|
// command prompt.
|
|
|
|
if err := simulateAustralianInternet(); err != nil {
|
|
|
|
fmt.Fprintln(w, "Error:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, generator())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown command: %s", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) CompleteCommand(words []string, i int) []string {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix("ls", words[i]):
|
|
|
|
return []string{"ls"}
|
|
|
|
|
|
|
|
case strings.HasPrefix("random", words[i]):
|
|
|
|
return []string{
|
|
|
|
"random paragraph",
|
|
|
|
"random noun",
|
|
|
|
"random silly_name",
|
|
|
|
}
|
|
|
|
|
|
|
|
case lookbackCheck(words, i, "random", "paragraph"):
|
|
|
|
return []string{"paragraph"}
|
|
|
|
|
|
|
|
case lookbackCheck(words, i, "random", "noun"):
|
|
|
|
return []string{"noun"}
|
|
|
|
|
|
|
|
case lookbackCheck(words, i, "random", "silly_name"):
|
|
|
|
return []string{"silly_name"}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func arg(sl []string, i int) string {
|
|
|
|
if i >= len(sl) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return sl[i]
|
|
|
|
}
|