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