From ac23f21e41ca06f948f7f299aae1436e439b0637 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 29 Jul 2015 19:56:32 +0200 Subject: Add personal timeout --- command.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'command.go') diff --git a/command.go b/command.go index 7f11829..f576b13 100644 --- a/command.go +++ b/command.go @@ -12,14 +12,14 @@ import ( type Commander interface { irc.Handler fmt.Stringer - Timeout() bool + Timeout(string) bool WithArgs(int) bool } type Command struct { Help string Arg string - Last time.Time + Last map[string]time.Time } var commands = make(map[string]Commander) @@ -29,13 +29,18 @@ 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 +func (v *Command) Timeout(nick string) bool { + defer func() { v.Last[nick] = time.Now() }() + if v.Last == nil { + v.Last = make(map[string]time.Time) } - return true + if last, ok := v.Last[nick]; ok { + if time.Since(last) < time.Minute { + log.Println(nick, "timeout", time.Since(last)) + return true + } + } + return false } func (_ Command) WithArgs(n int) bool { return n == 1 } @@ -43,7 +48,8 @@ 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()) { + if c.WithArgs(len(f)) && + !(line.Public() && c.Timeout(line.Nick)) { log.Println(line.Nick, f) c.Handle(conn, line) } -- cgit v1.2.3