summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--flood.go38
-rw-r--r--flood_test.go43
2 files changed, 73 insertions, 8 deletions
diff --git a/flood.go b/flood.go
index 59949e7..9ba541e 100644
--- a/flood.go
+++ b/flood.go
@@ -8,10 +8,34 @@ import (
irc "github.com/fluffle/goirc/client"
)
-func MedianLength(v []string) int {
- if len(v) == 0 {
- return 0
+func Flood(s string) bool {
+ if s == "" {
+ return false
+ }
+ v := strings.Fields(s)
+ if CommonWord(v) >= len(v)/2 {
+ return true
+ }
+ if len(v) >= 10 && MedianLength(v) == 1 {
+ return true
+ }
+ return false
+}
+
+func CommonWord(v []string) int {
+ m := make(map[string]int)
+ for _, w := range v {
+ m[w]++
+ }
+ l := make([]int, len(m))
+ for _, n := range m {
+ l = append(l, n)
}
+ sort.Sort(sort.Reverse(sort.IntSlice(l)))
+ return l[0]
+}
+
+func MedianLength(v []string) int {
l := make([]int, len(v))
for i, s := range v {
l[i] = len(s)
@@ -21,10 +45,8 @@ func MedianLength(v []string) int {
}
func DetectFlood(conn *irc.Conn, line *irc.Line) {
- if f := strings.Fields(line.Text()); len(f) > 8 {
- if m := MedianLength(f); m < 2 {
- log.Println("flood", line.Nick)
- conn.Kick(*room, line.Nick, "flood")
- }
+ if Flood(line.Text()) {
+ log.Println("flood", line.Nick)
+ conn.Kick(*room, line.Nick, "flood")
}
}
diff --git a/flood_test.go b/flood_test.go
new file mode 100644
index 0000000..47425ae
--- /dev/null
+++ b/flood_test.go
@@ -0,0 +1,43 @@
+package main
+
+import "testing"
+
+type floodTest struct {
+ Input string
+ Result bool
+}
+
+var floodTestData = []floodTest{
+ {
+ Input: `! ! ! ! ! ! ! !`,
+ Result: true,
+ },
+ {
+ Input: `test test test test`,
+ Result: true,
+ },
+ {
+ Input: `! test ! test ! test ! test !`,
+ Result: true,
+ },
+ {
+ Input: `a b c d e f g h i j`,
+ Result: true,
+ },
+ {
+ Input: `const union __infinity_un __infinity = { { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } };`,
+ Result: false,
+ },
+ {
+ Input: `a = b > 3 ? 2 : 4`,
+ Result: false,
+ },
+}
+
+func TestFlood(t *testing.T) {
+ for _, test := range floodTestData {
+ if Flood(test.Input) != test.Result {
+ t.Error(test.Input, "expected", test.Result)
+ }
+ }
+}