-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterp.go
More file actions
72 lines (61 loc) · 1.7 KB
/
interp.go
File metadata and controls
72 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package axb
import (
"sort"
"strings"
"github.com/kballard/go-shellquote"
"github.com/keybase/go-keybase-chat-bot/kbchat"
)
// BotCommand describes a BotCommand
type BotCommand struct {
Function func(*Bot, *kbchat.SubscriptionMessage, []string) error
AdminRequired bool
}
// IsAdmin returns true if the user passed in is an admin
func (bot *Bot) IsAdmin(user string) bool {
for _, n := range bot.admins {
if user == n {
return true
}
}
return false
}
// IsFromAdmin returns true if the message is from an admin user, false otherwise
func (bot *Bot) IsFromAdmin(msg *kbchat.SubscriptionMessage) bool {
return bot.IsAdmin(msg.Message.Sender.Username)
}
func (bot *Bot) interp(msg *kbchat.SubscriptionMessage, message string) error {
bot.In.Lock()
defer bot.In.Unlock()
oneOnOne := true
// use a tokenizer so that quotes and things are handled right
args, err := shellquote.Split(message)
if err != nil {
return nil // super chatty, cause lots of things don't parse
}
// are you talking to me?
if !strings.Contains(msg.Message.Channel.Name, ",") {
if len(args) == 0 || args[0] != "@"+bot.API().GetUsername() {
return nil
}
oneOnOne = false
args = args[1:]
}
// dumb to do every time but it's just the beginning here
keys := make([]string, 0)
for k := range bot.commands {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if strings.HasPrefix(k, strings.ToLower(args[0])) {
if bot.commands[k].AdminRequired == true && !bot.IsFromAdmin(msg) {
continue
}
return bot.commands[k].Function(bot, msg, args)
}
}
if oneOnOne {
return bot.ReplyTo(msg, "Huh???")
}
return nil
}