Update go-nc-talk (nctalk) (#1333)

Signed-off-by: Gary Kim <gary@garykim.dev>
This commit is contained in:
Gary Kim 2020-12-10 07:06:27 +08:00 committed by GitHub
parent 0d7315249d
commit 41b4e64be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 87 additions and 45 deletions

View File

@ -83,6 +83,13 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
go func() { go func() {
for msg := range c { for msg := range c {
msg := msg msg := msg
if msg.Error != nil {
b.Log.Errorf("Fatal message poll error: %s\n", msg.Error)
return
}
// ignore messages that are one of the following // ignore messages that are one of the following
// * not a message from a user // * not a message from a user
// * from ourselves // * from ourselves

2
go.mod
View File

@ -49,7 +49,7 @@ require (
github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2 github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 golang.org/x/image v0.0.0-20200927104501-e162460cd6b5
golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19 golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19
gomod.garykim.dev/nc-talk v0.1.6 gomod.garykim.dev/nc-talk v0.1.7
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
layeh.com/gumble v0.0.0-20200818122324-146f9205029b layeh.com/gumble v0.0.0-20200818122324-146f9205029b
) )

8
go.sum
View File

@ -581,8 +581,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/monaco-io/request v1.0.4 h1:AbogA+IvPOWqyGZIFU7kSb8YS2Jv5Dnl5ncMj8cQV+o= github.com/monaco-io/request v1.0.5 h1:QAJb5m1pCPZUGv3zzTZn7GlQI3q+uJWi7fH9QxDGbm4=
github.com/monaco-io/request v1.0.4/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ= github.com/monaco-io/request v1.0.5/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 h1:oKIteTqeSpenyTrOVj5zkiyCaflLa8B+CD0324otT+o= github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 h1:oKIteTqeSpenyTrOVj5zkiyCaflLa8B+CD0324otT+o=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8= github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8=
@ -1208,8 +1208,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomod.garykim.dev/nc-talk v0.1.6 h1:ZanTsFiAYKwuEJkV5GjMHop5dEGuCNzEDi4kWh/aqYg= gomod.garykim.dev/nc-talk v0.1.7 h1:G2qsiRcyaj5FEADQlulsBAFJHs27tPmH9VtKK+at9SM=
gomod.garykim.dev/nc-talk v0.1.6/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc= gomod.garykim.dev/nc-talk v0.1.7/go.mod h1:DNucAJ6zeaumBEwV5NiYk+Eea8Ca+Q5f+plhz9F7d58=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=

View File

@ -14,17 +14,18 @@ func (c *Client) buildRequest() (err error) {
return return
} }
c.transport = &http.Transport{}
c.applyHTTPHeader() c.applyHTTPHeader()
c.applyBasicAuth() c.applyBasicAuth()
c.applyClient() c.applyClient()
c.applyTimeout() c.applyTimeout()
c.applyCookies() c.applyCookies()
// Apply transport needs to be called before TLSConfig as TLSConfig modifies
// the http transport
c.applyTransport()
c.applyTLSConfig() c.applyTLSConfig()
err = c.applyProxy() err = c.applyProxy()
c.client.Transport = c.transport c.client.Transport = c.Transport
return return
} }
@ -84,15 +85,20 @@ func (c *Client) applyProxy() (err error) {
if proxy, err = url.Parse(c.ProxyURL); err != nil { if proxy, err = url.Parse(c.ProxyURL); err != nil {
return return
} else if proxy != nil { } else if proxy != nil {
c.transport.Proxy = http.ProxyURL(proxy) c.Transport.Proxy = http.ProxyURL(proxy)
} }
} }
return return
} }
func (c *Client) applyTLSConfig() { func (c *Client) applyTLSConfig() {
// &tls.Config{InsecureSkipVerify: true}
if c.TLSConfig != nil { if c.TLSConfig != nil {
c.transport.TLSClientConfig = c.TLSConfig c.Transport.TLSClientConfig = c.TLSConfig
}
}
func (c *Client) applyTransport() {
if c.Transport == nil {
c.Transport = &http.Transport{}
} }
} }

View File

@ -39,12 +39,12 @@ type Client struct {
ContentType ContentType ContentType ContentType
Cookies []*http.Cookie Cookies []*http.Cookie
TLSConfig *tls.Config TLSConfig *tls.Config
Transport *http.Transport
// private // private
client *http.Client client *http.Client
requestURL requestURL requestURL requestURL
req *http.Request req *http.Request
transport *http.Transport
} }
// BasicAuth Add Username:Password as Basic Auth // BasicAuth Add Username:Password as Basic Auth

View File

@ -21,5 +21,5 @@ const (
// RemoteDavEndpoint returns the endpoint for the Dav API for Nextcloud // RemoteDavEndpoint returns the endpoint for the Dav API for Nextcloud
func RemoteDavEndpoint(username string, davType string) string { func RemoteDavEndpoint(username string, davType string) string {
return "/remote.php/dav/" + username + "/" + davType + "/" return "/remote.php/dav/" + davType + "/" + username + "/"
} }

View File

@ -43,7 +43,10 @@ const (
) )
// TalkRoomMessageData describes the data part of a ocs response for a Talk room message // TalkRoomMessageData describes the data part of a ocs response for a Talk room message
//
// Error will be set if a message request ran into an error.
type TalkRoomMessageData struct { type TalkRoomMessageData struct {
Error error `json:"-"`
Message string `json:"message"` Message string `json:"message"`
ID int `json:"id"` ID int `json:"id"`
ActorType ActorType `json:"actorType"` ActorType ActorType `json:"actorType"`

View File

@ -33,10 +33,14 @@ var (
ErrEmptyToken = errors.New("given an empty token") ErrEmptyToken = errors.New("given an empty token")
// ErrRoomNotFound is returned when a room with the given token could not be found // ErrRoomNotFound is returned when a room with the given token could not be found
ErrRoomNotFound = errors.New("room could not be found") ErrRoomNotFound = errors.New("room could not be found")
// ErrUnauthorized is returned when the room could not be accessed due to being unauthorized
ErrUnauthorized = errors.New("unauthorized error when accessing room")
// ErrNotModeratorInLobby is returned when the room is in lobby mode but the user is not a moderator // ErrNotModeratorInLobby is returned when the room is in lobby mode but the user is not a moderator
ErrNotModeratorInLobby = errors.New("room is in lobby mode but user is not a moderator") ErrNotModeratorInLobby = errors.New("room is in lobby mode but user is not a moderator")
// ErrUnexpectedReturnCode is returned when the server did not respond with an expected return code // ErrUnexpectedReturnCode is returned when the server did not respond with an expected return code
ErrUnexpectedReturnCode = errors.New("unexpected return code") ErrUnexpectedReturnCode = errors.New("unexpected return code")
// ErrTooManyRequests is returned if the server returns a 429
ErrTooManyRequests = errors.New("too many requests")
) )
// TalkRoom represents a room in Nextcloud Talk // TalkRoom represents a room in Nextcloud Talk
@ -62,7 +66,7 @@ func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) {
// SendMessage sends a message in the Talk room // SendMessage sends a message in the Talk room
func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) { func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) {
url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParams := map[string]string{ requestParams := map[string]string{
"message": msg, "message": msg,
} }
@ -93,7 +97,7 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
if err != nil { if err != nil {
return nil, err return nil, err
} }
url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParam := map[string]string{ requestParam := map[string]string{
"lookIntoFuture": "1", "lookIntoFuture": "1",
"includeLastKnown": "0", "includeLastKnown": "0",
@ -127,6 +131,24 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
if err != nil { if err != nil {
continue continue
} }
// If it seems that we no longer have access to the chat for one reason or another, stop the goroutine and set error in the next return.
if res.StatusCode == 404 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrRoomNotFound}
return
}
if res.StatusCode == 401 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrUnauthorized}
return
}
if res.StatusCode == 429 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrTooManyRequests}
return
}
if res.StatusCode == 200 { if res.StatusCode == 200 {
lastKnown = res.Header.Get("X-Chat-Last-Given") lastKnown = res.Header.Get("X-Chat-Last-Given")
data, err := ioutil.ReadAll(res.Body) data, err := ioutil.ReadAll(res.Body)
@ -154,7 +176,7 @@ func (t *TalkRoom) TestConnection() error {
if t.Token == "" { if t.Token == "" {
return ErrEmptyToken return ErrEmptyToken
} }
url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParam := map[string]string{ requestParam := map[string]string{
"lookIntoFuture": "0", "lookIntoFuture": "0",
"includeLastKnown": "0", "includeLastKnown": "0",

View File

@ -111,14 +111,14 @@ type RoomInfo struct {
CanLeaveConversation bool `json:"canLeaveConversation"` CanLeaveConversation bool `json:"canLeaveConversation"`
IsFavorite bool `json:"isFavorite"` IsFavorite bool `json:"isFavorite"`
UnreadMention bool `json:"unreadMention"` UnreadMention bool `json:"unreadMention"`
LastMessage ocs.TalkRoomMessageData `json:"lastMessage"` LastMessage *ocs.TalkRoomMessageData `json:"lastMessage"`
} }
// NewUser returns a TalkUser instance // NewUser returns a TalkUser instance
// The url should be the full URL of the Nextcloud instance (e.g. https://cloud.mydomain.me) // The url should be the full URL of the Nextcloud instance (e.g. https://cloud.mydomain.me)
func NewUser(url string, username string, password string, config *TalkUserConfig) (*TalkUser, error) { func NewUser(url string, username string, password string, config *TalkUserConfig) (*TalkUser, error) {
return &TalkUser{ return &TalkUser{
NextcloudURL: url, NextcloudURL: strings.TrimSuffix(url, "/"),
User: username, User: username,
Pass: password, Pass: password,
Config: config, Config: config,
@ -143,8 +143,12 @@ func (t *TalkUser) RequestClient(client request.Client) *request.Client {
// Set Nextcloud URL if there is no host // Set Nextcloud URL if there is no host
if !strings.HasPrefix(client.URL, t.NextcloudURL) { if !strings.HasPrefix(client.URL, t.NextcloudURL) {
if strings.HasPrefix(client.URL, "/") {
client.URL = t.NextcloudURL + client.URL
} else {
client.URL = t.NextcloudURL + "/" + client.URL client.URL = t.NextcloudURL + "/" + client.URL
} }
}
// Set TLS Config // Set TLS Config
if t.Config != nil { if t.Config != nil {

4
vendor/modules.txt vendored
View File

@ -180,7 +180,7 @@ github.com/mgutz/ansi
github.com/missdeer/golib/ic github.com/missdeer/golib/ic
# github.com/mitchellh/mapstructure v1.3.3 # github.com/mitchellh/mapstructure v1.3.3
github.com/mitchellh/mapstructure github.com/mitchellh/mapstructure
# github.com/monaco-io/request v1.0.4 # github.com/monaco-io/request v1.0.5
github.com/monaco-io/request github.com/monaco-io/request
# github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 # github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474
## explicit ## explicit
@ -362,7 +362,7 @@ golang.org/x/text/transform
golang.org/x/text/unicode/bidi golang.org/x/text/unicode/bidi
golang.org/x/text/unicode/norm golang.org/x/text/unicode/norm
golang.org/x/text/width golang.org/x/text/width
# gomod.garykim.dev/nc-talk v0.1.6 # gomod.garykim.dev/nc-talk v0.1.7
## explicit ## explicit
gomod.garykim.dev/nc-talk/constants gomod.garykim.dev/nc-talk/constants
gomod.garykim.dev/nc-talk/ocs gomod.garykim.dev/nc-talk/ocs