1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "crypto/tls"
|
---|
5 | "encoding/base64"
|
---|
6 | "fmt"
|
---|
7 | "io"
|
---|
8 | "net"
|
---|
9 | "strconv"
|
---|
10 | "strings"
|
---|
11 | "time"
|
---|
12 |
|
---|
13 | "github.com/emersion/go-sasl"
|
---|
14 | "golang.org/x/crypto/bcrypt"
|
---|
15 | "gopkg.in/irc.v3"
|
---|
16 | )
|
---|
17 |
|
---|
18 | type ircError struct {
|
---|
19 | Message *irc.Message
|
---|
20 | }
|
---|
21 |
|
---|
22 | func (err ircError) Error() string {
|
---|
23 | return err.Message.String()
|
---|
24 | }
|
---|
25 |
|
---|
26 | func newUnknownCommandError(cmd string) ircError {
|
---|
27 | return ircError{&irc.Message{
|
---|
28 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
29 | Params: []string{
|
---|
30 | "*",
|
---|
31 | cmd,
|
---|
32 | "Unknown command",
|
---|
33 | },
|
---|
34 | }}
|
---|
35 | }
|
---|
36 |
|
---|
37 | func newNeedMoreParamsError(cmd string) ircError {
|
---|
38 | return ircError{&irc.Message{
|
---|
39 | Command: irc.ERR_NEEDMOREPARAMS,
|
---|
40 | Params: []string{
|
---|
41 | "*",
|
---|
42 | cmd,
|
---|
43 | "Not enough parameters",
|
---|
44 | },
|
---|
45 | }}
|
---|
46 | }
|
---|
47 |
|
---|
48 | func newChatHistoryError(subcommand string, target string) ircError {
|
---|
49 | return ircError{&irc.Message{
|
---|
50 | Command: "FAIL",
|
---|
51 | Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, target, "Messages could not be retrieved"},
|
---|
52 | }}
|
---|
53 | }
|
---|
54 |
|
---|
55 | var errAuthFailed = ircError{&irc.Message{
|
---|
56 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
57 | Params: []string{"*", "Invalid username or password"},
|
---|
58 | }}
|
---|
59 |
|
---|
60 | const illegalNickChars = " :/@!*?"
|
---|
61 |
|
---|
62 | // permanentDownstreamCaps is the list of always-supported downstream
|
---|
63 | // capabilities.
|
---|
64 | var permanentDownstreamCaps = map[string]string{
|
---|
65 | "batch": "",
|
---|
66 | "cap-notify": "",
|
---|
67 | "echo-message": "",
|
---|
68 | "message-tags": "",
|
---|
69 | "sasl": "PLAIN",
|
---|
70 | "server-time": "",
|
---|
71 | }
|
---|
72 |
|
---|
73 | // needAllDownstreamCaps is the list of downstream capabilities that
|
---|
74 | // require support from all upstreams to be enabled
|
---|
75 | var needAllDownstreamCaps = map[string]string{
|
---|
76 | "away-notify": "",
|
---|
77 | "multi-prefix": "",
|
---|
78 | }
|
---|
79 |
|
---|
80 | type downstreamConn struct {
|
---|
81 | conn
|
---|
82 |
|
---|
83 | id uint64
|
---|
84 |
|
---|
85 | registered bool
|
---|
86 | user *user
|
---|
87 | nick string
|
---|
88 | rawUsername string
|
---|
89 | networkName string
|
---|
90 | clientName string
|
---|
91 | realname string
|
---|
92 | hostname string
|
---|
93 | password string // empty after authentication
|
---|
94 | network *network // can be nil
|
---|
95 |
|
---|
96 | negociatingCaps bool
|
---|
97 | capVersion int
|
---|
98 | supportedCaps map[string]string
|
---|
99 | caps map[string]bool
|
---|
100 |
|
---|
101 | saslServer sasl.Server
|
---|
102 | }
|
---|
103 |
|
---|
104 | func newDownstreamConn(srv *Server, ic ircConn, id uint64) *downstreamConn {
|
---|
105 | remoteAddr := ic.RemoteAddr().String()
|
---|
106 | logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", remoteAddr)}
|
---|
107 | options := connOptions{Logger: logger}
|
---|
108 | dc := &downstreamConn{
|
---|
109 | conn: *newConn(srv, ic, &options),
|
---|
110 | id: id,
|
---|
111 | supportedCaps: make(map[string]string),
|
---|
112 | caps: make(map[string]bool),
|
---|
113 | }
|
---|
114 | dc.hostname = remoteAddr
|
---|
115 | if host, _, err := net.SplitHostPort(dc.hostname); err == nil {
|
---|
116 | dc.hostname = host
|
---|
117 | }
|
---|
118 | for k, v := range permanentDownstreamCaps {
|
---|
119 | dc.supportedCaps[k] = v
|
---|
120 | }
|
---|
121 | if srv.LogPath != "" {
|
---|
122 | dc.supportedCaps["draft/chathistory"] = ""
|
---|
123 | }
|
---|
124 | return dc
|
---|
125 | }
|
---|
126 |
|
---|
127 | func (dc *downstreamConn) prefix() *irc.Prefix {
|
---|
128 | return &irc.Prefix{
|
---|
129 | Name: dc.nick,
|
---|
130 | User: dc.user.Username,
|
---|
131 | Host: dc.hostname,
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | func (dc *downstreamConn) forEachNetwork(f func(*network)) {
|
---|
136 | if dc.network != nil {
|
---|
137 | f(dc.network)
|
---|
138 | } else {
|
---|
139 | dc.user.forEachNetwork(f)
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
---|
144 | dc.user.forEachUpstream(func(uc *upstreamConn) {
|
---|
145 | if dc.network != nil && uc.network != dc.network {
|
---|
146 | return
|
---|
147 | }
|
---|
148 | f(uc)
|
---|
149 | })
|
---|
150 | }
|
---|
151 |
|
---|
152 | // upstream returns the upstream connection, if any. If there are zero or if
|
---|
153 | // there are multiple upstream connections, it returns nil.
|
---|
154 | func (dc *downstreamConn) upstream() *upstreamConn {
|
---|
155 | if dc.network == nil {
|
---|
156 | return nil
|
---|
157 | }
|
---|
158 | return dc.network.conn
|
---|
159 | }
|
---|
160 |
|
---|
161 | func isOurNick(net *network, nick string) bool {
|
---|
162 | // TODO: this doesn't account for nick changes
|
---|
163 | if net.conn != nil {
|
---|
164 | return nick == net.conn.nick
|
---|
165 | }
|
---|
166 | // We're not currently connected to the upstream connection, so we don't
|
---|
167 | // know whether this name is our nickname. Best-effort: use the network's
|
---|
168 | // configured nickname and hope it was the one being used when we were
|
---|
169 | // connected.
|
---|
170 | return nick == net.Nick
|
---|
171 | }
|
---|
172 |
|
---|
173 | // marshalEntity converts an upstream entity name (ie. channel or nick) into a
|
---|
174 | // downstream entity name.
|
---|
175 | //
|
---|
176 | // This involves adding a "/<network>" suffix if the entity isn't the current
|
---|
177 | // user.
|
---|
178 | func (dc *downstreamConn) marshalEntity(net *network, name string) string {
|
---|
179 | if isOurNick(net, name) {
|
---|
180 | return dc.nick
|
---|
181 | }
|
---|
182 | if dc.network != nil {
|
---|
183 | if dc.network != net {
|
---|
184 | panic("soju: tried to marshal an entity for another network")
|
---|
185 | }
|
---|
186 | return name
|
---|
187 | }
|
---|
188 | return name + "/" + net.GetName()
|
---|
189 | }
|
---|
190 |
|
---|
191 | func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *irc.Prefix {
|
---|
192 | if isOurNick(net, prefix.Name) {
|
---|
193 | return dc.prefix()
|
---|
194 | }
|
---|
195 | if dc.network != nil {
|
---|
196 | if dc.network != net {
|
---|
197 | panic("soju: tried to marshal a user prefix for another network")
|
---|
198 | }
|
---|
199 | return prefix
|
---|
200 | }
|
---|
201 | return &irc.Prefix{
|
---|
202 | Name: prefix.Name + "/" + net.GetName(),
|
---|
203 | User: prefix.User,
|
---|
204 | Host: prefix.Host,
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | // unmarshalEntity converts a downstream entity name (ie. channel or nick) into
|
---|
209 | // an upstream entity name.
|
---|
210 | //
|
---|
211 | // This involves removing the "/<network>" suffix.
|
---|
212 | func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, error) {
|
---|
213 | if uc := dc.upstream(); uc != nil {
|
---|
214 | return uc, name, nil
|
---|
215 | }
|
---|
216 |
|
---|
217 | var conn *upstreamConn
|
---|
218 | if i := strings.LastIndexByte(name, '/'); i >= 0 {
|
---|
219 | network := name[i+1:]
|
---|
220 | name = name[:i]
|
---|
221 |
|
---|
222 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
223 | if network != uc.network.GetName() {
|
---|
224 | return
|
---|
225 | }
|
---|
226 | conn = uc
|
---|
227 | })
|
---|
228 | }
|
---|
229 |
|
---|
230 | if conn == nil {
|
---|
231 | return nil, "", ircError{&irc.Message{
|
---|
232 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
233 | Params: []string{name, "No such channel"},
|
---|
234 | }}
|
---|
235 | }
|
---|
236 | return conn, name, nil
|
---|
237 | }
|
---|
238 |
|
---|
239 | func (dc *downstreamConn) unmarshalText(uc *upstreamConn, text string) string {
|
---|
240 | if dc.upstream() != nil {
|
---|
241 | return text
|
---|
242 | }
|
---|
243 | // TODO: smarter parsing that ignores URLs
|
---|
244 | return strings.ReplaceAll(text, "/"+uc.network.GetName(), "")
|
---|
245 | }
|
---|
246 |
|
---|
247 | func (dc *downstreamConn) readMessages(ch chan<- event) error {
|
---|
248 | for {
|
---|
249 | msg, err := dc.ReadMessage()
|
---|
250 | if err == io.EOF {
|
---|
251 | break
|
---|
252 | } else if err != nil {
|
---|
253 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
254 | }
|
---|
255 |
|
---|
256 | ch <- eventDownstreamMessage{msg, dc}
|
---|
257 | }
|
---|
258 |
|
---|
259 | return nil
|
---|
260 | }
|
---|
261 |
|
---|
262 | // SendMessage sends an outgoing message.
|
---|
263 | //
|
---|
264 | // This can only called from the user goroutine.
|
---|
265 | func (dc *downstreamConn) SendMessage(msg *irc.Message) {
|
---|
266 | if !dc.caps["message-tags"] {
|
---|
267 | if msg.Command == "TAGMSG" {
|
---|
268 | return
|
---|
269 | }
|
---|
270 | msg = msg.Copy()
|
---|
271 | for name := range msg.Tags {
|
---|
272 | supported := false
|
---|
273 | switch name {
|
---|
274 | case "time":
|
---|
275 | supported = dc.caps["server-time"]
|
---|
276 | }
|
---|
277 | if !supported {
|
---|
278 | delete(msg.Tags, name)
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | dc.conn.SendMessage(msg)
|
---|
284 | }
|
---|
285 |
|
---|
286 | // marshalMessage re-formats a message coming from an upstream connection so
|
---|
287 | // that it's suitable for being sent on this downstream connection. Only
|
---|
288 | // messages that may appear in logs are supported, except MODE.
|
---|
289 | func (dc *downstreamConn) marshalMessage(msg *irc.Message, net *network) *irc.Message {
|
---|
290 | msg = msg.Copy()
|
---|
291 | msg.Prefix = dc.marshalUserPrefix(net, msg.Prefix)
|
---|
292 |
|
---|
293 | switch msg.Command {
|
---|
294 | case "PRIVMSG", "NOTICE", "TAGMSG":
|
---|
295 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
296 | case "NICK":
|
---|
297 | // Nick change for another user
|
---|
298 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
299 | case "JOIN", "PART":
|
---|
300 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
301 | case "KICK":
|
---|
302 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
303 | msg.Params[1] = dc.marshalEntity(net, msg.Params[1])
|
---|
304 | case "TOPIC":
|
---|
305 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
306 | case "QUIT":
|
---|
307 | // This space is intentionally left blank
|
---|
308 | default:
|
---|
309 | panic(fmt.Sprintf("unexpected %q message", msg.Command))
|
---|
310 | }
|
---|
311 |
|
---|
312 | return msg
|
---|
313 | }
|
---|
314 |
|
---|
315 | func (dc *downstreamConn) handleMessage(msg *irc.Message) error {
|
---|
316 | switch msg.Command {
|
---|
317 | case "QUIT":
|
---|
318 | return dc.Close()
|
---|
319 | default:
|
---|
320 | if dc.registered {
|
---|
321 | return dc.handleMessageRegistered(msg)
|
---|
322 | } else {
|
---|
323 | return dc.handleMessageUnregistered(msg)
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
|
---|
329 | switch msg.Command {
|
---|
330 | case "NICK":
|
---|
331 | var nick string
|
---|
332 | if err := parseMessageParams(msg, &nick); err != nil {
|
---|
333 | return err
|
---|
334 | }
|
---|
335 | if strings.ContainsAny(nick, illegalNickChars) {
|
---|
336 | return ircError{&irc.Message{
|
---|
337 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
338 | Params: []string{dc.nick, nick, "contains illegal characters"},
|
---|
339 | }}
|
---|
340 | }
|
---|
341 | if nick == serviceNick {
|
---|
342 | return ircError{&irc.Message{
|
---|
343 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
344 | Params: []string{dc.nick, nick, "Nickname reserved for bouncer service"},
|
---|
345 | }}
|
---|
346 | }
|
---|
347 | dc.nick = nick
|
---|
348 | case "USER":
|
---|
349 | if err := parseMessageParams(msg, &dc.rawUsername, nil, nil, &dc.realname); err != nil {
|
---|
350 | return err
|
---|
351 | }
|
---|
352 | case "PASS":
|
---|
353 | if err := parseMessageParams(msg, &dc.password); err != nil {
|
---|
354 | return err
|
---|
355 | }
|
---|
356 | case "CAP":
|
---|
357 | var subCmd string
|
---|
358 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
359 | return err
|
---|
360 | }
|
---|
361 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
362 | return err
|
---|
363 | }
|
---|
364 | case "AUTHENTICATE":
|
---|
365 | if !dc.caps["sasl"] {
|
---|
366 | return ircError{&irc.Message{
|
---|
367 | Command: irc.ERR_SASLFAIL,
|
---|
368 | Params: []string{"*", "AUTHENTICATE requires the \"sasl\" capability to be enabled"},
|
---|
369 | }}
|
---|
370 | }
|
---|
371 | if len(msg.Params) == 0 {
|
---|
372 | return ircError{&irc.Message{
|
---|
373 | Command: irc.ERR_SASLFAIL,
|
---|
374 | Params: []string{"*", "Missing AUTHENTICATE argument"},
|
---|
375 | }}
|
---|
376 | }
|
---|
377 | if dc.nick == "" {
|
---|
378 | return ircError{&irc.Message{
|
---|
379 | Command: irc.ERR_SASLFAIL,
|
---|
380 | Params: []string{"*", "Expected NICK command before AUTHENTICATE"},
|
---|
381 | }}
|
---|
382 | }
|
---|
383 |
|
---|
384 | var resp []byte
|
---|
385 | if dc.saslServer == nil {
|
---|
386 | mech := strings.ToUpper(msg.Params[0])
|
---|
387 | switch mech {
|
---|
388 | case "PLAIN":
|
---|
389 | dc.saslServer = sasl.NewPlainServer(sasl.PlainAuthenticator(func(identity, username, password string) error {
|
---|
390 | return dc.authenticate(username, password)
|
---|
391 | }))
|
---|
392 | default:
|
---|
393 | return ircError{&irc.Message{
|
---|
394 | Command: irc.ERR_SASLFAIL,
|
---|
395 | Params: []string{"*", fmt.Sprintf("Unsupported SASL mechanism %q", mech)},
|
---|
396 | }}
|
---|
397 | }
|
---|
398 | } else if msg.Params[0] == "*" {
|
---|
399 | dc.saslServer = nil
|
---|
400 | return ircError{&irc.Message{
|
---|
401 | Command: irc.ERR_SASLABORTED,
|
---|
402 | Params: []string{"*", "SASL authentication aborted"},
|
---|
403 | }}
|
---|
404 | } else if msg.Params[0] == "+" {
|
---|
405 | resp = nil
|
---|
406 | } else {
|
---|
407 | // TODO: multi-line messages
|
---|
408 | var err error
|
---|
409 | resp, err = base64.StdEncoding.DecodeString(msg.Params[0])
|
---|
410 | if err != nil {
|
---|
411 | dc.saslServer = nil
|
---|
412 | return ircError{&irc.Message{
|
---|
413 | Command: irc.ERR_SASLFAIL,
|
---|
414 | Params: []string{"*", "Invalid base64-encoded response"},
|
---|
415 | }}
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | challenge, done, err := dc.saslServer.Next(resp)
|
---|
420 | if err != nil {
|
---|
421 | dc.saslServer = nil
|
---|
422 | if ircErr, ok := err.(ircError); ok && ircErr.Message.Command == irc.ERR_PASSWDMISMATCH {
|
---|
423 | return ircError{&irc.Message{
|
---|
424 | Command: irc.ERR_SASLFAIL,
|
---|
425 | Params: []string{"*", ircErr.Message.Params[1]},
|
---|
426 | }}
|
---|
427 | }
|
---|
428 | dc.SendMessage(&irc.Message{
|
---|
429 | Prefix: dc.srv.prefix(),
|
---|
430 | Command: irc.ERR_SASLFAIL,
|
---|
431 | Params: []string{"*", "SASL error"},
|
---|
432 | })
|
---|
433 | return fmt.Errorf("SASL authentication failed: %v", err)
|
---|
434 | } else if done {
|
---|
435 | dc.saslServer = nil
|
---|
436 | dc.SendMessage(&irc.Message{
|
---|
437 | Prefix: dc.srv.prefix(),
|
---|
438 | Command: irc.RPL_LOGGEDIN,
|
---|
439 | Params: []string{dc.nick, dc.prefix().String(), dc.user.Username, "You are now logged in"},
|
---|
440 | })
|
---|
441 | dc.SendMessage(&irc.Message{
|
---|
442 | Prefix: dc.srv.prefix(),
|
---|
443 | Command: irc.RPL_SASLSUCCESS,
|
---|
444 | Params: []string{dc.nick, "SASL authentication successful"},
|
---|
445 | })
|
---|
446 | } else {
|
---|
447 | challengeStr := "+"
|
---|
448 | if len(challenge) > 0 {
|
---|
449 | challengeStr = base64.StdEncoding.EncodeToString(challenge)
|
---|
450 | }
|
---|
451 |
|
---|
452 | // TODO: multi-line messages
|
---|
453 | dc.SendMessage(&irc.Message{
|
---|
454 | Prefix: dc.srv.prefix(),
|
---|
455 | Command: "AUTHENTICATE",
|
---|
456 | Params: []string{challengeStr},
|
---|
457 | })
|
---|
458 | }
|
---|
459 | default:
|
---|
460 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
461 | return newUnknownCommandError(msg.Command)
|
---|
462 | }
|
---|
463 | if dc.rawUsername != "" && dc.nick != "" && !dc.negociatingCaps {
|
---|
464 | return dc.register()
|
---|
465 | }
|
---|
466 | return nil
|
---|
467 | }
|
---|
468 |
|
---|
469 | func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
|
---|
470 | cmd = strings.ToUpper(cmd)
|
---|
471 |
|
---|
472 | replyTo := dc.nick
|
---|
473 | if !dc.registered {
|
---|
474 | replyTo = "*"
|
---|
475 | }
|
---|
476 |
|
---|
477 | switch cmd {
|
---|
478 | case "LS":
|
---|
479 | if len(args) > 0 {
|
---|
480 | var err error
|
---|
481 | if dc.capVersion, err = strconv.Atoi(args[0]); err != nil {
|
---|
482 | return err
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | caps := make([]string, 0, len(dc.supportedCaps))
|
---|
487 | for k, v := range dc.supportedCaps {
|
---|
488 | if dc.capVersion >= 302 && v != "" {
|
---|
489 | caps = append(caps, k+"="+v)
|
---|
490 | } else {
|
---|
491 | caps = append(caps, k)
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | // TODO: multi-line replies
|
---|
496 | dc.SendMessage(&irc.Message{
|
---|
497 | Prefix: dc.srv.prefix(),
|
---|
498 | Command: "CAP",
|
---|
499 | Params: []string{replyTo, "LS", strings.Join(caps, " ")},
|
---|
500 | })
|
---|
501 |
|
---|
502 | if dc.capVersion >= 302 {
|
---|
503 | // CAP version 302 implicitly enables cap-notify
|
---|
504 | dc.caps["cap-notify"] = true
|
---|
505 | }
|
---|
506 |
|
---|
507 | if !dc.registered {
|
---|
508 | dc.negociatingCaps = true
|
---|
509 | }
|
---|
510 | case "LIST":
|
---|
511 | var caps []string
|
---|
512 | for name := range dc.caps {
|
---|
513 | caps = append(caps, name)
|
---|
514 | }
|
---|
515 |
|
---|
516 | // TODO: multi-line replies
|
---|
517 | dc.SendMessage(&irc.Message{
|
---|
518 | Prefix: dc.srv.prefix(),
|
---|
519 | Command: "CAP",
|
---|
520 | Params: []string{replyTo, "LIST", strings.Join(caps, " ")},
|
---|
521 | })
|
---|
522 | case "REQ":
|
---|
523 | if len(args) == 0 {
|
---|
524 | return ircError{&irc.Message{
|
---|
525 | Command: err_invalidcapcmd,
|
---|
526 | Params: []string{replyTo, cmd, "Missing argument in CAP REQ command"},
|
---|
527 | }}
|
---|
528 | }
|
---|
529 |
|
---|
530 | // TODO: atomically ack/nak the whole capability set
|
---|
531 | caps := strings.Fields(args[0])
|
---|
532 | ack := true
|
---|
533 | for _, name := range caps {
|
---|
534 | name = strings.ToLower(name)
|
---|
535 | enable := !strings.HasPrefix(name, "-")
|
---|
536 | if !enable {
|
---|
537 | name = strings.TrimPrefix(name, "-")
|
---|
538 | }
|
---|
539 |
|
---|
540 | if enable == dc.caps[name] {
|
---|
541 | continue
|
---|
542 | }
|
---|
543 |
|
---|
544 | _, ok := dc.supportedCaps[name]
|
---|
545 | if !ok {
|
---|
546 | ack = false
|
---|
547 | break
|
---|
548 | }
|
---|
549 |
|
---|
550 | if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
---|
551 | // cap-notify cannot be disabled with CAP version 302
|
---|
552 | ack = false
|
---|
553 | break
|
---|
554 | }
|
---|
555 |
|
---|
556 | dc.caps[name] = enable
|
---|
557 | }
|
---|
558 |
|
---|
559 | reply := "NAK"
|
---|
560 | if ack {
|
---|
561 | reply = "ACK"
|
---|
562 | }
|
---|
563 | dc.SendMessage(&irc.Message{
|
---|
564 | Prefix: dc.srv.prefix(),
|
---|
565 | Command: "CAP",
|
---|
566 | Params: []string{replyTo, reply, args[0]},
|
---|
567 | })
|
---|
568 | case "END":
|
---|
569 | dc.negociatingCaps = false
|
---|
570 | default:
|
---|
571 | return ircError{&irc.Message{
|
---|
572 | Command: err_invalidcapcmd,
|
---|
573 | Params: []string{replyTo, cmd, "Unknown CAP command"},
|
---|
574 | }}
|
---|
575 | }
|
---|
576 | return nil
|
---|
577 | }
|
---|
578 |
|
---|
579 | func (dc *downstreamConn) setSupportedCap(name, value string) {
|
---|
580 | prevValue, hasPrev := dc.supportedCaps[name]
|
---|
581 | changed := !hasPrev || prevValue != value
|
---|
582 | dc.supportedCaps[name] = value
|
---|
583 |
|
---|
584 | if !dc.caps["cap-notify"] || !changed {
|
---|
585 | return
|
---|
586 | }
|
---|
587 |
|
---|
588 | replyTo := dc.nick
|
---|
589 | if !dc.registered {
|
---|
590 | replyTo = "*"
|
---|
591 | }
|
---|
592 |
|
---|
593 | cap := name
|
---|
594 | if value != "" && dc.capVersion >= 302 {
|
---|
595 | cap = name + "=" + value
|
---|
596 | }
|
---|
597 |
|
---|
598 | dc.SendMessage(&irc.Message{
|
---|
599 | Prefix: dc.srv.prefix(),
|
---|
600 | Command: "CAP",
|
---|
601 | Params: []string{replyTo, "NEW", cap},
|
---|
602 | })
|
---|
603 | }
|
---|
604 |
|
---|
605 | func (dc *downstreamConn) unsetSupportedCap(name string) {
|
---|
606 | _, hasPrev := dc.supportedCaps[name]
|
---|
607 | delete(dc.supportedCaps, name)
|
---|
608 | delete(dc.caps, name)
|
---|
609 |
|
---|
610 | if !dc.caps["cap-notify"] || !hasPrev {
|
---|
611 | return
|
---|
612 | }
|
---|
613 |
|
---|
614 | replyTo := dc.nick
|
---|
615 | if !dc.registered {
|
---|
616 | replyTo = "*"
|
---|
617 | }
|
---|
618 |
|
---|
619 | dc.SendMessage(&irc.Message{
|
---|
620 | Prefix: dc.srv.prefix(),
|
---|
621 | Command: "CAP",
|
---|
622 | Params: []string{replyTo, "DEL", name},
|
---|
623 | })
|
---|
624 | }
|
---|
625 |
|
---|
626 | func (dc *downstreamConn) updateSupportedCaps() {
|
---|
627 | supportedCaps := make(map[string]bool)
|
---|
628 | for cap := range needAllDownstreamCaps {
|
---|
629 | supportedCaps[cap] = true
|
---|
630 | }
|
---|
631 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
632 | for cap, supported := range supportedCaps {
|
---|
633 | supportedCaps[cap] = supported && uc.caps[cap]
|
---|
634 | }
|
---|
635 | })
|
---|
636 |
|
---|
637 | for cap, supported := range supportedCaps {
|
---|
638 | if supported {
|
---|
639 | dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
---|
640 | } else {
|
---|
641 | dc.unsetSupportedCap(cap)
|
---|
642 | }
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | func (dc *downstreamConn) updateNick() {
|
---|
647 | if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
---|
648 | dc.SendMessage(&irc.Message{
|
---|
649 | Prefix: dc.prefix(),
|
---|
650 | Command: "NICK",
|
---|
651 | Params: []string{uc.nick},
|
---|
652 | })
|
---|
653 | dc.nick = uc.nick
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | func sanityCheckServer(addr string) error {
|
---|
658 | dialer := net.Dialer{Timeout: 30 * time.Second}
|
---|
659 | conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
---|
660 | if err != nil {
|
---|
661 | return err
|
---|
662 | }
|
---|
663 | return conn.Close()
|
---|
664 | }
|
---|
665 |
|
---|
666 | func unmarshalUsername(rawUsername string) (username, client, network string) {
|
---|
667 | username = rawUsername
|
---|
668 |
|
---|
669 | i := strings.IndexAny(username, "/@")
|
---|
670 | j := strings.LastIndexAny(username, "/@")
|
---|
671 | if i >= 0 {
|
---|
672 | username = rawUsername[:i]
|
---|
673 | }
|
---|
674 | if j >= 0 {
|
---|
675 | if rawUsername[j] == '@' {
|
---|
676 | client = rawUsername[j+1:]
|
---|
677 | } else {
|
---|
678 | network = rawUsername[j+1:]
|
---|
679 | }
|
---|
680 | }
|
---|
681 | if i >= 0 && j >= 0 && i < j {
|
---|
682 | if rawUsername[i] == '@' {
|
---|
683 | client = rawUsername[i+1 : j]
|
---|
684 | } else {
|
---|
685 | network = rawUsername[i+1 : j]
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | return username, client, network
|
---|
690 | }
|
---|
691 |
|
---|
692 | func (dc *downstreamConn) authenticate(username, password string) error {
|
---|
693 | username, clientName, networkName := unmarshalUsername(username)
|
---|
694 |
|
---|
695 | u, err := dc.srv.db.GetUser(username)
|
---|
696 | if err != nil {
|
---|
697 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
698 | return errAuthFailed
|
---|
699 | }
|
---|
700 |
|
---|
701 | // Password auth disabled
|
---|
702 | if u.Password == "" {
|
---|
703 | return errAuthFailed
|
---|
704 | }
|
---|
705 |
|
---|
706 | err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
---|
707 | if err != nil {
|
---|
708 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
709 | return errAuthFailed
|
---|
710 | }
|
---|
711 |
|
---|
712 | dc.user = dc.srv.getUser(username)
|
---|
713 | if dc.user == nil {
|
---|
714 | dc.logger.Printf("failed authentication for %q: user not active", username)
|
---|
715 | return errAuthFailed
|
---|
716 | }
|
---|
717 | dc.clientName = clientName
|
---|
718 | dc.networkName = networkName
|
---|
719 | return nil
|
---|
720 | }
|
---|
721 |
|
---|
722 | func (dc *downstreamConn) register() error {
|
---|
723 | if dc.registered {
|
---|
724 | return fmt.Errorf("tried to register twice")
|
---|
725 | }
|
---|
726 |
|
---|
727 | password := dc.password
|
---|
728 | dc.password = ""
|
---|
729 | if dc.user == nil {
|
---|
730 | if err := dc.authenticate(dc.rawUsername, password); err != nil {
|
---|
731 | return err
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | if dc.clientName == "" && dc.networkName == "" {
|
---|
736 | _, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
---|
737 | }
|
---|
738 |
|
---|
739 | dc.registered = true
|
---|
740 | dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
---|
741 | return nil
|
---|
742 | }
|
---|
743 |
|
---|
744 | func (dc *downstreamConn) loadNetwork() error {
|
---|
745 | if dc.networkName == "" {
|
---|
746 | return nil
|
---|
747 | }
|
---|
748 |
|
---|
749 | network := dc.user.getNetwork(dc.networkName)
|
---|
750 | if network == nil {
|
---|
751 | addr := dc.networkName
|
---|
752 | if !strings.ContainsRune(addr, ':') {
|
---|
753 | addr = addr + ":6697"
|
---|
754 | }
|
---|
755 |
|
---|
756 | dc.logger.Printf("trying to connect to new network %q", addr)
|
---|
757 | if err := sanityCheckServer(addr); err != nil {
|
---|
758 | dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
---|
759 | return ircError{&irc.Message{
|
---|
760 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
761 | Params: []string{"*", fmt.Sprintf("Failed to connect to %q", dc.networkName)},
|
---|
762 | }}
|
---|
763 | }
|
---|
764 |
|
---|
765 | // Some clients only allow specifying the nickname (and use the
|
---|
766 | // nickname as a username too). Strip the network name from the
|
---|
767 | // nickname when auto-saving networks.
|
---|
768 | nick, _, _ := unmarshalUsername(dc.nick)
|
---|
769 |
|
---|
770 | dc.logger.Printf("auto-saving network %q", dc.networkName)
|
---|
771 | var err error
|
---|
772 | network, err = dc.user.createNetwork(&Network{
|
---|
773 | Addr: dc.networkName,
|
---|
774 | Nick: nick,
|
---|
775 | })
|
---|
776 | if err != nil {
|
---|
777 | return err
|
---|
778 | }
|
---|
779 | }
|
---|
780 |
|
---|
781 | dc.network = network
|
---|
782 | return nil
|
---|
783 | }
|
---|
784 |
|
---|
785 | func (dc *downstreamConn) welcome() error {
|
---|
786 | if dc.user == nil || !dc.registered {
|
---|
787 | panic("tried to welcome an unregistered connection")
|
---|
788 | }
|
---|
789 |
|
---|
790 | // TODO: doing this might take some time. We should do it in dc.register
|
---|
791 | // instead, but we'll potentially be adding a new network and this must be
|
---|
792 | // done in the user goroutine.
|
---|
793 | if err := dc.loadNetwork(); err != nil {
|
---|
794 | return err
|
---|
795 | }
|
---|
796 |
|
---|
797 | dc.SendMessage(&irc.Message{
|
---|
798 | Prefix: dc.srv.prefix(),
|
---|
799 | Command: irc.RPL_WELCOME,
|
---|
800 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
801 | })
|
---|
802 | dc.SendMessage(&irc.Message{
|
---|
803 | Prefix: dc.srv.prefix(),
|
---|
804 | Command: irc.RPL_YOURHOST,
|
---|
805 | Params: []string{dc.nick, "Your host is " + dc.srv.Hostname},
|
---|
806 | })
|
---|
807 | dc.SendMessage(&irc.Message{
|
---|
808 | Prefix: dc.srv.prefix(),
|
---|
809 | Command: irc.RPL_CREATED,
|
---|
810 | Params: []string{dc.nick, "Who cares when the server was created?"},
|
---|
811 | })
|
---|
812 | dc.SendMessage(&irc.Message{
|
---|
813 | Prefix: dc.srv.prefix(),
|
---|
814 | Command: irc.RPL_MYINFO,
|
---|
815 | Params: []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
816 | })
|
---|
817 | // TODO: RPL_ISUPPORT
|
---|
818 | // TODO: send CHATHISTORY in RPL_ISUPPORT when implemented
|
---|
819 | dc.SendMessage(&irc.Message{
|
---|
820 | Prefix: dc.srv.prefix(),
|
---|
821 | Command: irc.ERR_NOMOTD,
|
---|
822 | Params: []string{dc.nick, "No MOTD"},
|
---|
823 | })
|
---|
824 |
|
---|
825 | dc.updateNick()
|
---|
826 |
|
---|
827 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
828 | for _, ch := range uc.channels {
|
---|
829 | if !ch.complete {
|
---|
830 | continue
|
---|
831 | }
|
---|
832 | if record, ok := uc.network.channels[ch.Name]; ok && record.Detached {
|
---|
833 | continue
|
---|
834 | }
|
---|
835 |
|
---|
836 | dc.SendMessage(&irc.Message{
|
---|
837 | Prefix: dc.prefix(),
|
---|
838 | Command: "JOIN",
|
---|
839 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
840 | })
|
---|
841 |
|
---|
842 | forwardChannel(dc, ch)
|
---|
843 | }
|
---|
844 | })
|
---|
845 |
|
---|
846 | dc.forEachNetwork(func(net *network) {
|
---|
847 | // Only send history if we're the first connected client with that name
|
---|
848 | // for the network
|
---|
849 | if _, ok := net.offlineClients[dc.clientName]; ok {
|
---|
850 | dc.sendNetworkHistory(net)
|
---|
851 | delete(net.offlineClients, dc.clientName)
|
---|
852 | }
|
---|
853 | })
|
---|
854 |
|
---|
855 | return nil
|
---|
856 | }
|
---|
857 |
|
---|
858 | func (dc *downstreamConn) sendNetworkHistory(net *network) {
|
---|
859 | if dc.caps["draft/chathistory"] {
|
---|
860 | return
|
---|
861 | }
|
---|
862 | for target, history := range net.history {
|
---|
863 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
864 | continue
|
---|
865 | }
|
---|
866 |
|
---|
867 | seq, ok := history.clients[dc.clientName]
|
---|
868 | if !ok {
|
---|
869 | continue
|
---|
870 | }
|
---|
871 | history.clients[dc.clientName] = history.ring.Cur()
|
---|
872 |
|
---|
873 | consumer := history.ring.NewConsumer(seq)
|
---|
874 |
|
---|
875 | batchRef := "history"
|
---|
876 | if dc.caps["batch"] {
|
---|
877 | dc.SendMessage(&irc.Message{
|
---|
878 | Prefix: dc.srv.prefix(),
|
---|
879 | Command: "BATCH",
|
---|
880 | Params: []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
|
---|
881 | })
|
---|
882 | }
|
---|
883 |
|
---|
884 | for {
|
---|
885 | msg := consumer.Consume()
|
---|
886 | if msg == nil {
|
---|
887 | break
|
---|
888 | }
|
---|
889 |
|
---|
890 | // Don't replay all messages, because that would mess up client
|
---|
891 | // state. For instance we just sent the list of users, sending
|
---|
892 | // PART messages for one of these users would be incorrect.
|
---|
893 | ignore := true
|
---|
894 | switch msg.Command {
|
---|
895 | case "PRIVMSG", "NOTICE":
|
---|
896 | ignore = false
|
---|
897 | }
|
---|
898 | if ignore {
|
---|
899 | continue
|
---|
900 | }
|
---|
901 |
|
---|
902 | if dc.caps["batch"] {
|
---|
903 | msg = msg.Copy()
|
---|
904 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
905 | }
|
---|
906 |
|
---|
907 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
908 | }
|
---|
909 |
|
---|
910 | if dc.caps["batch"] {
|
---|
911 | dc.SendMessage(&irc.Message{
|
---|
912 | Prefix: dc.srv.prefix(),
|
---|
913 | Command: "BATCH",
|
---|
914 | Params: []string{"-" + batchRef},
|
---|
915 | })
|
---|
916 | }
|
---|
917 | }
|
---|
918 | }
|
---|
919 |
|
---|
920 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
921 | for !dc.registered {
|
---|
922 | msg, err := dc.ReadMessage()
|
---|
923 | if err != nil {
|
---|
924 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
925 | }
|
---|
926 |
|
---|
927 | err = dc.handleMessage(msg)
|
---|
928 | if ircErr, ok := err.(ircError); ok {
|
---|
929 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
930 | dc.SendMessage(ircErr.Message)
|
---|
931 | } else if err != nil {
|
---|
932 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
933 | }
|
---|
934 | }
|
---|
935 |
|
---|
936 | return nil
|
---|
937 | }
|
---|
938 |
|
---|
939 | func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
---|
940 | switch msg.Command {
|
---|
941 | case "CAP":
|
---|
942 | var subCmd string
|
---|
943 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
944 | return err
|
---|
945 | }
|
---|
946 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
947 | return err
|
---|
948 | }
|
---|
949 | case "PING":
|
---|
950 | dc.SendMessage(&irc.Message{
|
---|
951 | Prefix: dc.srv.prefix(),
|
---|
952 | Command: "PONG",
|
---|
953 | Params: msg.Params,
|
---|
954 | })
|
---|
955 | return nil
|
---|
956 | case "USER":
|
---|
957 | return ircError{&irc.Message{
|
---|
958 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
959 | Params: []string{dc.nick, "You may not reregister"},
|
---|
960 | }}
|
---|
961 | case "NICK":
|
---|
962 | var nick string
|
---|
963 | if err := parseMessageParams(msg, &nick); err != nil {
|
---|
964 | return err
|
---|
965 | }
|
---|
966 |
|
---|
967 | var upstream *upstreamConn
|
---|
968 | if dc.upstream() == nil {
|
---|
969 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
970 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
971 | upstream = uc
|
---|
972 | nick = unmarshaledNick
|
---|
973 | }
|
---|
974 | }
|
---|
975 |
|
---|
976 | if strings.ContainsAny(nick, illegalNickChars) {
|
---|
977 | return ircError{&irc.Message{
|
---|
978 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
979 | Params: []string{dc.nick, nick, "contains illegal characters"},
|
---|
980 | }}
|
---|
981 | }
|
---|
982 |
|
---|
983 | var err error
|
---|
984 | dc.forEachNetwork(func(n *network) {
|
---|
985 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
986 | return
|
---|
987 | }
|
---|
988 | n.Nick = nick
|
---|
989 | err = dc.srv.db.StoreNetwork(dc.user.Username, &n.Network)
|
---|
990 | })
|
---|
991 | if err != nil {
|
---|
992 | return err
|
---|
993 | }
|
---|
994 |
|
---|
995 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
996 | if upstream != nil && upstream != uc {
|
---|
997 | return
|
---|
998 | }
|
---|
999 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1000 | Command: "NICK",
|
---|
1001 | Params: []string{nick},
|
---|
1002 | })
|
---|
1003 | })
|
---|
1004 |
|
---|
1005 | if dc.upstream() == nil && dc.nick != nick {
|
---|
1006 | dc.SendMessage(&irc.Message{
|
---|
1007 | Prefix: dc.prefix(),
|
---|
1008 | Command: "NICK",
|
---|
1009 | Params: []string{nick},
|
---|
1010 | })
|
---|
1011 | dc.nick = nick
|
---|
1012 | }
|
---|
1013 | case "JOIN":
|
---|
1014 | var namesStr string
|
---|
1015 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
1016 | return err
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | var keys []string
|
---|
1020 | if len(msg.Params) > 1 {
|
---|
1021 | keys = strings.Split(msg.Params[1], ",")
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | for i, name := range strings.Split(namesStr, ",") {
|
---|
1025 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1026 | if err != nil {
|
---|
1027 | return err
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | var key string
|
---|
1031 | if len(keys) > i {
|
---|
1032 | key = keys[i]
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | params := []string{upstreamName}
|
---|
1036 | if key != "" {
|
---|
1037 | params = append(params, key)
|
---|
1038 | }
|
---|
1039 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1040 | Command: "JOIN",
|
---|
1041 | Params: params,
|
---|
1042 | })
|
---|
1043 |
|
---|
1044 | ch := &Channel{Name: upstreamName, Key: key, Detached: false}
|
---|
1045 | if current, ok := uc.network.channels[ch.Name]; ok && key == "" {
|
---|
1046 | // Don't clear the channel key if there's one set
|
---|
1047 | // TODO: add a way to unset the channel key
|
---|
1048 | ch.Key = current.Key
|
---|
1049 | }
|
---|
1050 | if err := uc.network.createUpdateChannel(ch); err != nil {
|
---|
1051 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 | case "PART":
|
---|
1055 | var namesStr string
|
---|
1056 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
1057 | return err
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | var reason string
|
---|
1061 | if len(msg.Params) > 1 {
|
---|
1062 | reason = msg.Params[1]
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | for _, name := range strings.Split(namesStr, ",") {
|
---|
1066 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1067 | if err != nil {
|
---|
1068 | return err
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | if strings.EqualFold(reason, "detach") {
|
---|
1072 | ch := &Channel{Name: upstreamName, Detached: true}
|
---|
1073 | if err := uc.network.createUpdateChannel(ch); err != nil {
|
---|
1074 | dc.logger.Printf("failed to detach channel %q: %v", upstreamName, err)
|
---|
1075 | }
|
---|
1076 | } else {
|
---|
1077 | params := []string{upstreamName}
|
---|
1078 | if reason != "" {
|
---|
1079 | params = append(params, reason)
|
---|
1080 | }
|
---|
1081 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1082 | Command: "PART",
|
---|
1083 | Params: params,
|
---|
1084 | })
|
---|
1085 |
|
---|
1086 | if err := uc.network.deleteChannel(upstreamName); err != nil {
|
---|
1087 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | case "KICK":
|
---|
1092 | var channelStr, userStr string
|
---|
1093 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
1094 | return err
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | channels := strings.Split(channelStr, ",")
|
---|
1098 | users := strings.Split(userStr, ",")
|
---|
1099 |
|
---|
1100 | var reason string
|
---|
1101 | if len(msg.Params) > 2 {
|
---|
1102 | reason = msg.Params[2]
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
1106 | return ircError{&irc.Message{
|
---|
1107 | Command: irc.ERR_BADCHANMASK,
|
---|
1108 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
1109 | }}
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | for i, user := range users {
|
---|
1113 | var channel string
|
---|
1114 | if len(channels) == 1 {
|
---|
1115 | channel = channels[0]
|
---|
1116 | } else {
|
---|
1117 | channel = channels[i]
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
1121 | if err != nil {
|
---|
1122 | return err
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
1126 | if err != nil {
|
---|
1127 | return err
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | if ucChannel != ucUser {
|
---|
1131 | return ircError{&irc.Message{
|
---|
1132 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
1133 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
1134 | }}
|
---|
1135 | }
|
---|
1136 | uc := ucChannel
|
---|
1137 |
|
---|
1138 | params := []string{upstreamChannel, upstreamUser}
|
---|
1139 | if reason != "" {
|
---|
1140 | params = append(params, reason)
|
---|
1141 | }
|
---|
1142 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1143 | Command: "KICK",
|
---|
1144 | Params: params,
|
---|
1145 | })
|
---|
1146 | }
|
---|
1147 | case "MODE":
|
---|
1148 | var name string
|
---|
1149 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
1150 | return err
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | var modeStr string
|
---|
1154 | if len(msg.Params) > 1 {
|
---|
1155 | modeStr = msg.Params[1]
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | if name == dc.nick {
|
---|
1159 | if modeStr != "" {
|
---|
1160 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
1161 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1162 | Command: "MODE",
|
---|
1163 | Params: []string{uc.nick, modeStr},
|
---|
1164 | })
|
---|
1165 | })
|
---|
1166 | } else {
|
---|
1167 | dc.SendMessage(&irc.Message{
|
---|
1168 | Prefix: dc.srv.prefix(),
|
---|
1169 | Command: irc.RPL_UMODEIS,
|
---|
1170 | Params: []string{dc.nick, ""}, // TODO
|
---|
1171 | })
|
---|
1172 | }
|
---|
1173 | return nil
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1177 | if err != nil {
|
---|
1178 | return err
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | if !uc.isChannel(upstreamName) {
|
---|
1182 | return ircError{&irc.Message{
|
---|
1183 | Command: irc.ERR_USERSDONTMATCH,
|
---|
1184 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
1185 | }}
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | if modeStr != "" {
|
---|
1189 | params := []string{upstreamName, modeStr}
|
---|
1190 | params = append(params, msg.Params[2:]...)
|
---|
1191 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1192 | Command: "MODE",
|
---|
1193 | Params: params,
|
---|
1194 | })
|
---|
1195 | } else {
|
---|
1196 | ch, ok := uc.channels[upstreamName]
|
---|
1197 | if !ok {
|
---|
1198 | return ircError{&irc.Message{
|
---|
1199 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
1200 | Params: []string{dc.nick, name, "No such channel"},
|
---|
1201 | }}
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | if ch.modes == nil {
|
---|
1205 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
1206 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
1207 | return nil
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | modeStr, modeParams := ch.modes.Format()
|
---|
1211 | params := []string{dc.nick, name, modeStr}
|
---|
1212 | params = append(params, modeParams...)
|
---|
1213 |
|
---|
1214 | dc.SendMessage(&irc.Message{
|
---|
1215 | Prefix: dc.srv.prefix(),
|
---|
1216 | Command: irc.RPL_CHANNELMODEIS,
|
---|
1217 | Params: params,
|
---|
1218 | })
|
---|
1219 | if ch.creationTime != "" {
|
---|
1220 | dc.SendMessage(&irc.Message{
|
---|
1221 | Prefix: dc.srv.prefix(),
|
---|
1222 | Command: rpl_creationtime,
|
---|
1223 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
1224 | })
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 | case "TOPIC":
|
---|
1228 | var channel string
|
---|
1229 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
1230 | return err
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
1234 | if err != nil {
|
---|
1235 | return err
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | if len(msg.Params) > 1 { // setting topic
|
---|
1239 | topic := msg.Params[1]
|
---|
1240 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1241 | Command: "TOPIC",
|
---|
1242 | Params: []string{upstreamChannel, topic},
|
---|
1243 | })
|
---|
1244 | } else { // getting topic
|
---|
1245 | ch, ok := uc.channels[upstreamChannel]
|
---|
1246 | if !ok {
|
---|
1247 | return ircError{&irc.Message{
|
---|
1248 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
1249 | Params: []string{dc.nick, upstreamChannel, "No such channel"},
|
---|
1250 | }}
|
---|
1251 | }
|
---|
1252 | sendTopic(dc, ch)
|
---|
1253 | }
|
---|
1254 | case "LIST":
|
---|
1255 | // TODO: support ELIST when supported by all upstreams
|
---|
1256 |
|
---|
1257 | pl := pendingLIST{
|
---|
1258 | downstreamID: dc.id,
|
---|
1259 | pendingCommands: make(map[int64]*irc.Message),
|
---|
1260 | }
|
---|
1261 | var upstream *upstreamConn
|
---|
1262 | var upstreamChannels map[int64][]string
|
---|
1263 | if len(msg.Params) > 0 {
|
---|
1264 | uc, upstreamMask, err := dc.unmarshalEntity(msg.Params[0])
|
---|
1265 | if err == nil && upstreamMask == "*" { // LIST */network: send LIST only to one network
|
---|
1266 | upstream = uc
|
---|
1267 | } else {
|
---|
1268 | upstreamChannels = make(map[int64][]string)
|
---|
1269 | channels := strings.Split(msg.Params[0], ",")
|
---|
1270 | for _, channel := range channels {
|
---|
1271 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
1272 | if err != nil {
|
---|
1273 | return err
|
---|
1274 | }
|
---|
1275 | upstreamChannels[uc.network.ID] = append(upstreamChannels[uc.network.ID], upstreamChannel)
|
---|
1276 | }
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | dc.user.pendingLISTs = append(dc.user.pendingLISTs, pl)
|
---|
1281 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
1282 | if upstream != nil && upstream != uc {
|
---|
1283 | return
|
---|
1284 | }
|
---|
1285 | var params []string
|
---|
1286 | if upstreamChannels != nil {
|
---|
1287 | if channels, ok := upstreamChannels[uc.network.ID]; ok {
|
---|
1288 | params = []string{strings.Join(channels, ",")}
|
---|
1289 | } else {
|
---|
1290 | return
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 | pl.pendingCommands[uc.network.ID] = &irc.Message{
|
---|
1294 | Command: "LIST",
|
---|
1295 | Params: params,
|
---|
1296 | }
|
---|
1297 | uc.trySendLIST(dc.id)
|
---|
1298 | })
|
---|
1299 | case "NAMES":
|
---|
1300 | if len(msg.Params) == 0 {
|
---|
1301 | dc.SendMessage(&irc.Message{
|
---|
1302 | Prefix: dc.srv.prefix(),
|
---|
1303 | Command: irc.RPL_ENDOFNAMES,
|
---|
1304 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
1305 | })
|
---|
1306 | return nil
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | channels := strings.Split(msg.Params[0], ",")
|
---|
1310 | for _, channel := range channels {
|
---|
1311 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
1312 | if err != nil {
|
---|
1313 | return err
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | ch, ok := uc.channels[upstreamChannel]
|
---|
1317 | if ok {
|
---|
1318 | sendNames(dc, ch)
|
---|
1319 | } else {
|
---|
1320 | // NAMES on a channel we have not joined, ask upstream
|
---|
1321 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1322 | Command: "NAMES",
|
---|
1323 | Params: []string{upstreamChannel},
|
---|
1324 | })
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | case "WHO":
|
---|
1328 | if len(msg.Params) == 0 {
|
---|
1329 | // TODO: support WHO without parameters
|
---|
1330 | dc.SendMessage(&irc.Message{
|
---|
1331 | Prefix: dc.srv.prefix(),
|
---|
1332 | Command: irc.RPL_ENDOFWHO,
|
---|
1333 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
1334 | })
|
---|
1335 | return nil
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | // TODO: support WHO masks
|
---|
1339 | entity := msg.Params[0]
|
---|
1340 |
|
---|
1341 | if entity == dc.nick {
|
---|
1342 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
1343 | dc.SendMessage(&irc.Message{
|
---|
1344 | Prefix: dc.srv.prefix(),
|
---|
1345 | Command: irc.RPL_WHOREPLY,
|
---|
1346 | Params: []string{dc.nick, "*", dc.user.Username, dc.hostname, dc.srv.Hostname, dc.nick, "H", "0 " + dc.realname},
|
---|
1347 | })
|
---|
1348 | dc.SendMessage(&irc.Message{
|
---|
1349 | Prefix: dc.srv.prefix(),
|
---|
1350 | Command: irc.RPL_ENDOFWHO,
|
---|
1351 | Params: []string{dc.nick, dc.nick, "End of /WHO list"},
|
---|
1352 | })
|
---|
1353 | return nil
|
---|
1354 | }
|
---|
1355 | if entity == serviceNick {
|
---|
1356 | dc.SendMessage(&irc.Message{
|
---|
1357 | Prefix: dc.srv.prefix(),
|
---|
1358 | Command: irc.RPL_WHOREPLY,
|
---|
1359 | Params: []string{serviceNick, "*", servicePrefix.User, servicePrefix.Host, dc.srv.Hostname, serviceNick, "H", "0 " + serviceRealname},
|
---|
1360 | })
|
---|
1361 | dc.SendMessage(&irc.Message{
|
---|
1362 | Prefix: dc.srv.prefix(),
|
---|
1363 | Command: irc.RPL_ENDOFWHO,
|
---|
1364 | Params: []string{dc.nick, serviceNick, "End of /WHO list"},
|
---|
1365 | })
|
---|
1366 | return nil
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | uc, upstreamName, err := dc.unmarshalEntity(entity)
|
---|
1370 | if err != nil {
|
---|
1371 | return err
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | var params []string
|
---|
1375 | if len(msg.Params) == 2 {
|
---|
1376 | params = []string{upstreamName, msg.Params[1]}
|
---|
1377 | } else {
|
---|
1378 | params = []string{upstreamName}
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1382 | Command: "WHO",
|
---|
1383 | Params: params,
|
---|
1384 | })
|
---|
1385 | case "WHOIS":
|
---|
1386 | if len(msg.Params) == 0 {
|
---|
1387 | return ircError{&irc.Message{
|
---|
1388 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
1389 | Params: []string{dc.nick, "No nickname given"},
|
---|
1390 | }}
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | var target, mask string
|
---|
1394 | if len(msg.Params) == 1 {
|
---|
1395 | target = ""
|
---|
1396 | mask = msg.Params[0]
|
---|
1397 | } else {
|
---|
1398 | target = msg.Params[0]
|
---|
1399 | mask = msg.Params[1]
|
---|
1400 | }
|
---|
1401 | // TODO: support multiple WHOIS users
|
---|
1402 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
1403 | mask = mask[:i]
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | if mask == dc.nick {
|
---|
1407 | dc.SendMessage(&irc.Message{
|
---|
1408 | Prefix: dc.srv.prefix(),
|
---|
1409 | Command: irc.RPL_WHOISUSER,
|
---|
1410 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
1411 | })
|
---|
1412 | dc.SendMessage(&irc.Message{
|
---|
1413 | Prefix: dc.srv.prefix(),
|
---|
1414 | Command: irc.RPL_WHOISSERVER,
|
---|
1415 | Params: []string{dc.nick, dc.nick, dc.srv.Hostname, "soju"},
|
---|
1416 | })
|
---|
1417 | dc.SendMessage(&irc.Message{
|
---|
1418 | Prefix: dc.srv.prefix(),
|
---|
1419 | Command: irc.RPL_ENDOFWHOIS,
|
---|
1420 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
1421 | })
|
---|
1422 | return nil
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | // TODO: support WHOIS masks
|
---|
1426 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
1427 | if err != nil {
|
---|
1428 | return err
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | var params []string
|
---|
1432 | if target != "" {
|
---|
1433 | if target == mask { // WHOIS nick nick
|
---|
1434 | params = []string{upstreamNick, upstreamNick}
|
---|
1435 | } else {
|
---|
1436 | params = []string{target, upstreamNick}
|
---|
1437 | }
|
---|
1438 | } else {
|
---|
1439 | params = []string{upstreamNick}
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1443 | Command: "WHOIS",
|
---|
1444 | Params: params,
|
---|
1445 | })
|
---|
1446 | case "PRIVMSG":
|
---|
1447 | var targetsStr, text string
|
---|
1448 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
1449 | return err
|
---|
1450 | }
|
---|
1451 | tags := copyClientTags(msg.Tags)
|
---|
1452 |
|
---|
1453 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
1454 | if name == serviceNick {
|
---|
1455 | handleServicePRIVMSG(dc, text)
|
---|
1456 | continue
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1460 | if err != nil {
|
---|
1461 | return err
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | if upstreamName == "NickServ" {
|
---|
1465 | dc.handleNickServPRIVMSG(uc, text)
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | unmarshaledText := text
|
---|
1469 | if uc.isChannel(upstreamName) {
|
---|
1470 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
1471 | }
|
---|
1472 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1473 | Tags: tags,
|
---|
1474 | Command: "PRIVMSG",
|
---|
1475 | Params: []string{upstreamName, unmarshaledText},
|
---|
1476 | })
|
---|
1477 |
|
---|
1478 | echoTags := tags.Copy()
|
---|
1479 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
1480 | echoMsg := &irc.Message{
|
---|
1481 | Tags: echoTags,
|
---|
1482 | Prefix: &irc.Prefix{
|
---|
1483 | Name: uc.nick,
|
---|
1484 | User: uc.username,
|
---|
1485 | },
|
---|
1486 | Command: "PRIVMSG",
|
---|
1487 | Params: []string{upstreamName, text},
|
---|
1488 | }
|
---|
1489 | uc.produce(upstreamName, echoMsg, dc)
|
---|
1490 | }
|
---|
1491 | case "NOTICE":
|
---|
1492 | var targetsStr, text string
|
---|
1493 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
1494 | return err
|
---|
1495 | }
|
---|
1496 | tags := copyClientTags(msg.Tags)
|
---|
1497 |
|
---|
1498 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
1499 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1500 | if err != nil {
|
---|
1501 | return err
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | unmarshaledText := text
|
---|
1505 | if uc.isChannel(upstreamName) {
|
---|
1506 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
1507 | }
|
---|
1508 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1509 | Tags: tags,
|
---|
1510 | Command: "NOTICE",
|
---|
1511 | Params: []string{upstreamName, unmarshaledText},
|
---|
1512 | })
|
---|
1513 | }
|
---|
1514 | case "TAGMSG":
|
---|
1515 | var targetsStr string
|
---|
1516 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
1517 | return err
|
---|
1518 | }
|
---|
1519 | tags := copyClientTags(msg.Tags)
|
---|
1520 |
|
---|
1521 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
1522 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
1523 | if err != nil {
|
---|
1524 | return err
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1528 | Tags: tags,
|
---|
1529 | Command: "TAGMSG",
|
---|
1530 | Params: []string{upstreamName},
|
---|
1531 | })
|
---|
1532 | }
|
---|
1533 | case "INVITE":
|
---|
1534 | var user, channel string
|
---|
1535 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
1536 | return err
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
1540 | if err != nil {
|
---|
1541 | return err
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
1545 | if err != nil {
|
---|
1546 | return err
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | if ucChannel != ucUser {
|
---|
1550 | return ircError{&irc.Message{
|
---|
1551 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
1552 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
1553 | }}
|
---|
1554 | }
|
---|
1555 | uc := ucChannel
|
---|
1556 |
|
---|
1557 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
1558 | Command: "INVITE",
|
---|
1559 | Params: []string{upstreamUser, upstreamChannel},
|
---|
1560 | })
|
---|
1561 | case "CHATHISTORY":
|
---|
1562 | var subcommand string
|
---|
1563 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
1564 | return err
|
---|
1565 | }
|
---|
1566 | var target, criteria, limitStr string
|
---|
1567 | if err := parseMessageParams(msg, nil, &target, &criteria, &limitStr); err != nil {
|
---|
1568 | return ircError{&irc.Message{
|
---|
1569 | Command: "FAIL",
|
---|
1570 | Params: []string{"CHATHISTORY", "NEED_MORE_PARAMS", subcommand, "Missing parameters"},
|
---|
1571 | }}
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | if dc.srv.LogPath == "" {
|
---|
1575 | return ircError{&irc.Message{
|
---|
1576 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
1577 | Params: []string{dc.nick, subcommand, "Unknown command"},
|
---|
1578 | }}
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | uc, entity, err := dc.unmarshalEntity(target)
|
---|
1582 | if err != nil {
|
---|
1583 | return err
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | // TODO: support msgid criteria
|
---|
1587 | criteriaParts := strings.SplitN(criteria, "=", 2)
|
---|
1588 | if len(criteriaParts) != 2 || criteriaParts[0] != "timestamp" {
|
---|
1589 | return ircError{&irc.Message{
|
---|
1590 | Command: "FAIL",
|
---|
1591 | Params: []string{"CHATHISTORY", "UNKNOWN_CRITERIA", criteria, "Unknown criteria"},
|
---|
1592 | }}
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | timestamp, err := time.Parse(serverTimeLayout, criteriaParts[1])
|
---|
1596 | if err != nil {
|
---|
1597 | return ircError{&irc.Message{
|
---|
1598 | Command: "FAIL",
|
---|
1599 | Params: []string{"CHATHISTORY", "INVALID_CRITERIA", criteria, "Invalid criteria"},
|
---|
1600 | }}
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | limit, err := strconv.Atoi(limitStr)
|
---|
1604 | if err != nil || limit < 0 || limit > dc.srv.HistoryLimit {
|
---|
1605 | return ircError{&irc.Message{
|
---|
1606 | Command: "FAIL",
|
---|
1607 | Params: []string{"CHATHISTORY", "INVALID_LIMIT", limitStr, "Invalid limit"},
|
---|
1608 | }}
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | var history []*irc.Message
|
---|
1612 | switch subcommand {
|
---|
1613 | case "BEFORE":
|
---|
1614 | history, err = loadHistoryBeforeTime(uc.network, entity, timestamp, limit)
|
---|
1615 | case "AFTER":
|
---|
1616 | history, err = loadHistoryAfterTime(uc.network, entity, timestamp, limit)
|
---|
1617 | default:
|
---|
1618 | // TODO: support LATEST, BETWEEN
|
---|
1619 | return ircError{&irc.Message{
|
---|
1620 | Command: "FAIL",
|
---|
1621 | Params: []string{"CHATHISTORY", "UNKNOWN_COMMAND", subcommand, "Unknown command"},
|
---|
1622 | }}
|
---|
1623 | }
|
---|
1624 | if err != nil {
|
---|
1625 | dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
---|
1626 | return newChatHistoryError(subcommand, target)
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | batchRef := "history"
|
---|
1630 | dc.SendMessage(&irc.Message{
|
---|
1631 | Prefix: dc.srv.prefix(),
|
---|
1632 | Command: "BATCH",
|
---|
1633 | Params: []string{"+" + batchRef, "chathistory", target},
|
---|
1634 | })
|
---|
1635 |
|
---|
1636 | for _, msg := range history {
|
---|
1637 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
1638 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | dc.SendMessage(&irc.Message{
|
---|
1642 | Prefix: dc.srv.prefix(),
|
---|
1643 | Command: "BATCH",
|
---|
1644 | Params: []string{"-" + batchRef},
|
---|
1645 | })
|
---|
1646 | default:
|
---|
1647 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
1648 | return newUnknownCommandError(msg.Command)
|
---|
1649 | }
|
---|
1650 | return nil
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | func (dc *downstreamConn) handleNickServPRIVMSG(uc *upstreamConn, text string) {
|
---|
1654 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
1655 | if !ok {
|
---|
1656 | return
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
1660 | // automatically erase the key pair or any other credentials.
|
---|
1661 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
1662 | return
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
1666 | n := uc.network
|
---|
1667 | n.SASL.Mechanism = "PLAIN"
|
---|
1668 | n.SASL.Plain.Username = username
|
---|
1669 | n.SASL.Plain.Password = password
|
---|
1670 | if err := dc.srv.db.StoreNetwork(dc.user.Username, &n.Network); err != nil {
|
---|
1671 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
1672 | }
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
1676 | fields := strings.Fields(text)
|
---|
1677 | if len(fields) < 2 {
|
---|
1678 | return "", "", false
|
---|
1679 | }
|
---|
1680 | cmd := strings.ToUpper(fields[0])
|
---|
1681 | params := fields[1:]
|
---|
1682 | switch cmd {
|
---|
1683 | case "REGISTER":
|
---|
1684 | username = nick
|
---|
1685 | password = params[0]
|
---|
1686 | case "IDENTIFY":
|
---|
1687 | if len(params) == 1 {
|
---|
1688 | username = nick
|
---|
1689 | password = params[0]
|
---|
1690 | } else {
|
---|
1691 | username = params[0]
|
---|
1692 | password = params[1]
|
---|
1693 | }
|
---|
1694 | case "SET":
|
---|
1695 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
1696 | username = nick
|
---|
1697 | password = params[1]
|
---|
1698 | }
|
---|
1699 | default:
|
---|
1700 | return "", "", false
|
---|
1701 | }
|
---|
1702 | return username, password, true
|
---|
1703 | }
|
---|