-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd.go
175 lines (146 loc) · 3.83 KB
/
cmd.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package bot
import (
"fmt"
"log"
"regexp"
"strings"
"sync"
"time"
)
// Cmd holds the parsed user's input for easier handling of commands
type Cmd struct {
Raw string // Raw is full string passed to the command
Channel string // Channel where the command was called
Nick string // User who sent the message
Message string // Full string without the prefix
Command string // Command is the first argument passed to the bot
FullArg string // Full argument as a single string
Args []string // Arguments as array
}
// PassiveCmd holds the information which will be passed to passive commands when receiving a message on the channel
type PassiveCmd struct {
Raw string // Raw message sent to the channel
Channel string // Channel which the message was sent to
Nick string // Nick of the user which sent the message
}
type customCommand struct {
Version int
Cmd *regexp.Regexp
CmdFunc activeCmdFunc
}
type incomingMessage struct {
Channel string
Text string
SenderNick string
BotCurrentNick string
}
const (
commandNotAvailable = "Command %v not available."
noCommandsAvailable = "No commands available."
errorExecutingCommand = "Error executing %s: %s"
)
type passiveCmdFunc func(cmd *PassiveCmd) (string, error)
type activeCmdFunc func(cmd *Cmd, matches []string) (string, error)
var (
commands = make(map[string]*customCommand)
passiveCommands = make(map[string]passiveCmdFunc)
)
func RegisterCommand(command string, cmdFunc activeCmdFunc) {
commands[command] = &customCommand{
Cmd: regexp.MustCompile(command),
CmdFunc: cmdFunc,
}
}
func RegisterPassiveCommand(command string, cmdFunc func(cmd *PassiveCmd) (string, error)) {
passiveCommands[command] = cmdFunc
}
func IsPrivateMsg(channel, currentNick string) bool {
return channel == currentNick
}
func IsIgnored(senderNick string) bool {
state := GetUserKey(senderNick, "ignore")
if state == "true" {
return true
} else {
return false
}
}
func IsAdmin(senderNick string) bool {
state := GetUserKey(senderNick, "admin")
if state == "true" {
return true
} else {
return false
}
}
func messageReceived(channel, text, senderNick string, conn ircConnection) {
if IsPrivateMsg(channel, conn.GetNick()) {
channel = senderNick // should reply in private
}
if IsIgnored(senderNick) {
return
}
command := parse(text, channel, senderNick)
if command == nil {
executePassiveCommands(&PassiveCmd{
Raw: text,
Channel: channel,
Nick: senderNick,
}, conn)
return
}
switch command.Command {
case joinCommand:
join(command, channel, senderNick, conn)
case partCommand:
part(command, channel, senderNick, conn)
default:
handleCmd(command, conn)
}
}
func executePassiveCommands(cmd *PassiveCmd, conn ircConnection) {
var wg sync.WaitGroup
for k, v := range passiveCommands {
cmdName := k
cmdFunc := v
wg.Add(1)
go func() {
defer wg.Done()
log.Println("Executing passive command: ", cmdName)
result, err := cmdFunc(cmd)
if err != nil {
log.Println(err)
} else if result != "" {
conn.Privmsg(cmd.Channel, result)
}
}()
}
wg.Wait()
}
func handleCmd(c *Cmd, conn ircConnection) {
for _, k := range commands {
if matches := k.Cmd.FindStringSubmatch(c.Message); len(matches) > 0 {
message, err := k.CmdFunc(c, matches)
checkCmdError(err, c, conn)
msg_arr := strings.Split(message, "\n")
for i, v := range msg_arr {
if v != "" {
// Flood Protection
if i > 0 {
time.Sleep(time.Millisecond * 800)
}
conn.Privmsg(c.Channel, v)
}
}
}
}
// log.Printf("HandleCmd %v %v", c.Command, c.FullArg)
return
}
func checkCmdError(err error, c *Cmd, conn ircConnection) {
if err != nil {
errorMsg := fmt.Sprintf(errorExecutingCommand, c.Command, err.Error())
log.Printf(errorMsg)
conn.Privmsg(c.Channel, errorMsg)
}
}