diff --git a/go.mod b/go.mod index abd61ae..eb80831 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.14 require ( github.com/Pallinder/go-randomdata v1.2.0 github.com/diamondburned/aqs v0.0.0-20200704043812-99b676ee44eb - github.com/diamondburned/cchat v0.0.43 + github.com/diamondburned/cchat v0.0.46 github.com/lucasb-eyer/go-colorful v1.0.3 github.com/pkg/errors v0.9.1 golang.org/x/text v0.3.3 // indirect diff --git a/go.sum b/go.sum index 0b1418a..cc980ac 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/diamondburned/cchat v0.0.40 h1:38gPyJnnDoNDHrXcV8Qchfv3y6jlS3Fzz/6FY0 github.com/diamondburned/cchat v0.0.40/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/cchat v0.0.43 h1:HetAujSaUSdnQgAUZgprNLARjf/MSWXpCfWdvX2wOCU= github.com/diamondburned/cchat v0.0.43/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.46 h1:fzm2XA9uGasX0uaic1AFfUMGA53PlO+GGmkYbx49A5k= +github.com/diamondburned/cchat v0.0.46/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= diff --git a/service.go b/service.go index 736f5f8..ada76b6 100644 --- a/service.go +++ b/service.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "math/rand" "strconv" "strings" "time" @@ -46,7 +47,12 @@ func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error return nil, ErrInvalidSession } - return newSession(username), nil + sessionID, ok := storage["sessionID"] + if !ok { + return nil, ErrInvalidSession + } + + return newSession(username, sessionID), nil } func (s Service) Authenticate() cchat.Authenticator { @@ -79,7 +85,7 @@ func (Authenticator) Authenticate(form []string) (cchat.Session, error) { return nil, errors.Wrap(err, "Authentication failed") } - return newSession(form[0]), nil + return newSession(form[0], ""), nil } func (s Service) Configuration() (map[string]string, error) { @@ -117,6 +123,7 @@ func unmarshalConfig(config map[string]string, key string, value interface{}) er } type Session struct { + sesID string username string servers []cchat.Server lastid uint32 // used for generation @@ -131,14 +138,19 @@ var ( _ cchat.CommandCompleter = (*Session)(nil) ) -func newSession(username string) *Session { - ses := &Session{username: username} +func newSession(username, sessionID string) *Session { + ses := &Session{username: username, sesID: sessionID} ses.servers = GenerateServers(ses) + + if sessionID == "" { + ses.sesID = strconv.FormatUint(rand.Uint64(), 10) + } + return ses } func (s *Session) ID() string { - return s.username + return s.sesID } func (s *Session) Name() text.Rich { @@ -168,7 +180,8 @@ func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), func (s *Session) Save() (map[string]string, error) { return map[string]string{ - "username": s.username, + "sessionID": s.sesID, + "username": s.username, }, nil }