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