-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite.go
139 lines (125 loc) · 3.92 KB
/
composite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"log"
"sort"
"strings"
"time"
)
func compositeBans() {
var compositeBanlist []banDataType
//Add our bans first
for _, ban := range ourBanData {
compositeBanlist = append(compositeBanlist, banDataType{
UserName: strings.ToLower(ban.UserName),
Sources: []string{serverConfig.CommunityName},
Reasons: []string{ban.Reason},
Revokes: []bool{ban.Revoked},
Adds: []string{ban.Added}})
}
dupes := 0
//Now add the bans from other servers
for _, server := range serverList.ServerList {
//Only if subscribed, and skip self
if server.LocalData.Subscribed && !strings.EqualFold(server.CommunityName, serverConfig.CommunityName) {
for _, ban := range server.LocalData.BanList {
//Don't composite revoked bans
if !ban.Revoked {
found := false
for ipos, iban := range compositeBanlist {
//Duplicate, add data to existing entry
if strings.EqualFold(iban.UserName, ban.UserName) {
found = true
dupes++
compositeBanlist[ipos].Sources = append(compositeBanlist[ipos].Sources, server.CommunityName)
if server.LocalData.StripReasons {
compositeBanlist[ipos].Reasons = append(compositeBanlist[ipos].Reasons, "")
} else {
compositeBanlist[ipos].Reasons = append(compositeBanlist[ipos].Reasons, ban.Reason)
}
compositeBanlist[ipos].Revokes = append(compositeBanlist[ipos].Revokes, ban.Revoked)
compositeBanlist[ipos].Adds = append(compositeBanlist[ipos].Adds, ban.Added)
break
}
}
//Strip ban reasons if set to
if server.LocalData.StripReasons {
ban.Reason = ""
}
//This isn't already in the list, so add it (avoid dupes)
if !found {
//If we require a reason, and there isn't one... skip
if serverConfig.ServerPrefs.RequireReason && ban.Reason == "" {
continue
}
compositeBanlist = append(compositeBanlist, banDataType{
UserName: strings.ToLower(ban.UserName),
Sources: []string{server.CommunityName},
Reasons: []string{ban.Reason},
Revokes: []bool{ban.Revoked},
Adds: []string{ban.Added}})
}
}
}
}
}
if serverConfig.ServerPrefs.VerboseLogging {
log.Printf("Composited %v bans, %v duplicates.\n", len(compositeBanlist), dupes)
}
//Sort by time added, new to old
sort.Slice(compositeBanlist, func(i, j int) bool {
//Use newest date, if there are multiple sources
var newest_a time.Time = time.Time{}
for _, addA := range compositeBanlist[j].Adds {
bTime, errb := time.Parse(timeFormat, addA)
if errb == nil {
if bTime.After(newest_a) || newest_a.IsZero() {
newest_a = bTime
}
}
}
var newest_b time.Time = time.Time{}
for _, addB := range compositeBanlist[j].Adds {
bTime, errb := time.Parse(timeFormat, addB)
if errb == nil {
if bTime.After(newest_b) || newest_b.IsZero() {
newest_b = bTime
}
}
}
return newest_a.Before(newest_b)
})
//Cut list to size, new entries are at the start
compBan := []banDataType{}
for bpos, ban := range compositeBanlist {
if bpos < serverConfig.ServerPrefs.MaxBanOutputCount {
compBan = append(compBan, ban)
} else {
log.Printf(" Banlist max size exceeded (%v items), truncating...\n", serverConfig.ServerPrefs.MaxBanOutputCount)
break
}
}
if serverConfig.ServerPrefs.VerboseLogging {
log.Printf("Composite banlist updated: %v items.\n", len(compBan))
}
var condList []minBanDataType
//output as a Factorio-friendly list
for _, ban := range compBan {
if !ban.Revoked {
reasonList := ""
for rpos, reason := range ban.Reasons {
if rpos > 0 {
reasonList += ", "
}
if reason != "" {
reasonList += ban.Sources[rpos] + ": " + reason
} else {
reasonList += "(" + ban.Sources[rpos] + ")"
}
}
condList = append(condList, minBanDataType{
UserName: strings.ToLower(ban.UserName), Reason: reasonList})
}
}
compositeBanData = condList
writeCompositeBanlist()
}