Skip to content

Commit

Permalink
Improve banning API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkaeriit committed Aug 9, 2024
1 parent 2dc2feb commit afca4b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 2 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,11 @@ func banCMD(line string, u *User) {
banReason = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(line, split[0]), split[len(split)-1]))
}
if err == nil { // there was a duration
unbanTime := time.Now().Add(dur)
victim.ban(victim.Name+" has been banned by "+banner+" for "+dur.String()+" "+banReason, true, unbanTime)
victim.banTemporarily(victim.Name+" has been banned by "+banner+" for "+dur.String()+" "+banReason, dur)
return
}
}
victim.ban(victim.Name+" has been banned by "+banner+" "+banReason, false, time.Now())
victim.banForever(victim.Name + " has been banned by " + banner + " " + banReason)
}

func kickCMD(line string, u *User) {
Expand Down
2 changes: 1 addition & 1 deletion devzat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func performTestBan(t *testing.T, id0 string, id1 string, id2 string, id3 string
r.users[1].id = id1
r.users[2].id = id2
r.users[3].id = id3
r.users[0].ban("Tim is a meany")
r.users[0].banForever("Tim is a meany")
if len(r.users) != 4-usersBanned {
t.Log("Error,", usersBanned, "users should have been kicked but", 4-len(r.users), "have been kicked.")
t.Fail()
Expand Down
18 changes: 13 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func newUser(s ssh.Session) *User {
IDandIPsToTimesJoinedInMin[u.id]--
})
if IDandIPsToTimesJoinedInMin[u.addr] > 6 || IDandIPsToTimesJoinedInMin[u.id] > 6 {
u.ban("", false, time.Now())
u.banForever("")
MainRoom.broadcast(Devbot, u.Name+" has been banned automatically. ID: "+u.id)
return nil
}
Expand Down Expand Up @@ -603,14 +603,23 @@ func (u *User) close(msg string) {
u.room.broadcast("", Red.Paint(" <-- ")+msg)
}

func (u *User) ban(banner string, useTime bool, unbanTime time.Time) {
func (u *User) banForever(banMsg string) {
u.ban(banMsg, false, time.Now())
}

func (u *User) banTemporarily(banMsg string, banDuration time.Duration) {
unbanTime := time.Now().Add(banDuration)
u.ban(banMsg, true, unbanTime)
}

func (u *User) ban(banMsg string, useTime bool, unbanTime time.Time) {
if u.addr == "" && u.id == "" {
return
}
Bans = append(Bans, Ban{u.addr, u.id, useTime, unbanTime})
saveBans()
uid := u.id
u.close(banner)
u.close(banMsg)
for i := range Rooms { // close all users that have this id (including this user)
for j := 0; j < len(Rooms[i].users); j++ {
if Rooms[i].users[j].id == uid {
Expand Down Expand Up @@ -894,8 +903,7 @@ func (u *User) repl() {
if AntispamMessages[u.id] >= 50 {
if getBan(Bans, u.addr, u.id) == nil {
oneMonth, _ := time.ParseDuration("730h")
Bans = append(Bans, Ban{u.addr, u.id, true, time.Now().Add(oneMonth)})
saveBans()
u.banTemporarily("", oneMonth)
}
u.writeln(Devbot, "anti-spam triggered")
u.close(Red.Paint(u.Name + " has been banned for spamming"))
Expand Down

0 comments on commit afca4b7

Please sign in to comment.