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