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