1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "context"
|
---|
5 | "crypto"
|
---|
6 | "crypto/sha256"
|
---|
7 | "crypto/tls"
|
---|
8 | "crypto/x509"
|
---|
9 | "encoding/base64"
|
---|
10 | "errors"
|
---|
11 | "fmt"
|
---|
12 | "io"
|
---|
13 | "net"
|
---|
14 | "strconv"
|
---|
15 | "strings"
|
---|
16 | "time"
|
---|
17 |
|
---|
18 | "github.com/emersion/go-sasl"
|
---|
19 | "gopkg.in/irc.v3"
|
---|
20 | )
|
---|
21 |
|
---|
22 | // permanentUpstreamCaps is the static list of upstream capabilities always
|
---|
23 | // requested when supported.
|
---|
24 | var permanentUpstreamCaps = map[string]bool{
|
---|
25 | "account-tag": true,
|
---|
26 | "away-notify": true,
|
---|
27 | "batch": true,
|
---|
28 | "extended-join": true,
|
---|
29 | "invite-notify": true,
|
---|
30 | "labeled-response": true,
|
---|
31 | "message-tags": true,
|
---|
32 | "multi-prefix": true,
|
---|
33 | "server-time": true,
|
---|
34 | "setname": true,
|
---|
35 |
|
---|
36 | "draft/extended-monitor": true,
|
---|
37 | }
|
---|
38 |
|
---|
39 | type registrationError string
|
---|
40 |
|
---|
41 | func (err registrationError) Error() string {
|
---|
42 | return fmt.Sprintf("registration error: %v", string(err))
|
---|
43 | }
|
---|
44 |
|
---|
45 | type upstreamChannel struct {
|
---|
46 | Name string
|
---|
47 | conn *upstreamConn
|
---|
48 | Topic string
|
---|
49 | TopicWho *irc.Prefix
|
---|
50 | TopicTime time.Time
|
---|
51 | Status channelStatus
|
---|
52 | modes channelModes
|
---|
53 | creationTime string
|
---|
54 | Members membershipsCasemapMap
|
---|
55 | complete bool
|
---|
56 | detachTimer *time.Timer
|
---|
57 | }
|
---|
58 |
|
---|
59 | func (uc *upstreamChannel) updateAutoDetach(dur time.Duration) {
|
---|
60 | if uc.detachTimer != nil {
|
---|
61 | uc.detachTimer.Stop()
|
---|
62 | uc.detachTimer = nil
|
---|
63 | }
|
---|
64 |
|
---|
65 | if dur == 0 {
|
---|
66 | return
|
---|
67 | }
|
---|
68 |
|
---|
69 | uc.detachTimer = time.AfterFunc(dur, func() {
|
---|
70 | uc.conn.network.user.events <- eventChannelDetach{
|
---|
71 | uc: uc.conn,
|
---|
72 | name: uc.Name,
|
---|
73 | }
|
---|
74 | })
|
---|
75 | }
|
---|
76 |
|
---|
77 | type pendingUpstreamCommand struct {
|
---|
78 | downstreamID uint64
|
---|
79 | msg *irc.Message
|
---|
80 | }
|
---|
81 |
|
---|
82 | type upstreamConn struct {
|
---|
83 | conn
|
---|
84 |
|
---|
85 | network *network
|
---|
86 | user *user
|
---|
87 |
|
---|
88 | serverName string
|
---|
89 | availableUserModes string
|
---|
90 | availableChannelModes map[byte]channelModeType
|
---|
91 | availableChannelTypes string
|
---|
92 | availableMemberships []membership
|
---|
93 | isupport map[string]*string
|
---|
94 |
|
---|
95 | registered bool
|
---|
96 | nick string
|
---|
97 | nickCM string
|
---|
98 | username string
|
---|
99 | realname string
|
---|
100 | modes userModes
|
---|
101 | channels upstreamChannelCasemapMap
|
---|
102 | supportedCaps map[string]string
|
---|
103 | caps map[string]bool
|
---|
104 | batches map[string]batch
|
---|
105 | away bool
|
---|
106 | account string
|
---|
107 | nextLabelID uint64
|
---|
108 | monitored monitorCasemapMap
|
---|
109 |
|
---|
110 | saslClient sasl.Client
|
---|
111 | saslStarted bool
|
---|
112 |
|
---|
113 | casemapIsSet bool
|
---|
114 |
|
---|
115 | // Queue of commands in progress, indexed by type. The first entry has been
|
---|
116 | // sent to the server and is awaiting reply. The following entries have not
|
---|
117 | // been sent yet.
|
---|
118 | pendingCmds map[string][]pendingUpstreamCommand
|
---|
119 |
|
---|
120 | gotMotd bool
|
---|
121 | }
|
---|
122 |
|
---|
123 | func connectToUpstream(network *network) (*upstreamConn, error) {
|
---|
124 | logger := &prefixLogger{network.user.logger, fmt.Sprintf("upstream %q: ", network.GetName())}
|
---|
125 |
|
---|
126 | dialer := net.Dialer{Timeout: connectTimeout}
|
---|
127 |
|
---|
128 | u, err := network.URL()
|
---|
129 | if err != nil {
|
---|
130 | return nil, err
|
---|
131 | }
|
---|
132 |
|
---|
133 | var netConn net.Conn
|
---|
134 | switch u.Scheme {
|
---|
135 | case "ircs":
|
---|
136 | addr := u.Host
|
---|
137 | host, _, err := net.SplitHostPort(u.Host)
|
---|
138 | if err != nil {
|
---|
139 | host = u.Host
|
---|
140 | addr = u.Host + ":6697"
|
---|
141 | }
|
---|
142 |
|
---|
143 | dialer.LocalAddr, err = network.user.localTCPAddrForHost(host)
|
---|
144 | if err != nil {
|
---|
145 | return nil, fmt.Errorf("failed to pick local IP for remote host %q: %v", host, err)
|
---|
146 | }
|
---|
147 |
|
---|
148 | logger.Printf("connecting to TLS server at address %q", addr)
|
---|
149 |
|
---|
150 | tlsConfig := &tls.Config{ServerName: host, NextProtos: []string{"irc"}}
|
---|
151 | if network.SASL.Mechanism == "EXTERNAL" {
|
---|
152 | if network.SASL.External.CertBlob == nil {
|
---|
153 | return nil, fmt.Errorf("missing certificate for authentication")
|
---|
154 | }
|
---|
155 | if network.SASL.External.PrivKeyBlob == nil {
|
---|
156 | return nil, fmt.Errorf("missing private key for authentication")
|
---|
157 | }
|
---|
158 | key, err := x509.ParsePKCS8PrivateKey(network.SASL.External.PrivKeyBlob)
|
---|
159 | if err != nil {
|
---|
160 | return nil, fmt.Errorf("failed to parse private key: %v", err)
|
---|
161 | }
|
---|
162 | tlsConfig.Certificates = []tls.Certificate{
|
---|
163 | {
|
---|
164 | Certificate: [][]byte{network.SASL.External.CertBlob},
|
---|
165 | PrivateKey: key.(crypto.PrivateKey),
|
---|
166 | },
|
---|
167 | }
|
---|
168 | logger.Printf("using TLS client certificate %x", sha256.Sum256(network.SASL.External.CertBlob))
|
---|
169 | }
|
---|
170 |
|
---|
171 | netConn, err = dialer.Dial("tcp", addr)
|
---|
172 | if err != nil {
|
---|
173 | return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Don't do the TLS handshake immediately, because we need to register
|
---|
177 | // the new connection with identd ASAP. See:
|
---|
178 | // https://todo.sr.ht/~emersion/soju/69#event-41859
|
---|
179 | netConn = tls.Client(netConn, tlsConfig)
|
---|
180 | case "irc+insecure":
|
---|
181 | addr := u.Host
|
---|
182 | host, _, err := net.SplitHostPort(addr)
|
---|
183 | if err != nil {
|
---|
184 | host = u.Host
|
---|
185 | addr = u.Host + ":6667"
|
---|
186 | }
|
---|
187 |
|
---|
188 | dialer.LocalAddr, err = network.user.localTCPAddrForHost(host)
|
---|
189 | if err != nil {
|
---|
190 | return nil, fmt.Errorf("failed to pick local IP for remote host %q: %v", host, err)
|
---|
191 | }
|
---|
192 |
|
---|
193 | logger.Printf("connecting to plain-text server at address %q", addr)
|
---|
194 | netConn, err = dialer.Dial("tcp", addr)
|
---|
195 | if err != nil {
|
---|
196 | return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
---|
197 | }
|
---|
198 | case "irc+unix", "unix":
|
---|
199 | logger.Printf("connecting to Unix socket at path %q", u.Path)
|
---|
200 | netConn, err = dialer.Dial("unix", u.Path)
|
---|
201 | if err != nil {
|
---|
202 | return nil, fmt.Errorf("failed to connect to Unix socket %q: %v", u.Path, err)
|
---|
203 | }
|
---|
204 | default:
|
---|
205 | return nil, fmt.Errorf("failed to dial %q: unknown scheme: %v", network.Addr, u.Scheme)
|
---|
206 | }
|
---|
207 |
|
---|
208 | options := connOptions{
|
---|
209 | Logger: logger,
|
---|
210 | RateLimitDelay: upstreamMessageDelay,
|
---|
211 | RateLimitBurst: upstreamMessageBurst,
|
---|
212 | }
|
---|
213 |
|
---|
214 | uc := &upstreamConn{
|
---|
215 | conn: *newConn(network.user.srv, newNetIRCConn(netConn), &options),
|
---|
216 | network: network,
|
---|
217 | user: network.user,
|
---|
218 | channels: upstreamChannelCasemapMap{newCasemapMap(0)},
|
---|
219 | supportedCaps: make(map[string]string),
|
---|
220 | caps: make(map[string]bool),
|
---|
221 | batches: make(map[string]batch),
|
---|
222 | availableChannelTypes: stdChannelTypes,
|
---|
223 | availableChannelModes: stdChannelModes,
|
---|
224 | availableMemberships: stdMemberships,
|
---|
225 | isupport: make(map[string]*string),
|
---|
226 | pendingCmds: make(map[string][]pendingUpstreamCommand),
|
---|
227 | monitored: monitorCasemapMap{newCasemapMap(0)},
|
---|
228 | }
|
---|
229 | return uc, nil
|
---|
230 | }
|
---|
231 |
|
---|
232 | func (uc *upstreamConn) forEachDownstream(f func(*downstreamConn)) {
|
---|
233 | uc.network.forEachDownstream(f)
|
---|
234 | }
|
---|
235 |
|
---|
236 | func (uc *upstreamConn) forEachDownstreamByID(id uint64, f func(*downstreamConn)) {
|
---|
237 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
238 | if id != 0 && id != dc.id {
|
---|
239 | return
|
---|
240 | }
|
---|
241 | f(dc)
|
---|
242 | })
|
---|
243 | }
|
---|
244 |
|
---|
245 | func (uc *upstreamConn) downstreamByID(id uint64) *downstreamConn {
|
---|
246 | for _, dc := range uc.user.downstreamConns {
|
---|
247 | if dc.id == id {
|
---|
248 | return dc
|
---|
249 | }
|
---|
250 | }
|
---|
251 | return nil
|
---|
252 | }
|
---|
253 |
|
---|
254 | func (uc *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
|
---|
255 | ch := uc.channels.Value(name)
|
---|
256 | if ch == nil {
|
---|
257 | return nil, fmt.Errorf("unknown channel %q", name)
|
---|
258 | }
|
---|
259 | return ch, nil
|
---|
260 | }
|
---|
261 |
|
---|
262 | func (uc *upstreamConn) isChannel(entity string) bool {
|
---|
263 | return strings.ContainsRune(uc.availableChannelTypes, rune(entity[0]))
|
---|
264 | }
|
---|
265 |
|
---|
266 | func (uc *upstreamConn) isOurNick(nick string) bool {
|
---|
267 | return uc.nickCM == uc.network.casemap(nick)
|
---|
268 | }
|
---|
269 |
|
---|
270 | func (uc *upstreamConn) endPendingCommands() {
|
---|
271 | for _, l := range uc.pendingCmds {
|
---|
272 | for _, pendingCmd := range l {
|
---|
273 | dc := uc.downstreamByID(pendingCmd.downstreamID)
|
---|
274 | if dc == nil {
|
---|
275 | continue
|
---|
276 | }
|
---|
277 |
|
---|
278 | switch pendingCmd.msg.Command {
|
---|
279 | case "LIST":
|
---|
280 | dc.SendMessage(&irc.Message{
|
---|
281 | Prefix: dc.srv.prefix(),
|
---|
282 | Command: irc.RPL_LISTEND,
|
---|
283 | Params: []string{dc.nick, "End of /LIST"},
|
---|
284 | })
|
---|
285 | case "WHO":
|
---|
286 | mask := "*"
|
---|
287 | if len(pendingCmd.msg.Params) > 0 {
|
---|
288 | mask = pendingCmd.msg.Params[0]
|
---|
289 | }
|
---|
290 | dc.SendMessage(&irc.Message{
|
---|
291 | Prefix: dc.srv.prefix(),
|
---|
292 | Command: irc.RPL_ENDOFWHO,
|
---|
293 | Params: []string{dc.nick, mask, "End of /WHO"},
|
---|
294 | })
|
---|
295 | default:
|
---|
296 | panic(fmt.Errorf("Unsupported pending command %q", pendingCmd.msg.Command))
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | uc.pendingCmds = make(map[string][]pendingUpstreamCommand)
|
---|
302 | }
|
---|
303 |
|
---|
304 | func (uc *upstreamConn) sendNextPendingCommand(cmd string) {
|
---|
305 | if len(uc.pendingCmds[cmd]) == 0 {
|
---|
306 | return
|
---|
307 | }
|
---|
308 | uc.SendMessage(uc.pendingCmds[cmd][0].msg)
|
---|
309 | }
|
---|
310 |
|
---|
311 | func (uc *upstreamConn) enqueueCommand(dc *downstreamConn, msg *irc.Message) {
|
---|
312 | switch msg.Command {
|
---|
313 | case "LIST", "WHO":
|
---|
314 | // Supported
|
---|
315 | default:
|
---|
316 | panic(fmt.Errorf("Unsupported pending command %q", msg.Command))
|
---|
317 | }
|
---|
318 |
|
---|
319 | uc.pendingCmds[msg.Command] = append(uc.pendingCmds[msg.Command], pendingUpstreamCommand{
|
---|
320 | downstreamID: dc.id,
|
---|
321 | msg: msg,
|
---|
322 | })
|
---|
323 |
|
---|
324 | if len(uc.pendingCmds[msg.Command]) == 1 {
|
---|
325 | uc.sendNextPendingCommand(msg.Command)
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | func (uc *upstreamConn) currentPendingCommand(cmd string) (*downstreamConn, *irc.Message) {
|
---|
330 | if len(uc.pendingCmds[cmd]) == 0 {
|
---|
331 | return nil, nil
|
---|
332 | }
|
---|
333 |
|
---|
334 | pendingCmd := uc.pendingCmds[cmd][0]
|
---|
335 | return uc.downstreamByID(pendingCmd.downstreamID), pendingCmd.msg
|
---|
336 | }
|
---|
337 |
|
---|
338 | func (uc *upstreamConn) dequeueCommand(cmd string) (*downstreamConn, *irc.Message) {
|
---|
339 | dc, msg := uc.currentPendingCommand(cmd)
|
---|
340 |
|
---|
341 | if len(uc.pendingCmds[cmd]) > 0 {
|
---|
342 | copy(uc.pendingCmds[cmd], uc.pendingCmds[cmd][1:])
|
---|
343 | uc.pendingCmds[cmd] = uc.pendingCmds[cmd][:len(uc.pendingCmds[cmd])-1]
|
---|
344 | }
|
---|
345 |
|
---|
346 | uc.sendNextPendingCommand(cmd)
|
---|
347 |
|
---|
348 | return dc, msg
|
---|
349 | }
|
---|
350 |
|
---|
351 | func (uc *upstreamConn) parseMembershipPrefix(s string) (ms *memberships, nick string) {
|
---|
352 | memberships := make(memberships, 0, 4)
|
---|
353 | i := 0
|
---|
354 | for _, m := range uc.availableMemberships {
|
---|
355 | if i >= len(s) {
|
---|
356 | break
|
---|
357 | }
|
---|
358 | if s[i] == m.Prefix {
|
---|
359 | memberships = append(memberships, m)
|
---|
360 | i++
|
---|
361 | }
|
---|
362 | }
|
---|
363 | return &memberships, s[i:]
|
---|
364 | }
|
---|
365 |
|
---|
366 | func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
|
---|
367 | var label string
|
---|
368 | if l, ok := msg.GetTag("label"); ok {
|
---|
369 | label = l
|
---|
370 | delete(msg.Tags, "label")
|
---|
371 | }
|
---|
372 |
|
---|
373 | var msgBatch *batch
|
---|
374 | if batchName, ok := msg.GetTag("batch"); ok {
|
---|
375 | b, ok := uc.batches[batchName]
|
---|
376 | if !ok {
|
---|
377 | return fmt.Errorf("unexpected batch reference: batch was not defined: %q", batchName)
|
---|
378 | }
|
---|
379 | msgBatch = &b
|
---|
380 | if label == "" {
|
---|
381 | label = msgBatch.Label
|
---|
382 | }
|
---|
383 | delete(msg.Tags, "batch")
|
---|
384 | }
|
---|
385 |
|
---|
386 | var downstreamID uint64 = 0
|
---|
387 | if label != "" {
|
---|
388 | var labelOffset uint64
|
---|
389 | n, err := fmt.Sscanf(label, "sd-%d-%d", &downstreamID, &labelOffset)
|
---|
390 | if err == nil && n < 2 {
|
---|
391 | err = errors.New("not enough arguments")
|
---|
392 | }
|
---|
393 | if err != nil {
|
---|
394 | return fmt.Errorf("unexpected message label: invalid downstream reference for label %q: %v", label, err)
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | if _, ok := msg.Tags["time"]; !ok {
|
---|
399 | msg.Tags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
400 | }
|
---|
401 |
|
---|
402 | switch msg.Command {
|
---|
403 | case "PING":
|
---|
404 | uc.SendMessage(&irc.Message{
|
---|
405 | Command: "PONG",
|
---|
406 | Params: msg.Params,
|
---|
407 | })
|
---|
408 | return nil
|
---|
409 | case "NOTICE", "PRIVMSG", "TAGMSG":
|
---|
410 | if msg.Prefix == nil {
|
---|
411 | return fmt.Errorf("expected a prefix")
|
---|
412 | }
|
---|
413 |
|
---|
414 | var entity, text string
|
---|
415 | if msg.Command != "TAGMSG" {
|
---|
416 | if err := parseMessageParams(msg, &entity, &text); err != nil {
|
---|
417 | return err
|
---|
418 | }
|
---|
419 | } else {
|
---|
420 | if err := parseMessageParams(msg, &entity); err != nil {
|
---|
421 | return err
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | if msg.Prefix.Name == serviceNick {
|
---|
426 | uc.logger.Printf("skipping %v from soju's service: %v", msg.Command, msg)
|
---|
427 | break
|
---|
428 | }
|
---|
429 | if entity == serviceNick {
|
---|
430 | uc.logger.Printf("skipping %v to soju's service: %v", msg.Command, msg)
|
---|
431 | break
|
---|
432 | }
|
---|
433 |
|
---|
434 | if msg.Prefix.User == "" && msg.Prefix.Host == "" { // server message
|
---|
435 | uc.produce("", msg, nil)
|
---|
436 | } else { // regular user message
|
---|
437 | target := entity
|
---|
438 | if uc.isOurNick(target) {
|
---|
439 | target = msg.Prefix.Name
|
---|
440 | }
|
---|
441 |
|
---|
442 | ch := uc.network.channels.Value(target)
|
---|
443 | if ch != nil && msg.Command != "TAGMSG" {
|
---|
444 | if ch.Detached {
|
---|
445 | uc.handleDetachedMessage(ch, msg)
|
---|
446 | }
|
---|
447 |
|
---|
448 | highlight := uc.network.isHighlight(msg)
|
---|
449 | if ch.DetachOn == FilterMessage || ch.DetachOn == FilterDefault || (ch.DetachOn == FilterHighlight && highlight) {
|
---|
450 | uc.updateChannelAutoDetach(target)
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | uc.produce(target, msg, nil)
|
---|
455 | }
|
---|
456 | case "CAP":
|
---|
457 | var subCmd string
|
---|
458 | if err := parseMessageParams(msg, nil, &subCmd); err != nil {
|
---|
459 | return err
|
---|
460 | }
|
---|
461 | subCmd = strings.ToUpper(subCmd)
|
---|
462 | subParams := msg.Params[2:]
|
---|
463 | switch subCmd {
|
---|
464 | case "LS":
|
---|
465 | if len(subParams) < 1 {
|
---|
466 | return newNeedMoreParamsError(msg.Command)
|
---|
467 | }
|
---|
468 | caps := subParams[len(subParams)-1]
|
---|
469 | more := len(subParams) >= 2 && msg.Params[len(subParams)-2] == "*"
|
---|
470 |
|
---|
471 | uc.handleSupportedCaps(caps)
|
---|
472 |
|
---|
473 | if more {
|
---|
474 | break // wait to receive all capabilities
|
---|
475 | }
|
---|
476 |
|
---|
477 | uc.requestCaps()
|
---|
478 |
|
---|
479 | if uc.requestSASL() {
|
---|
480 | break // we'll send CAP END after authentication is completed
|
---|
481 | }
|
---|
482 |
|
---|
483 | uc.SendMessage(&irc.Message{
|
---|
484 | Command: "CAP",
|
---|
485 | Params: []string{"END"},
|
---|
486 | })
|
---|
487 | case "ACK", "NAK":
|
---|
488 | if len(subParams) < 1 {
|
---|
489 | return newNeedMoreParamsError(msg.Command)
|
---|
490 | }
|
---|
491 | caps := strings.Fields(subParams[0])
|
---|
492 |
|
---|
493 | for _, name := range caps {
|
---|
494 | if err := uc.handleCapAck(strings.ToLower(name), subCmd == "ACK"); err != nil {
|
---|
495 | return err
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | if uc.registered {
|
---|
500 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
501 | dc.updateSupportedCaps()
|
---|
502 | })
|
---|
503 | }
|
---|
504 | case "NEW":
|
---|
505 | if len(subParams) < 1 {
|
---|
506 | return newNeedMoreParamsError(msg.Command)
|
---|
507 | }
|
---|
508 | uc.handleSupportedCaps(subParams[0])
|
---|
509 | uc.requestCaps()
|
---|
510 | case "DEL":
|
---|
511 | if len(subParams) < 1 {
|
---|
512 | return newNeedMoreParamsError(msg.Command)
|
---|
513 | }
|
---|
514 | caps := strings.Fields(subParams[0])
|
---|
515 |
|
---|
516 | for _, c := range caps {
|
---|
517 | delete(uc.supportedCaps, c)
|
---|
518 | delete(uc.caps, c)
|
---|
519 | }
|
---|
520 |
|
---|
521 | if uc.registered {
|
---|
522 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
523 | dc.updateSupportedCaps()
|
---|
524 | })
|
---|
525 | }
|
---|
526 | default:
|
---|
527 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
528 | }
|
---|
529 | case "AUTHENTICATE":
|
---|
530 | if uc.saslClient == nil {
|
---|
531 | return fmt.Errorf("received unexpected AUTHENTICATE message")
|
---|
532 | }
|
---|
533 |
|
---|
534 | // TODO: if a challenge is 400 bytes long, buffer it
|
---|
535 | var challengeStr string
|
---|
536 | if err := parseMessageParams(msg, &challengeStr); err != nil {
|
---|
537 | uc.SendMessage(&irc.Message{
|
---|
538 | Command: "AUTHENTICATE",
|
---|
539 | Params: []string{"*"},
|
---|
540 | })
|
---|
541 | return err
|
---|
542 | }
|
---|
543 |
|
---|
544 | var challenge []byte
|
---|
545 | if challengeStr != "+" {
|
---|
546 | var err error
|
---|
547 | challenge, err = base64.StdEncoding.DecodeString(challengeStr)
|
---|
548 | if err != nil {
|
---|
549 | uc.SendMessage(&irc.Message{
|
---|
550 | Command: "AUTHENTICATE",
|
---|
551 | Params: []string{"*"},
|
---|
552 | })
|
---|
553 | return err
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | var resp []byte
|
---|
558 | var err error
|
---|
559 | if !uc.saslStarted {
|
---|
560 | _, resp, err = uc.saslClient.Start()
|
---|
561 | uc.saslStarted = true
|
---|
562 | } else {
|
---|
563 | resp, err = uc.saslClient.Next(challenge)
|
---|
564 | }
|
---|
565 | if err != nil {
|
---|
566 | uc.SendMessage(&irc.Message{
|
---|
567 | Command: "AUTHENTICATE",
|
---|
568 | Params: []string{"*"},
|
---|
569 | })
|
---|
570 | return err
|
---|
571 | }
|
---|
572 |
|
---|
573 | // TODO: send response in multiple chunks if >= 400 bytes
|
---|
574 | var respStr = "+"
|
---|
575 | if len(resp) != 0 {
|
---|
576 | respStr = base64.StdEncoding.EncodeToString(resp)
|
---|
577 | }
|
---|
578 |
|
---|
579 | uc.SendMessage(&irc.Message{
|
---|
580 | Command: "AUTHENTICATE",
|
---|
581 | Params: []string{respStr},
|
---|
582 | })
|
---|
583 | case irc.RPL_LOGGEDIN:
|
---|
584 | if err := parseMessageParams(msg, nil, nil, &uc.account); err != nil {
|
---|
585 | return err
|
---|
586 | }
|
---|
587 | uc.logger.Printf("logged in with account %q", uc.account)
|
---|
588 | case irc.RPL_LOGGEDOUT:
|
---|
589 | uc.account = ""
|
---|
590 | uc.logger.Printf("logged out")
|
---|
591 | case irc.ERR_NICKLOCKED, irc.RPL_SASLSUCCESS, irc.ERR_SASLFAIL, irc.ERR_SASLTOOLONG, irc.ERR_SASLABORTED:
|
---|
592 | var info string
|
---|
593 | if err := parseMessageParams(msg, nil, &info); err != nil {
|
---|
594 | return err
|
---|
595 | }
|
---|
596 | switch msg.Command {
|
---|
597 | case irc.ERR_NICKLOCKED:
|
---|
598 | uc.logger.Printf("invalid nick used with SASL authentication: %v", info)
|
---|
599 | case irc.ERR_SASLFAIL:
|
---|
600 | uc.logger.Printf("SASL authentication failed: %v", info)
|
---|
601 | case irc.ERR_SASLTOOLONG:
|
---|
602 | uc.logger.Printf("SASL message too long: %v", info)
|
---|
603 | }
|
---|
604 |
|
---|
605 | uc.saslClient = nil
|
---|
606 | uc.saslStarted = false
|
---|
607 |
|
---|
608 | uc.SendMessage(&irc.Message{
|
---|
609 | Command: "CAP",
|
---|
610 | Params: []string{"END"},
|
---|
611 | })
|
---|
612 | case irc.RPL_WELCOME:
|
---|
613 | uc.registered = true
|
---|
614 | uc.logger.Printf("connection registered")
|
---|
615 |
|
---|
616 | if uc.network.channels.Len() > 0 {
|
---|
617 | var channels, keys []string
|
---|
618 | for _, entry := range uc.network.channels.innerMap {
|
---|
619 | ch := entry.value.(*Channel)
|
---|
620 | channels = append(channels, ch.Name)
|
---|
621 | keys = append(keys, ch.Key)
|
---|
622 | }
|
---|
623 |
|
---|
624 | for _, msg := range join(channels, keys) {
|
---|
625 | uc.SendMessage(msg)
|
---|
626 | }
|
---|
627 | }
|
---|
628 | case irc.RPL_MYINFO:
|
---|
629 | if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, nil); err != nil {
|
---|
630 | return err
|
---|
631 | }
|
---|
632 | case irc.RPL_ISUPPORT:
|
---|
633 | if err := parseMessageParams(msg, nil, nil); err != nil {
|
---|
634 | return err
|
---|
635 | }
|
---|
636 |
|
---|
637 | var downstreamIsupport []string
|
---|
638 | for _, token := range msg.Params[1 : len(msg.Params)-1] {
|
---|
639 | parameter := token
|
---|
640 | var negate, hasValue bool
|
---|
641 | var value string
|
---|
642 | if strings.HasPrefix(token, "-") {
|
---|
643 | negate = true
|
---|
644 | token = token[1:]
|
---|
645 | } else if i := strings.IndexByte(token, '='); i >= 0 {
|
---|
646 | parameter = token[:i]
|
---|
647 | value = token[i+1:]
|
---|
648 | hasValue = true
|
---|
649 | }
|
---|
650 |
|
---|
651 | if hasValue {
|
---|
652 | uc.isupport[parameter] = &value
|
---|
653 | } else if !negate {
|
---|
654 | uc.isupport[parameter] = nil
|
---|
655 | } else {
|
---|
656 | delete(uc.isupport, parameter)
|
---|
657 | }
|
---|
658 |
|
---|
659 | var err error
|
---|
660 | switch parameter {
|
---|
661 | case "CASEMAPPING":
|
---|
662 | casemap, ok := parseCasemappingToken(value)
|
---|
663 | if !ok {
|
---|
664 | casemap = casemapRFC1459
|
---|
665 | }
|
---|
666 | uc.network.updateCasemapping(casemap)
|
---|
667 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
668 | uc.casemapIsSet = true
|
---|
669 | case "CHANMODES":
|
---|
670 | if !negate {
|
---|
671 | err = uc.handleChanModes(value)
|
---|
672 | } else {
|
---|
673 | uc.availableChannelModes = stdChannelModes
|
---|
674 | }
|
---|
675 | case "CHANTYPES":
|
---|
676 | if !negate {
|
---|
677 | uc.availableChannelTypes = value
|
---|
678 | } else {
|
---|
679 | uc.availableChannelTypes = stdChannelTypes
|
---|
680 | }
|
---|
681 | case "PREFIX":
|
---|
682 | if !negate {
|
---|
683 | err = uc.handleMemberships(value)
|
---|
684 | } else {
|
---|
685 | uc.availableMemberships = stdMemberships
|
---|
686 | }
|
---|
687 | }
|
---|
688 | if err != nil {
|
---|
689 | return err
|
---|
690 | }
|
---|
691 |
|
---|
692 | if passthroughIsupport[parameter] {
|
---|
693 | downstreamIsupport = append(downstreamIsupport, token)
|
---|
694 | }
|
---|
695 | }
|
---|
696 |
|
---|
697 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
698 | if dc.network == nil {
|
---|
699 | return
|
---|
700 | }
|
---|
701 | msgs := generateIsupport(dc.srv.prefix(), dc.nick, downstreamIsupport)
|
---|
702 | for _, msg := range msgs {
|
---|
703 | dc.SendMessage(msg)
|
---|
704 | }
|
---|
705 | })
|
---|
706 | case irc.ERR_NOMOTD, irc.RPL_ENDOFMOTD:
|
---|
707 | if !uc.casemapIsSet {
|
---|
708 | // upstream did not send any CASEMAPPING token, thus
|
---|
709 | // we assume it implements the old RFCs with rfc1459.
|
---|
710 | uc.casemapIsSet = true
|
---|
711 | uc.network.updateCasemapping(casemapRFC1459)
|
---|
712 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
713 | }
|
---|
714 |
|
---|
715 | if !uc.gotMotd {
|
---|
716 | // Ignore the initial MOTD upon connection, but forward
|
---|
717 | // subsequent MOTD messages downstream
|
---|
718 | uc.gotMotd = true
|
---|
719 | return nil
|
---|
720 | }
|
---|
721 |
|
---|
722 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
723 | dc.SendMessage(&irc.Message{
|
---|
724 | Prefix: uc.srv.prefix(),
|
---|
725 | Command: msg.Command,
|
---|
726 | Params: msg.Params,
|
---|
727 | })
|
---|
728 | })
|
---|
729 | case "BATCH":
|
---|
730 | var tag string
|
---|
731 | if err := parseMessageParams(msg, &tag); err != nil {
|
---|
732 | return err
|
---|
733 | }
|
---|
734 |
|
---|
735 | if strings.HasPrefix(tag, "+") {
|
---|
736 | tag = tag[1:]
|
---|
737 | if _, ok := uc.batches[tag]; ok {
|
---|
738 | return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
---|
739 | }
|
---|
740 | var batchType string
|
---|
741 | if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
---|
742 | return err
|
---|
743 | }
|
---|
744 | label := label
|
---|
745 | if label == "" && msgBatch != nil {
|
---|
746 | label = msgBatch.Label
|
---|
747 | }
|
---|
748 | uc.batches[tag] = batch{
|
---|
749 | Type: batchType,
|
---|
750 | Params: msg.Params[2:],
|
---|
751 | Outer: msgBatch,
|
---|
752 | Label: label,
|
---|
753 | }
|
---|
754 | } else if strings.HasPrefix(tag, "-") {
|
---|
755 | tag = tag[1:]
|
---|
756 | if _, ok := uc.batches[tag]; !ok {
|
---|
757 | return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
---|
758 | }
|
---|
759 | delete(uc.batches, tag)
|
---|
760 | } else {
|
---|
761 | return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
---|
762 | }
|
---|
763 | case "NICK":
|
---|
764 | if msg.Prefix == nil {
|
---|
765 | return fmt.Errorf("expected a prefix")
|
---|
766 | }
|
---|
767 |
|
---|
768 | var newNick string
|
---|
769 | if err := parseMessageParams(msg, &newNick); err != nil {
|
---|
770 | return err
|
---|
771 | }
|
---|
772 |
|
---|
773 | me := false
|
---|
774 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
775 | uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
---|
776 | me = true
|
---|
777 | uc.nick = newNick
|
---|
778 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
779 | }
|
---|
780 |
|
---|
781 | for _, entry := range uc.channels.innerMap {
|
---|
782 | ch := entry.value.(*upstreamChannel)
|
---|
783 | memberships := ch.Members.Value(msg.Prefix.Name)
|
---|
784 | if memberships != nil {
|
---|
785 | ch.Members.Delete(msg.Prefix.Name)
|
---|
786 | ch.Members.SetValue(newNick, memberships)
|
---|
787 | uc.appendLog(ch.Name, msg)
|
---|
788 | }
|
---|
789 | }
|
---|
790 |
|
---|
791 | if !me {
|
---|
792 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
793 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
794 | })
|
---|
795 | } else {
|
---|
796 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
797 | dc.updateNick()
|
---|
798 | })
|
---|
799 | }
|
---|
800 | case "SETNAME":
|
---|
801 | if msg.Prefix == nil {
|
---|
802 | return fmt.Errorf("expected a prefix")
|
---|
803 | }
|
---|
804 |
|
---|
805 | var newRealname string
|
---|
806 | if err := parseMessageParams(msg, &newRealname); err != nil {
|
---|
807 | return err
|
---|
808 | }
|
---|
809 |
|
---|
810 | // TODO: consider appending this message to logs
|
---|
811 |
|
---|
812 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
813 | uc.logger.Printf("changed realname from %q to %q", uc.realname, newRealname)
|
---|
814 | uc.realname = newRealname
|
---|
815 |
|
---|
816 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
817 | dc.updateRealname()
|
---|
818 | })
|
---|
819 | } else {
|
---|
820 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
821 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
822 | })
|
---|
823 | }
|
---|
824 | case "JOIN":
|
---|
825 | if msg.Prefix == nil {
|
---|
826 | return fmt.Errorf("expected a prefix")
|
---|
827 | }
|
---|
828 |
|
---|
829 | var channels string
|
---|
830 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
831 | return err
|
---|
832 | }
|
---|
833 |
|
---|
834 | for _, ch := range strings.Split(channels, ",") {
|
---|
835 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
836 | uc.logger.Printf("joined channel %q", ch)
|
---|
837 | members := membershipsCasemapMap{newCasemapMap(0)}
|
---|
838 | members.casemap = uc.network.casemap
|
---|
839 | uc.channels.SetValue(ch, &upstreamChannel{
|
---|
840 | Name: ch,
|
---|
841 | conn: uc,
|
---|
842 | Members: members,
|
---|
843 | })
|
---|
844 | uc.updateChannelAutoDetach(ch)
|
---|
845 |
|
---|
846 | uc.SendMessage(&irc.Message{
|
---|
847 | Command: "MODE",
|
---|
848 | Params: []string{ch},
|
---|
849 | })
|
---|
850 | } else {
|
---|
851 | ch, err := uc.getChannel(ch)
|
---|
852 | if err != nil {
|
---|
853 | return err
|
---|
854 | }
|
---|
855 | ch.Members.SetValue(msg.Prefix.Name, &memberships{})
|
---|
856 | }
|
---|
857 |
|
---|
858 | chMsg := msg.Copy()
|
---|
859 | chMsg.Params[0] = ch
|
---|
860 | uc.produce(ch, chMsg, nil)
|
---|
861 | }
|
---|
862 | case "PART":
|
---|
863 | if msg.Prefix == nil {
|
---|
864 | return fmt.Errorf("expected a prefix")
|
---|
865 | }
|
---|
866 |
|
---|
867 | var channels string
|
---|
868 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
869 | return err
|
---|
870 | }
|
---|
871 |
|
---|
872 | for _, ch := range strings.Split(channels, ",") {
|
---|
873 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
874 | uc.logger.Printf("parted channel %q", ch)
|
---|
875 | uch := uc.channels.Value(ch)
|
---|
876 | if uch != nil {
|
---|
877 | uc.channels.Delete(ch)
|
---|
878 | uch.updateAutoDetach(0)
|
---|
879 | }
|
---|
880 | } else {
|
---|
881 | ch, err := uc.getChannel(ch)
|
---|
882 | if err != nil {
|
---|
883 | return err
|
---|
884 | }
|
---|
885 | ch.Members.Delete(msg.Prefix.Name)
|
---|
886 | }
|
---|
887 |
|
---|
888 | chMsg := msg.Copy()
|
---|
889 | chMsg.Params[0] = ch
|
---|
890 | uc.produce(ch, chMsg, nil)
|
---|
891 | }
|
---|
892 | case "KICK":
|
---|
893 | if msg.Prefix == nil {
|
---|
894 | return fmt.Errorf("expected a prefix")
|
---|
895 | }
|
---|
896 |
|
---|
897 | var channel, user string
|
---|
898 | if err := parseMessageParams(msg, &channel, &user); err != nil {
|
---|
899 | return err
|
---|
900 | }
|
---|
901 |
|
---|
902 | if uc.isOurNick(user) {
|
---|
903 | uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
---|
904 | uc.channels.Delete(channel)
|
---|
905 | } else {
|
---|
906 | ch, err := uc.getChannel(channel)
|
---|
907 | if err != nil {
|
---|
908 | return err
|
---|
909 | }
|
---|
910 | ch.Members.Delete(user)
|
---|
911 | }
|
---|
912 |
|
---|
913 | uc.produce(channel, msg, nil)
|
---|
914 | case "QUIT":
|
---|
915 | if msg.Prefix == nil {
|
---|
916 | return fmt.Errorf("expected a prefix")
|
---|
917 | }
|
---|
918 |
|
---|
919 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
920 | uc.logger.Printf("quit")
|
---|
921 | }
|
---|
922 |
|
---|
923 | for _, entry := range uc.channels.innerMap {
|
---|
924 | ch := entry.value.(*upstreamChannel)
|
---|
925 | if ch.Members.Has(msg.Prefix.Name) {
|
---|
926 | ch.Members.Delete(msg.Prefix.Name)
|
---|
927 |
|
---|
928 | uc.appendLog(ch.Name, msg)
|
---|
929 | }
|
---|
930 | }
|
---|
931 |
|
---|
932 | if msg.Prefix.Name != uc.nick {
|
---|
933 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
934 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
935 | })
|
---|
936 | }
|
---|
937 | case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
---|
938 | var name, topic string
|
---|
939 | if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
---|
940 | return err
|
---|
941 | }
|
---|
942 | ch, err := uc.getChannel(name)
|
---|
943 | if err != nil {
|
---|
944 | return err
|
---|
945 | }
|
---|
946 | if msg.Command == irc.RPL_TOPIC {
|
---|
947 | ch.Topic = topic
|
---|
948 | } else {
|
---|
949 | ch.Topic = ""
|
---|
950 | }
|
---|
951 | case "TOPIC":
|
---|
952 | if msg.Prefix == nil {
|
---|
953 | return fmt.Errorf("expected a prefix")
|
---|
954 | }
|
---|
955 |
|
---|
956 | var name string
|
---|
957 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
958 | return err
|
---|
959 | }
|
---|
960 | ch, err := uc.getChannel(name)
|
---|
961 | if err != nil {
|
---|
962 | return err
|
---|
963 | }
|
---|
964 | if len(msg.Params) > 1 {
|
---|
965 | ch.Topic = msg.Params[1]
|
---|
966 | ch.TopicWho = msg.Prefix.Copy()
|
---|
967 | ch.TopicTime = time.Now() // TODO use msg.Tags["time"]
|
---|
968 | } else {
|
---|
969 | ch.Topic = ""
|
---|
970 | }
|
---|
971 | uc.produce(ch.Name, msg, nil)
|
---|
972 | case "MODE":
|
---|
973 | var name, modeStr string
|
---|
974 | if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
---|
975 | return err
|
---|
976 | }
|
---|
977 |
|
---|
978 | if !uc.isChannel(name) { // user mode change
|
---|
979 | if name != uc.nick {
|
---|
980 | return fmt.Errorf("received MODE message for unknown nick %q", name)
|
---|
981 | }
|
---|
982 |
|
---|
983 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
984 | return err
|
---|
985 | }
|
---|
986 |
|
---|
987 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
988 | if dc.upstream() == nil {
|
---|
989 | return
|
---|
990 | }
|
---|
991 |
|
---|
992 | dc.SendMessage(msg)
|
---|
993 | })
|
---|
994 | } else { // channel mode change
|
---|
995 | ch, err := uc.getChannel(name)
|
---|
996 | if err != nil {
|
---|
997 | return err
|
---|
998 | }
|
---|
999 |
|
---|
1000 | needMarshaling, err := applyChannelModes(ch, modeStr, msg.Params[2:])
|
---|
1001 | if err != nil {
|
---|
1002 | return err
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | uc.appendLog(ch.Name, msg)
|
---|
1006 |
|
---|
1007 | c := uc.network.channels.Value(name)
|
---|
1008 | if c == nil || !c.Detached {
|
---|
1009 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1010 | params := make([]string, len(msg.Params))
|
---|
1011 | params[0] = dc.marshalEntity(uc.network, name)
|
---|
1012 | params[1] = modeStr
|
---|
1013 |
|
---|
1014 | copy(params[2:], msg.Params[2:])
|
---|
1015 | for i, modeParam := range params[2:] {
|
---|
1016 | if _, ok := needMarshaling[i]; ok {
|
---|
1017 | params[2+i] = dc.marshalEntity(uc.network, modeParam)
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | dc.SendMessage(&irc.Message{
|
---|
1022 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
1023 | Command: "MODE",
|
---|
1024 | Params: params,
|
---|
1025 | })
|
---|
1026 | })
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | case irc.RPL_UMODEIS:
|
---|
1030 | if err := parseMessageParams(msg, nil); err != nil {
|
---|
1031 | return err
|
---|
1032 | }
|
---|
1033 | modeStr := ""
|
---|
1034 | if len(msg.Params) > 1 {
|
---|
1035 | modeStr = msg.Params[1]
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | uc.modes = ""
|
---|
1039 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
1040 | return err
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1044 | if dc.upstream() == nil {
|
---|
1045 | return
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | dc.SendMessage(msg)
|
---|
1049 | })
|
---|
1050 | case irc.RPL_CHANNELMODEIS:
|
---|
1051 | var channel string
|
---|
1052 | if err := parseMessageParams(msg, nil, &channel); err != nil {
|
---|
1053 | return err
|
---|
1054 | }
|
---|
1055 | modeStr := ""
|
---|
1056 | if len(msg.Params) > 2 {
|
---|
1057 | modeStr = msg.Params[2]
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | ch, err := uc.getChannel(channel)
|
---|
1061 | if err != nil {
|
---|
1062 | return err
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | firstMode := ch.modes == nil
|
---|
1066 | ch.modes = make(map[byte]string)
|
---|
1067 | if _, err := applyChannelModes(ch, modeStr, msg.Params[3:]); err != nil {
|
---|
1068 | return err
|
---|
1069 | }
|
---|
1070 | if firstMode {
|
---|
1071 | c := uc.network.channels.Value(channel)
|
---|
1072 | if c == nil || !c.Detached {
|
---|
1073 | modeStr, modeParams := ch.modes.Format()
|
---|
1074 |
|
---|
1075 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1076 | params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
|
---|
1077 | params = append(params, modeParams...)
|
---|
1078 |
|
---|
1079 | dc.SendMessage(&irc.Message{
|
---|
1080 | Prefix: dc.srv.prefix(),
|
---|
1081 | Command: irc.RPL_CHANNELMODEIS,
|
---|
1082 | Params: params,
|
---|
1083 | })
|
---|
1084 | })
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | case rpl_creationtime:
|
---|
1088 | var channel, creationTime string
|
---|
1089 | if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
---|
1090 | return err
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | ch, err := uc.getChannel(channel)
|
---|
1094 | if err != nil {
|
---|
1095 | return err
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | firstCreationTime := ch.creationTime == ""
|
---|
1099 | ch.creationTime = creationTime
|
---|
1100 | if firstCreationTime {
|
---|
1101 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1102 | dc.SendMessage(&irc.Message{
|
---|
1103 | Prefix: dc.srv.prefix(),
|
---|
1104 | Command: rpl_creationtime,
|
---|
1105 | Params: []string{dc.nick, dc.marshalEntity(uc.network, ch.Name), creationTime},
|
---|
1106 | })
|
---|
1107 | })
|
---|
1108 | }
|
---|
1109 | case rpl_topicwhotime:
|
---|
1110 | var name, who, timeStr string
|
---|
1111 | if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
---|
1112 | return err
|
---|
1113 | }
|
---|
1114 | ch, err := uc.getChannel(name)
|
---|
1115 | if err != nil {
|
---|
1116 | return err
|
---|
1117 | }
|
---|
1118 | firstTopicWhoTime := ch.TopicWho == nil
|
---|
1119 | ch.TopicWho = irc.ParsePrefix(who)
|
---|
1120 | sec, err := strconv.ParseInt(timeStr, 10, 64)
|
---|
1121 | if err != nil {
|
---|
1122 | return fmt.Errorf("failed to parse topic time: %v", err)
|
---|
1123 | }
|
---|
1124 | ch.TopicTime = time.Unix(sec, 0)
|
---|
1125 | if firstTopicWhoTime {
|
---|
1126 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1127 | topicWho := dc.marshalUserPrefix(uc.network, ch.TopicWho)
|
---|
1128 | dc.SendMessage(&irc.Message{
|
---|
1129 | Prefix: dc.srv.prefix(),
|
---|
1130 | Command: rpl_topicwhotime,
|
---|
1131 | Params: []string{
|
---|
1132 | dc.nick,
|
---|
1133 | dc.marshalEntity(uc.network, ch.Name),
|
---|
1134 | topicWho.String(),
|
---|
1135 | timeStr,
|
---|
1136 | },
|
---|
1137 | })
|
---|
1138 | })
|
---|
1139 | }
|
---|
1140 | case irc.RPL_LIST:
|
---|
1141 | var channel, clients, topic string
|
---|
1142 | if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
---|
1143 | return err
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | dc, cmd := uc.currentPendingCommand("LIST")
|
---|
1147 | if cmd == nil {
|
---|
1148 | return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
---|
1149 | } else if dc == nil {
|
---|
1150 | return nil
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | dc.SendMessage(&irc.Message{
|
---|
1154 | Prefix: dc.srv.prefix(),
|
---|
1155 | Command: irc.RPL_LIST,
|
---|
1156 | Params: []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
|
---|
1157 | })
|
---|
1158 | case irc.RPL_LISTEND:
|
---|
1159 | dc, cmd := uc.dequeueCommand("LIST")
|
---|
1160 | if cmd == nil {
|
---|
1161 | return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
---|
1162 | } else if dc == nil {
|
---|
1163 | return nil
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | dc.SendMessage(&irc.Message{
|
---|
1167 | Prefix: dc.srv.prefix(),
|
---|
1168 | Command: irc.RPL_LISTEND,
|
---|
1169 | Params: []string{dc.nick, "End of /LIST"},
|
---|
1170 | })
|
---|
1171 | case irc.RPL_NAMREPLY:
|
---|
1172 | var name, statusStr, members string
|
---|
1173 | if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
---|
1174 | return err
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | ch := uc.channels.Value(name)
|
---|
1178 | if ch == nil {
|
---|
1179 | // NAMES on a channel we have not joined, forward to downstream
|
---|
1180 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1181 | channel := dc.marshalEntity(uc.network, name)
|
---|
1182 | members := splitSpace(members)
|
---|
1183 | for i, member := range members {
|
---|
1184 | memberships, nick := uc.parseMembershipPrefix(member)
|
---|
1185 | members[i] = memberships.Format(dc) + dc.marshalEntity(uc.network, nick)
|
---|
1186 | }
|
---|
1187 | memberStr := strings.Join(members, " ")
|
---|
1188 |
|
---|
1189 | dc.SendMessage(&irc.Message{
|
---|
1190 | Prefix: dc.srv.prefix(),
|
---|
1191 | Command: irc.RPL_NAMREPLY,
|
---|
1192 | Params: []string{dc.nick, statusStr, channel, memberStr},
|
---|
1193 | })
|
---|
1194 | })
|
---|
1195 | return nil
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | status, err := parseChannelStatus(statusStr)
|
---|
1199 | if err != nil {
|
---|
1200 | return err
|
---|
1201 | }
|
---|
1202 | ch.Status = status
|
---|
1203 |
|
---|
1204 | for _, s := range splitSpace(members) {
|
---|
1205 | memberships, nick := uc.parseMembershipPrefix(s)
|
---|
1206 | ch.Members.SetValue(nick, memberships)
|
---|
1207 | }
|
---|
1208 | case irc.RPL_ENDOFNAMES:
|
---|
1209 | var name string
|
---|
1210 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
1211 | return err
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | ch := uc.channels.Value(name)
|
---|
1215 | if ch == nil {
|
---|
1216 | // NAMES on a channel we have not joined, forward to downstream
|
---|
1217 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1218 | channel := dc.marshalEntity(uc.network, name)
|
---|
1219 |
|
---|
1220 | dc.SendMessage(&irc.Message{
|
---|
1221 | Prefix: dc.srv.prefix(),
|
---|
1222 | Command: irc.RPL_ENDOFNAMES,
|
---|
1223 | Params: []string{dc.nick, channel, "End of /NAMES list"},
|
---|
1224 | })
|
---|
1225 | })
|
---|
1226 | return nil
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | if ch.complete {
|
---|
1230 | return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
---|
1231 | }
|
---|
1232 | ch.complete = true
|
---|
1233 |
|
---|
1234 | c := uc.network.channels.Value(name)
|
---|
1235 | if c == nil || !c.Detached {
|
---|
1236 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1237 | forwardChannel(dc, ch)
|
---|
1238 | })
|
---|
1239 | }
|
---|
1240 | case irc.RPL_WHOREPLY:
|
---|
1241 | var channel, username, host, server, nick, mode, trailing string
|
---|
1242 | if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
---|
1243 | return err
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | dc, cmd := uc.currentPendingCommand("WHO")
|
---|
1247 | if cmd == nil {
|
---|
1248 | return fmt.Errorf("unexpected RPL_WHOREPLY: no matching pending WHO")
|
---|
1249 | } else if dc == nil {
|
---|
1250 | return nil
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | parts := strings.SplitN(trailing, " ", 2)
|
---|
1254 | if len(parts) != 2 {
|
---|
1255 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
---|
1256 | }
|
---|
1257 | realname := parts[1]
|
---|
1258 | hops, err := strconv.Atoi(parts[0])
|
---|
1259 | if err != nil {
|
---|
1260 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
---|
1261 | }
|
---|
1262 | hops++
|
---|
1263 |
|
---|
1264 | trailing = strconv.Itoa(hops) + " " + realname
|
---|
1265 |
|
---|
1266 | if channel != "*" {
|
---|
1267 | channel = dc.marshalEntity(uc.network, channel)
|
---|
1268 | }
|
---|
1269 | nick = dc.marshalEntity(uc.network, nick)
|
---|
1270 | dc.SendMessage(&irc.Message{
|
---|
1271 | Prefix: dc.srv.prefix(),
|
---|
1272 | Command: irc.RPL_WHOREPLY,
|
---|
1273 | Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
---|
1274 | })
|
---|
1275 | case rpl_whospcrpl:
|
---|
1276 | dc, cmd := uc.currentPendingCommand("WHO")
|
---|
1277 | if cmd == nil {
|
---|
1278 | return fmt.Errorf("unexpected RPL_WHOSPCRPL: no matching pending WHO")
|
---|
1279 | } else if dc == nil {
|
---|
1280 | return nil
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | // Only supported in single-upstream mode, so forward as-is
|
---|
1284 | dc.SendMessage(msg)
|
---|
1285 | case irc.RPL_ENDOFWHO:
|
---|
1286 | var name string
|
---|
1287 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
1288 | return err
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | dc, cmd := uc.dequeueCommand("WHO")
|
---|
1292 | if cmd == nil {
|
---|
1293 | return fmt.Errorf("unexpected RPL_ENDOFWHO: no matching pending WHO")
|
---|
1294 | } else if dc == nil {
|
---|
1295 | return nil
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | mask := "*"
|
---|
1299 | if len(cmd.Params) > 0 {
|
---|
1300 | mask = cmd.Params[0]
|
---|
1301 | }
|
---|
1302 | dc.SendMessage(&irc.Message{
|
---|
1303 | Prefix: dc.srv.prefix(),
|
---|
1304 | Command: irc.RPL_ENDOFWHO,
|
---|
1305 | Params: []string{dc.nick, mask, "End of /WHO list"},
|
---|
1306 | })
|
---|
1307 | case irc.RPL_WHOISUSER:
|
---|
1308 | var nick, username, host, realname string
|
---|
1309 | if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
---|
1310 | return err
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1314 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1315 | dc.SendMessage(&irc.Message{
|
---|
1316 | Prefix: dc.srv.prefix(),
|
---|
1317 | Command: irc.RPL_WHOISUSER,
|
---|
1318 | Params: []string{dc.nick, nick, username, host, "*", realname},
|
---|
1319 | })
|
---|
1320 | })
|
---|
1321 | case irc.RPL_WHOISSERVER:
|
---|
1322 | var nick, server, serverInfo string
|
---|
1323 | if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
---|
1324 | return err
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1328 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1329 | dc.SendMessage(&irc.Message{
|
---|
1330 | Prefix: dc.srv.prefix(),
|
---|
1331 | Command: irc.RPL_WHOISSERVER,
|
---|
1332 | Params: []string{dc.nick, nick, server, serverInfo},
|
---|
1333 | })
|
---|
1334 | })
|
---|
1335 | case irc.RPL_WHOISOPERATOR:
|
---|
1336 | var nick string
|
---|
1337 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
1338 | return err
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1342 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1343 | dc.SendMessage(&irc.Message{
|
---|
1344 | Prefix: dc.srv.prefix(),
|
---|
1345 | Command: irc.RPL_WHOISOPERATOR,
|
---|
1346 | Params: []string{dc.nick, nick, "is an IRC operator"},
|
---|
1347 | })
|
---|
1348 | })
|
---|
1349 | case irc.RPL_WHOISIDLE:
|
---|
1350 | var nick string
|
---|
1351 | if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
---|
1352 | return err
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1356 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1357 | params := []string{dc.nick, nick}
|
---|
1358 | params = append(params, msg.Params[2:]...)
|
---|
1359 | dc.SendMessage(&irc.Message{
|
---|
1360 | Prefix: dc.srv.prefix(),
|
---|
1361 | Command: irc.RPL_WHOISIDLE,
|
---|
1362 | Params: params,
|
---|
1363 | })
|
---|
1364 | })
|
---|
1365 | case irc.RPL_WHOISCHANNELS:
|
---|
1366 | var nick, channelList string
|
---|
1367 | if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
---|
1368 | return err
|
---|
1369 | }
|
---|
1370 | channels := splitSpace(channelList)
|
---|
1371 |
|
---|
1372 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1373 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1374 | channelList := make([]string, len(channels))
|
---|
1375 | for i, channel := range channels {
|
---|
1376 | prefix, channel := uc.parseMembershipPrefix(channel)
|
---|
1377 | channel = dc.marshalEntity(uc.network, channel)
|
---|
1378 | channelList[i] = prefix.Format(dc) + channel
|
---|
1379 | }
|
---|
1380 | channels := strings.Join(channelList, " ")
|
---|
1381 | dc.SendMessage(&irc.Message{
|
---|
1382 | Prefix: dc.srv.prefix(),
|
---|
1383 | Command: irc.RPL_WHOISCHANNELS,
|
---|
1384 | Params: []string{dc.nick, nick, channels},
|
---|
1385 | })
|
---|
1386 | })
|
---|
1387 | case irc.RPL_ENDOFWHOIS:
|
---|
1388 | var nick string
|
---|
1389 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
1390 | return err
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1394 | nick := dc.marshalEntity(uc.network, nick)
|
---|
1395 | dc.SendMessage(&irc.Message{
|
---|
1396 | Prefix: dc.srv.prefix(),
|
---|
1397 | Command: irc.RPL_ENDOFWHOIS,
|
---|
1398 | Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
---|
1399 | })
|
---|
1400 | })
|
---|
1401 | case "INVITE":
|
---|
1402 | var nick, channel string
|
---|
1403 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
1404 | return err
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | weAreInvited := uc.isOurNick(nick)
|
---|
1408 |
|
---|
1409 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1410 | if !weAreInvited && !dc.caps["invite-notify"] {
|
---|
1411 | return
|
---|
1412 | }
|
---|
1413 | dc.SendMessage(&irc.Message{
|
---|
1414 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
1415 | Command: "INVITE",
|
---|
1416 | Params: []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
1417 | })
|
---|
1418 | })
|
---|
1419 | case irc.RPL_INVITING:
|
---|
1420 | var nick, channel string
|
---|
1421 | if err := parseMessageParams(msg, nil, &nick, &channel); err != nil {
|
---|
1422 | return err
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1426 | dc.SendMessage(&irc.Message{
|
---|
1427 | Prefix: dc.srv.prefix(),
|
---|
1428 | Command: irc.RPL_INVITING,
|
---|
1429 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
1430 | })
|
---|
1431 | })
|
---|
1432 | case irc.RPL_MONONLINE, irc.RPL_MONOFFLINE:
|
---|
1433 | var targetsStr string
|
---|
1434 | if err := parseMessageParams(msg, nil, &targetsStr); err != nil {
|
---|
1435 | return err
|
---|
1436 | }
|
---|
1437 | targets := strings.Split(targetsStr, ",")
|
---|
1438 |
|
---|
1439 | online := msg.Command == irc.RPL_MONONLINE
|
---|
1440 | for _, target := range targets {
|
---|
1441 | prefix := irc.ParsePrefix(target)
|
---|
1442 | uc.monitored.SetValue(prefix.Name, online)
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1446 | for _, target := range targets {
|
---|
1447 | prefix := irc.ParsePrefix(target)
|
---|
1448 | if dc.monitored.Has(prefix.Name) {
|
---|
1449 | dc.SendMessage(&irc.Message{
|
---|
1450 | Prefix: dc.srv.prefix(),
|
---|
1451 | Command: msg.Command,
|
---|
1452 | Params: []string{dc.nick, target},
|
---|
1453 | })
|
---|
1454 | }
|
---|
1455 | }
|
---|
1456 | })
|
---|
1457 | case irc.ERR_MONLISTFULL:
|
---|
1458 | var limit, targetsStr string
|
---|
1459 | if err := parseMessageParams(msg, nil, &limit, &targetsStr); err != nil {
|
---|
1460 | return err
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | targets := strings.Split(targetsStr, ",")
|
---|
1464 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1465 | for _, target := range targets {
|
---|
1466 | if dc.monitored.Has(target) {
|
---|
1467 | dc.SendMessage(&irc.Message{
|
---|
1468 | Prefix: dc.srv.prefix(),
|
---|
1469 | Command: msg.Command,
|
---|
1470 | Params: []string{dc.nick, limit, target},
|
---|
1471 | })
|
---|
1472 | }
|
---|
1473 | }
|
---|
1474 | })
|
---|
1475 | case irc.RPL_AWAY:
|
---|
1476 | var nick, reason string
|
---|
1477 | if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
|
---|
1478 | return err
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1482 | dc.SendMessage(&irc.Message{
|
---|
1483 | Prefix: dc.srv.prefix(),
|
---|
1484 | Command: irc.RPL_AWAY,
|
---|
1485 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), reason},
|
---|
1486 | })
|
---|
1487 | })
|
---|
1488 | case "AWAY", "ACCOUNT":
|
---|
1489 | if msg.Prefix == nil {
|
---|
1490 | return fmt.Errorf("expected a prefix")
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1494 | dc.SendMessage(&irc.Message{
|
---|
1495 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
1496 | Command: msg.Command,
|
---|
1497 | Params: msg.Params,
|
---|
1498 | })
|
---|
1499 | })
|
---|
1500 | case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST:
|
---|
1501 | var channel, mask string
|
---|
1502 | if err := parseMessageParams(msg, nil, &channel, &mask); err != nil {
|
---|
1503 | return err
|
---|
1504 | }
|
---|
1505 | var addNick, addTime string
|
---|
1506 | if len(msg.Params) >= 5 {
|
---|
1507 | addNick = msg.Params[3]
|
---|
1508 | addTime = msg.Params[4]
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1512 | channel := dc.marshalEntity(uc.network, channel)
|
---|
1513 |
|
---|
1514 | var params []string
|
---|
1515 | if addNick != "" && addTime != "" {
|
---|
1516 | addNick := dc.marshalEntity(uc.network, addNick)
|
---|
1517 | params = []string{dc.nick, channel, mask, addNick, addTime}
|
---|
1518 | } else {
|
---|
1519 | params = []string{dc.nick, channel, mask}
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | dc.SendMessage(&irc.Message{
|
---|
1523 | Prefix: dc.srv.prefix(),
|
---|
1524 | Command: msg.Command,
|
---|
1525 | Params: params,
|
---|
1526 | })
|
---|
1527 | })
|
---|
1528 | case irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
|
---|
1529 | var channel, trailing string
|
---|
1530 | if err := parseMessageParams(msg, nil, &channel, &trailing); err != nil {
|
---|
1531 | return err
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1535 | upstreamChannel := dc.marshalEntity(uc.network, channel)
|
---|
1536 | dc.SendMessage(&irc.Message{
|
---|
1537 | Prefix: dc.srv.prefix(),
|
---|
1538 | Command: msg.Command,
|
---|
1539 | Params: []string{dc.nick, upstreamChannel, trailing},
|
---|
1540 | })
|
---|
1541 | })
|
---|
1542 | case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
---|
1543 | var command, reason string
|
---|
1544 | if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
---|
1545 | return err
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | if command == "LIST" || command == "WHO" {
|
---|
1549 | dc, _ := uc.dequeueCommand(command)
|
---|
1550 | if dc != nil && downstreamID == 0 {
|
---|
1551 | downstreamID = dc.id
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1556 | dc.SendMessage(&irc.Message{
|
---|
1557 | Prefix: uc.srv.prefix(),
|
---|
1558 | Command: msg.Command,
|
---|
1559 | Params: []string{dc.nick, command, reason},
|
---|
1560 | })
|
---|
1561 | })
|
---|
1562 | case "ACK":
|
---|
1563 | // Ignore
|
---|
1564 | case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
|
---|
1565 | // Ignore
|
---|
1566 | case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
---|
1567 | // Ignore
|
---|
1568 | case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
---|
1569 | fallthrough
|
---|
1570 | case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
---|
1571 | fallthrough
|
---|
1572 | case rpl_localusers, rpl_globalusers:
|
---|
1573 | fallthrough
|
---|
1574 | case irc.RPL_MOTDSTART, irc.RPL_MOTD:
|
---|
1575 | // Ignore these messages if they're part of the initial registration
|
---|
1576 | // message burst. Forward them if the user explicitly asked for them.
|
---|
1577 | if !uc.gotMotd {
|
---|
1578 | return nil
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1582 | dc.SendMessage(&irc.Message{
|
---|
1583 | Prefix: uc.srv.prefix(),
|
---|
1584 | Command: msg.Command,
|
---|
1585 | Params: msg.Params,
|
---|
1586 | })
|
---|
1587 | })
|
---|
1588 | case irc.RPL_LISTSTART:
|
---|
1589 | // Ignore
|
---|
1590 | case "ERROR":
|
---|
1591 | var text string
|
---|
1592 | if err := parseMessageParams(msg, &text); err != nil {
|
---|
1593 | return err
|
---|
1594 | }
|
---|
1595 | return fmt.Errorf("fatal server error: %v", text)
|
---|
1596 | case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
|
---|
1597 | if !uc.registered {
|
---|
1598 | text := msg.Params[len(msg.Params)-1]
|
---|
1599 | return registrationError(text)
|
---|
1600 | }
|
---|
1601 | fallthrough
|
---|
1602 | default:
|
---|
1603 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
1604 |
|
---|
1605 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
1606 | // best effort marshaling for unknown messages, replies and errors:
|
---|
1607 | // most numerics start with the user nick, marshal it if that's the case
|
---|
1608 | // otherwise, conservately keep the params without marshaling
|
---|
1609 | params := msg.Params
|
---|
1610 | if _, err := strconv.Atoi(msg.Command); err == nil { // numeric
|
---|
1611 | if len(msg.Params) > 0 && isOurNick(uc.network, msg.Params[0]) {
|
---|
1612 | params[0] = dc.nick
|
---|
1613 | }
|
---|
1614 | }
|
---|
1615 | dc.SendMessage(&irc.Message{
|
---|
1616 | Prefix: uc.srv.prefix(),
|
---|
1617 | Command: msg.Command,
|
---|
1618 | Params: params,
|
---|
1619 | })
|
---|
1620 | })
|
---|
1621 | }
|
---|
1622 | return nil
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | func (uc *upstreamConn) handleDetachedMessage(ch *Channel, msg *irc.Message) {
|
---|
1626 | if uc.network.detachedMessageNeedsRelay(ch, msg) {
|
---|
1627 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1628 | dc.relayDetachedMessage(uc.network, msg)
|
---|
1629 | })
|
---|
1630 | }
|
---|
1631 | if ch.ReattachOn == FilterMessage || (ch.ReattachOn == FilterHighlight && uc.network.isHighlight(msg)) {
|
---|
1632 | uc.network.attach(ch)
|
---|
1633 | if err := uc.srv.db.StoreChannel(context.TODO(), uc.network.ID, ch); err != nil {
|
---|
1634 | uc.logger.Printf("failed to update channel %q: %v", ch.Name, err)
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 | func (uc *upstreamConn) handleChanModes(s string) error {
|
---|
1640 | parts := strings.SplitN(s, ",", 5)
|
---|
1641 | if len(parts) < 4 {
|
---|
1642 | return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", s)
|
---|
1643 | }
|
---|
1644 | modes := make(map[byte]channelModeType)
|
---|
1645 | for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
---|
1646 | for j := 0; j < len(parts[i]); j++ {
|
---|
1647 | mode := parts[i][j]
|
---|
1648 | modes[mode] = mt
|
---|
1649 | }
|
---|
1650 | }
|
---|
1651 | uc.availableChannelModes = modes
|
---|
1652 | return nil
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | func (uc *upstreamConn) handleMemberships(s string) error {
|
---|
1656 | if s == "" {
|
---|
1657 | uc.availableMemberships = nil
|
---|
1658 | return nil
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | if s[0] != '(' {
|
---|
1662 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
1663 | }
|
---|
1664 | sep := strings.IndexByte(s, ')')
|
---|
1665 | if sep < 0 || len(s) != sep*2 {
|
---|
1666 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
1667 | }
|
---|
1668 | memberships := make([]membership, len(s)/2-1)
|
---|
1669 | for i := range memberships {
|
---|
1670 | memberships[i] = membership{
|
---|
1671 | Mode: s[i+1],
|
---|
1672 | Prefix: s[sep+i+1],
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 | uc.availableMemberships = memberships
|
---|
1676 | return nil
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
|
---|
1680 | caps := strings.Fields(capsStr)
|
---|
1681 | for _, s := range caps {
|
---|
1682 | kv := strings.SplitN(s, "=", 2)
|
---|
1683 | k := strings.ToLower(kv[0])
|
---|
1684 | var v string
|
---|
1685 | if len(kv) == 2 {
|
---|
1686 | v = kv[1]
|
---|
1687 | }
|
---|
1688 | uc.supportedCaps[k] = v
|
---|
1689 | }
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | func (uc *upstreamConn) requestCaps() {
|
---|
1693 | var requestCaps []string
|
---|
1694 | for c := range permanentUpstreamCaps {
|
---|
1695 | if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
|
---|
1696 | requestCaps = append(requestCaps, c)
|
---|
1697 | }
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | if uc.requestSASL() && !uc.caps["sasl"] {
|
---|
1701 | requestCaps = append(requestCaps, "sasl")
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | if len(requestCaps) == 0 {
|
---|
1705 | return
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | uc.SendMessage(&irc.Message{
|
---|
1709 | Command: "CAP",
|
---|
1710 | Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
---|
1711 | })
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | func (uc *upstreamConn) requestSASL() bool {
|
---|
1715 | if uc.network.SASL.Mechanism == "" {
|
---|
1716 | return false
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | v, ok := uc.supportedCaps["sasl"]
|
---|
1720 | if !ok {
|
---|
1721 | return false
|
---|
1722 | }
|
---|
1723 | if v != "" {
|
---|
1724 | mechanisms := strings.Split(v, ",")
|
---|
1725 | found := false
|
---|
1726 | for _, mech := range mechanisms {
|
---|
1727 | if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
|
---|
1728 | found = true
|
---|
1729 | break
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 | if !found {
|
---|
1733 | return false
|
---|
1734 | }
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | return true
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
---|
1741 | uc.caps[name] = ok
|
---|
1742 |
|
---|
1743 | switch name {
|
---|
1744 | case "sasl":
|
---|
1745 | if !ok {
|
---|
1746 | uc.logger.Printf("server refused to acknowledge the SASL capability")
|
---|
1747 | return nil
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | auth := &uc.network.SASL
|
---|
1751 | switch auth.Mechanism {
|
---|
1752 | case "PLAIN":
|
---|
1753 | uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
---|
1754 | uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
---|
1755 | case "EXTERNAL":
|
---|
1756 | uc.logger.Printf("starting SASL EXTERNAL authentication")
|
---|
1757 | uc.saslClient = sasl.NewExternalClient("")
|
---|
1758 | default:
|
---|
1759 | return fmt.Errorf("unsupported SASL mechanism %q", name)
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | uc.SendMessage(&irc.Message{
|
---|
1763 | Command: "AUTHENTICATE",
|
---|
1764 | Params: []string{auth.Mechanism},
|
---|
1765 | })
|
---|
1766 | default:
|
---|
1767 | if permanentUpstreamCaps[name] {
|
---|
1768 | break
|
---|
1769 | }
|
---|
1770 | uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
|
---|
1771 | }
|
---|
1772 | return nil
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | func splitSpace(s string) []string {
|
---|
1776 | return strings.FieldsFunc(s, func(r rune) bool {
|
---|
1777 | return r == ' '
|
---|
1778 | })
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | func (uc *upstreamConn) register() {
|
---|
1782 | uc.nick = GetNick(&uc.user.User, &uc.network.Network)
|
---|
1783 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
1784 | uc.username = GetUsername(&uc.user.User, &uc.network.Network)
|
---|
1785 | uc.realname = GetRealname(&uc.user.User, &uc.network.Network)
|
---|
1786 |
|
---|
1787 | uc.SendMessage(&irc.Message{
|
---|
1788 | Command: "CAP",
|
---|
1789 | Params: []string{"LS", "302"},
|
---|
1790 | })
|
---|
1791 |
|
---|
1792 | if uc.network.Pass != "" {
|
---|
1793 | uc.SendMessage(&irc.Message{
|
---|
1794 | Command: "PASS",
|
---|
1795 | Params: []string{uc.network.Pass},
|
---|
1796 | })
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | uc.SendMessage(&irc.Message{
|
---|
1800 | Command: "NICK",
|
---|
1801 | Params: []string{uc.nick},
|
---|
1802 | })
|
---|
1803 | uc.SendMessage(&irc.Message{
|
---|
1804 | Command: "USER",
|
---|
1805 | Params: []string{uc.username, "0", "*", uc.realname},
|
---|
1806 | })
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | func (uc *upstreamConn) runUntilRegistered() error {
|
---|
1810 | for !uc.registered {
|
---|
1811 | msg, err := uc.ReadMessage()
|
---|
1812 | if err != nil {
|
---|
1813 | return fmt.Errorf("failed to read message: %v", err)
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | if err := uc.handleMessage(msg); err != nil {
|
---|
1817 | if _, ok := err.(registrationError); ok {
|
---|
1818 | return err
|
---|
1819 | } else {
|
---|
1820 | msg.Tags = nil // prevent message tags from cluttering logs
|
---|
1821 | return fmt.Errorf("failed to handle message %q: %v", msg, err)
|
---|
1822 | }
|
---|
1823 | }
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | for _, command := range uc.network.ConnectCommands {
|
---|
1827 | m, err := irc.ParseMessage(command)
|
---|
1828 | if err != nil {
|
---|
1829 | uc.logger.Printf("failed to parse connect command %q: %v", command, err)
|
---|
1830 | } else {
|
---|
1831 | uc.SendMessage(m)
|
---|
1832 | }
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | return nil
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
---|
1839 | for {
|
---|
1840 | msg, err := uc.ReadMessage()
|
---|
1841 | if errors.Is(err, io.EOF) {
|
---|
1842 | break
|
---|
1843 | } else if err != nil {
|
---|
1844 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | ch <- eventUpstreamMessage{msg, uc}
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | return nil
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
---|
1854 | if !uc.caps["message-tags"] {
|
---|
1855 | msg = msg.Copy()
|
---|
1856 | msg.Tags = nil
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | uc.conn.SendMessage(msg)
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 | func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
---|
1863 | if uc.caps["labeled-response"] {
|
---|
1864 | if msg.Tags == nil {
|
---|
1865 | msg.Tags = make(map[string]irc.TagValue)
|
---|
1866 | }
|
---|
1867 | msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
---|
1868 | uc.nextLabelID++
|
---|
1869 | }
|
---|
1870 | uc.SendMessage(msg)
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | // appendLog appends a message to the log file.
|
---|
1874 | //
|
---|
1875 | // The internal message ID is returned. If the message isn't recorded in the
|
---|
1876 | // log file, an empty string is returned.
|
---|
1877 | func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string) {
|
---|
1878 | if uc.user.msgStore == nil {
|
---|
1879 | return ""
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | // Don't store messages with a server mask target
|
---|
1883 | if strings.HasPrefix(entity, "$") {
|
---|
1884 | return ""
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | entityCM := uc.network.casemap(entity)
|
---|
1888 | if entityCM == "nickserv" {
|
---|
1889 | // The messages sent/received from NickServ may contain
|
---|
1890 | // security-related information (like passwords). Don't store these.
|
---|
1891 | return ""
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | if !uc.network.delivered.HasTarget(entity) {
|
---|
1895 | // This is the first message we receive from this target. Save the last
|
---|
1896 | // message ID in delivery receipts, so that we can send the new message
|
---|
1897 | // in the backlog if an offline client reconnects.
|
---|
1898 | lastID, err := uc.user.msgStore.LastMsgID(&uc.network.Network, entityCM, time.Now())
|
---|
1899 | if err != nil {
|
---|
1900 | uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
|
---|
1901 | return ""
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | uc.network.delivered.ForEachClient(func(clientName string) {
|
---|
1905 | uc.network.delivered.StoreID(entity, clientName, lastID)
|
---|
1906 | })
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | msgID, err := uc.user.msgStore.Append(&uc.network.Network, entityCM, msg)
|
---|
1910 | if err != nil {
|
---|
1911 | uc.logger.Printf("failed to log message: %v", err)
|
---|
1912 | return ""
|
---|
1913 | }
|
---|
1914 |
|
---|
1915 | return msgID
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | // produce appends a message to the logs and forwards it to connected downstream
|
---|
1919 | // connections.
|
---|
1920 | //
|
---|
1921 | // If origin is not nil and origin doesn't support echo-message, the message is
|
---|
1922 | // forwarded to all connections except origin.
|
---|
1923 | func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
|
---|
1924 | var msgID string
|
---|
1925 | if target != "" {
|
---|
1926 | msgID = uc.appendLog(target, msg)
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | // Don't forward messages if it's a detached channel
|
---|
1930 | ch := uc.network.channels.Value(target)
|
---|
1931 | detached := ch != nil && ch.Detached
|
---|
1932 |
|
---|
1933 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1934 | if !detached && (dc != origin || dc.caps["echo-message"]) {
|
---|
1935 | dc.sendMessageWithID(dc.marshalMessage(msg, uc.network), msgID)
|
---|
1936 | } else {
|
---|
1937 | dc.advanceMessageWithID(msg, msgID)
|
---|
1938 | }
|
---|
1939 | })
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | func (uc *upstreamConn) updateAway() {
|
---|
1943 | away := true
|
---|
1944 | uc.forEachDownstream(func(*downstreamConn) {
|
---|
1945 | away = false
|
---|
1946 | })
|
---|
1947 | if away == uc.away {
|
---|
1948 | return
|
---|
1949 | }
|
---|
1950 | if away {
|
---|
1951 | uc.SendMessage(&irc.Message{
|
---|
1952 | Command: "AWAY",
|
---|
1953 | Params: []string{"Auto away"},
|
---|
1954 | })
|
---|
1955 | } else {
|
---|
1956 | uc.SendMessage(&irc.Message{
|
---|
1957 | Command: "AWAY",
|
---|
1958 | })
|
---|
1959 | }
|
---|
1960 | uc.away = away
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | func (uc *upstreamConn) updateChannelAutoDetach(name string) {
|
---|
1964 | uch := uc.channels.Value(name)
|
---|
1965 | if uch == nil {
|
---|
1966 | return
|
---|
1967 | }
|
---|
1968 | ch := uc.network.channels.Value(name)
|
---|
1969 | if ch == nil || ch.Detached {
|
---|
1970 | return
|
---|
1971 | }
|
---|
1972 | uch.updateAutoDetach(ch.DetachAfter)
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | func (uc *upstreamConn) updateMonitor() {
|
---|
1976 | add := make(map[string]struct{})
|
---|
1977 | var addList []string
|
---|
1978 | seen := make(map[string]struct{})
|
---|
1979 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
1980 | for targetCM := range dc.monitored.innerMap {
|
---|
1981 | if !uc.monitored.Has(targetCM) {
|
---|
1982 | if _, ok := add[targetCM]; !ok {
|
---|
1983 | addList = append(addList, targetCM)
|
---|
1984 | }
|
---|
1985 | add[targetCM] = struct{}{}
|
---|
1986 | } else {
|
---|
1987 | seen[targetCM] = struct{}{}
|
---|
1988 | }
|
---|
1989 | }
|
---|
1990 | })
|
---|
1991 |
|
---|
1992 | removeAll := true
|
---|
1993 | var removeList []string
|
---|
1994 | for targetCM, entry := range uc.monitored.innerMap {
|
---|
1995 | if _, ok := seen[targetCM]; ok {
|
---|
1996 | removeAll = false
|
---|
1997 | } else {
|
---|
1998 | removeList = append(removeList, entry.originalKey)
|
---|
1999 | }
|
---|
2000 | }
|
---|
2001 |
|
---|
2002 | // TODO: better handle the case where len(uc.monitored) + len(addList)
|
---|
2003 | // exceeds the limit, probably by immediately sending ERR_MONLISTFULL?
|
---|
2004 |
|
---|
2005 | if removeAll && len(addList) == 0 && len(removeList) > 0 {
|
---|
2006 | // Optimization when the last MONITOR-aware downstream disconnects
|
---|
2007 | uc.SendMessage(&irc.Message{
|
---|
2008 | Command: "MONITOR",
|
---|
2009 | Params: []string{"C"},
|
---|
2010 | })
|
---|
2011 | } else {
|
---|
2012 | msgs := generateMonitor("-", removeList)
|
---|
2013 | msgs = append(msgs, generateMonitor("+", addList)...)
|
---|
2014 | for _, msg := range msgs {
|
---|
2015 | uc.SendMessage(msg)
|
---|
2016 | }
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | for _, target := range removeList {
|
---|
2020 | uc.monitored.Delete(target)
|
---|
2021 | }
|
---|
2022 | }
|
---|