From 9478c76ea54a69f09676df6d3c86479723f363d9 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 17 Jul 2015 14:18:34 +0200 Subject: Add cmd timeout and score percent --- main.go | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 7424398..1a2cdaa 100644 --- a/main.go +++ b/main.go @@ -23,19 +23,21 @@ var ( type Commander interface { irc.Handler fmt.Stringer + Timeout() bool } type Command struct { Help string Arg string + Last time.Time } type ( - Last struct{ Command } - RSS struct{ Command } - Theo struct{ Command } - Help struct{ Command } - Top struct{ Command } + Last struct{ Command } + RSS struct{ Command } + Theo struct{ Command } + Help struct{ Command } + Top struct{ Command } ) var ( @@ -48,6 +50,14 @@ func Register(cmd string, f Commander) { } func (v Command) String() string { return v.Help } +func (v *Command) Timeout() bool { + log.Println("timeout:", time.Since(v.Last)) + if time.Since(v.Last) > time.Minute { + v.Last = time.Now() + return false + } + return true +} func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { @@ -109,8 +119,10 @@ func privmsg(conn *irc.Conn, line *irc.Line) { if len(f) == 1 { cmd := strings.ToLower(f[0]) if c, ok := commands[cmd]; ok { - log.Println(line.Nick, f) - go c.Handle(conn, line) + if !line.Public() || !c.Timeout() { + log.Println(line.Nick, f) + go c.Handle(conn, line) + } } } @@ -133,46 +145,46 @@ func privmsg(conn *irc.Conn, line *irc.Line) { func init() { flag.Parse() - Register("last", Last{ + Register("last", &Last{ Command{ Help: "Return last 10 messages", }, }) - Register("theo", Theo{ + Register("theo", &Theo{ Command{ Help: "Quote Theo De Raadt", }, }) - Register("news", RSS{ + Register("news", &RSS{ Command{ Help: "LOR news (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=1`, }, }) - Register("forum", RSS{ + Register("forum", &RSS{ Command{ Help: "LOR forum (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=2`, }, }) - Register("gallery", RSS{ + Register("gallery", &RSS{ Command{ Help: "LOR gallery (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=3`, }, }) - Register("bsd", RSS{ + Register("bsd", &RSS{ Command{ Help: "Undeadly news (msg private to see all)", Arg: `http://undeadly.org/cgi?action=rss`, }, }) - Register("help", Help{ + Register("help", &Help{ Command{ Help: "This help", }, }) - Register("top", Top{ + Register("top", &Top{ Command{ Help: "Top 10 flooder", }, -- cgit v1.2.3