Browse Source

use custime.Duration for more config fields

tags/v2.0.0-rc1
Shivaram Lingamneni 4 years ago
parent
commit
ef161c47ed
6 changed files with 38 additions and 27 deletions
  1. 1
    1
      irc/accounts.go
  2. 14
    19
      irc/config.go
  3. 15
    0
      irc/custime/parseduration.go
  4. 3
    2
      irc/hostserv.go
  5. 4
    4
      irc/server.go
  6. 1
    1
      oragono.yaml

+ 1
- 1
irc/accounts.go View File

@@ -381,7 +381,7 @@ func (am *AccountManager) Register(client *Client, account string, callbackNames
381 381
 	callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue)
382 382
 
383 383
 	var setOptions *buntdb.SetOptions
384
-	ttl := config.Registration.VerifyTimeout
384
+	ttl := time.Duration(config.Registration.VerifyTimeout)
385 385
 	if ttl != 0 {
386 386
 		setOptions = &buntdb.SetOptions{Expires: true, TTL: ttl}
387 387
 	}

+ 14
- 19
irc/config.go View File

@@ -233,9 +233,9 @@ type AccountConfig struct {
233 233
 // AccountRegistrationConfig controls account registration.
234 234
 type AccountRegistrationConfig struct {
235 235
 	Enabled                bool
236
-	EnabledCallbacks       []string      `yaml:"enabled-callbacks"`
237
-	EnabledCredentialTypes []string      `yaml:"-"`
238
-	VerifyTimeout          time.Duration `yaml:"verify-timeout"`
236
+	EnabledCallbacks       []string         `yaml:"enabled-callbacks"`
237
+	EnabledCredentialTypes []string         `yaml:"-"`
238
+	VerifyTimeout          custime.Duration `yaml:"verify-timeout"`
239 239
 	Callbacks              struct {
240 240
 		Mailto struct {
241 241
 			Server string
@@ -263,7 +263,7 @@ type VHostConfig struct {
263 263
 	UserRequests   struct {
264 264
 		Enabled  bool
265 265
 		Channel  string
266
-		Cooldown time.Duration
266
+		Cooldown custime.Duration
267 267
 	} `yaml:"user-requests"`
268 268
 	OfferList []string `yaml:"offer-list"`
269 269
 }
@@ -406,18 +406,17 @@ type Limits struct {
406 406
 
407 407
 // STSConfig controls the STS configuration/
408 408
 type STSConfig struct {
409
-	Enabled        bool
410
-	Duration       time.Duration `yaml:"duration-real"`
411
-	DurationString string        `yaml:"duration"`
412
-	Port           int
413
-	Preload        bool
414
-	STSOnlyBanner  string `yaml:"sts-only-banner"`
415
-	bannerLines    []string
409
+	Enabled       bool
410
+	Duration      custime.Duration
411
+	Port          int
412
+	Preload       bool
413
+	STSOnlyBanner string `yaml:"sts-only-banner"`
414
+	bannerLines   []string
416 415
 }
417 416
 
418 417
 // Value returns the STS value to advertise in CAP
419 418
 func (sts *STSConfig) Value() string {
420
-	val := fmt.Sprintf("duration=%d", int(sts.Duration.Seconds()))
419
+	val := fmt.Sprintf("duration=%d", int(time.Duration(sts.Duration).Seconds()))
421 420
 	if sts.Enabled && sts.Port > 0 {
422 421
 		val += fmt.Sprintf(",port=%d", sts.Port)
423 422
 	}
@@ -553,9 +552,9 @@ type Config struct {
553 552
 		ChathistoryMax   int           `yaml:"chathistory-maxmessages"`
554 553
 		ZNCMax           int           `yaml:"znc-maxmessages"`
555 554
 		Restrictions     struct {
556
-			ExpireTime              time.Duration `yaml:"expire-time"`
557
-			EnforceRegistrationDate bool          `yaml:"enforce-registration-date"`
558
-			GracePeriod             time.Duration `yaml:"grace-period"`
555
+			ExpireTime              custime.Duration `yaml:"expire-time"`
556
+			EnforceRegistrationDate bool             `yaml:"enforce-registration-date"`
557
+			GracePeriod             custime.Duration `yaml:"grace-period"`
559 558
 		}
560 559
 		Persistent struct {
561 560
 			Enabled              bool
@@ -828,10 +827,6 @@ func LoadConfig(filename string) (config *Config, err error) {
828 827
 	}
829 828
 
830 829
 	if config.Server.STS.Enabled {
831
-		config.Server.STS.Duration, err = custime.ParseDuration(config.Server.STS.DurationString)
832
-		if err != nil {
833
-			return nil, fmt.Errorf("Could not parse STS duration: %s", err.Error())
834
-		}
835 830
 		if config.Server.STS.Port < 0 || config.Server.STS.Port > 65535 {
836 831
 			return nil, fmt.Errorf("STS port is incorrect, should be 0 if disabled: %d", config.Server.STS.Port)
837 832
 		}

+ 15
- 0
irc/custime/parseduration.go View File

@@ -182,3 +182,18 @@ func ParseDuration(s string) (time.Duration, error) {
182 182
 	}
183 183
 	return time.Duration(d), nil
184 184
 }
185
+
186
+type Duration time.Duration
187
+
188
+func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
189
+	var orig string
190
+	var err error
191
+	if err = unmarshal(&orig); err != nil {
192
+		return err
193
+	}
194
+	result, err := ParseDuration(orig)
195
+	if err == nil {
196
+		*d = Duration(result)
197
+	}
198
+	return err
199
+}

+ 3
- 2
irc/hostserv.go View File

@@ -7,6 +7,7 @@ import (
7 7
 	"errors"
8 8
 	"fmt"
9 9
 	"regexp"
10
+	"time"
10 11
 
11 12
 	"github.com/oragono/oragono/irc/sno"
12 13
 )
@@ -214,7 +215,7 @@ func hsRequestHandler(server *Server, client *Client, command string, params []s
214 215
 	}
215 216
 
216 217
 	accountName := client.Account()
217
-	_, err := server.accounts.VHostRequest(accountName, vhost, server.Config().Accounts.VHosts.UserRequests.Cooldown)
218
+	_, err := server.accounts.VHostRequest(accountName, vhost, time.Duration(server.Config().Accounts.VHosts.UserRequests.Cooldown))
218 219
 	if err != nil {
219 220
 		if throttled, ok := err.(*vhostThrottleExceeded); ok {
220 221
 			hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before making another request"), throttled.timeRemaining))
@@ -411,7 +412,7 @@ func hsTakeHandler(server *Server, client *Client, command string, params []stri
411 412
 	}
412 413
 
413 414
 	account := client.Account()
414
-	_, err := server.accounts.VHostTake(account, vhost, config.Accounts.VHosts.UserRequests.Cooldown)
415
+	_, err := server.accounts.VHostTake(account, vhost, time.Duration(config.Accounts.VHosts.UserRequests.Cooldown))
415 416
 	if err != nil {
416 417
 		if throttled, ok := err.(*vhostThrottleExceeded); ok {
417 418
 			hsNotice(rb, fmt.Sprintf(client.t("You must wait an additional %v before taking a vhost"), throttled.timeRemaining))

+ 4
- 4
irc/server.go View File

@@ -670,7 +670,7 @@ func (server *Server) applyConfig(config *Config) (err error) {
670 670
 		}
671 671
 	} else {
672 672
 		if config.Datastore.MySQL.Enabled {
673
-			server.historyDB.SetExpireTime(config.History.Restrictions.ExpireTime)
673
+			server.historyDB.SetExpireTime(time.Duration(config.History.Restrictions.ExpireTime))
674 674
 		}
675 675
 	}
676 676
 
@@ -793,7 +793,7 @@ func (server *Server) loadDatastore(config *Config) error {
793 793
 	server.accounts.Initialize(server)
794 794
 
795 795
 	if config.Datastore.MySQL.Enabled {
796
-		server.historyDB.Initialize(server.logger, config.History.Restrictions.ExpireTime)
796
+		server.historyDB.Initialize(server.logger, time.Duration(config.History.Restrictions.ExpireTime))
797 797
 		err = server.historyDB.Open(config.Datastore.MySQL.User, config.Datastore.MySQL.Password, config.Datastore.MySQL.Host, config.Datastore.MySQL.Port, config.Datastore.MySQL.HistoryDatabase)
798 798
 		if err != nil {
799 799
 			server.logger.Error("internal", "could not connect to mysql", err.Error())
@@ -906,11 +906,11 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
906 906
 
907 907
 	var cutoff time.Time
908 908
 	if config.History.Restrictions.ExpireTime != 0 {
909
-		cutoff = time.Now().UTC().Add(-config.History.Restrictions.ExpireTime)
909
+		cutoff = time.Now().UTC().Add(-time.Duration(config.History.Restrictions.ExpireTime))
910 910
 	}
911 911
 	if config.History.Restrictions.EnforceRegistrationDate {
912 912
 		regCutoff := client.historyCutoff()
913
-		regCutoff.Add(-config.History.Restrictions.GracePeriod)
913
+		regCutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
914 914
 		// take the earlier of the two cutoffs
915 915
 		if regCutoff.After(cutoff) {
916 916
 			cutoff = regCutoff

+ 1
- 1
oragono.yaml View File

@@ -716,7 +716,7 @@ history:
716 716
     restrictions:
717 717
         # if this is set, messages older than this cannot be retrieved by anyone
718 718
         # (and will eventually be deleted from persistent storage, if that's enabled)
719
-        #expire-time: 168h # 7 days
719
+        #expire-time: 1w
720 720
 
721 721
         # if this is set, logged-in users cannot retrieve messages older than their
722 722
         # account registration date, and logged-out users cannot retrieve messages

Loading…
Cancel
Save