-
Notifications
You must be signed in to change notification settings - Fork 3
/
commandhandlers.go
196 lines (164 loc) · 5.29 KB
/
commandhandlers.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package ircbnc
import (
"fmt"
"strings"
"log"
"github.com/goshuirc/irc-go/ircmsg"
)
func loadClientCommands() {
ClientCommands["NICK"] = ClientCommand{
usablePreReg: true,
minParams: 1,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// always reject dodgy nicknames, makes things immensely easier
nick, nickError := IrcName(msg.Params[0], false)
if nickError != nil {
listener.Send(nil, "", "422", listener.ClientNick, msg.Params[0], "Erroneus nickname")
return true
}
// we ignore NICK messages during registration
if !listener.Registered {
listener.ClientNick = nick
listener.regLocks.Set("nick", true)
return true
}
//TODO(dan): Handle NICK messages when connected to servers.
//listener.Send(nil, "", "ERROR", "We're supposed to handle NICK changes here!")
listener.ServerConnection.Nickname = nick
return false
},
}
ClientCommands["USER"] = ClientCommand{
usablePreReg: true,
minParams: 4,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// we ignore the content of USER messages entirely, since we use our internal
// user and realname when actually connecting to servers
if !listener.Registered {
listener.regLocks.Set("user", true)
}
return true
},
}
ClientCommands["PASS"] = ClientCommand{
usablePreReg: true,
minParams: 1,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// only accept PASS before registration finishes
if listener.Registered {
return false
}
splitString := strings.SplitN(msg.Params[0], ":", 2)
if len(splitString) < 2 {
listener.Send(nil, "", "ERROR", `Password must be of the format "<username>/<network>:<password>"`)
listener.Socket.Close()
return true
}
password := splitString[1]
var userid, networkID string
if strings.Contains(splitString[0], "/") {
splitString = strings.Split(splitString[0], "/")
userid, networkID = splitString[0], splitString[1]
} else {
userid = splitString[0]
}
authedUserId, authSuccess := listener.Manager.Ds.AuthUser(userid, password)
if !authSuccess {
listener.Socket.SetFinalData(fmt.Sprintf(":%s 464 %s :Invalid password\n", listener.Manager.Source, listener.ClientNick))
listener.Socket.Close()
return true
}
user := listener.Manager.Users[authedUserId]
listener.User = user
// An empty network ID may be a user logging in just to control his account or networks
if networkID != "" {
network, netExists := user.Networks[networkID]
if netExists {
network.AddListener(listener)
if !network.Foo.Connected {
go network.Connect()
}
} else {
log.Printf("Network %s/%s doesnt exist", userid, networkID)
}
}
listener.regLocks.Set("pass", true)
return true
},
}
ClientCommands["CAP"] = ClientCommand{
usablePreReg: true,
minParams: 1,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// We're starting CAP negotiations so don't complete regisration until then
listener.regLocks.Set("cap", false)
command := strings.ToUpper(getParam(&msg, 0))
if command == "LS" {
capList := Capabilities.SupportedString()
listener.Send(nil, "", "CAP", "*", "LS", capList)
} else if command == "REQ" {
requestedCaps := strings.Split(getParam(&msg, 1), " ")
canUseCaps := Capabilities.FilterSupported(requestedCaps)
// This must be set before any .InitCapOnListener is run just incase a CAP
// being initialized depends on other CAPs being set too.
listener.Caps = canUseCaps
acked := []string{}
for cap := range canUseCaps {
Capabilities.InitCapOnListener(listener, cap)
acked = append(acked, cap)
}
listener.Send(nil, "", "CAP", "*", "ACK", strings.Join(acked, " "))
} else if command == "ENABLED" {
// Not in the spec, but just a handy command to debug caps in the client
line := ""
for cap, val := range listener.Caps {
line += cap
if val != "" {
line += "=" + val
}
line += " "
}
listener.SendLine(fmt.Sprintf(":%s NOTICE %s :%s", listener.Manager.Source, listener.ClientNick, line))
} else if command == "END" {
listener.regLocks.Set("cap", true)
}
return true
},
}
ClientCommands["PING"] = ClientCommand{
usablePreReg: true,
minParams: 1,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// The BNC responds to pings from both the server and client as either
// could be detached at any point.
listener.Send(nil, "", "PONG", msg.Params[0])
return true
},
}
ClientCommands["QUIT"] = ClientCommand{
usablePreReg: true,
minParams: 0,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
// Just ignore it as clients usually send QUIT when the client is closed
return true
},
}
ClientCommands["PART"] = ClientCommand{
usablePreReg: true,
minParams: 1,
handler: func(listener *Listener, msg ircmsg.IrcMessage) bool {
channelName := msg.Params[0]
listener.ServerConnection.Buffers.Remove(channelName)
listener.ServerConnection.Save()
return false
},
}
}
func getParam(msg *ircmsg.IrcMessage, idx int) string {
if len(msg.Params)-1 < idx {
return ""
}
return msg.Params[idx]
}