[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 |
|
---|
[682] | 295 | func (uc *upstreamConn) endPendingCommands() {
|
---|
| 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,
|
---|
| 308 | Params: []string{dc.nick, "End of /LIST"},
|
---|
| 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,
|
---|
| 318 | Params: []string{dc.nick, mask, "End of /WHO"},
|
---|
| 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 | }
|
---|
[682] | 345 | uc.SendMessage(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":
|
---|
[60] | 453 | uc.SendMessage(&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 |
|
---|
[92] | 532 | uc.SendMessage(&irc.Message{
|
---|
| 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 {
|
---|
| 543 | if err := uc.handleCapAck(strings.ToLower(name), subCmd == "ACK"); err != nil {
|
---|
| 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 {
|
---|
| 586 | uc.SendMessage(&irc.Message{
|
---|
| 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 {
|
---|
| 598 | uc.SendMessage(&irc.Message{
|
---|
| 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 {
|
---|
| 615 | uc.SendMessage(&irc.Message{
|
---|
| 616 | Command: "AUTHENTICATE",
|
---|
| 617 | Params: []string{"*"},
|
---|
| 618 | })
|
---|
| 619 | return err
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | // TODO: send response in multiple chunks if >= 400 bytes
|
---|
| 623 | var respStr = "+"
|
---|
[318] | 624 | if len(resp) != 0 {
|
---|
[95] | 625 | respStr = base64.StdEncoding.EncodeToString(resp)
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | uc.SendMessage(&irc.Message{
|
---|
| 629 | Command: "AUTHENTICATE",
|
---|
| 630 | Params: []string{respStr},
|
---|
| 631 | })
|
---|
[125] | 632 | case irc.RPL_LOGGEDIN:
|
---|
[559] | 633 | if err := parseMessageParams(msg, nil, nil, &uc.account); err != nil {
|
---|
[95] | 634 | return err
|
---|
| 635 | }
|
---|
[559] | 636 | uc.logger.Printf("logged in with account %q", uc.account)
|
---|
[722] | 637 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 638 | dc.updateAccount()
|
---|
| 639 | })
|
---|
[125] | 640 | case irc.RPL_LOGGEDOUT:
|
---|
[559] | 641 | uc.account = ""
|
---|
[95] | 642 | uc.logger.Printf("logged out")
|
---|
[722] | 643 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 644 | dc.updateAccount()
|
---|
| 645 | })
|
---|
[125] | 646 | case irc.ERR_NICKLOCKED, irc.RPL_SASLSUCCESS, irc.ERR_SASLFAIL, irc.ERR_SASLTOOLONG, irc.ERR_SASLABORTED:
|
---|
[95] | 647 | var info string
|
---|
| 648 | if err := parseMessageParams(msg, nil, &info); err != nil {
|
---|
| 649 | return err
|
---|
| 650 | }
|
---|
| 651 | switch msg.Command {
|
---|
[125] | 652 | case irc.ERR_NICKLOCKED:
|
---|
[95] | 653 | uc.logger.Printf("invalid nick used with SASL authentication: %v", info)
|
---|
[125] | 654 | case irc.ERR_SASLFAIL:
|
---|
[95] | 655 | uc.logger.Printf("SASL authentication failed: %v", info)
|
---|
[125] | 656 | case irc.ERR_SASLTOOLONG:
|
---|
[95] | 657 | uc.logger.Printf("SASL message too long: %v", info)
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | uc.saslClient = nil
|
---|
| 661 | uc.saslStarted = false
|
---|
| 662 |
|
---|
[724] | 663 | if dc, _ := uc.dequeueCommand("AUTHENTICATE"); dc != nil && dc.sasl != nil {
|
---|
| 664 | if msg.Command == irc.RPL_SASLSUCCESS {
|
---|
[739] | 665 | uc.network.autoSaveSASLPlain(ctx, dc.sasl.plainUsername, dc.sasl.plainPassword)
|
---|
[724] | 666 | }
|
---|
| 667 |
|
---|
| 668 | dc.endSASL(msg)
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | if !uc.registered {
|
---|
| 672 | uc.SendMessage(&irc.Message{
|
---|
| 673 | Command: "CAP",
|
---|
| 674 | Params: []string{"END"},
|
---|
| 675 | })
|
---|
| 676 | }
|
---|
[729] | 677 | case "REGISTER", "VERIFY":
|
---|
| 678 | if dc, cmd := uc.dequeueCommand(msg.Command); dc != nil {
|
---|
| 679 | if msg.Command == "REGISTER" {
|
---|
| 680 | var account, password string
|
---|
| 681 | if err := parseMessageParams(msg, nil, &account); err != nil {
|
---|
| 682 | return err
|
---|
| 683 | }
|
---|
| 684 | if err := parseMessageParams(cmd, nil, nil, &password); err != nil {
|
---|
| 685 | return err
|
---|
| 686 | }
|
---|
[739] | 687 | uc.network.autoSaveSASLPlain(ctx, account, password)
|
---|
[729] | 688 | }
|
---|
| 689 |
|
---|
| 690 | dc.SendMessage(msg)
|
---|
| 691 | }
|
---|
[14] | 692 | case irc.RPL_WELCOME:
|
---|
[55] | 693 | uc.registered = true
|
---|
| 694 | uc.logger.Printf("connection registered")
|
---|
[19] | 695 |
|
---|
[478] | 696 | if uc.network.channels.Len() > 0 {
|
---|
[350] | 697 | var channels, keys []string
|
---|
[478] | 698 | for _, entry := range uc.network.channels.innerMap {
|
---|
| 699 | ch := entry.value.(*Channel)
|
---|
[350] | 700 | channels = append(channels, ch.Name)
|
---|
[310] | 701 | keys = append(keys, ch.Key)
|
---|
| 702 | }
|
---|
[350] | 703 |
|
---|
| 704 | for _, msg := range join(channels, keys) {
|
---|
| 705 | uc.SendMessage(msg)
|
---|
| 706 | }
|
---|
[19] | 707 | }
|
---|
[16] | 708 | case irc.RPL_MYINFO:
|
---|
[139] | 709 | if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, nil); err != nil {
|
---|
[43] | 710 | return err
|
---|
[16] | 711 | }
|
---|
[139] | 712 | case irc.RPL_ISUPPORT:
|
---|
| 713 | if err := parseMessageParams(msg, nil, nil); err != nil {
|
---|
| 714 | return err
|
---|
[16] | 715 | }
|
---|
[463] | 716 |
|
---|
| 717 | var downstreamIsupport []string
|
---|
[139] | 718 | for _, token := range msg.Params[1 : len(msg.Params)-1] {
|
---|
| 719 | parameter := token
|
---|
[460] | 720 | var negate, hasValue bool
|
---|
| 721 | var value string
|
---|
[139] | 722 | if strings.HasPrefix(token, "-") {
|
---|
| 723 | negate = true
|
---|
| 724 | token = token[1:]
|
---|
[459] | 725 | } else if i := strings.IndexByte(token, '='); i >= 0 {
|
---|
| 726 | parameter = token[:i]
|
---|
| 727 | value = token[i+1:]
|
---|
[460] | 728 | hasValue = true
|
---|
[139] | 729 | }
|
---|
[460] | 730 |
|
---|
| 731 | if hasValue {
|
---|
| 732 | uc.isupport[parameter] = &value
|
---|
| 733 | } else if !negate {
|
---|
| 734 | uc.isupport[parameter] = nil
|
---|
| 735 | } else {
|
---|
| 736 | delete(uc.isupport, parameter)
|
---|
| 737 | }
|
---|
| 738 |
|
---|
[462] | 739 | var err error
|
---|
| 740 | switch parameter {
|
---|
[478] | 741 | case "CASEMAPPING":
|
---|
| 742 | casemap, ok := parseCasemappingToken(value)
|
---|
| 743 | if !ok {
|
---|
| 744 | casemap = casemapRFC1459
|
---|
| 745 | }
|
---|
| 746 | uc.network.updateCasemapping(casemap)
|
---|
| 747 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
| 748 | uc.casemapIsSet = true
|
---|
[462] | 749 | case "CHANMODES":
|
---|
| 750 | if !negate {
|
---|
| 751 | err = uc.handleChanModes(value)
|
---|
| 752 | } else {
|
---|
| 753 | uc.availableChannelModes = stdChannelModes
|
---|
| 754 | }
|
---|
| 755 | case "CHANTYPES":
|
---|
| 756 | if !negate {
|
---|
[139] | 757 | uc.availableChannelTypes = value
|
---|
[462] | 758 | } else {
|
---|
| 759 | uc.availableChannelTypes = stdChannelTypes
|
---|
[139] | 760 | }
|
---|
[462] | 761 | case "PREFIX":
|
---|
| 762 | if !negate {
|
---|
| 763 | err = uc.handleMemberships(value)
|
---|
| 764 | } else {
|
---|
| 765 | uc.availableMemberships = stdMemberships
|
---|
| 766 | }
|
---|
[139] | 767 | }
|
---|
[462] | 768 | if err != nil {
|
---|
| 769 | return err
|
---|
| 770 | }
|
---|
[463] | 771 |
|
---|
| 772 | if passthroughIsupport[parameter] {
|
---|
| 773 | downstreamIsupport = append(downstreamIsupport, token)
|
---|
| 774 | }
|
---|
[139] | 775 | }
|
---|
[463] | 776 |
|
---|
| 777 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 778 | if dc.network == nil {
|
---|
| 779 | return
|
---|
| 780 | }
|
---|
| 781 | msgs := generateIsupport(dc.srv.prefix(), dc.nick, downstreamIsupport)
|
---|
| 782 | for _, msg := range msgs {
|
---|
| 783 | dc.SendMessage(msg)
|
---|
| 784 | }
|
---|
| 785 | })
|
---|
[478] | 786 | case irc.ERR_NOMOTD, irc.RPL_ENDOFMOTD:
|
---|
| 787 | if !uc.casemapIsSet {
|
---|
| 788 | // upstream did not send any CASEMAPPING token, thus
|
---|
| 789 | // we assume it implements the old RFCs with rfc1459.
|
---|
| 790 | uc.casemapIsSet = true
|
---|
| 791 | uc.network.updateCasemapping(casemapRFC1459)
|
---|
| 792 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
| 793 | }
|
---|
[552] | 794 |
|
---|
| 795 | if !uc.gotMotd {
|
---|
| 796 | // Ignore the initial MOTD upon connection, but forward
|
---|
| 797 | // subsequent MOTD messages downstream
|
---|
| 798 | uc.gotMotd = true
|
---|
| 799 | return nil
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[553] | 802 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 803 | dc.SendMessage(&irc.Message{
|
---|
| 804 | Prefix: uc.srv.prefix(),
|
---|
[552] | 805 | Command: msg.Command,
|
---|
[553] | 806 | Params: msg.Params,
|
---|
[552] | 807 | })
|
---|
| 808 | })
|
---|
[153] | 809 | case "BATCH":
|
---|
| 810 | var tag string
|
---|
| 811 | if err := parseMessageParams(msg, &tag); err != nil {
|
---|
| 812 | return err
|
---|
| 813 | }
|
---|
| 814 |
|
---|
| 815 | if strings.HasPrefix(tag, "+") {
|
---|
| 816 | tag = tag[1:]
|
---|
| 817 | if _, ok := uc.batches[tag]; ok {
|
---|
| 818 | return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
---|
| 819 | }
|
---|
| 820 | var batchType string
|
---|
| 821 | if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
---|
| 822 | return err
|
---|
| 823 | }
|
---|
[155] | 824 | label := label
|
---|
| 825 | if label == "" && msgBatch != nil {
|
---|
| 826 | label = msgBatch.Label
|
---|
| 827 | }
|
---|
[153] | 828 | uc.batches[tag] = batch{
|
---|
| 829 | Type: batchType,
|
---|
| 830 | Params: msg.Params[2:],
|
---|
| 831 | Outer: msgBatch,
|
---|
[155] | 832 | Label: label,
|
---|
[153] | 833 | }
|
---|
| 834 | } else if strings.HasPrefix(tag, "-") {
|
---|
| 835 | tag = tag[1:]
|
---|
| 836 | if _, ok := uc.batches[tag]; !ok {
|
---|
| 837 | return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
---|
| 838 | }
|
---|
| 839 | delete(uc.batches, tag)
|
---|
| 840 | } else {
|
---|
| 841 | return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
---|
| 842 | }
|
---|
[42] | 843 | case "NICK":
|
---|
[83] | 844 | if msg.Prefix == nil {
|
---|
| 845 | return fmt.Errorf("expected a prefix")
|
---|
| 846 | }
|
---|
| 847 |
|
---|
[43] | 848 | var newNick string
|
---|
| 849 | if err := parseMessageParams(msg, &newNick); err != nil {
|
---|
| 850 | return err
|
---|
[42] | 851 | }
|
---|
| 852 |
|
---|
[244] | 853 | me := false
|
---|
[478] | 854 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
[55] | 855 | uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
---|
[244] | 856 | me = true
|
---|
[55] | 857 | uc.nick = newNick
|
---|
[478] | 858 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
[42] | 859 | }
|
---|
| 860 |
|
---|
[478] | 861 | for _, entry := range uc.channels.innerMap {
|
---|
| 862 | ch := entry.value.(*upstreamChannel)
|
---|
| 863 | memberships := ch.Members.Value(msg.Prefix.Name)
|
---|
| 864 | if memberships != nil {
|
---|
| 865 | ch.Members.Delete(msg.Prefix.Name)
|
---|
| 866 | ch.Members.SetValue(newNick, memberships)
|
---|
[215] | 867 | uc.appendLog(ch.Name, msg)
|
---|
[42] | 868 | }
|
---|
| 869 | }
|
---|
[82] | 870 |
|
---|
[244] | 871 | if !me {
|
---|
[82] | 872 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 873 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[82] | 874 | })
|
---|
[296] | 875 | } else {
|
---|
| 876 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 877 | dc.updateNick()
|
---|
| 878 | })
|
---|
[82] | 879 | }
|
---|
[540] | 880 | case "SETNAME":
|
---|
| 881 | if msg.Prefix == nil {
|
---|
| 882 | return fmt.Errorf("expected a prefix")
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | var newRealname string
|
---|
| 886 | if err := parseMessageParams(msg, &newRealname); err != nil {
|
---|
| 887 | return err
|
---|
| 888 | }
|
---|
| 889 |
|
---|
| 890 | // TODO: consider appending this message to logs
|
---|
| 891 |
|
---|
| 892 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
| 893 | uc.logger.Printf("changed realname from %q to %q", uc.realname, newRealname)
|
---|
| 894 | uc.realname = newRealname
|
---|
| 895 |
|
---|
| 896 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 897 | dc.updateRealname()
|
---|
| 898 | })
|
---|
| 899 | } else {
|
---|
| 900 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 901 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
| 902 | })
|
---|
| 903 | }
|
---|
[69] | 904 | case "JOIN":
|
---|
| 905 | if msg.Prefix == nil {
|
---|
| 906 | return fmt.Errorf("expected a prefix")
|
---|
| 907 | }
|
---|
[42] | 908 |
|
---|
[43] | 909 | var channels string
|
---|
| 910 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 911 | return err
|
---|
[19] | 912 | }
|
---|
[34] | 913 |
|
---|
[43] | 914 | for _, ch := range strings.Split(channels, ",") {
|
---|
[478] | 915 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
[55] | 916 | uc.logger.Printf("joined channel %q", ch)
|
---|
[478] | 917 | members := membershipsCasemapMap{newCasemapMap(0)}
|
---|
| 918 | members.casemap = uc.network.casemap
|
---|
| 919 | uc.channels.SetValue(ch, &upstreamChannel{
|
---|
[34] | 920 | Name: ch,
|
---|
[55] | 921 | conn: uc,
|
---|
[478] | 922 | Members: members,
|
---|
| 923 | })
|
---|
[435] | 924 | uc.updateChannelAutoDetach(ch)
|
---|
[139] | 925 |
|
---|
| 926 | uc.SendMessage(&irc.Message{
|
---|
| 927 | Command: "MODE",
|
---|
| 928 | Params: []string{ch},
|
---|
| 929 | })
|
---|
[34] | 930 | } else {
|
---|
[55] | 931 | ch, err := uc.getChannel(ch)
|
---|
[34] | 932 | if err != nil {
|
---|
| 933 | return err
|
---|
| 934 | }
|
---|
[478] | 935 | ch.Members.SetValue(msg.Prefix.Name, &memberships{})
|
---|
[19] | 936 | }
|
---|
[69] | 937 |
|
---|
[245] | 938 | chMsg := msg.Copy()
|
---|
| 939 | chMsg.Params[0] = ch
|
---|
| 940 | uc.produce(ch, chMsg, nil)
|
---|
[19] | 941 | }
|
---|
[69] | 942 | case "PART":
|
---|
| 943 | if msg.Prefix == nil {
|
---|
| 944 | return fmt.Errorf("expected a prefix")
|
---|
| 945 | }
|
---|
[34] | 946 |
|
---|
[43] | 947 | var channels string
|
---|
| 948 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 949 | return err
|
---|
[34] | 950 | }
|
---|
| 951 |
|
---|
[43] | 952 | for _, ch := range strings.Split(channels, ",") {
|
---|
[478] | 953 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
[55] | 954 | uc.logger.Printf("parted channel %q", ch)
|
---|
[478] | 955 | uch := uc.channels.Value(ch)
|
---|
| 956 | if uch != nil {
|
---|
| 957 | uc.channels.Delete(ch)
|
---|
[435] | 958 | uch.updateAutoDetach(0)
|
---|
| 959 | }
|
---|
[34] | 960 | } else {
|
---|
[55] | 961 | ch, err := uc.getChannel(ch)
|
---|
[34] | 962 | if err != nil {
|
---|
| 963 | return err
|
---|
| 964 | }
|
---|
[478] | 965 | ch.Members.Delete(msg.Prefix.Name)
|
---|
[34] | 966 | }
|
---|
[69] | 967 |
|
---|
[245] | 968 | chMsg := msg.Copy()
|
---|
| 969 | chMsg.Params[0] = ch
|
---|
| 970 | uc.produce(ch, chMsg, nil)
|
---|
[34] | 971 | }
|
---|
[159] | 972 | case "KICK":
|
---|
| 973 | if msg.Prefix == nil {
|
---|
| 974 | return fmt.Errorf("expected a prefix")
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | var channel, user string
|
---|
| 978 | if err := parseMessageParams(msg, &channel, &user); err != nil {
|
---|
| 979 | return err
|
---|
| 980 | }
|
---|
| 981 |
|
---|
[478] | 982 | if uc.isOurNick(user) {
|
---|
[159] | 983 | uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
---|
[478] | 984 | uc.channels.Delete(channel)
|
---|
[159] | 985 | } else {
|
---|
| 986 | ch, err := uc.getChannel(channel)
|
---|
| 987 | if err != nil {
|
---|
| 988 | return err
|
---|
| 989 | }
|
---|
[478] | 990 | ch.Members.Delete(user)
|
---|
[159] | 991 | }
|
---|
| 992 |
|
---|
[245] | 993 | uc.produce(channel, msg, nil)
|
---|
[83] | 994 | case "QUIT":
|
---|
| 995 | if msg.Prefix == nil {
|
---|
| 996 | return fmt.Errorf("expected a prefix")
|
---|
| 997 | }
|
---|
| 998 |
|
---|
[478] | 999 | if uc.isOurNick(msg.Prefix.Name) {
|
---|
[83] | 1000 | uc.logger.Printf("quit")
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
[478] | 1003 | for _, entry := range uc.channels.innerMap {
|
---|
| 1004 | ch := entry.value.(*upstreamChannel)
|
---|
| 1005 | if ch.Members.Has(msg.Prefix.Name) {
|
---|
| 1006 | ch.Members.Delete(msg.Prefix.Name)
|
---|
[178] | 1007 |
|
---|
[215] | 1008 | uc.appendLog(ch.Name, msg)
|
---|
[178] | 1009 | }
|
---|
[83] | 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | if msg.Prefix.Name != uc.nick {
|
---|
| 1013 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 1014 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[83] | 1015 | })
|
---|
| 1016 | }
|
---|
[19] | 1017 | case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
---|
[43] | 1018 | var name, topic string
|
---|
| 1019 | if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
---|
| 1020 | return err
|
---|
[19] | 1021 | }
|
---|
[55] | 1022 | ch, err := uc.getChannel(name)
|
---|
[19] | 1023 | if err != nil {
|
---|
| 1024 | return err
|
---|
| 1025 | }
|
---|
| 1026 | if msg.Command == irc.RPL_TOPIC {
|
---|
[43] | 1027 | ch.Topic = topic
|
---|
[19] | 1028 | } else {
|
---|
| 1029 | ch.Topic = ""
|
---|
| 1030 | }
|
---|
| 1031 | case "TOPIC":
|
---|
[405] | 1032 | if msg.Prefix == nil {
|
---|
| 1033 | return fmt.Errorf("expected a prefix")
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
[43] | 1036 | var name string
|
---|
[74] | 1037 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
[43] | 1038 | return err
|
---|
[19] | 1039 | }
|
---|
[55] | 1040 | ch, err := uc.getChannel(name)
|
---|
[19] | 1041 | if err != nil {
|
---|
| 1042 | return err
|
---|
| 1043 | }
|
---|
| 1044 | if len(msg.Params) > 1 {
|
---|
| 1045 | ch.Topic = msg.Params[1]
|
---|
[405] | 1046 | ch.TopicWho = msg.Prefix.Copy()
|
---|
| 1047 | ch.TopicTime = time.Now() // TODO use msg.Tags["time"]
|
---|
[19] | 1048 | } else {
|
---|
| 1049 | ch.Topic = ""
|
---|
| 1050 | }
|
---|
[245] | 1051 | uc.produce(ch.Name, msg, nil)
|
---|
[139] | 1052 | case "MODE":
|
---|
| 1053 | var name, modeStr string
|
---|
| 1054 | if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
---|
| 1055 | return err
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
| 1058 | if !uc.isChannel(name) { // user mode change
|
---|
| 1059 | if name != uc.nick {
|
---|
| 1060 | return fmt.Errorf("received MODE message for unknown nick %q", name)
|
---|
| 1061 | }
|
---|
[553] | 1062 |
|
---|
| 1063 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
| 1064 | return err
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1068 | if dc.upstream() == nil {
|
---|
| 1069 | return
|
---|
| 1070 | }
|
---|
| 1071 |
|
---|
| 1072 | dc.SendMessage(msg)
|
---|
| 1073 | })
|
---|
[139] | 1074 | } else { // channel mode change
|
---|
| 1075 | ch, err := uc.getChannel(name)
|
---|
| 1076 | if err != nil {
|
---|
| 1077 | return err
|
---|
| 1078 | }
|
---|
| 1079 |
|
---|
[293] | 1080 | needMarshaling, err := applyChannelModes(ch, modeStr, msg.Params[2:])
|
---|
| 1081 | if err != nil {
|
---|
| 1082 | return err
|
---|
[139] | 1083 | }
|
---|
| 1084 |
|
---|
[293] | 1085 | uc.appendLog(ch.Name, msg)
|
---|
| 1086 |
|
---|
[478] | 1087 | c := uc.network.channels.Value(name)
|
---|
| 1088 | if c == nil || !c.Detached {
|
---|
[338] | 1089 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1090 | params := make([]string, len(msg.Params))
|
---|
| 1091 | params[0] = dc.marshalEntity(uc.network, name)
|
---|
| 1092 | params[1] = modeStr
|
---|
| 1093 |
|
---|
| 1094 | copy(params[2:], msg.Params[2:])
|
---|
| 1095 | for i, modeParam := range params[2:] {
|
---|
| 1096 | if _, ok := needMarshaling[i]; ok {
|
---|
| 1097 | params[2+i] = dc.marshalEntity(uc.network, modeParam)
|
---|
| 1098 | }
|
---|
[293] | 1099 | }
|
---|
| 1100 |
|
---|
[338] | 1101 | dc.SendMessage(&irc.Message{
|
---|
| 1102 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
| 1103 | Command: "MODE",
|
---|
| 1104 | Params: params,
|
---|
| 1105 | })
|
---|
[293] | 1106 | })
|
---|
[338] | 1107 | }
|
---|
[139] | 1108 | }
|
---|
| 1109 | case irc.RPL_UMODEIS:
|
---|
| 1110 | if err := parseMessageParams(msg, nil); err != nil {
|
---|
| 1111 | return err
|
---|
| 1112 | }
|
---|
| 1113 | modeStr := ""
|
---|
| 1114 | if len(msg.Params) > 1 {
|
---|
| 1115 | modeStr = msg.Params[1]
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | uc.modes = ""
|
---|
| 1119 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
| 1120 | return err
|
---|
| 1121 | }
|
---|
[553] | 1122 |
|
---|
| 1123 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1124 | if dc.upstream() == nil {
|
---|
| 1125 | return
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
| 1128 | dc.SendMessage(msg)
|
---|
| 1129 | })
|
---|
[139] | 1130 | case irc.RPL_CHANNELMODEIS:
|
---|
| 1131 | var channel string
|
---|
| 1132 | if err := parseMessageParams(msg, nil, &channel); err != nil {
|
---|
| 1133 | return err
|
---|
| 1134 | }
|
---|
| 1135 | modeStr := ""
|
---|
| 1136 | if len(msg.Params) > 2 {
|
---|
| 1137 | modeStr = msg.Params[2]
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
| 1140 | ch, err := uc.getChannel(channel)
|
---|
| 1141 | if err != nil {
|
---|
| 1142 | return err
|
---|
| 1143 | }
|
---|
| 1144 |
|
---|
| 1145 | firstMode := ch.modes == nil
|
---|
| 1146 | ch.modes = make(map[byte]string)
|
---|
[293] | 1147 | if _, err := applyChannelModes(ch, modeStr, msg.Params[3:]); err != nil {
|
---|
[139] | 1148 | return err
|
---|
| 1149 | }
|
---|
| 1150 | if firstMode {
|
---|
[478] | 1151 | c := uc.network.channels.Value(channel)
|
---|
| 1152 | if c == nil || !c.Detached {
|
---|
[338] | 1153 | modeStr, modeParams := ch.modes.Format()
|
---|
[139] | 1154 |
|
---|
[338] | 1155 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1156 | params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
|
---|
| 1157 | params = append(params, modeParams...)
|
---|
[139] | 1158 |
|
---|
[338] | 1159 | dc.SendMessage(&irc.Message{
|
---|
| 1160 | Prefix: dc.srv.prefix(),
|
---|
| 1161 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1162 | Params: params,
|
---|
| 1163 | })
|
---|
[139] | 1164 | })
|
---|
[338] | 1165 | }
|
---|
[139] | 1166 | }
|
---|
[162] | 1167 | case rpl_creationtime:
|
---|
| 1168 | var channel, creationTime string
|
---|
| 1169 | if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
---|
| 1170 | return err
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | ch, err := uc.getChannel(channel)
|
---|
| 1174 | if err != nil {
|
---|
| 1175 | return err
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
| 1178 | firstCreationTime := ch.creationTime == ""
|
---|
| 1179 | ch.creationTime = creationTime
|
---|
| 1180 | if firstCreationTime {
|
---|
| 1181 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1182 | dc.SendMessage(&irc.Message{
|
---|
| 1183 | Prefix: dc.srv.prefix(),
|
---|
| 1184 | Command: rpl_creationtime,
|
---|
[403] | 1185 | Params: []string{dc.nick, dc.marshalEntity(uc.network, ch.Name), creationTime},
|
---|
[162] | 1186 | })
|
---|
| 1187 | })
|
---|
| 1188 | }
|
---|
[19] | 1189 | case rpl_topicwhotime:
|
---|
[43] | 1190 | var name, who, timeStr string
|
---|
| 1191 | if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
---|
| 1192 | return err
|
---|
[19] | 1193 | }
|
---|
[55] | 1194 | ch, err := uc.getChannel(name)
|
---|
[19] | 1195 | if err != nil {
|
---|
| 1196 | return err
|
---|
| 1197 | }
|
---|
[405] | 1198 | firstTopicWhoTime := ch.TopicWho == nil
|
---|
| 1199 | ch.TopicWho = irc.ParsePrefix(who)
|
---|
[43] | 1200 | sec, err := strconv.ParseInt(timeStr, 10, 64)
|
---|
[19] | 1201 | if err != nil {
|
---|
| 1202 | return fmt.Errorf("failed to parse topic time: %v", err)
|
---|
| 1203 | }
|
---|
| 1204 | ch.TopicTime = time.Unix(sec, 0)
|
---|
[405] | 1205 | if firstTopicWhoTime {
|
---|
| 1206 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1207 | topicWho := dc.marshalUserPrefix(uc.network, ch.TopicWho)
|
---|
| 1208 | dc.SendMessage(&irc.Message{
|
---|
| 1209 | Prefix: dc.srv.prefix(),
|
---|
| 1210 | Command: rpl_topicwhotime,
|
---|
| 1211 | Params: []string{
|
---|
| 1212 | dc.nick,
|
---|
| 1213 | dc.marshalEntity(uc.network, ch.Name),
|
---|
| 1214 | topicWho.String(),
|
---|
| 1215 | timeStr,
|
---|
| 1216 | },
|
---|
| 1217 | })
|
---|
| 1218 | })
|
---|
| 1219 | }
|
---|
[177] | 1220 | case irc.RPL_LIST:
|
---|
| 1221 | var channel, clients, topic string
|
---|
| 1222 | if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
---|
| 1223 | return err
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
[682] | 1226 | dc, cmd := uc.currentPendingCommand("LIST")
|
---|
[681] | 1227 | if cmd == nil {
|
---|
[177] | 1228 | return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
---|
[681] | 1229 | } else if dc == nil {
|
---|
| 1230 | return nil
|
---|
[177] | 1231 | }
|
---|
| 1232 |
|
---|
[681] | 1233 | dc.SendMessage(&irc.Message{
|
---|
| 1234 | Prefix: dc.srv.prefix(),
|
---|
| 1235 | Command: irc.RPL_LIST,
|
---|
| 1236 | Params: []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
|
---|
[177] | 1237 | })
|
---|
| 1238 | case irc.RPL_LISTEND:
|
---|
[682] | 1239 | dc, cmd := uc.dequeueCommand("LIST")
|
---|
[681] | 1240 | if cmd == nil {
|
---|
[177] | 1241 | return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
---|
[681] | 1242 | } else if dc == nil {
|
---|
| 1243 | return nil
|
---|
[177] | 1244 | }
|
---|
[681] | 1245 |
|
---|
| 1246 | dc.SendMessage(&irc.Message{
|
---|
| 1247 | Prefix: dc.srv.prefix(),
|
---|
| 1248 | Command: irc.RPL_LISTEND,
|
---|
| 1249 | Params: []string{dc.nick, "End of /LIST"},
|
---|
| 1250 | })
|
---|
[19] | 1251 | case irc.RPL_NAMREPLY:
|
---|
[43] | 1252 | var name, statusStr, members string
|
---|
| 1253 | if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
---|
| 1254 | return err
|
---|
[19] | 1255 | }
|
---|
[140] | 1256 |
|
---|
[478] | 1257 | ch := uc.channels.Value(name)
|
---|
| 1258 | if ch == nil {
|
---|
[140] | 1259 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1260 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1261 | channel := dc.marshalEntity(uc.network, name)
|
---|
[174] | 1262 | members := splitSpace(members)
|
---|
[140] | 1263 | for i, member := range members {
|
---|
[292] | 1264 | memberships, nick := uc.parseMembershipPrefix(member)
|
---|
| 1265 | members[i] = memberships.Format(dc) + dc.marshalEntity(uc.network, nick)
|
---|
[140] | 1266 | }
|
---|
| 1267 | memberStr := strings.Join(members, " ")
|
---|
| 1268 |
|
---|
| 1269 | dc.SendMessage(&irc.Message{
|
---|
| 1270 | Prefix: dc.srv.prefix(),
|
---|
| 1271 | Command: irc.RPL_NAMREPLY,
|
---|
| 1272 | Params: []string{dc.nick, statusStr, channel, memberStr},
|
---|
| 1273 | })
|
---|
| 1274 | })
|
---|
| 1275 | return nil
|
---|
[19] | 1276 | }
|
---|
| 1277 |
|
---|
[43] | 1278 | status, err := parseChannelStatus(statusStr)
|
---|
[19] | 1279 | if err != nil {
|
---|
| 1280 | return err
|
---|
| 1281 | }
|
---|
| 1282 | ch.Status = status
|
---|
| 1283 |
|
---|
[174] | 1284 | for _, s := range splitSpace(members) {
|
---|
[292] | 1285 | memberships, nick := uc.parseMembershipPrefix(s)
|
---|
[478] | 1286 | ch.Members.SetValue(nick, memberships)
|
---|
[19] | 1287 | }
|
---|
| 1288 | case irc.RPL_ENDOFNAMES:
|
---|
[43] | 1289 | var name string
|
---|
| 1290 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1291 | return err
|
---|
[25] | 1292 | }
|
---|
[140] | 1293 |
|
---|
[478] | 1294 | ch := uc.channels.Value(name)
|
---|
| 1295 | if ch == nil {
|
---|
[140] | 1296 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1297 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1298 | channel := dc.marshalEntity(uc.network, name)
|
---|
[140] | 1299 |
|
---|
| 1300 | dc.SendMessage(&irc.Message{
|
---|
| 1301 | Prefix: dc.srv.prefix(),
|
---|
| 1302 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1303 | Params: []string{dc.nick, channel, "End of /NAMES list"},
|
---|
| 1304 | })
|
---|
| 1305 | })
|
---|
| 1306 | return nil
|
---|
[25] | 1307 | }
|
---|
| 1308 |
|
---|
[34] | 1309 | if ch.complete {
|
---|
| 1310 | return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
---|
| 1311 | }
|
---|
[25] | 1312 | ch.complete = true
|
---|
[27] | 1313 |
|
---|
[478] | 1314 | c := uc.network.channels.Value(name)
|
---|
| 1315 | if c == nil || !c.Detached {
|
---|
[338] | 1316 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1317 | forwardChannel(dc, ch)
|
---|
| 1318 | })
|
---|
| 1319 | }
|
---|
[127] | 1320 | case irc.RPL_WHOREPLY:
|
---|
| 1321 | var channel, username, host, server, nick, mode, trailing string
|
---|
| 1322 | if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
---|
| 1323 | return err
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
[682] | 1326 | dc, cmd := uc.currentPendingCommand("WHO")
|
---|
| 1327 | if cmd == nil {
|
---|
| 1328 | return fmt.Errorf("unexpected RPL_WHOREPLY: no matching pending WHO")
|
---|
| 1329 | } else if dc == nil {
|
---|
| 1330 | return nil
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
[127] | 1333 | parts := strings.SplitN(trailing, " ", 2)
|
---|
| 1334 | if len(parts) != 2 {
|
---|
| 1335 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
---|
| 1336 | }
|
---|
| 1337 | realname := parts[1]
|
---|
| 1338 | hops, err := strconv.Atoi(parts[0])
|
---|
| 1339 | if err != nil {
|
---|
| 1340 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
---|
| 1341 | }
|
---|
| 1342 | hops++
|
---|
| 1343 |
|
---|
| 1344 | trailing = strconv.Itoa(hops) + " " + realname
|
---|
| 1345 |
|
---|
[682] | 1346 | if channel != "*" {
|
---|
| 1347 | channel = dc.marshalEntity(uc.network, channel)
|
---|
| 1348 | }
|
---|
| 1349 | nick = dc.marshalEntity(uc.network, nick)
|
---|
| 1350 | dc.SendMessage(&irc.Message{
|
---|
| 1351 | Prefix: dc.srv.prefix(),
|
---|
| 1352 | Command: irc.RPL_WHOREPLY,
|
---|
| 1353 | Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
---|
[127] | 1354 | })
|
---|
[682] | 1355 | case rpl_whospcrpl:
|
---|
| 1356 | dc, cmd := uc.currentPendingCommand("WHO")
|
---|
| 1357 | if cmd == nil {
|
---|
| 1358 | return fmt.Errorf("unexpected RPL_WHOSPCRPL: no matching pending WHO")
|
---|
| 1359 | } else if dc == nil {
|
---|
| 1360 | return nil
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 | // Only supported in single-upstream mode, so forward as-is
|
---|
| 1364 | dc.SendMessage(msg)
|
---|
[127] | 1365 | case irc.RPL_ENDOFWHO:
|
---|
| 1366 | var name string
|
---|
| 1367 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1368 | return err
|
---|
| 1369 | }
|
---|
| 1370 |
|
---|
[682] | 1371 | dc, cmd := uc.dequeueCommand("WHO")
|
---|
| 1372 | if cmd == nil {
|
---|
| 1373 | return fmt.Errorf("unexpected RPL_ENDOFWHO: no matching pending WHO")
|
---|
| 1374 | } else if dc == nil {
|
---|
| 1375 | return nil
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 | mask := "*"
|
---|
| 1379 | if len(cmd.Params) > 0 {
|
---|
| 1380 | mask = cmd.Params[0]
|
---|
| 1381 | }
|
---|
| 1382 | dc.SendMessage(&irc.Message{
|
---|
| 1383 | Prefix: dc.srv.prefix(),
|
---|
| 1384 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1385 | Params: []string{dc.nick, mask, "End of /WHO list"},
|
---|
[127] | 1386 | })
|
---|
[128] | 1387 | case irc.RPL_WHOISUSER:
|
---|
| 1388 | var nick, username, host, realname string
|
---|
| 1389 | if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
---|
| 1390 | return err
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
[161] | 1393 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1394 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1395 | dc.SendMessage(&irc.Message{
|
---|
| 1396 | Prefix: dc.srv.prefix(),
|
---|
| 1397 | Command: irc.RPL_WHOISUSER,
|
---|
| 1398 | Params: []string{dc.nick, nick, username, host, "*", realname},
|
---|
| 1399 | })
|
---|
| 1400 | })
|
---|
| 1401 | case irc.RPL_WHOISSERVER:
|
---|
| 1402 | var nick, server, serverInfo string
|
---|
| 1403 | if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
---|
| 1404 | return err
|
---|
| 1405 | }
|
---|
| 1406 |
|
---|
[161] | 1407 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1408 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1409 | dc.SendMessage(&irc.Message{
|
---|
| 1410 | Prefix: dc.srv.prefix(),
|
---|
| 1411 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1412 | Params: []string{dc.nick, nick, server, serverInfo},
|
---|
| 1413 | })
|
---|
| 1414 | })
|
---|
| 1415 | case irc.RPL_WHOISOPERATOR:
|
---|
| 1416 | var nick string
|
---|
| 1417 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1418 | return err
|
---|
| 1419 | }
|
---|
| 1420 |
|
---|
[161] | 1421 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1422 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1423 | dc.SendMessage(&irc.Message{
|
---|
| 1424 | Prefix: dc.srv.prefix(),
|
---|
| 1425 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 1426 | Params: []string{dc.nick, nick, "is an IRC operator"},
|
---|
| 1427 | })
|
---|
| 1428 | })
|
---|
| 1429 | case irc.RPL_WHOISIDLE:
|
---|
| 1430 | var nick string
|
---|
| 1431 | if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
---|
| 1432 | return err
|
---|
| 1433 | }
|
---|
| 1434 |
|
---|
[161] | 1435 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1436 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1437 | params := []string{dc.nick, nick}
|
---|
| 1438 | params = append(params, msg.Params[2:]...)
|
---|
| 1439 | dc.SendMessage(&irc.Message{
|
---|
| 1440 | Prefix: dc.srv.prefix(),
|
---|
| 1441 | Command: irc.RPL_WHOISIDLE,
|
---|
| 1442 | Params: params,
|
---|
| 1443 | })
|
---|
| 1444 | })
|
---|
| 1445 | case irc.RPL_WHOISCHANNELS:
|
---|
| 1446 | var nick, channelList string
|
---|
| 1447 | if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
---|
| 1448 | return err
|
---|
| 1449 | }
|
---|
[174] | 1450 | channels := splitSpace(channelList)
|
---|
[128] | 1451 |
|
---|
[161] | 1452 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1453 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1454 | channelList := make([]string, len(channels))
|
---|
| 1455 | for i, channel := range channels {
|
---|
[139] | 1456 | prefix, channel := uc.parseMembershipPrefix(channel)
|
---|
[260] | 1457 | channel = dc.marshalEntity(uc.network, channel)
|
---|
[292] | 1458 | channelList[i] = prefix.Format(dc) + channel
|
---|
[128] | 1459 | }
|
---|
| 1460 | channels := strings.Join(channelList, " ")
|
---|
| 1461 | dc.SendMessage(&irc.Message{
|
---|
| 1462 | Prefix: dc.srv.prefix(),
|
---|
| 1463 | Command: irc.RPL_WHOISCHANNELS,
|
---|
| 1464 | Params: []string{dc.nick, nick, channels},
|
---|
| 1465 | })
|
---|
| 1466 | })
|
---|
| 1467 | case irc.RPL_ENDOFWHOIS:
|
---|
| 1468 | var nick string
|
---|
| 1469 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1470 | return err
|
---|
| 1471 | }
|
---|
| 1472 |
|
---|
[161] | 1473 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1474 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1475 | dc.SendMessage(&irc.Message{
|
---|
| 1476 | Prefix: dc.srv.prefix(),
|
---|
| 1477 | Command: irc.RPL_ENDOFWHOIS,
|
---|
[142] | 1478 | Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
---|
[128] | 1479 | })
|
---|
| 1480 | })
|
---|
[115] | 1481 | case "INVITE":
|
---|
[273] | 1482 | var nick, channel string
|
---|
[115] | 1483 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
| 1484 | return err
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
[478] | 1487 | weAreInvited := uc.isOurNick(nick)
|
---|
[448] | 1488 |
|
---|
[115] | 1489 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[448] | 1490 | if !weAreInvited && !dc.caps["invite-notify"] {
|
---|
| 1491 | return
|
---|
| 1492 | }
|
---|
[115] | 1493 | dc.SendMessage(&irc.Message{
|
---|
[260] | 1494 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
[115] | 1495 | Command: "INVITE",
|
---|
[260] | 1496 | Params: []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[115] | 1497 | })
|
---|
| 1498 | })
|
---|
[163] | 1499 | case irc.RPL_INVITING:
|
---|
[273] | 1500 | var nick, channel string
|
---|
[304] | 1501 | if err := parseMessageParams(msg, nil, &nick, &channel); err != nil {
|
---|
[163] | 1502 | return err
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
| 1505 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1506 | dc.SendMessage(&irc.Message{
|
---|
| 1507 | Prefix: dc.srv.prefix(),
|
---|
| 1508 | Command: irc.RPL_INVITING,
|
---|
[260] | 1509 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[163] | 1510 | })
|
---|
| 1511 | })
|
---|
[684] | 1512 | case irc.RPL_MONONLINE, irc.RPL_MONOFFLINE:
|
---|
| 1513 | var targetsStr string
|
---|
| 1514 | if err := parseMessageParams(msg, nil, &targetsStr); err != nil {
|
---|
| 1515 | return err
|
---|
| 1516 | }
|
---|
| 1517 | targets := strings.Split(targetsStr, ",")
|
---|
| 1518 |
|
---|
| 1519 | online := msg.Command == irc.RPL_MONONLINE
|
---|
| 1520 | for _, target := range targets {
|
---|
| 1521 | prefix := irc.ParsePrefix(target)
|
---|
| 1522 | uc.monitored.SetValue(prefix.Name, online)
|
---|
| 1523 | }
|
---|
| 1524 |
|
---|
| 1525 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1526 | for _, target := range targets {
|
---|
| 1527 | prefix := irc.ParsePrefix(target)
|
---|
| 1528 | if dc.monitored.Has(prefix.Name) {
|
---|
| 1529 | dc.SendMessage(&irc.Message{
|
---|
| 1530 | Prefix: dc.srv.prefix(),
|
---|
| 1531 | Command: msg.Command,
|
---|
| 1532 | Params: []string{dc.nick, target},
|
---|
| 1533 | })
|
---|
| 1534 | }
|
---|
| 1535 | }
|
---|
| 1536 | })
|
---|
| 1537 | case irc.ERR_MONLISTFULL:
|
---|
| 1538 | var limit, targetsStr string
|
---|
| 1539 | if err := parseMessageParams(msg, nil, &limit, &targetsStr); err != nil {
|
---|
| 1540 | return err
|
---|
| 1541 | }
|
---|
| 1542 |
|
---|
| 1543 | targets := strings.Split(targetsStr, ",")
|
---|
| 1544 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1545 | for _, target := range targets {
|
---|
| 1546 | if dc.monitored.Has(target) {
|
---|
| 1547 | dc.SendMessage(&irc.Message{
|
---|
| 1548 | Prefix: dc.srv.prefix(),
|
---|
| 1549 | Command: msg.Command,
|
---|
| 1550 | Params: []string{dc.nick, limit, target},
|
---|
| 1551 | })
|
---|
| 1552 | }
|
---|
| 1553 | }
|
---|
| 1554 | })
|
---|
[272] | 1555 | case irc.RPL_AWAY:
|
---|
| 1556 | var nick, reason string
|
---|
| 1557 | if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
|
---|
| 1558 | return err
|
---|
| 1559 | }
|
---|
| 1560 |
|
---|
[274] | 1561 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[272] | 1562 | dc.SendMessage(&irc.Message{
|
---|
| 1563 | Prefix: dc.srv.prefix(),
|
---|
| 1564 | Command: irc.RPL_AWAY,
|
---|
| 1565 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), reason},
|
---|
| 1566 | })
|
---|
| 1567 | })
|
---|
[649] | 1568 | case "AWAY", "ACCOUNT":
|
---|
[276] | 1569 | if msg.Prefix == nil {
|
---|
| 1570 | return fmt.Errorf("expected a prefix")
|
---|
| 1571 | }
|
---|
| 1572 |
|
---|
| 1573 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1574 | dc.SendMessage(&irc.Message{
|
---|
| 1575 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
[648] | 1576 | Command: msg.Command,
|
---|
| 1577 | Params: msg.Params,
|
---|
| 1578 | })
|
---|
| 1579 | })
|
---|
[300] | 1580 | case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST:
|
---|
| 1581 | var channel, mask string
|
---|
| 1582 | if err := parseMessageParams(msg, nil, &channel, &mask); err != nil {
|
---|
| 1583 | return err
|
---|
| 1584 | }
|
---|
| 1585 | var addNick, addTime string
|
---|
| 1586 | if len(msg.Params) >= 5 {
|
---|
| 1587 | addNick = msg.Params[3]
|
---|
| 1588 | addTime = msg.Params[4]
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
| 1591 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1592 | channel := dc.marshalEntity(uc.network, channel)
|
---|
| 1593 |
|
---|
| 1594 | var params []string
|
---|
| 1595 | if addNick != "" && addTime != "" {
|
---|
| 1596 | addNick := dc.marshalEntity(uc.network, addNick)
|
---|
| 1597 | params = []string{dc.nick, channel, mask, addNick, addTime}
|
---|
| 1598 | } else {
|
---|
| 1599 | params = []string{dc.nick, channel, mask}
|
---|
| 1600 | }
|
---|
| 1601 |
|
---|
| 1602 | dc.SendMessage(&irc.Message{
|
---|
| 1603 | Prefix: dc.srv.prefix(),
|
---|
| 1604 | Command: msg.Command,
|
---|
| 1605 | Params: params,
|
---|
| 1606 | })
|
---|
| 1607 | })
|
---|
| 1608 | case irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
|
---|
| 1609 | var channel, trailing string
|
---|
| 1610 | if err := parseMessageParams(msg, nil, &channel, &trailing); err != nil {
|
---|
| 1611 | return err
|
---|
| 1612 | }
|
---|
| 1613 |
|
---|
| 1614 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1615 | upstreamChannel := dc.marshalEntity(uc.network, channel)
|
---|
| 1616 | dc.SendMessage(&irc.Message{
|
---|
| 1617 | Prefix: dc.srv.prefix(),
|
---|
| 1618 | Command: msg.Command,
|
---|
| 1619 | Params: []string{dc.nick, upstreamChannel, trailing},
|
---|
| 1620 | })
|
---|
| 1621 | })
|
---|
[302] | 1622 | case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
---|
| 1623 | var command, reason string
|
---|
| 1624 | if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
---|
| 1625 | return err
|
---|
| 1626 | }
|
---|
| 1627 |
|
---|
[729] | 1628 | if dc, _ := uc.dequeueCommand(command); dc != nil && downstreamID == 0 {
|
---|
| 1629 | downstreamID = dc.id
|
---|
[302] | 1630 | }
|
---|
| 1631 |
|
---|
[355] | 1632 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1633 | dc.SendMessage(&irc.Message{
|
---|
| 1634 | Prefix: uc.srv.prefix(),
|
---|
| 1635 | Command: msg.Command,
|
---|
| 1636 | Params: []string{dc.nick, command, reason},
|
---|
[302] | 1637 | })
|
---|
[355] | 1638 | })
|
---|
[729] | 1639 | case "FAIL":
|
---|
[737] | 1640 | var command, code string
|
---|
| 1641 | if err := parseMessageParams(msg, &command, &code); err != nil {
|
---|
[729] | 1642 | return err
|
---|
| 1643 | }
|
---|
| 1644 |
|
---|
[737] | 1645 | if !uc.registered && command == "*" && code == "ACCOUNT_REQUIRED" {
|
---|
| 1646 | return registrationError{msg}
|
---|
| 1647 | }
|
---|
| 1648 |
|
---|
[729] | 1649 | if dc, _ := uc.dequeueCommand(command); dc != nil && downstreamID == 0 {
|
---|
| 1650 | downstreamID = dc.id
|
---|
| 1651 | }
|
---|
| 1652 |
|
---|
| 1653 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1654 | dc.SendMessage(msg)
|
---|
| 1655 | })
|
---|
[155] | 1656 | case "ACK":
|
---|
| 1657 | // Ignore
|
---|
[198] | 1658 | case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
|
---|
| 1659 | // Ignore
|
---|
[16] | 1660 | case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
---|
[14] | 1661 | // Ignore
|
---|
| 1662 | case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
---|
[561] | 1663 | fallthrough
|
---|
| 1664 | case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
---|
| 1665 | fallthrough
|
---|
| 1666 | case rpl_localusers, rpl_globalusers:
|
---|
| 1667 | fallthrough
|
---|
[478] | 1668 | case irc.RPL_MOTDSTART, irc.RPL_MOTD:
|
---|
[561] | 1669 | // Ignore these messages if they're part of the initial registration
|
---|
| 1670 | // message burst. Forward them if the user explicitly asked for them.
|
---|
[552] | 1671 | if !uc.gotMotd {
|
---|
| 1672 | return nil
|
---|
| 1673 | }
|
---|
| 1674 |
|
---|
[553] | 1675 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1676 | dc.SendMessage(&irc.Message{
|
---|
| 1677 | Prefix: uc.srv.prefix(),
|
---|
[552] | 1678 | Command: msg.Command,
|
---|
[553] | 1679 | Params: msg.Params,
|
---|
[552] | 1680 | })
|
---|
| 1681 | })
|
---|
[177] | 1682 | case irc.RPL_LISTSTART:
|
---|
| 1683 | // Ignore
|
---|
[390] | 1684 | case "ERROR":
|
---|
| 1685 | var text string
|
---|
| 1686 | if err := parseMessageParams(msg, &text); err != nil {
|
---|
| 1687 | return err
|
---|
| 1688 | }
|
---|
| 1689 | return fmt.Errorf("fatal server error: %v", text)
|
---|
[389] | 1690 | case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
|
---|
[342] | 1691 | if !uc.registered {
|
---|
[736] | 1692 | return registrationError{msg}
|
---|
[342] | 1693 | }
|
---|
| 1694 | fallthrough
|
---|
[13] | 1695 | default:
|
---|
[95] | 1696 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
[355] | 1697 |
|
---|
| 1698 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1699 | // best effort marshaling for unknown messages, replies and errors:
|
---|
| 1700 | // most numerics start with the user nick, marshal it if that's the case
|
---|
| 1701 | // otherwise, conservately keep the params without marshaling
|
---|
| 1702 | params := msg.Params
|
---|
| 1703 | if _, err := strconv.Atoi(msg.Command); err == nil { // numeric
|
---|
| 1704 | if len(msg.Params) > 0 && isOurNick(uc.network, msg.Params[0]) {
|
---|
| 1705 | params[0] = dc.nick
|
---|
[302] | 1706 | }
|
---|
[355] | 1707 | }
|
---|
| 1708 | dc.SendMessage(&irc.Message{
|
---|
| 1709 | Prefix: uc.srv.prefix(),
|
---|
| 1710 | Command: msg.Command,
|
---|
| 1711 | Params: params,
|
---|
[302] | 1712 | })
|
---|
[355] | 1713 | })
|
---|
[13] | 1714 | }
|
---|
[14] | 1715 | return nil
|
---|
[13] | 1716 | }
|
---|
| 1717 |
|
---|
[739] | 1718 | func (uc *upstreamConn) handleDetachedMessage(ctx context.Context, ch *Channel, msg *irc.Message) {
|
---|
[499] | 1719 | if uc.network.detachedMessageNeedsRelay(ch, msg) {
|
---|
[435] | 1720 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[499] | 1721 | dc.relayDetachedMessage(uc.network, msg)
|
---|
[435] | 1722 | })
|
---|
| 1723 | }
|
---|
[499] | 1724 | if ch.ReattachOn == FilterMessage || (ch.ReattachOn == FilterHighlight && uc.network.isHighlight(msg)) {
|
---|
[435] | 1725 | uc.network.attach(ch)
|
---|
[739] | 1726 | if err := uc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
---|
[435] | 1727 | uc.logger.Printf("failed to update channel %q: %v", ch.Name, err)
|
---|
| 1728 | }
|
---|
| 1729 | }
|
---|
| 1730 | }
|
---|
| 1731 |
|
---|
[458] | 1732 | func (uc *upstreamConn) handleChanModes(s string) error {
|
---|
| 1733 | parts := strings.SplitN(s, ",", 5)
|
---|
| 1734 | if len(parts) < 4 {
|
---|
| 1735 | return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", s)
|
---|
| 1736 | }
|
---|
| 1737 | modes := make(map[byte]channelModeType)
|
---|
| 1738 | for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
---|
| 1739 | for j := 0; j < len(parts[i]); j++ {
|
---|
| 1740 | mode := parts[i][j]
|
---|
| 1741 | modes[mode] = mt
|
---|
| 1742 | }
|
---|
| 1743 | }
|
---|
| 1744 | uc.availableChannelModes = modes
|
---|
| 1745 | return nil
|
---|
| 1746 | }
|
---|
| 1747 |
|
---|
| 1748 | func (uc *upstreamConn) handleMemberships(s string) error {
|
---|
| 1749 | if s == "" {
|
---|
| 1750 | uc.availableMemberships = nil
|
---|
| 1751 | return nil
|
---|
| 1752 | }
|
---|
| 1753 |
|
---|
| 1754 | if s[0] != '(' {
|
---|
| 1755 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
| 1756 | }
|
---|
| 1757 | sep := strings.IndexByte(s, ')')
|
---|
| 1758 | if sep < 0 || len(s) != sep*2 {
|
---|
| 1759 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
| 1760 | }
|
---|
| 1761 | memberships := make([]membership, len(s)/2-1)
|
---|
| 1762 | for i := range memberships {
|
---|
| 1763 | memberships[i] = membership{
|
---|
| 1764 | Mode: s[i+1],
|
---|
| 1765 | Prefix: s[sep+i+1],
|
---|
| 1766 | }
|
---|
| 1767 | }
|
---|
| 1768 | uc.availableMemberships = memberships
|
---|
| 1769 | return nil
|
---|
| 1770 | }
|
---|
| 1771 |
|
---|
[281] | 1772 | func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
|
---|
| 1773 | caps := strings.Fields(capsStr)
|
---|
| 1774 | for _, s := range caps {
|
---|
| 1775 | kv := strings.SplitN(s, "=", 2)
|
---|
| 1776 | k := strings.ToLower(kv[0])
|
---|
| 1777 | var v string
|
---|
| 1778 | if len(kv) == 2 {
|
---|
| 1779 | v = kv[1]
|
---|
| 1780 | }
|
---|
| 1781 | uc.supportedCaps[k] = v
|
---|
| 1782 | }
|
---|
| 1783 | }
|
---|
| 1784 |
|
---|
| 1785 | func (uc *upstreamConn) requestCaps() {
|
---|
| 1786 | var requestCaps []string
|
---|
[282] | 1787 | for c := range permanentUpstreamCaps {
|
---|
[281] | 1788 | if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
|
---|
| 1789 | requestCaps = append(requestCaps, c)
|
---|
| 1790 | }
|
---|
| 1791 | }
|
---|
| 1792 |
|
---|
[282] | 1793 | if len(requestCaps) == 0 {
|
---|
| 1794 | return
|
---|
| 1795 | }
|
---|
| 1796 |
|
---|
| 1797 | uc.SendMessage(&irc.Message{
|
---|
| 1798 | Command: "CAP",
|
---|
| 1799 | Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
---|
| 1800 | })
|
---|
| 1801 | }
|
---|
| 1802 |
|
---|
[725] | 1803 | func (uc *upstreamConn) supportsSASL(mech string) bool {
|
---|
[282] | 1804 | v, ok := uc.supportedCaps["sasl"]
|
---|
| 1805 | if !ok {
|
---|
| 1806 | return false
|
---|
| 1807 | }
|
---|
[725] | 1808 |
|
---|
| 1809 | if v == "" {
|
---|
| 1810 | return true
|
---|
| 1811 | }
|
---|
| 1812 |
|
---|
| 1813 | mechanisms := strings.Split(v, ",")
|
---|
| 1814 | for _, mech := range mechanisms {
|
---|
| 1815 | if strings.EqualFold(mech, mech) {
|
---|
| 1816 | return true
|
---|
[282] | 1817 | }
|
---|
| 1818 | }
|
---|
[725] | 1819 | return false
|
---|
| 1820 | }
|
---|
[282] | 1821 |
|
---|
[725] | 1822 | func (uc *upstreamConn) requestSASL() bool {
|
---|
| 1823 | if uc.network.SASL.Mechanism == "" {
|
---|
| 1824 | return false
|
---|
| 1825 | }
|
---|
| 1826 | return uc.supportsSASL(uc.network.SASL.Mechanism)
|
---|
[282] | 1827 | }
|
---|
| 1828 |
|
---|
| 1829 | func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
---|
| 1830 | uc.caps[name] = ok
|
---|
| 1831 |
|
---|
| 1832 | switch name {
|
---|
| 1833 | case "sasl":
|
---|
[724] | 1834 | if !uc.requestSASL() {
|
---|
| 1835 | return nil
|
---|
| 1836 | }
|
---|
[282] | 1837 | if !ok {
|
---|
| 1838 | uc.logger.Printf("server refused to acknowledge the SASL capability")
|
---|
| 1839 | return nil
|
---|
| 1840 | }
|
---|
| 1841 |
|
---|
| 1842 | auth := &uc.network.SASL
|
---|
| 1843 | switch auth.Mechanism {
|
---|
| 1844 | case "PLAIN":
|
---|
| 1845 | uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
---|
| 1846 | uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
---|
[307] | 1847 | case "EXTERNAL":
|
---|
| 1848 | uc.logger.Printf("starting SASL EXTERNAL authentication")
|
---|
| 1849 | uc.saslClient = sasl.NewExternalClient("")
|
---|
[282] | 1850 | default:
|
---|
| 1851 | return fmt.Errorf("unsupported SASL mechanism %q", name)
|
---|
| 1852 | }
|
---|
| 1853 |
|
---|
[281] | 1854 | uc.SendMessage(&irc.Message{
|
---|
[282] | 1855 | Command: "AUTHENTICATE",
|
---|
| 1856 | Params: []string{auth.Mechanism},
|
---|
[281] | 1857 | })
|
---|
[282] | 1858 | default:
|
---|
| 1859 | if permanentUpstreamCaps[name] {
|
---|
| 1860 | break
|
---|
| 1861 | }
|
---|
| 1862 | uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
|
---|
[281] | 1863 | }
|
---|
[282] | 1864 | return nil
|
---|
[281] | 1865 | }
|
---|
| 1866 |
|
---|
[174] | 1867 | func splitSpace(s string) []string {
|
---|
| 1868 | return strings.FieldsFunc(s, func(r rune) bool {
|
---|
| 1869 | return r == ' '
|
---|
| 1870 | })
|
---|
| 1871 | }
|
---|
| 1872 |
|
---|
[55] | 1873 | func (uc *upstreamConn) register() {
|
---|
[664] | 1874 | uc.nick = GetNick(&uc.user.User, &uc.network.Network)
|
---|
[478] | 1875 | uc.nickCM = uc.network.casemap(uc.nick)
|
---|
[674] | 1876 | uc.username = GetUsername(&uc.user.User, &uc.network.Network)
|
---|
[568] | 1877 | uc.realname = GetRealname(&uc.user.User, &uc.network.Network)
|
---|
[77] | 1878 |
|
---|
[60] | 1879 | uc.SendMessage(&irc.Message{
|
---|
[92] | 1880 | Command: "CAP",
|
---|
| 1881 | Params: []string{"LS", "302"},
|
---|
| 1882 | })
|
---|
| 1883 |
|
---|
[93] | 1884 | if uc.network.Pass != "" {
|
---|
| 1885 | uc.SendMessage(&irc.Message{
|
---|
| 1886 | Command: "PASS",
|
---|
| 1887 | Params: []string{uc.network.Pass},
|
---|
| 1888 | })
|
---|
| 1889 | }
|
---|
| 1890 |
|
---|
[92] | 1891 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1892 | Command: "NICK",
|
---|
[69] | 1893 | Params: []string{uc.nick},
|
---|
[60] | 1894 | })
|
---|
| 1895 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1896 | Command: "USER",
|
---|
[77] | 1897 | Params: []string{uc.username, "0", "*", uc.realname},
|
---|
[60] | 1898 | })
|
---|
[44] | 1899 | }
|
---|
[13] | 1900 |
|
---|
[711] | 1901 | func (uc *upstreamConn) ReadMessage() (*irc.Message, error) {
|
---|
| 1902 | msg, err := uc.conn.ReadMessage()
|
---|
| 1903 | if err != nil {
|
---|
| 1904 | return nil, err
|
---|
| 1905 | }
|
---|
| 1906 | uc.srv.metrics.upstreamInMessagesTotal.Inc()
|
---|
| 1907 | return msg, nil
|
---|
| 1908 | }
|
---|
| 1909 |
|
---|
[197] | 1910 | func (uc *upstreamConn) runUntilRegistered() error {
|
---|
| 1911 | for !uc.registered {
|
---|
[212] | 1912 | msg, err := uc.ReadMessage()
|
---|
[197] | 1913 | if err != nil {
|
---|
| 1914 | return fmt.Errorf("failed to read message: %v", err)
|
---|
| 1915 | }
|
---|
| 1916 |
|
---|
[739] | 1917 | if err := uc.handleMessage(context.TODO(), msg); err != nil {
|
---|
[399] | 1918 | if _, ok := err.(registrationError); ok {
|
---|
| 1919 | return err
|
---|
| 1920 | } else {
|
---|
| 1921 | msg.Tags = nil // prevent message tags from cluttering logs
|
---|
| 1922 | return fmt.Errorf("failed to handle message %q: %v", msg, err)
|
---|
| 1923 | }
|
---|
[197] | 1924 | }
|
---|
| 1925 | }
|
---|
| 1926 |
|
---|
[263] | 1927 | for _, command := range uc.network.ConnectCommands {
|
---|
| 1928 | m, err := irc.ParseMessage(command)
|
---|
| 1929 | if err != nil {
|
---|
| 1930 | uc.logger.Printf("failed to parse connect command %q: %v", command, err)
|
---|
| 1931 | } else {
|
---|
| 1932 | uc.SendMessage(m)
|
---|
| 1933 | }
|
---|
| 1934 | }
|
---|
| 1935 |
|
---|
[197] | 1936 | return nil
|
---|
| 1937 | }
|
---|
| 1938 |
|
---|
[165] | 1939 | func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
---|
[13] | 1940 | for {
|
---|
[210] | 1941 | msg, err := uc.ReadMessage()
|
---|
[655] | 1942 | if errors.Is(err, io.EOF) {
|
---|
[13] | 1943 | break
|
---|
| 1944 | } else if err != nil {
|
---|
| 1945 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1946 | }
|
---|
| 1947 |
|
---|
[165] | 1948 | ch <- eventUpstreamMessage{msg, uc}
|
---|
[13] | 1949 | }
|
---|
| 1950 |
|
---|
[45] | 1951 | return nil
|
---|
[13] | 1952 | }
|
---|
[60] | 1953 |
|
---|
[303] | 1954 | func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
---|
| 1955 | if !uc.caps["message-tags"] {
|
---|
| 1956 | msg = msg.Copy()
|
---|
| 1957 | msg.Tags = nil
|
---|
| 1958 | }
|
---|
| 1959 |
|
---|
[711] | 1960 | uc.srv.metrics.upstreamOutMessagesTotal.Inc()
|
---|
[303] | 1961 | uc.conn.SendMessage(msg)
|
---|
| 1962 | }
|
---|
| 1963 |
|
---|
[176] | 1964 | func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
---|
[278] | 1965 | if uc.caps["labeled-response"] {
|
---|
[155] | 1966 | if msg.Tags == nil {
|
---|
| 1967 | msg.Tags = make(map[string]irc.TagValue)
|
---|
| 1968 | }
|
---|
[176] | 1969 | msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
---|
[161] | 1970 | uc.nextLabelID++
|
---|
[155] | 1971 | }
|
---|
| 1972 | uc.SendMessage(msg)
|
---|
| 1973 | }
|
---|
[178] | 1974 |
|
---|
[428] | 1975 | // appendLog appends a message to the log file.
|
---|
| 1976 | //
|
---|
| 1977 | // The internal message ID is returned. If the message isn't recorded in the
|
---|
| 1978 | // log file, an empty string is returned.
|
---|
| 1979 | func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string) {
|
---|
[423] | 1980 | if uc.user.msgStore == nil {
|
---|
[428] | 1981 | return ""
|
---|
[178] | 1982 | }
|
---|
[486] | 1983 |
|
---|
[563] | 1984 | // Don't store messages with a server mask target
|
---|
| 1985 | if strings.HasPrefix(entity, "$") {
|
---|
| 1986 | return ""
|
---|
| 1987 | }
|
---|
| 1988 |
|
---|
[486] | 1989 | entityCM := uc.network.casemap(entity)
|
---|
| 1990 | if entityCM == "nickserv" {
|
---|
[468] | 1991 | // The messages sent/received from NickServ may contain
|
---|
| 1992 | // security-related information (like passwords). Don't store these.
|
---|
| 1993 | return ""
|
---|
| 1994 | }
|
---|
[215] | 1995 |
|
---|
[485] | 1996 | if !uc.network.delivered.HasTarget(entity) {
|
---|
[482] | 1997 | // This is the first message we receive from this target. Save the last
|
---|
| 1998 | // message ID in delivery receipts, so that we can send the new message
|
---|
| 1999 | // in the backlog if an offline client reconnects.
|
---|
[666] | 2000 | lastID, err := uc.user.msgStore.LastMsgID(&uc.network.Network, entityCM, time.Now())
|
---|
[409] | 2001 | if err != nil {
|
---|
| 2002 | uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
|
---|
[428] | 2003 | return ""
|
---|
[409] | 2004 | }
|
---|
| 2005 |
|
---|
[489] | 2006 | uc.network.delivered.ForEachClient(func(clientName string) {
|
---|
[485] | 2007 | uc.network.delivered.StoreID(entity, clientName, lastID)
|
---|
[489] | 2008 | })
|
---|
[253] | 2009 | }
|
---|
| 2010 |
|
---|
[666] | 2011 | msgID, err := uc.user.msgStore.Append(&uc.network.Network, entityCM, msg)
|
---|
[409] | 2012 | if err != nil {
|
---|
| 2013 | uc.logger.Printf("failed to log message: %v", err)
|
---|
[428] | 2014 | return ""
|
---|
[409] | 2015 | }
|
---|
[406] | 2016 |
|
---|
[428] | 2017 | return msgID
|
---|
[253] | 2018 | }
|
---|
| 2019 |
|
---|
[409] | 2020 | // produce appends a message to the logs and forwards it to connected downstream
|
---|
| 2021 | // connections.
|
---|
[245] | 2022 | //
|
---|
| 2023 | // If origin is not nil and origin doesn't support echo-message, the message is
|
---|
| 2024 | // forwarded to all connections except origin.
|
---|
[239] | 2025 | func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
|
---|
[428] | 2026 | var msgID string
|
---|
[239] | 2027 | if target != "" {
|
---|
[428] | 2028 | msgID = uc.appendLog(target, msg)
|
---|
[239] | 2029 | }
|
---|
| 2030 |
|
---|
[284] | 2031 | // Don't forward messages if it's a detached channel
|
---|
[478] | 2032 | ch := uc.network.channels.Value(target)
|
---|
[499] | 2033 | detached := ch != nil && ch.Detached
|
---|
[284] | 2034 |
|
---|
[227] | 2035 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[499] | 2036 | if !detached && (dc != origin || dc.caps["echo-message"]) {
|
---|
[428] | 2037 | dc.sendMessageWithID(dc.marshalMessage(msg, uc.network), msgID)
|
---|
| 2038 | } else {
|
---|
| 2039 | dc.advanceMessageWithID(msg, msgID)
|
---|
[238] | 2040 | }
|
---|
[227] | 2041 | })
|
---|
[226] | 2042 | }
|
---|
| 2043 |
|
---|
[198] | 2044 | func (uc *upstreamConn) updateAway() {
|
---|
| 2045 | away := true
|
---|
| 2046 | uc.forEachDownstream(func(*downstreamConn) {
|
---|
| 2047 | away = false
|
---|
| 2048 | })
|
---|
| 2049 | if away == uc.away {
|
---|
| 2050 | return
|
---|
| 2051 | }
|
---|
| 2052 | if away {
|
---|
| 2053 | uc.SendMessage(&irc.Message{
|
---|
| 2054 | Command: "AWAY",
|
---|
| 2055 | Params: []string{"Auto away"},
|
---|
| 2056 | })
|
---|
| 2057 | } else {
|
---|
| 2058 | uc.SendMessage(&irc.Message{
|
---|
| 2059 | Command: "AWAY",
|
---|
| 2060 | })
|
---|
| 2061 | }
|
---|
| 2062 | uc.away = away
|
---|
| 2063 | }
|
---|
[435] | 2064 |
|
---|
| 2065 | func (uc *upstreamConn) updateChannelAutoDetach(name string) {
|
---|
[478] | 2066 | uch := uc.channels.Value(name)
|
---|
| 2067 | if uch == nil {
|
---|
| 2068 | return
|
---|
[435] | 2069 | }
|
---|
[478] | 2070 | ch := uc.network.channels.Value(name)
|
---|
| 2071 | if ch == nil || ch.Detached {
|
---|
| 2072 | return
|
---|
| 2073 | }
|
---|
| 2074 | uch.updateAutoDetach(ch.DetachAfter)
|
---|
[435] | 2075 | }
|
---|
[684] | 2076 |
|
---|
| 2077 | func (uc *upstreamConn) updateMonitor() {
|
---|
| 2078 | add := make(map[string]struct{})
|
---|
| 2079 | var addList []string
|
---|
| 2080 | seen := make(map[string]struct{})
|
---|
| 2081 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 2082 | for targetCM := range dc.monitored.innerMap {
|
---|
| 2083 | if !uc.monitored.Has(targetCM) {
|
---|
| 2084 | if _, ok := add[targetCM]; !ok {
|
---|
| 2085 | addList = append(addList, targetCM)
|
---|
| 2086 | }
|
---|
| 2087 | add[targetCM] = struct{}{}
|
---|
| 2088 | } else {
|
---|
| 2089 | seen[targetCM] = struct{}{}
|
---|
| 2090 | }
|
---|
| 2091 | }
|
---|
| 2092 | })
|
---|
| 2093 |
|
---|
| 2094 | removeAll := true
|
---|
| 2095 | var removeList []string
|
---|
| 2096 | for targetCM, entry := range uc.monitored.innerMap {
|
---|
| 2097 | if _, ok := seen[targetCM]; ok {
|
---|
| 2098 | removeAll = false
|
---|
| 2099 | } else {
|
---|
| 2100 | removeList = append(removeList, entry.originalKey)
|
---|
| 2101 | }
|
---|
| 2102 | }
|
---|
| 2103 |
|
---|
| 2104 | // TODO: better handle the case where len(uc.monitored) + len(addList)
|
---|
| 2105 | // exceeds the limit, probably by immediately sending ERR_MONLISTFULL?
|
---|
| 2106 |
|
---|
| 2107 | if removeAll && len(addList) == 0 && len(removeList) > 0 {
|
---|
| 2108 | // Optimization when the last MONITOR-aware downstream disconnects
|
---|
| 2109 | uc.SendMessage(&irc.Message{
|
---|
| 2110 | Command: "MONITOR",
|
---|
| 2111 | Params: []string{"C"},
|
---|
| 2112 | })
|
---|
| 2113 | } else {
|
---|
| 2114 | msgs := generateMonitor("-", removeList)
|
---|
| 2115 | msgs = append(msgs, generateMonitor("+", addList)...)
|
---|
| 2116 | for _, msg := range msgs {
|
---|
| 2117 | uc.SendMessage(msg)
|
---|
| 2118 | }
|
---|
| 2119 | }
|
---|
| 2120 |
|
---|
| 2121 | for _, target := range removeList {
|
---|
| 2122 | uc.monitored.Delete(target)
|
---|
| 2123 | }
|
---|
| 2124 | }
|
---|