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