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 +++++++++++++++--------- duck.go | 1 - last.go | 1 - rfc.go | 1 - urban.go | 1 - 5 files changed, 15 insertions(+), 13 deletions(-) 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) } diff --git a/duck.go b/duck.go index 2801125..c3df947 100644 --- a/duck.go +++ b/duck.go @@ -9,7 +9,6 @@ import ( type Duck struct{ Command } -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 { diff --git a/last.go b/last.go index ff63bb0..39a6cc3 100644 --- a/last.go +++ b/last.go @@ -11,7 +11,6 @@ type Last struct{ Command } var buffer = ring.New(10) -func (_ Last) Timeout() bool { return false } func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { if v != nil { diff --git a/rfc.go b/rfc.go index b083216..273cc0f 100644 --- a/rfc.go +++ b/rfc.go @@ -16,7 +16,6 @@ var ( rfcKW = make(map[string][]string) ) -func (_ RFC) Timeout() bool { return false } func (_ RFC) WithArgs(n int) bool { return n == 2 } func (_ RFC) Handle(conn *irc.Conn, line *irc.Line) { if f := strings.Fields(line.Text()); len(f) > 1 { diff --git a/urban.go b/urban.go index 0886863..49007fb 100644 --- a/urban.go +++ b/urban.go @@ -10,7 +10,6 @@ import ( type Urban struct{ Command } -func (_ Urban) Timeout() bool { return false } func (_ Urban) WithArgs(_ int) bool { return true } func (_ Urban) Handle(conn *irc.Conn, line *irc.Line) { if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { -- cgit v1.2.3