...
1 package model
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 . "codeberg.org/tslocum/sriracha/util"
9 )
10
11 type Ban struct {
12 ID int
13 IP string
14 Timestamp int64
15 Expire int64
16 Reason string
17 }
18
19 func (b *Ban) Validate() error {
20 switch {
21 case strings.TrimSpace(b.IP) == "":
22 return fmt.Errorf("IP must be set")
23 case b.Expire < 0:
24 return fmt.Errorf("expiraton must be greater than or equal to zero")
25 }
26 return nil
27 }
28
29 func (b *Ban) TypeLabel() string {
30 if strings.HasPrefix(b.IP, "r ") {
31 return fmt.Sprintf("Range %s", strings.ReplaceAll(strings.ReplaceAll(b.IP[2:], `\.`, "."), ".*", "*"))
32 }
33 return "Address"
34 }
35
36 func (b *Ban) ExpireDate() string {
37 if b.Expire == 0 {
38 return "Never"
39 }
40 return time.Unix(b.Expire, 0).Format("2006-01-02 15:04:05 MST")
41 }
42
43 func (b *Ban) Info() string {
44 var info string
45 if b.Expire == 0 {
46 info += "This ban is permanent."
47 } else {
48 info += fmt.Sprintf("This ban will expire at %s.", FormatRawTimestamp(b.Expire))
49 }
50 if b.Reason != "" {
51 info += " Reason: " + b.Reason
52 }
53 return info
54 }
55
View as plain text