summaryrefslogtreecommitdiff
path: root/command.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-07-29 19:56:32 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-07-29 19:56:32 +0200
commitac23f21e41ca06f948f7f299aae1436e439b0637 (patch)
treebca7ebb92238464049c52bbc613c6172f50d08f1 /command.go
parent6e30eab40b911e7abc168f1380465e599395587e (diff)
Add personal timeout
Diffstat (limited to 'command.go')
-rw-r--r--command.go24
1 files changed, 15 insertions, 9 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)
}