1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-22 20:36:45 +00:00

added discordlogin; bumped cchat

This commit is contained in:
diamondburned 2020-10-26 22:04:55 -07:00
parent 2209348d23
commit 7ce513cf68
4 changed files with 113 additions and 3 deletions

View file

@ -23,8 +23,11 @@ func (Service) Name() text.Rich {
return text.Rich{Content: "Discord"}
}
func (Service) Authenticate() cchat.Authenticator {
return authenticate.New()
func (Service) Authenticate() []cchat.Authenticator {
return []cchat.Authenticator{
authenticate.New(),
authenticate.NewDiscordLogin(),
}
}
func (Service) AsIconer() cchat.Iconer {

3
go.mod
View file

@ -4,10 +4,11 @@ go 1.14
require (
github.com/diamondburned/arikawa v1.3.6
github.com/diamondburned/cchat v0.3.7
github.com/diamondburned/cchat v0.3.8
github.com/diamondburned/ningen v0.2.1-0.20201023061015-ce64ffb0bb12
github.com/dustin/go-humanize v1.0.0
github.com/go-test/deep v1.0.7
github.com/pkg/errors v0.9.1
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/yuin/goldmark v1.1.30
)

4
go.sum
View file

@ -99,6 +99,8 @@ github.com/diamondburned/cchat v0.3.6 h1:at9bnxlABa3TGscra/cRq/ASsmJh6GCdQ0vnCD9
github.com/diamondburned/cchat v0.3.6/go.mod h1:IlMtF+XIvAJh0GL/2yFdf0/34w+Hdy5A1GgvSwAXtQI=
github.com/diamondburned/cchat v0.3.7 h1:0t3FkbzC/pBRAR3w0uYznJ+7dYqcR1M48a9wgz4JkIg=
github.com/diamondburned/cchat v0.3.7/go.mod h1:IlMtF+XIvAJh0GL/2yFdf0/34w+Hdy5A1GgvSwAXtQI=
github.com/diamondburned/cchat v0.3.8 h1:vgFe8giVfwsAO+WpTYsTDIXvRUN48osVPNu0pZNvPEk=
github.com/diamondburned/cchat v0.3.8/go.mod h1:IlMtF+XIvAJh0GL/2yFdf0/34w+Hdy5A1GgvSwAXtQI=
github.com/diamondburned/ningen v0.1.1-0.20200621014632-6babb812b249 h1:yP7kJ+xCGpDz6XbcfACJcju4SH1XDPwlrvbofz3lP8I=
github.com/diamondburned/ningen v0.1.1-0.20200621014632-6babb812b249/go.mod h1:xW9hpBZsGi8KpAh10TyP+YQlYBo+Xc+2w4TR6N0951A=
github.com/diamondburned/ningen v0.1.1-0.20200708085949-b64e350f3b8c h1:3h/kyk6HplYZF3zLi106itjYJWjbuMK/twijeGLEy2M=
@ -204,6 +206,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/twmb/murmur3 v1.1.3 h1:D83U0XYKcHRYwYIpBKf3Pks91Z0Byda/9SJ8B6EMRcA=

View file

@ -0,0 +1,102 @@
package authenticate
import (
"os"
"os/exec"
"path/filepath"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-discord/internal/discord/session"
"github.com/diamondburned/cchat-discord/internal/discord/state"
"github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
)
var ErrDLNotFound = errors.New("DiscordLogin not found. Please install it from the GitHub page.")
type DiscordLoginAuth struct{}
func NewDiscordLogin() cchat.Authenticator {
return DiscordLoginAuth{}
}
// AuthenticateForm returns an empty slice.
func (DiscordLoginAuth) AuthenticateForm() []cchat.AuthenticateEntry {
return []cchat.AuthenticateEntry{}
}
// Authenticate pops up discordlogin.
func (DiscordLoginAuth) Authenticate([]string) (cchat.Session, error) {
path, err := lookPathExtras("discordlogin")
if err != nil {
openDiscordLoginPage()
return nil, ErrDLNotFound
}
cmd := &exec.Cmd{Path: path}
cmd.Stderr = os.Stderr
// UI will actually block during this time.
b, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(err, "DiscordLogin failed")
}
if len(b) == 0 {
return nil, errors.New("DiscordLogin returned nothing, check Console.")
}
i, err := state.NewFromToken(string(b))
if err != nil {
return nil, err
}
return session.NewFromInstance(i)
}
func openDiscordLoginPage() {
go open.Run("https://github.com/diamondburned/discordlogin")
}
// lookPathExtras searches for PATH as well as GOBIN and GOPATH/bin.
func lookPathExtras(file string) (string, error) {
// Add extra PATHs, just in case:
paths := filepath.SplitList(os.Getenv("PATH"))
if gobin := os.Getenv("GOBIN"); gobin != "" {
paths = append(paths, gobin)
}
if gopath := os.Getenv("GOPATH"); gopath != "" {
paths = append(paths, gopath)
}
if home, err := os.UserHomeDir(); err == nil {
paths = append(paths, filepath.Join(home, "go", "bin"))
}
const filename = "discordlogin"
for _, dir := range paths {
if dir == "" {
dir = "."
}
path := filepath.Join(dir, filename)
if err := findExecutable(path); err == nil {
return path, nil
}
}
return "", exec.ErrNotFound
}
func findExecutable(file string) error {
d, err := os.Stat(file)
if err != nil {
return err
}
if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
return nil
}
return os.ErrPermission
}