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