Browse Source

Merge pull request #115 from matrix-org/kegan/fail-if-bad-user

400 if a service which needs a syncing client is configured without one
kegan/realm-docs
Kegsay 8 years ago
committed by GitHub
parent
commit
d8265d35ac
  1. 4
      src/github.com/matrix-org/go-neb/api/api.go
  2. 21
      src/github.com/matrix-org/go-neb/api/handlers/service.go
  3. 5
      src/github.com/matrix-org/go-neb/clients/clients.go
  4. 7
      src/github.com/matrix-org/go-neb/matrix/matrix.go
  5. 2
      src/github.com/matrix-org/go-neb/services/echo/echo.go
  6. 2
      src/github.com/matrix-org/go-neb/services/giphy/giphy.go
  7. 7
      src/github.com/matrix-org/go-neb/services/github/github.go
  8. 2
      src/github.com/matrix-org/go-neb/services/guggy/guggy.go
  9. 4
      src/github.com/matrix-org/go-neb/services/jira/jira.go
  10. 8
      src/github.com/matrix-org/go-neb/types/service.go

4
src/github.com/matrix-org/go-neb/api/api.go

@ -61,8 +61,8 @@ type ClientConfig struct {
HomeserverURL string HomeserverURL string
// The matrix access token to authenticate the requests with. // The matrix access token to authenticate the requests with.
AccessToken string AccessToken string
// True to start a sync stream for this user. If false, no /sync goroutine will be
// created and this client won't listen for new events from Matrix. For services
// True to start a sync stream for this user, making this a "syncing client". If false, no
// /sync goroutine will be created and this client won't listen for new events from Matrix. For services
// which only SEND events into Matrix, it may be desirable to set Sync to false to reduce the // which only SEND events into Matrix, it may be desirable to set Sync to false to reduce the
// number of goroutines Go-NEB has to maintain. For services which respond to !commands, // number of goroutines Go-NEB has to maintain. For services which respond to !commands,
// Sync MUST be set to true in order to receive those commands. // Sync MUST be set to true in order to receive those commands.

21
src/github.com/matrix-org/go-neb/api/handlers/service.go

@ -3,6 +3,7 @@ package handlers
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"sync" "sync"
@ -11,6 +12,7 @@ import (
"github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/clients"
"github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/errors" "github.com/matrix-org/go-neb/errors"
"github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/metrics" "github.com/matrix-org/go-neb/metrics"
"github.com/matrix-org/go-neb/polling" "github.com/matrix-org/go-neb/polling"
"github.com/matrix-org/go-neb/types" "github.com/matrix-org/go-neb/types"
@ -103,6 +105,10 @@ func (s *ConfigureService) OnIncomingRequest(req *http.Request) (interface{}, *e
return nil, &errors.HTTPError{err, "Unknown matrix client", 400} return nil, &errors.HTTPError{err, "Unknown matrix client", 400}
} }
if err := checkClientForService(service, client); err != nil {
return nil, &errors.HTTPError{err, err.Error(), 400}
}
if err = service.Register(old, client); err != nil { if err = service.Register(old, client); err != nil {
return nil, &errors.HTTPError{err, "Failed to register service: " + err.Error(), 500} return nil, &errors.HTTPError{err, "Failed to register service: " + err.Error(), 500}
} }
@ -204,3 +210,18 @@ func (h *GetService) OnIncomingRequest(req *http.Request) (interface{}, *errors.
Config types.Service Config types.Service
}{srv.ServiceID(), srv.ServiceType(), srv}, nil }{srv.ServiceID(), srv.ServiceType(), srv}, nil
} }
func checkClientForService(service types.Service, client *matrix.Client) error {
// If there are any commands or expansions for this Service then the service user ID
// MUST be a syncing client or else the Service will never get the incoming command/expansion!
cmds := service.Commands(client)
expans := service.Expansions(client)
if len(cmds) > 0 || len(expans) > 0 {
if !client.ClientConfig.Sync {
return fmt.Errorf(
"Service type '%s' requires a syncing client", service.ServiceType(),
)
}
}
return nil
}

5
src/github.com/matrix-org/go-neb/clients/clients.go

@ -201,11 +201,11 @@ func (c *Clients) onMessageEvent(client *matrix.Client, event *matrix.Event) {
args = strings.Split(body[1:], " ") args = strings.Split(body[1:], " ")
} }
if response := runCommandForService(service.Commands(client, event.RoomID), event, args); response != nil {
if response := runCommandForService(service.Commands(client), event, args); response != nil {
responses = append(responses, response) responses = append(responses, response)
} }
} else { // message isn't a command, it might need expanding } else { // message isn't a command, it might need expanding
expansions := runExpansionsForService(service.Expansions(client, event.RoomID), event, body)
expansions := runExpansionsForService(service.Expansions(client), event, body)
responses = append(responses, expansions...) responses = append(responses, expansions...)
} }
} }
@ -345,6 +345,7 @@ func (c *Clients) newClient(config api.ClientConfig) (*matrix.Client, error) {
client := matrix.NewClient(c.httpClient, homeserverURL, config.AccessToken, config.UserID) client := matrix.NewClient(c.httpClient, homeserverURL, config.AccessToken, config.UserID)
client.NextBatchStorer = nextBatchStore{c.db} client.NextBatchStorer = nextBatchStore{c.db}
client.ClientConfig = config
// TODO: Check that the access token is valid for the userID by peforming // TODO: Check that the access token is valid for the userID by peforming
// a request against the server. // a request against the server.

7
src/github.com/matrix-org/go-neb/matrix/matrix.go

@ -15,8 +15,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/errors"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -25,6 +23,10 @@ import (
"strconv" "strconv"
"sync" "sync"
"time" "time"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/api"
"github.com/matrix-org/go-neb/errors"
) )
var ( var (
@ -58,6 +60,7 @@ type Client struct {
httpClient *http.Client httpClient *http.Client
filterID string filterID string
NextBatchStorer NextBatchStorer NextBatchStorer NextBatchStorer
ClientConfig api.ClientConfig
} }
func (cli *Client) buildURL(urlPath ...string) string { func (cli *Client) buildURL(urlPath ...string) string {

2
src/github.com/matrix-org/go-neb/services/echo/echo.go

@ -19,7 +19,7 @@ type Service struct {
// Commands supported: // Commands supported:
// !echo some message // !echo some message
// Responds with a notice of "some message". // Responds with a notice of "some message".
func (e *Service) Commands(cli *matrix.Client, roomID string) []types.Command {
func (e *Service) Commands(cli *matrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"echo"}, Path: []string{"echo"},

2
src/github.com/matrix-org/go-neb/services/giphy/giphy.go

@ -50,7 +50,7 @@ type Service struct {
// Commands supported: // Commands supported:
// !giphy some search query without quotes // !giphy some search query without quotes
// Responds with a suitable GIF into the same room as the command. // Responds with a suitable GIF into the same room as the command.
func (s *Service) Commands(client *matrix.Client, roomID string) []types.Command {
func (s *Service) Commands(client *matrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"giphy"}, Path: []string{"giphy"},

7
src/github.com/matrix-org/go-neb/services/github/github.go

@ -30,8 +30,7 @@ var ownerRepoRegex = regexp.MustCompile(`^([A-z0-9-_]+)/([A-z0-9-_]+)$`)
// Service contains the Config fields for the Github service. // Service contains the Config fields for the Github service.
// //
// Before you can set up a Github Service, you need to set up a Github Realm. This
// service requires a syncing client.
// Before you can set up a Github Service, you need to set up a Github Realm.
// //
// You can set a "default repository" for a Matrix room by sending a `m.room.bot.options` state event // You can set a "default repository" for a Matrix room by sending a `m.room.bot.options` state event
// which has the following `content`: // which has the following `content`:
@ -155,7 +154,7 @@ func (s *Service) expandIssue(roomID, userID, owner, repo string, issueNum int)
// Responds with the outcome of the issue creation request. This command requires // Responds with the outcome of the issue creation request. This command requires
// a Github account to be linked to the Matrix user ID issuing the command. If there // a Github account to be linked to the Matrix user ID issuing the command. If there
// is no link, it will return a Starter Link instead. // is no link, it will return a Starter Link instead.
func (s *Service) Commands(cli *matrix.Client, roomID string) []types.Command {
func (s *Service) Commands(cli *matrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"github", "create"}, Path: []string{"github", "create"},
@ -172,7 +171,7 @@ func (s *Service) Commands(cli *matrix.Client, roomID string) []types.Command {
// it will also expand strings of the form: // it will also expand strings of the form:
// #12 // #12
// using the default repository. // using the default repository.
func (s *Service) Expansions(cli *matrix.Client, roomID string) []types.Expansion {
func (s *Service) Expansions(cli *matrix.Client) []types.Expansion {
return []types.Expansion{ return []types.Expansion{
types.Expansion{ types.Expansion{
Regexp: ownerRepoIssueRegex, Regexp: ownerRepoIssueRegex,

2
src/github.com/matrix-org/go-neb/services/guggy/guggy.go

@ -47,7 +47,7 @@ type Service struct {
// Commands supported: // Commands supported:
// !guggy some search query without quotes // !guggy some search query without quotes
// Responds with a suitable GIF into the same room as the command. // Responds with a suitable GIF into the same room as the command.
func (s *Service) Commands(client *matrix.Client, roomID string) []types.Command {
func (s *Service) Commands(client *matrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"guggy"}, Path: []string{"guggy"},

4
src/github.com/matrix-org/go-neb/services/jira/jira.go

@ -238,7 +238,7 @@ func (s *Service) expandIssue(roomID, userID string, issueKeyGroups []string) in
// same project key, which project is chosen is undefined. If there // same project key, which project is chosen is undefined. If there
// is no JIRA account linked to the Matrix user ID, it will return a Starter Link // is no JIRA account linked to the Matrix user ID, it will return a Starter Link
// if there is a known public project with that project key. // if there is a known public project with that project key.
func (s *Service) Commands(cli *matrix.Client, roomID string) []types.Command {
func (s *Service) Commands(cli *matrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"jira", "create"}, Path: []string{"jira", "create"},
@ -255,7 +255,7 @@ func (s *Service) Commands(cli *matrix.Client, roomID string) []types.Command {
// to map the project key to a realm, and subsequently the JIRA endpoint to hit. // to map the project key to a realm, and subsequently the JIRA endpoint to hit.
// If there are multiple projects with the same project key in the Service Config, one will // If there are multiple projects with the same project key in the Service Config, one will
// be chosen arbitrarily. // be chosen arbitrarily.
func (s *Service) Expansions(cli *matrix.Client, roomID string) []types.Expansion {
func (s *Service) Expansions(cli *matrix.Client) []types.Expansion {
return []types.Expansion{ return []types.Expansion{
types.Expansion{ types.Expansion{
Regexp: issueKeyRegex, Regexp: issueKeyRegex,

8
src/github.com/matrix-org/go-neb/types/service.go

@ -34,8 +34,8 @@ type Service interface {
ServiceID() string ServiceID() string
// Return the type of service. This string MUST NOT change. // Return the type of service. This string MUST NOT change.
ServiceType() string ServiceType() string
Commands(cli *matrix.Client, roomID string) []Command
Expansions(cli *matrix.Client, roomID string) []Expansion
Commands(cli *matrix.Client) []Command
Expansions(cli *matrix.Client) []Expansion
OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client)
// A lifecycle function which is invoked when the service is being registered. The old service, if one exists, is provided, // A lifecycle function which is invoked when the service is being registered. The old service, if one exists, is provided,
// along with a Client instance for ServiceUserID(). If this function returns an error, the service will not be registered // along with a Client instance for ServiceUserID(). If this function returns an error, the service will not be registered
@ -82,12 +82,12 @@ func (s *DefaultService) ServiceType() string {
} }
// Commands returns no commands. // Commands returns no commands.
func (s *DefaultService) Commands(cli *matrix.Client, roomID string) []Command {
func (s *DefaultService) Commands(cli *matrix.Client) []Command {
return []Command{} return []Command{}
} }
// Expansions returns no expansions. // Expansions returns no expansions.
func (s *DefaultService) Expansions(cli *matrix.Client, roomID string) []Expansion {
func (s *DefaultService) Expansions(cli *matrix.Client) []Expansion {
return []Expansion{} return []Expansion{}
} }

Loading…
Cancel
Save