diff --git a/.gitignore b/.gitignore index 2d29d1c..2c4a144 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.yaml discord +logs/ registration.yaml diff --git a/config/appservice.go b/config/appservice.go index 5fdeb9c..4c118f9 100644 --- a/config/appservice.go +++ b/config/appservice.go @@ -1,5 +1,9 @@ package config +import ( + as "maunium.net/go/mautrix/appservice" +) + type appservice struct { Address string `yaml:"address"` Hostname string `yaml:"hostname"` @@ -49,3 +53,16 @@ func (a *appservice) UnmarshalYAML(unmarshal func(interface{}) error) error { return a.validate() } + +func (cfg *Config) CreateAppService() (*as.AppService, error) { + appservice := as.Create() + + appservice.HomeserverURL = cfg.Homeserver.Address + appservice.HomeserverDomain = cfg.Homeserver.Domain + + appservice.Host.Hostname = cfg.Appservice.Hostname + appservice.Host.Port = cfg.Appservice.Port + appservice.DefaultHTTPRetries = 4 + + return appservice, nil +} diff --git a/config/config.go b/config/config.go index 403aa23..01a5755 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ type Config struct { Homeserver homeserver `yaml:"homeserver"` Appservice appservice `yaml:"appservice"` Bridge bridge `yaml:"bridge"` + Logging logging `yaml:"logging"` } func (cfg *Config) validate() error { @@ -25,6 +26,10 @@ func (cfg *Config) validate() error { return err } + if err := cfg.Logging.validate(); err != nil { + return err + } + return nil } diff --git a/config/logging.go b/config/logging.go new file mode 100644 index 0000000..c40d185 --- /dev/null +++ b/config/logging.go @@ -0,0 +1,89 @@ +package config + +import ( + "errors" + "strings" + + "maunium.net/go/maulogger/v2" + as "maunium.net/go/mautrix/appservice" +) + +type logging as.LogConfig + +func (l *logging) validate() error { + if l.Directory == "" { + l.Directory = "./logs" + } + + if l.FileNameFormat == "" { + l.FileNameFormat = "{{.Date}}-{{.Index}}.log" + } + + if l.FileDateFormat == "" { + l.FileDateFormat = "2006-01-02" + } + + if l.FileMode == 0 { + l.FileMode = 384 + } + + if l.TimestampFormat == "" { + l.TimestampFormat = "Jan _2, 2006 15:04:05" + } + + if l.RawPrintLevel == "" { + l.RawPrintLevel = "debug" + } else { + switch strings.ToUpper(l.RawPrintLevel) { + case "TRACE": + l.PrintLevel = -10 + case "DEBUG": + l.PrintLevel = maulogger.LevelDebug.Severity + case "INFO": + l.PrintLevel = maulogger.LevelInfo.Severity + case "WARN", "WARNING": + l.PrintLevel = maulogger.LevelWarn.Severity + case "ERR", "ERROR": + l.PrintLevel = maulogger.LevelError.Severity + case "FATAL": + l.PrintLevel = maulogger.LevelFatal.Severity + default: + return errors.New("invalid print level " + l.RawPrintLevel) + } + } + + return nil +} + +func (l *logging) UnmarshalYAML(unmarshal func(interface{}) error) error { + type rawLogging logging + + raw := rawLogging{} + if err := unmarshal(&raw); err != nil { + return err + } + + *l = logging(raw) + + return l.validate() +} + +func (cfg *Config) CreateLogger() (maulogger.Logger, error) { + logger := maulogger.Create() + + // create an as.LogConfig from our config so we can configure the logger + realLogConfig := as.LogConfig(cfg.Logging) + realLogConfig.Configure(logger) + + // Set the default logger. + maulogger.DefaultLogger = logger.(*maulogger.BasicLogger) + + // If we were given a filename format attempt to open the file. + if cfg.Logging.FileNameFormat != "" { + if err := maulogger.OpenFile(); err != nil { + return nil, err + } + } + + return logger, nil +} diff --git a/config/registration.go b/config/registration.go index dfed824..c46009d 100644 --- a/config/registration.go +++ b/config/registration.go @@ -31,3 +31,16 @@ func (cfg *Config) CopyToRegistration(registration *as.Registration) error { return nil } + +func (cfg *Config) getRegistration() (*as.Registration, error) { + registration := as.CreateRegistration() + + if err := cfg.CopyToRegistration(registration); err != nil { + return nil, err + } + + registration.AppToken = cfg.Appservice.ASToken + registration.ServerToken = cfg.Appservice.HSToken + + return registration, nil +}