diff --git a/dendrite-config.yaml b/dendrite-config.yaml index 6e2326595..394e27757 100644 --- a/dendrite-config.yaml +++ b/dendrite-config.yaml @@ -120,3 +120,7 @@ tracing: # for documtation. jaeger: disabled: true + +# A list of application service config files to use +applicationservice: + app_service_config_files: diff --git a/src/github.com/matrix-org/dendrite/common/config/appservice.go b/src/github.com/matrix-org/dendrite/common/config/appservice.go new file mode 100644 index 000000000..ee513c9fc --- /dev/null +++ b/src/github.com/matrix-org/dendrite/common/config/appservice.go @@ -0,0 +1,79 @@ +// Copyright 2017 Andrew Morgan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "io/ioutil" + "path/filepath" + + "gopkg.in/yaml.v2" +) + +// ApplicationServiceNamespace is the namespace that a specific application +// service has management over. +type ApplicationServiceNamespace struct { + // Whether or not the namespace is managed solely by this application service + Exclusive bool `yaml:"exclusive"` + // A regex pattern that represents the namespace + Regex string `yaml:"regex"` +} + +// ApplicationService represents a Matrix application service. +// https://matrix.org/docs/spec/application_service/unstable.html +type ApplicationService struct { + // User-defined, unique, persistent ID of the application service + ID string `yaml:"id"` + // Base URL of the application service + URL string `yaml:"url"` + // Application service token provided in requests to a homeserver + ASToken string `yaml:"as_token"` + // Homeserver token provided in requests to an application service + HSToken string `yaml:"hs_token"` + // Localpart of application service user + SenderLocalpart string `yaml:"sender_localpart"` + // Information about an application service's namespaces + Namespaces map[string][]interface{} `yaml:"namespaces"` +} + +func loadAppservices(config *Dendrite) error { + // Iterate through and return all the Application Services + for _, configPath := range config.ApplicationService.ConfigFiles { + // Create a new application service + var appservice ApplicationService + + // Create an absolute path from a potentially relative path + absPath, err := filepath.Abs(configPath) + if err != nil { + return err + } + + // Read the application service's config file + configData, err := ioutil.ReadFile(absPath) + if err != nil { + return err + } + + // Load the config data into our struct + if err = yaml.UnmarshalStrict(configData, &appservice); err != nil { + return err + } + + // Append the parsed application service to the global config + config.Derived.ApplicationServices = append( + config.Derived.ApplicationServices, appservice) + } + + return nil +} diff --git a/src/github.com/matrix-org/dendrite/common/config/config.go b/src/github.com/matrix-org/dendrite/common/config/config.go index f291a076d..2b8357648 100644 --- a/src/github.com/matrix-org/dendrite/common/config/config.go +++ b/src/github.com/matrix-org/dendrite/common/config/config.go @@ -206,6 +206,13 @@ type Dendrite struct { Jaeger jaegerconfig.Configuration `yaml:"jaeger"` } + // Application Services + // https://matrix.org/docs/spec/application_service/unstable.html + ApplicationService struct { + // Configuration files for various application services + ConfigFiles []string `yaml:"app_service_config_files"` + } + // Any information derived from the configuration options for later use. Derived struct { Registration struct { @@ -219,6 +226,10 @@ type Dendrite struct { // registration in order to complete registration stages. Params map[string]interface{} `json:"params"` } + + // Application Services parsed from their config files + // The paths of which were given above in the main config file + ApplicationServices []ApplicationService } } @@ -360,6 +371,14 @@ func (config *Dendrite) derive() { config.Derived.Registration.Flows = append(config.Derived.Registration.Flows, authtypes.Flow{Stages: []authtypes.LoginType{authtypes.LoginTypeDummy}}) } + + // Load application service configuration files + fmt.Println(config.ApplicationService.ConfigFiles) + if len(config.ApplicationService.ConfigFiles) > 0 { + if err := loadAppservices(config); err != nil { + fmt.Println("Error loading AS config: ", err) + } + } } // setDefaults sets default config values if they are not explicitly set.