From 9e84f70c0ff3582d5627a2d6e3230707d4d08ad3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 19 Jul 2015 23:25:42 +0200 Subject: Add duckduckgo search, drop flooder kicker --- main.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 976b00e..ac37d16 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,11 @@ import ( "flag" "fmt" "log" - "regexp" "sort" "strings" "time" + "dim13.org/duck" "dim13.org/rss" "dim13.org/theo" irc "github.com/fluffle/goirc/client" @@ -25,6 +25,7 @@ type Commander interface { irc.Handler fmt.Stringer Timeout() bool + WithArgs(int) bool } type Command struct { @@ -39,6 +40,7 @@ type ( Theo struct{ Command } Help struct{ Command } Top struct{ Command } + Duck struct{ Command } ) var ( @@ -59,6 +61,7 @@ func (v *Command) Timeout() bool { } return true } +func (_ Command) WithArgs(n int) bool { return n == 1 } func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { @@ -111,7 +114,17 @@ func (_ Top) Handle(conn *irc.Conn, line *irc.Line) { conn.Privmsg(line.Target(), s) } -var flood = regexp.MustCompile(". . . .") +func (_ Duck) Timeout() bool { return false } +func (_ Duck) WithArgs(_ int) bool { return true } +func (_ Duck) Handle(conn *irc.Conn, line *irc.Line) { + if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { + if a, err := duck.Abstract(q[1]); err != nil { + conn.Privmsg(line.Target(), err.Error()) + } else { + conn.Privmsg(line.Target(), a) + } + } +} func privmsg(conn *irc.Conn, line *irc.Line) { f := strings.Fields(line.Text()) @@ -123,10 +136,10 @@ func privmsg(conn *irc.Conn, line *irc.Line) { } // lookup command - if len(f) == 1 { + if len(f) > 0 { cmd := strings.ToLower(f[0]) if c, ok := commands[cmd]; ok { - if !line.Public() || !c.Timeout() { + if c.WithArgs(len(f)) && !(line.Public() && c.Timeout()) { log.Println(line.Nick, f) go c.Handle(conn, line) } @@ -148,11 +161,6 @@ func privmsg(conn *irc.Conn, line *irc.Line) { }(v) } } - - if flood.MatchString(line.Text()) { - log.Println("kick", line.Nick) - //conn.Kick(*room, line.Nick, "flood") - } } func init() { @@ -201,6 +209,11 @@ func init() { Help: "Top 10 flooder (msg private to see top 100)", }, }) + Register("define", &Duck{ + Command{ + Help: "Perform duckduckgo instant answer search", + }, + }) } func main() { -- cgit v1.2.3