summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--command.go13
-rw-r--r--flood.go30
-rw-r--r--href.go19
-rw-r--r--last.go5
-rw-r--r--main.go56
-rw-r--r--score.go2
6 files changed, 73 insertions, 52 deletions
diff --git a/command.go b/command.go
index 1dcb9f1..5c973b6 100644
--- a/command.go
+++ b/command.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
+ "strings"
"time"
irc "github.com/fluffle/goirc/client"
@@ -37,3 +38,15 @@ func (v *Command) Timeout() bool {
return true
}
func (_ Command) WithArgs(n int) bool { return n == 1 }
+
+func Dispatch(conn *irc.Conn, line *irc.Line) {
+ if f := strings.Fields(line.Text()); len(f) > 0 {
+ cmd := strings.ToLower(f[0])
+ if c, ok := commands[cmd]; ok {
+ if c.WithArgs(len(f)) && !(line.Public() && c.Timeout()) {
+ log.Println(line.Nick, f)
+ go c.Handle(conn, line)
+ }
+ }
+ }
+}
diff --git a/flood.go b/flood.go
new file mode 100644
index 0000000..8fdcc05
--- /dev/null
+++ b/flood.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "log"
+ "sort"
+ "strings"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+func MedianLength(v []string) int {
+ if len(v) == 0 {
+ return 0
+ }
+ l := make([]int, len(v))
+ for i, s := range v {
+ l[i] = len(s)
+ }
+ sort.Sort(sort.IntSlice(l))
+ return l[len(l)/2]
+}
+
+func DetectFlood(conn *irc.Conn, line *irc.Line) {
+ if f := strings.Fields(line.Text()); len(f) > 4 {
+ if m := MedianLength(f); m < 2 {
+ log.Println("kick", line.Nick)
+ conn.Kick(*room, line.Nick, "flood")
+ }
+ }
+}
diff --git a/href.go b/href.go
index 3135840..3340a45 100644
--- a/href.go
+++ b/href.go
@@ -2,9 +2,11 @@ package main
import (
"errors"
+ "log"
"net/http"
"strings"
+ irc "github.com/fluffle/goirc/client"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
)
@@ -69,3 +71,20 @@ func FetchTitle(url string) (string, error) {
return title, nil
}
+
+func ExtractLinks(conn *irc.Conn, line *irc.Line) {
+ for _, v := range strings.Fields(line.Text()) {
+ if strings.HasPrefix(v, "http") {
+ go func(url string) {
+ log.Println(line.Nick, url)
+ t, err := FetchTitle(url)
+ if err != nil {
+ log.Println(err)
+ }
+ if t != "" {
+ conn.Privmsg(line.Target(), "Title: "+t)
+ }
+ }(v)
+ }
+ }
+}
diff --git a/last.go b/last.go
index c80e540..51625f4 100644
--- a/last.go
+++ b/last.go
@@ -24,6 +24,11 @@ func (_ Last) Handle(conn *irc.Conn, line *irc.Line) {
})
}
+func Push(line *irc.Line) {
+ buffer.Value = line
+ buffer = buffer.Next()
+}
+
func init() {
Register("last", &Last{
Command{
diff --git a/main.go b/main.go
index 9a2922b..b09d1c1 100644
--- a/main.go
+++ b/main.go
@@ -3,8 +3,6 @@ package main
import (
"flag"
"log"
- "sort"
- "strings"
irc "github.com/fluffle/goirc/client"
)
@@ -15,58 +13,14 @@ var (
name = flag.String("name", "dim13", "Bots Name")
)
-func MedianLength(v []string) int {
- if len(v) == 0 {
- return 0
- }
- l := make([]int, len(v))
- for i, s := range v {
- l[i] = len(s)
- }
- sort.Sort(sort.IntSlice(l))
- return l[len(l)/2]
-}
-
func privmsg(conn *irc.Conn, line *irc.Line) {
- f := strings.Fields(line.Text())
-
if line.Public() && line.Nick != conn.Me().Nick {
- buffer.Value = line
- buffer = buffer.Next()
+ Push(line)
Count(line.Nick)
}
-
- // lookup command
- if len(f) > 0 {
- cmd := strings.ToLower(f[0])
- if c, ok := commands[cmd]; ok {
- if c.WithArgs(len(f)) && !(line.Public() && c.Timeout()) {
- log.Println(line.Nick, f)
- go c.Handle(conn, line)
- }
- }
- }
-
- // extract single link and fetch title
- for _, v := range f {
- if strings.HasPrefix(v, "http") {
- go func(url string) {
- log.Println(line.Nick, url)
- t, err := FetchTitle(url)
- if err != nil {
- log.Println(err)
- }
- if t != "" {
- conn.Privmsg(line.Target(), "Title: "+t)
- }
- }(v)
- }
- }
-
- if m := MedianLength(f); len(f) > 4 && m < 2 {
- log.Println("kick", line.Nick)
- conn.Kick(*room, line.Nick, "flood")
- }
+ Dispatch(conn, line)
+ ExtractLinks(conn, line)
+ DetectFlood(conn, line)
}
func init() {
@@ -91,7 +45,7 @@ func main() {
log.Fatal(err)
}
- go autoSave()
+ go AutoSave()
<-quit
}
diff --git a/score.go b/score.go
index 3edf7c8..1e1b351 100644
--- a/score.go
+++ b/score.go
@@ -91,7 +91,7 @@ func Count(nick string) {
score[nick]++
}
-func autoSave() {
+func AutoSave() {
for {
saveScore(score)
time.Sleep(time.Minute)