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