diff options
-rw-r--r-- | href.go | 31 | ||||
-rw-r--r-- | main.go | 34 |
2 files changed, 37 insertions, 28 deletions
@@ -2,19 +2,17 @@ package main import ( "errors" + "fmt" "log" "net/http" "strings" - irc "github.com/fluffle/goirc/client" "golang.org/x/net/html" "golang.org/x/net/html/charset" ) var errNotHTML = errors.New("not HTML content") -const maxLen = 500 - func title(n *html.Node) (s string) { if n.Type == html.ElementNode && n.Data == "title" { for c := n.FirstChild; c != nil; c = c.NextSibling { @@ -55,7 +53,7 @@ func getTitle(uri string) (string, error) { return title(doc), nil } -func links(s string) (ret []string) { +func getLinks(s string) (ret []string) { for _, v := range strings.Fields(s) { switch { case strings.HasPrefix(v, "www"): @@ -68,17 +66,20 @@ func links(s string) (ret []string) { return } -func Links(conn *irc.Conn, line *irc.Line) { - for _, v := range links(line.Text()) { - t, err := getTitle(v) - if err != nil { - log.Println(err) - } - if t != "" { - if len(t) > maxLen { - t = t[:maxLen] + "..." +func links(msg chan string) chan string { + c := make(chan string) + go func() { + for l := range c { + for _, v := range getLinks(l) { + t, err := getTitle(v) + if err != nil { + log.Println(err) + } + if t != "" { + msg <- fmt.Sprint("Title:", t) + } } - conn.Notice(line.Target(), "Title: "+t) } - } + }() + return c } @@ -7,10 +7,16 @@ import ( irc "github.com/fluffle/goirc/client" ) +const maxLen = 500 + func notify(conn *irc.Conn, target string) chan string { c := make(chan string) go func() { for msg := range c { + log.Println("send", msg) + if len(msg) > maxLen { + msg = msg[:maxLen] + "..." + } conn.Notice(target, msg) } }() @@ -21,7 +27,10 @@ func kicker(conn *irc.Conn, channel string) chan string { c := make(chan string) go func() { for nick := range c { - conn.Kick(channel, nick) + if nick != conn.Me().Nick { + log.Println("kick", nick) + conn.Kick(channel, nick) + } } }() return c @@ -39,14 +48,14 @@ func discon(c chan struct{}) irc.HandlerFunc { } } -func privmsg(room string) irc.HandlerFunc { +func privmsg(n, k chan string, room string) irc.HandlerFunc { + go watchNews(n) // TODO shall it be there? + l := links(n) return func(conn *irc.Conn, line *irc.Line) { - go Links(conn, line) - if line.Public() && line.Nick != conn.Me().Nick { - if isFlood(line.Text()) { - log.Println("flood", line.Nick) - conn.Kick(room, line.Nick, "flood") - } + t := line.Text() + l <- t + if isFlood(t) { + k <- line.Nick } } } @@ -58,8 +67,10 @@ func main() { flag.Parse() c := irc.SimpleClient(*name) - c.EnableStateTracking() // XXX + //c.EnableStateTracking() // XXX done := make(chan struct{}) + n := notify(c, *room) + k := kicker(c, *room) // setup event handler handler := []struct { @@ -69,7 +80,7 @@ func main() { {irc.DISCONNECTED, discon(done)}, {irc.CONNECTED, join(*room)}, {irc.KICK, join(*room)}, - {irc.PRIVMSG, privmsg(*room)}, + {irc.PRIVMSG, privmsg(n, k, *room)}, } for _, h := range handler { c.HandleFunc(h.ev, h.f) @@ -80,9 +91,6 @@ func main() { log.Fatal(err) } - n := notify(c, *room) - go watchNews(n) - <-done log.Println("Disconnected") } |