[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:]
|
---|
| 622 | } else {
|
---|
| 623 | if i := strings.IndexByte(token, '='); i >= 0 {
|
---|
| 624 | parameter = token[:i]
|
---|
| 625 | value = token[i+1:]
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | if !negate {
|
---|
| 629 | switch parameter {
|
---|
| 630 | case "CHANMODES":
|
---|
[458] | 631 | if err := uc.handleChanModes(value); err != nil {
|
---|
| 632 | return err
|
---|
[139] | 633 | }
|
---|
| 634 | case "CHANTYPES":
|
---|
| 635 | uc.availableChannelTypes = value
|
---|
| 636 | case "PREFIX":
|
---|
[458] | 637 | if err := uc.handleMemberships(value); err != nil {
|
---|
| 638 | return err
|
---|
[139] | 639 | }
|
---|
[447] | 640 | case "NETWORK":
|
---|
| 641 | uc.networkName = value
|
---|
[139] | 642 | }
|
---|
| 643 | } else {
|
---|
| 644 | // TODO: handle ISUPPORT negations
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
[153] | 647 | case "BATCH":
|
---|
| 648 | var tag string
|
---|
| 649 | if err := parseMessageParams(msg, &tag); err != nil {
|
---|
| 650 | return err
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | if strings.HasPrefix(tag, "+") {
|
---|
| 654 | tag = tag[1:]
|
---|
| 655 | if _, ok := uc.batches[tag]; ok {
|
---|
| 656 | return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
---|
| 657 | }
|
---|
| 658 | var batchType string
|
---|
| 659 | if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
---|
| 660 | return err
|
---|
| 661 | }
|
---|
[155] | 662 | label := label
|
---|
| 663 | if label == "" && msgBatch != nil {
|
---|
| 664 | label = msgBatch.Label
|
---|
| 665 | }
|
---|
[153] | 666 | uc.batches[tag] = batch{
|
---|
| 667 | Type: batchType,
|
---|
| 668 | Params: msg.Params[2:],
|
---|
| 669 | Outer: msgBatch,
|
---|
[155] | 670 | Label: label,
|
---|
[153] | 671 | }
|
---|
| 672 | } else if strings.HasPrefix(tag, "-") {
|
---|
| 673 | tag = tag[1:]
|
---|
| 674 | if _, ok := uc.batches[tag]; !ok {
|
---|
| 675 | return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
---|
| 676 | }
|
---|
| 677 | delete(uc.batches, tag)
|
---|
| 678 | } else {
|
---|
| 679 | return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
---|
| 680 | }
|
---|
[42] | 681 | case "NICK":
|
---|
[83] | 682 | if msg.Prefix == nil {
|
---|
| 683 | return fmt.Errorf("expected a prefix")
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[43] | 686 | var newNick string
|
---|
| 687 | if err := parseMessageParams(msg, &newNick); err != nil {
|
---|
| 688 | return err
|
---|
[42] | 689 | }
|
---|
| 690 |
|
---|
[244] | 691 | me := false
|
---|
[55] | 692 | if msg.Prefix.Name == uc.nick {
|
---|
| 693 | uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
---|
[244] | 694 | me = true
|
---|
[55] | 695 | uc.nick = newNick
|
---|
[42] | 696 | }
|
---|
| 697 |
|
---|
[55] | 698 | for _, ch := range uc.channels {
|
---|
[292] | 699 | if memberships, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
[42] | 700 | delete(ch.Members, msg.Prefix.Name)
|
---|
[292] | 701 | ch.Members[newNick] = memberships
|
---|
[215] | 702 | uc.appendLog(ch.Name, msg)
|
---|
[42] | 703 | }
|
---|
| 704 | }
|
---|
[82] | 705 |
|
---|
[244] | 706 | if !me {
|
---|
[82] | 707 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 708 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[82] | 709 | })
|
---|
[296] | 710 | } else {
|
---|
| 711 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 712 | dc.updateNick()
|
---|
| 713 | })
|
---|
[82] | 714 | }
|
---|
[69] | 715 | case "JOIN":
|
---|
| 716 | if msg.Prefix == nil {
|
---|
| 717 | return fmt.Errorf("expected a prefix")
|
---|
| 718 | }
|
---|
[42] | 719 |
|
---|
[43] | 720 | var channels string
|
---|
| 721 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 722 | return err
|
---|
[19] | 723 | }
|
---|
[34] | 724 |
|
---|
[43] | 725 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 726 | if msg.Prefix.Name == uc.nick {
|
---|
| 727 | uc.logger.Printf("joined channel %q", ch)
|
---|
| 728 | uc.channels[ch] = &upstreamChannel{
|
---|
[34] | 729 | Name: ch,
|
---|
[55] | 730 | conn: uc,
|
---|
[292] | 731 | Members: make(map[string]*memberships),
|
---|
[34] | 732 | }
|
---|
[435] | 733 | uc.updateChannelAutoDetach(ch)
|
---|
[139] | 734 |
|
---|
| 735 | uc.SendMessage(&irc.Message{
|
---|
| 736 | Command: "MODE",
|
---|
| 737 | Params: []string{ch},
|
---|
| 738 | })
|
---|
[34] | 739 | } else {
|
---|
[55] | 740 | ch, err := uc.getChannel(ch)
|
---|
[34] | 741 | if err != nil {
|
---|
| 742 | return err
|
---|
| 743 | }
|
---|
[294] | 744 | ch.Members[msg.Prefix.Name] = &memberships{}
|
---|
[19] | 745 | }
|
---|
[69] | 746 |
|
---|
[245] | 747 | chMsg := msg.Copy()
|
---|
| 748 | chMsg.Params[0] = ch
|
---|
| 749 | uc.produce(ch, chMsg, nil)
|
---|
[19] | 750 | }
|
---|
[69] | 751 | case "PART":
|
---|
| 752 | if msg.Prefix == nil {
|
---|
| 753 | return fmt.Errorf("expected a prefix")
|
---|
| 754 | }
|
---|
[34] | 755 |
|
---|
[43] | 756 | var channels string
|
---|
| 757 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 758 | return err
|
---|
[34] | 759 | }
|
---|
| 760 |
|
---|
[43] | 761 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 762 | if msg.Prefix.Name == uc.nick {
|
---|
| 763 | uc.logger.Printf("parted channel %q", ch)
|
---|
[435] | 764 | if uch, ok := uc.channels[ch]; ok {
|
---|
| 765 | delete(uc.channels, ch)
|
---|
| 766 | uch.updateAutoDetach(0)
|
---|
| 767 | }
|
---|
[34] | 768 | } else {
|
---|
[55] | 769 | ch, err := uc.getChannel(ch)
|
---|
[34] | 770 | if err != nil {
|
---|
| 771 | return err
|
---|
| 772 | }
|
---|
| 773 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 774 | }
|
---|
[69] | 775 |
|
---|
[245] | 776 | chMsg := msg.Copy()
|
---|
| 777 | chMsg.Params[0] = ch
|
---|
| 778 | uc.produce(ch, chMsg, nil)
|
---|
[34] | 779 | }
|
---|
[159] | 780 | case "KICK":
|
---|
| 781 | if msg.Prefix == nil {
|
---|
| 782 | return fmt.Errorf("expected a prefix")
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | var channel, user string
|
---|
| 786 | if err := parseMessageParams(msg, &channel, &user); err != nil {
|
---|
| 787 | return err
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | if user == uc.nick {
|
---|
| 791 | uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
---|
| 792 | delete(uc.channels, channel)
|
---|
| 793 | } else {
|
---|
| 794 | ch, err := uc.getChannel(channel)
|
---|
| 795 | if err != nil {
|
---|
| 796 | return err
|
---|
| 797 | }
|
---|
| 798 | delete(ch.Members, user)
|
---|
| 799 | }
|
---|
| 800 |
|
---|
[245] | 801 | uc.produce(channel, msg, nil)
|
---|
[83] | 802 | case "QUIT":
|
---|
| 803 | if msg.Prefix == nil {
|
---|
| 804 | return fmt.Errorf("expected a prefix")
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | if msg.Prefix.Name == uc.nick {
|
---|
| 808 | uc.logger.Printf("quit")
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 | for _, ch := range uc.channels {
|
---|
[178] | 812 | if _, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
| 813 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 814 |
|
---|
[215] | 815 | uc.appendLog(ch.Name, msg)
|
---|
[178] | 816 | }
|
---|
[83] | 817 | }
|
---|
| 818 |
|
---|
| 819 | if msg.Prefix.Name != uc.nick {
|
---|
| 820 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 821 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[83] | 822 | })
|
---|
| 823 | }
|
---|
[19] | 824 | case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
---|
[43] | 825 | var name, topic string
|
---|
| 826 | if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
---|
| 827 | return err
|
---|
[19] | 828 | }
|
---|
[55] | 829 | ch, err := uc.getChannel(name)
|
---|
[19] | 830 | if err != nil {
|
---|
| 831 | return err
|
---|
| 832 | }
|
---|
| 833 | if msg.Command == irc.RPL_TOPIC {
|
---|
[43] | 834 | ch.Topic = topic
|
---|
[19] | 835 | } else {
|
---|
| 836 | ch.Topic = ""
|
---|
| 837 | }
|
---|
| 838 | case "TOPIC":
|
---|
[405] | 839 | if msg.Prefix == nil {
|
---|
| 840 | return fmt.Errorf("expected a prefix")
|
---|
| 841 | }
|
---|
| 842 |
|
---|
[43] | 843 | var name string
|
---|
[74] | 844 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
[43] | 845 | return err
|
---|
[19] | 846 | }
|
---|
[55] | 847 | ch, err := uc.getChannel(name)
|
---|
[19] | 848 | if err != nil {
|
---|
| 849 | return err
|
---|
| 850 | }
|
---|
| 851 | if len(msg.Params) > 1 {
|
---|
| 852 | ch.Topic = msg.Params[1]
|
---|
[405] | 853 | ch.TopicWho = msg.Prefix.Copy()
|
---|
| 854 | ch.TopicTime = time.Now() // TODO use msg.Tags["time"]
|
---|
[19] | 855 | } else {
|
---|
| 856 | ch.Topic = ""
|
---|
| 857 | }
|
---|
[245] | 858 | uc.produce(ch.Name, msg, nil)
|
---|
[139] | 859 | case "MODE":
|
---|
| 860 | var name, modeStr string
|
---|
| 861 | if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
---|
| 862 | return err
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | if !uc.isChannel(name) { // user mode change
|
---|
| 866 | if name != uc.nick {
|
---|
| 867 | return fmt.Errorf("received MODE message for unknown nick %q", name)
|
---|
| 868 | }
|
---|
| 869 | return uc.modes.Apply(modeStr)
|
---|
| 870 | // TODO: notify downstreams about user mode change?
|
---|
| 871 | } else { // channel mode change
|
---|
| 872 | ch, err := uc.getChannel(name)
|
---|
| 873 | if err != nil {
|
---|
| 874 | return err
|
---|
| 875 | }
|
---|
| 876 |
|
---|
[293] | 877 | needMarshaling, err := applyChannelModes(ch, modeStr, msg.Params[2:])
|
---|
| 878 | if err != nil {
|
---|
| 879 | return err
|
---|
[139] | 880 | }
|
---|
| 881 |
|
---|
[293] | 882 | uc.appendLog(ch.Name, msg)
|
---|
| 883 |
|
---|
[338] | 884 | if ch, ok := uc.network.channels[name]; !ok || !ch.Detached {
|
---|
| 885 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 886 | params := make([]string, len(msg.Params))
|
---|
| 887 | params[0] = dc.marshalEntity(uc.network, name)
|
---|
| 888 | params[1] = modeStr
|
---|
| 889 |
|
---|
| 890 | copy(params[2:], msg.Params[2:])
|
---|
| 891 | for i, modeParam := range params[2:] {
|
---|
| 892 | if _, ok := needMarshaling[i]; ok {
|
---|
| 893 | params[2+i] = dc.marshalEntity(uc.network, modeParam)
|
---|
| 894 | }
|
---|
[293] | 895 | }
|
---|
| 896 |
|
---|
[338] | 897 | dc.SendMessage(&irc.Message{
|
---|
| 898 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
| 899 | Command: "MODE",
|
---|
| 900 | Params: params,
|
---|
| 901 | })
|
---|
[293] | 902 | })
|
---|
[338] | 903 | }
|
---|
[139] | 904 | }
|
---|
| 905 | case irc.RPL_UMODEIS:
|
---|
| 906 | if err := parseMessageParams(msg, nil); err != nil {
|
---|
| 907 | return err
|
---|
| 908 | }
|
---|
| 909 | modeStr := ""
|
---|
| 910 | if len(msg.Params) > 1 {
|
---|
| 911 | modeStr = msg.Params[1]
|
---|
| 912 | }
|
---|
| 913 |
|
---|
| 914 | uc.modes = ""
|
---|
| 915 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
| 916 | return err
|
---|
| 917 | }
|
---|
| 918 | // TODO: send RPL_UMODEIS to downstream connections when applicable
|
---|
| 919 | case irc.RPL_CHANNELMODEIS:
|
---|
| 920 | var channel string
|
---|
| 921 | if err := parseMessageParams(msg, nil, &channel); err != nil {
|
---|
| 922 | return err
|
---|
| 923 | }
|
---|
| 924 | modeStr := ""
|
---|
| 925 | if len(msg.Params) > 2 {
|
---|
| 926 | modeStr = msg.Params[2]
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 | ch, err := uc.getChannel(channel)
|
---|
| 930 | if err != nil {
|
---|
| 931 | return err
|
---|
| 932 | }
|
---|
| 933 |
|
---|
| 934 | firstMode := ch.modes == nil
|
---|
| 935 | ch.modes = make(map[byte]string)
|
---|
[293] | 936 | if _, err := applyChannelModes(ch, modeStr, msg.Params[3:]); err != nil {
|
---|
[139] | 937 | return err
|
---|
| 938 | }
|
---|
| 939 | if firstMode {
|
---|
[338] | 940 | if c, ok := uc.network.channels[channel]; !ok || !c.Detached {
|
---|
| 941 | modeStr, modeParams := ch.modes.Format()
|
---|
[139] | 942 |
|
---|
[338] | 943 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 944 | params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
|
---|
| 945 | params = append(params, modeParams...)
|
---|
[139] | 946 |
|
---|
[338] | 947 | dc.SendMessage(&irc.Message{
|
---|
| 948 | Prefix: dc.srv.prefix(),
|
---|
| 949 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 950 | Params: params,
|
---|
| 951 | })
|
---|
[139] | 952 | })
|
---|
[338] | 953 | }
|
---|
[139] | 954 | }
|
---|
[162] | 955 | case rpl_creationtime:
|
---|
| 956 | var channel, creationTime string
|
---|
| 957 | if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
---|
| 958 | return err
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 | ch, err := uc.getChannel(channel)
|
---|
| 962 | if err != nil {
|
---|
| 963 | return err
|
---|
| 964 | }
|
---|
| 965 |
|
---|
| 966 | firstCreationTime := ch.creationTime == ""
|
---|
| 967 | ch.creationTime = creationTime
|
---|
| 968 | if firstCreationTime {
|
---|
| 969 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 970 | dc.SendMessage(&irc.Message{
|
---|
| 971 | Prefix: dc.srv.prefix(),
|
---|
| 972 | Command: rpl_creationtime,
|
---|
[403] | 973 | Params: []string{dc.nick, dc.marshalEntity(uc.network, ch.Name), creationTime},
|
---|
[162] | 974 | })
|
---|
| 975 | })
|
---|
| 976 | }
|
---|
[19] | 977 | case rpl_topicwhotime:
|
---|
[43] | 978 | var name, who, timeStr string
|
---|
| 979 | if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
---|
| 980 | return err
|
---|
[19] | 981 | }
|
---|
[55] | 982 | ch, err := uc.getChannel(name)
|
---|
[19] | 983 | if err != nil {
|
---|
| 984 | return err
|
---|
| 985 | }
|
---|
[405] | 986 | firstTopicWhoTime := ch.TopicWho == nil
|
---|
| 987 | ch.TopicWho = irc.ParsePrefix(who)
|
---|
[43] | 988 | sec, err := strconv.ParseInt(timeStr, 10, 64)
|
---|
[19] | 989 | if err != nil {
|
---|
| 990 | return fmt.Errorf("failed to parse topic time: %v", err)
|
---|
| 991 | }
|
---|
| 992 | ch.TopicTime = time.Unix(sec, 0)
|
---|
[405] | 993 | if firstTopicWhoTime {
|
---|
| 994 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 995 | topicWho := dc.marshalUserPrefix(uc.network, ch.TopicWho)
|
---|
| 996 | dc.SendMessage(&irc.Message{
|
---|
| 997 | Prefix: dc.srv.prefix(),
|
---|
| 998 | Command: rpl_topicwhotime,
|
---|
| 999 | Params: []string{
|
---|
| 1000 | dc.nick,
|
---|
| 1001 | dc.marshalEntity(uc.network, ch.Name),
|
---|
| 1002 | topicWho.String(),
|
---|
| 1003 | timeStr,
|
---|
| 1004 | },
|
---|
| 1005 | })
|
---|
| 1006 | })
|
---|
| 1007 | }
|
---|
[177] | 1008 | case irc.RPL_LIST:
|
---|
| 1009 | var channel, clients, topic string
|
---|
| 1010 | if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
---|
| 1011 | return err
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
[181] | 1014 | pl := uc.getPendingLIST()
|
---|
[177] | 1015 | if pl == nil {
|
---|
| 1016 | return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
| 1019 | uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
---|
| 1020 | dc.SendMessage(&irc.Message{
|
---|
| 1021 | Prefix: dc.srv.prefix(),
|
---|
| 1022 | Command: irc.RPL_LIST,
|
---|
[260] | 1023 | Params: []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
|
---|
[177] | 1024 | })
|
---|
| 1025 | })
|
---|
| 1026 | case irc.RPL_LISTEND:
|
---|
[181] | 1027 | ok := uc.endPendingLISTs(false)
|
---|
[177] | 1028 | if !ok {
|
---|
| 1029 | return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
---|
| 1030 | }
|
---|
[19] | 1031 | case irc.RPL_NAMREPLY:
|
---|
[43] | 1032 | var name, statusStr, members string
|
---|
| 1033 | if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
---|
| 1034 | return err
|
---|
[19] | 1035 | }
|
---|
[140] | 1036 |
|
---|
| 1037 | ch, ok := uc.channels[name]
|
---|
| 1038 | if !ok {
|
---|
| 1039 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1040 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1041 | channel := dc.marshalEntity(uc.network, name)
|
---|
[174] | 1042 | members := splitSpace(members)
|
---|
[140] | 1043 | for i, member := range members {
|
---|
[292] | 1044 | memberships, nick := uc.parseMembershipPrefix(member)
|
---|
| 1045 | members[i] = memberships.Format(dc) + dc.marshalEntity(uc.network, nick)
|
---|
[140] | 1046 | }
|
---|
| 1047 | memberStr := strings.Join(members, " ")
|
---|
| 1048 |
|
---|
| 1049 | dc.SendMessage(&irc.Message{
|
---|
| 1050 | Prefix: dc.srv.prefix(),
|
---|
| 1051 | Command: irc.RPL_NAMREPLY,
|
---|
| 1052 | Params: []string{dc.nick, statusStr, channel, memberStr},
|
---|
| 1053 | })
|
---|
| 1054 | })
|
---|
| 1055 | return nil
|
---|
[19] | 1056 | }
|
---|
| 1057 |
|
---|
[43] | 1058 | status, err := parseChannelStatus(statusStr)
|
---|
[19] | 1059 | if err != nil {
|
---|
| 1060 | return err
|
---|
| 1061 | }
|
---|
| 1062 | ch.Status = status
|
---|
| 1063 |
|
---|
[174] | 1064 | for _, s := range splitSpace(members) {
|
---|
[292] | 1065 | memberships, nick := uc.parseMembershipPrefix(s)
|
---|
| 1066 | ch.Members[nick] = memberships
|
---|
[19] | 1067 | }
|
---|
| 1068 | case irc.RPL_ENDOFNAMES:
|
---|
[43] | 1069 | var name string
|
---|
| 1070 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1071 | return err
|
---|
[25] | 1072 | }
|
---|
[140] | 1073 |
|
---|
| 1074 | ch, ok := uc.channels[name]
|
---|
| 1075 | if !ok {
|
---|
| 1076 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1077 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1078 | channel := dc.marshalEntity(uc.network, name)
|
---|
[140] | 1079 |
|
---|
| 1080 | dc.SendMessage(&irc.Message{
|
---|
| 1081 | Prefix: dc.srv.prefix(),
|
---|
| 1082 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1083 | Params: []string{dc.nick, channel, "End of /NAMES list"},
|
---|
| 1084 | })
|
---|
| 1085 | })
|
---|
| 1086 | return nil
|
---|
[25] | 1087 | }
|
---|
| 1088 |
|
---|
[34] | 1089 | if ch.complete {
|
---|
| 1090 | return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
---|
| 1091 | }
|
---|
[25] | 1092 | ch.complete = true
|
---|
[27] | 1093 |
|
---|
[338] | 1094 | if c, ok := uc.network.channels[name]; !ok || !c.Detached {
|
---|
| 1095 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1096 | forwardChannel(dc, ch)
|
---|
| 1097 | })
|
---|
| 1098 | }
|
---|
[127] | 1099 | case irc.RPL_WHOREPLY:
|
---|
| 1100 | var channel, username, host, server, nick, mode, trailing string
|
---|
| 1101 | if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
---|
| 1102 | return err
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | parts := strings.SplitN(trailing, " ", 2)
|
---|
| 1106 | if len(parts) != 2 {
|
---|
| 1107 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
---|
| 1108 | }
|
---|
| 1109 | realname := parts[1]
|
---|
| 1110 | hops, err := strconv.Atoi(parts[0])
|
---|
| 1111 | if err != nil {
|
---|
| 1112 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
---|
| 1113 | }
|
---|
| 1114 | hops++
|
---|
| 1115 |
|
---|
| 1116 | trailing = strconv.Itoa(hops) + " " + realname
|
---|
| 1117 |
|
---|
[161] | 1118 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1119 | channel := channel
|
---|
| 1120 | if channel != "*" {
|
---|
[260] | 1121 | channel = dc.marshalEntity(uc.network, channel)
|
---|
[127] | 1122 | }
|
---|
[260] | 1123 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[127] | 1124 | dc.SendMessage(&irc.Message{
|
---|
| 1125 | Prefix: dc.srv.prefix(),
|
---|
| 1126 | Command: irc.RPL_WHOREPLY,
|
---|
| 1127 | Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
---|
| 1128 | })
|
---|
| 1129 | })
|
---|
| 1130 | case irc.RPL_ENDOFWHO:
|
---|
| 1131 | var name string
|
---|
| 1132 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1133 | return err
|
---|
| 1134 | }
|
---|
| 1135 |
|
---|
[161] | 1136 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1137 | name := name
|
---|
| 1138 | if name != "*" {
|
---|
| 1139 | // TODO: support WHO masks
|
---|
[260] | 1140 | name = dc.marshalEntity(uc.network, name)
|
---|
[127] | 1141 | }
|
---|
| 1142 | dc.SendMessage(&irc.Message{
|
---|
| 1143 | Prefix: dc.srv.prefix(),
|
---|
| 1144 | Command: irc.RPL_ENDOFWHO,
|
---|
[142] | 1145 | Params: []string{dc.nick, name, "End of /WHO list"},
|
---|
[127] | 1146 | })
|
---|
| 1147 | })
|
---|
[128] | 1148 | case irc.RPL_WHOISUSER:
|
---|
| 1149 | var nick, username, host, realname string
|
---|
| 1150 | if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
---|
| 1151 | return err
|
---|
| 1152 | }
|
---|
| 1153 |
|
---|
[161] | 1154 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1155 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1156 | dc.SendMessage(&irc.Message{
|
---|
| 1157 | Prefix: dc.srv.prefix(),
|
---|
| 1158 | Command: irc.RPL_WHOISUSER,
|
---|
| 1159 | Params: []string{dc.nick, nick, username, host, "*", realname},
|
---|
| 1160 | })
|
---|
| 1161 | })
|
---|
| 1162 | case irc.RPL_WHOISSERVER:
|
---|
| 1163 | var nick, server, serverInfo string
|
---|
| 1164 | if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
---|
| 1165 | return err
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
[161] | 1168 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1169 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1170 | dc.SendMessage(&irc.Message{
|
---|
| 1171 | Prefix: dc.srv.prefix(),
|
---|
| 1172 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1173 | Params: []string{dc.nick, nick, server, serverInfo},
|
---|
| 1174 | })
|
---|
| 1175 | })
|
---|
| 1176 | case irc.RPL_WHOISOPERATOR:
|
---|
| 1177 | var nick string
|
---|
| 1178 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1179 | return err
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
[161] | 1182 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1183 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1184 | dc.SendMessage(&irc.Message{
|
---|
| 1185 | Prefix: dc.srv.prefix(),
|
---|
| 1186 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 1187 | Params: []string{dc.nick, nick, "is an IRC operator"},
|
---|
| 1188 | })
|
---|
| 1189 | })
|
---|
| 1190 | case irc.RPL_WHOISIDLE:
|
---|
| 1191 | var nick string
|
---|
| 1192 | if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
---|
| 1193 | return err
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
[161] | 1196 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1197 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1198 | params := []string{dc.nick, nick}
|
---|
| 1199 | params = append(params, msg.Params[2:]...)
|
---|
| 1200 | dc.SendMessage(&irc.Message{
|
---|
| 1201 | Prefix: dc.srv.prefix(),
|
---|
| 1202 | Command: irc.RPL_WHOISIDLE,
|
---|
| 1203 | Params: params,
|
---|
| 1204 | })
|
---|
| 1205 | })
|
---|
| 1206 | case irc.RPL_WHOISCHANNELS:
|
---|
| 1207 | var nick, channelList string
|
---|
| 1208 | if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
---|
| 1209 | return err
|
---|
| 1210 | }
|
---|
[174] | 1211 | channels := splitSpace(channelList)
|
---|
[128] | 1212 |
|
---|
[161] | 1213 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1214 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1215 | channelList := make([]string, len(channels))
|
---|
| 1216 | for i, channel := range channels {
|
---|
[139] | 1217 | prefix, channel := uc.parseMembershipPrefix(channel)
|
---|
[260] | 1218 | channel = dc.marshalEntity(uc.network, channel)
|
---|
[292] | 1219 | channelList[i] = prefix.Format(dc) + channel
|
---|
[128] | 1220 | }
|
---|
| 1221 | channels := strings.Join(channelList, " ")
|
---|
| 1222 | dc.SendMessage(&irc.Message{
|
---|
| 1223 | Prefix: dc.srv.prefix(),
|
---|
| 1224 | Command: irc.RPL_WHOISCHANNELS,
|
---|
| 1225 | Params: []string{dc.nick, nick, channels},
|
---|
| 1226 | })
|
---|
| 1227 | })
|
---|
| 1228 | case irc.RPL_ENDOFWHOIS:
|
---|
| 1229 | var nick string
|
---|
| 1230 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1231 | return err
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
[161] | 1234 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1235 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1236 | dc.SendMessage(&irc.Message{
|
---|
| 1237 | Prefix: dc.srv.prefix(),
|
---|
| 1238 | Command: irc.RPL_ENDOFWHOIS,
|
---|
[142] | 1239 | Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
---|
[128] | 1240 | })
|
---|
| 1241 | })
|
---|
[115] | 1242 | case "INVITE":
|
---|
[273] | 1243 | var nick, channel string
|
---|
[115] | 1244 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
| 1245 | return err
|
---|
| 1246 | }
|
---|
| 1247 |
|
---|
[448] | 1248 | weAreInvited := nick == uc.nick
|
---|
| 1249 |
|
---|
[115] | 1250 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[448] | 1251 | if !weAreInvited && !dc.caps["invite-notify"] {
|
---|
| 1252 | return
|
---|
| 1253 | }
|
---|
[115] | 1254 | dc.SendMessage(&irc.Message{
|
---|
[260] | 1255 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
[115] | 1256 | Command: "INVITE",
|
---|
[260] | 1257 | Params: []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[115] | 1258 | })
|
---|
| 1259 | })
|
---|
[163] | 1260 | case irc.RPL_INVITING:
|
---|
[273] | 1261 | var nick, channel string
|
---|
[304] | 1262 | if err := parseMessageParams(msg, nil, &nick, &channel); err != nil {
|
---|
[163] | 1263 | return err
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1267 | dc.SendMessage(&irc.Message{
|
---|
| 1268 | Prefix: dc.srv.prefix(),
|
---|
| 1269 | Command: irc.RPL_INVITING,
|
---|
[260] | 1270 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[163] | 1271 | })
|
---|
| 1272 | })
|
---|
[272] | 1273 | case irc.RPL_AWAY:
|
---|
| 1274 | var nick, reason string
|
---|
| 1275 | if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
|
---|
| 1276 | return err
|
---|
| 1277 | }
|
---|
| 1278 |
|
---|
[274] | 1279 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[272] | 1280 | dc.SendMessage(&irc.Message{
|
---|
| 1281 | Prefix: dc.srv.prefix(),
|
---|
| 1282 | Command: irc.RPL_AWAY,
|
---|
| 1283 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), reason},
|
---|
| 1284 | })
|
---|
| 1285 | })
|
---|
[276] | 1286 | case "AWAY":
|
---|
| 1287 | if msg.Prefix == nil {
|
---|
| 1288 | return fmt.Errorf("expected a prefix")
|
---|
| 1289 | }
|
---|
| 1290 |
|
---|
| 1291 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1292 | if !dc.caps["away-notify"] {
|
---|
| 1293 | return
|
---|
| 1294 | }
|
---|
| 1295 | dc.SendMessage(&irc.Message{
|
---|
| 1296 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
| 1297 | Command: "AWAY",
|
---|
| 1298 | Params: msg.Params,
|
---|
| 1299 | })
|
---|
| 1300 | })
|
---|
[300] | 1301 | case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST:
|
---|
| 1302 | var channel, mask string
|
---|
| 1303 | if err := parseMessageParams(msg, nil, &channel, &mask); err != nil {
|
---|
| 1304 | return err
|
---|
| 1305 | }
|
---|
| 1306 | var addNick, addTime string
|
---|
| 1307 | if len(msg.Params) >= 5 {
|
---|
| 1308 | addNick = msg.Params[3]
|
---|
| 1309 | addTime = msg.Params[4]
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
| 1312 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1313 | channel := dc.marshalEntity(uc.network, channel)
|
---|
| 1314 |
|
---|
| 1315 | var params []string
|
---|
| 1316 | if addNick != "" && addTime != "" {
|
---|
| 1317 | addNick := dc.marshalEntity(uc.network, addNick)
|
---|
| 1318 | params = []string{dc.nick, channel, mask, addNick, addTime}
|
---|
| 1319 | } else {
|
---|
| 1320 | params = []string{dc.nick, channel, mask}
|
---|
| 1321 | }
|
---|
| 1322 |
|
---|
| 1323 | dc.SendMessage(&irc.Message{
|
---|
| 1324 | Prefix: dc.srv.prefix(),
|
---|
| 1325 | Command: msg.Command,
|
---|
| 1326 | Params: params,
|
---|
| 1327 | })
|
---|
| 1328 | })
|
---|
| 1329 | case irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
|
---|
| 1330 | var channel, trailing string
|
---|
| 1331 | if err := parseMessageParams(msg, nil, &channel, &trailing); err != nil {
|
---|
| 1332 | return err
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
| 1335 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1336 | upstreamChannel := dc.marshalEntity(uc.network, channel)
|
---|
| 1337 | dc.SendMessage(&irc.Message{
|
---|
| 1338 | Prefix: dc.srv.prefix(),
|
---|
| 1339 | Command: msg.Command,
|
---|
| 1340 | Params: []string{dc.nick, upstreamChannel, trailing},
|
---|
| 1341 | })
|
---|
| 1342 | })
|
---|
[302] | 1343 | case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
---|
| 1344 | var command, reason string
|
---|
| 1345 | if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
---|
| 1346 | return err
|
---|
| 1347 | }
|
---|
| 1348 |
|
---|
| 1349 | if command == "LIST" {
|
---|
| 1350 | ok := uc.endPendingLISTs(false)
|
---|
| 1351 | if !ok {
|
---|
| 1352 | return fmt.Errorf("unexpected response for LIST: %q: no matching pending LIST", msg.Command)
|
---|
| 1353 | }
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
[355] | 1356 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1357 | dc.SendMessage(&irc.Message{
|
---|
| 1358 | Prefix: uc.srv.prefix(),
|
---|
| 1359 | Command: msg.Command,
|
---|
| 1360 | Params: []string{dc.nick, command, reason},
|
---|
[302] | 1361 | })
|
---|
[355] | 1362 | })
|
---|
[155] | 1363 | case "ACK":
|
---|
| 1364 | // Ignore
|
---|
[198] | 1365 | case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
|
---|
| 1366 | // Ignore
|
---|
[16] | 1367 | case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
---|
[14] | 1368 | // Ignore
|
---|
| 1369 | case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
---|
| 1370 | // Ignore
|
---|
| 1371 | case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
|
---|
| 1372 | // Ignore
|
---|
[177] | 1373 | case irc.RPL_LISTSTART:
|
---|
| 1374 | // Ignore
|
---|
[14] | 1375 | case rpl_localusers, rpl_globalusers:
|
---|
| 1376 | // Ignore
|
---|
[96] | 1377 | case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
---|
[14] | 1378 | // Ignore
|
---|
[390] | 1379 | case "ERROR":
|
---|
| 1380 | var text string
|
---|
| 1381 | if err := parseMessageParams(msg, &text); err != nil {
|
---|
| 1382 | return err
|
---|
| 1383 | }
|
---|
| 1384 | return fmt.Errorf("fatal server error: %v", text)
|
---|
[389] | 1385 | case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
|
---|
[342] | 1386 | if !uc.registered {
|
---|
[399] | 1387 | text := msg.Params[len(msg.Params)-1]
|
---|
| 1388 | return registrationError(text)
|
---|
[342] | 1389 | }
|
---|
| 1390 | fallthrough
|
---|
[13] | 1391 | default:
|
---|
[95] | 1392 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
[355] | 1393 |
|
---|
| 1394 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1395 | // best effort marshaling for unknown messages, replies and errors:
|
---|
| 1396 | // most numerics start with the user nick, marshal it if that's the case
|
---|
| 1397 | // otherwise, conservately keep the params without marshaling
|
---|
| 1398 | params := msg.Params
|
---|
| 1399 | if _, err := strconv.Atoi(msg.Command); err == nil { // numeric
|
---|
| 1400 | if len(msg.Params) > 0 && isOurNick(uc.network, msg.Params[0]) {
|
---|
| 1401 | params[0] = dc.nick
|
---|
[302] | 1402 | }
|
---|
[355] | 1403 | }
|
---|
| 1404 | dc.SendMessage(&irc.Message{
|
---|
| 1405 | Prefix: uc.srv.prefix(),
|
---|
| 1406 | Command: msg.Command,
|
---|
| 1407 | Params: params,
|
---|
[302] | 1408 | })
|
---|
[355] | 1409 | })
|
---|
[13] | 1410 | }
|
---|
[14] | 1411 | return nil
|
---|
[13] | 1412 | }
|
---|
| 1413 |
|
---|
[435] | 1414 | func (uc *upstreamConn) handleDetachedMessage(sender string, text string, ch *Channel) {
|
---|
| 1415 | highlight := sender != uc.nick && isHighlight(text, uc.nick)
|
---|
| 1416 | if ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight) {
|
---|
| 1417 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1418 | if highlight {
|
---|
| 1419 | sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
|
---|
| 1420 | } else {
|
---|
| 1421 | sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
|
---|
| 1422 | }
|
---|
| 1423 | })
|
---|
| 1424 | }
|
---|
| 1425 | if ch.ReattachOn == FilterMessage || (ch.ReattachOn == FilterHighlight && highlight) {
|
---|
| 1426 | uc.network.attach(ch)
|
---|
| 1427 | if err := uc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
| 1428 | uc.logger.Printf("failed to update channel %q: %v", ch.Name, err)
|
---|
| 1429 | }
|
---|
| 1430 | }
|
---|
| 1431 | }
|
---|
| 1432 |
|
---|
[458] | 1433 | func (uc *upstreamConn) handleChanModes(s string) error {
|
---|
| 1434 | parts := strings.SplitN(s, ",", 5)
|
---|
| 1435 | if len(parts) < 4 {
|
---|
| 1436 | return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", s)
|
---|
| 1437 | }
|
---|
| 1438 | modes := make(map[byte]channelModeType)
|
---|
| 1439 | for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
---|
| 1440 | for j := 0; j < len(parts[i]); j++ {
|
---|
| 1441 | mode := parts[i][j]
|
---|
| 1442 | modes[mode] = mt
|
---|
| 1443 | }
|
---|
| 1444 | }
|
---|
| 1445 | uc.availableChannelModes = modes
|
---|
| 1446 | return nil
|
---|
| 1447 | }
|
---|
| 1448 |
|
---|
| 1449 | func (uc *upstreamConn) handleMemberships(s string) error {
|
---|
| 1450 | if s == "" {
|
---|
| 1451 | uc.availableMemberships = nil
|
---|
| 1452 | return nil
|
---|
| 1453 | }
|
---|
| 1454 |
|
---|
| 1455 | if s[0] != '(' {
|
---|
| 1456 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
| 1457 | }
|
---|
| 1458 | sep := strings.IndexByte(s, ')')
|
---|
| 1459 | if sep < 0 || len(s) != sep*2 {
|
---|
| 1460 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", s)
|
---|
| 1461 | }
|
---|
| 1462 | memberships := make([]membership, len(s)/2-1)
|
---|
| 1463 | for i := range memberships {
|
---|
| 1464 | memberships[i] = membership{
|
---|
| 1465 | Mode: s[i+1],
|
---|
| 1466 | Prefix: s[sep+i+1],
|
---|
| 1467 | }
|
---|
| 1468 | }
|
---|
| 1469 | uc.availableMemberships = memberships
|
---|
| 1470 | return nil
|
---|
| 1471 | }
|
---|
| 1472 |
|
---|
[281] | 1473 | func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
|
---|
| 1474 | caps := strings.Fields(capsStr)
|
---|
| 1475 | for _, s := range caps {
|
---|
| 1476 | kv := strings.SplitN(s, "=", 2)
|
---|
| 1477 | k := strings.ToLower(kv[0])
|
---|
| 1478 | var v string
|
---|
| 1479 | if len(kv) == 2 {
|
---|
| 1480 | v = kv[1]
|
---|
| 1481 | }
|
---|
| 1482 | uc.supportedCaps[k] = v
|
---|
| 1483 | }
|
---|
| 1484 | }
|
---|
| 1485 |
|
---|
| 1486 | func (uc *upstreamConn) requestCaps() {
|
---|
| 1487 | var requestCaps []string
|
---|
[282] | 1488 | for c := range permanentUpstreamCaps {
|
---|
[281] | 1489 | if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
|
---|
| 1490 | requestCaps = append(requestCaps, c)
|
---|
| 1491 | }
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
| 1494 | if uc.requestSASL() && !uc.caps["sasl"] {
|
---|
| 1495 | requestCaps = append(requestCaps, "sasl")
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
[282] | 1498 | if len(requestCaps) == 0 {
|
---|
| 1499 | return
|
---|
| 1500 | }
|
---|
| 1501 |
|
---|
| 1502 | uc.SendMessage(&irc.Message{
|
---|
| 1503 | Command: "CAP",
|
---|
| 1504 | Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
---|
| 1505 | })
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | func (uc *upstreamConn) requestSASL() bool {
|
---|
| 1509 | if uc.network.SASL.Mechanism == "" {
|
---|
| 1510 | return false
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 | v, ok := uc.supportedCaps["sasl"]
|
---|
| 1514 | if !ok {
|
---|
| 1515 | return false
|
---|
| 1516 | }
|
---|
| 1517 | if v != "" {
|
---|
| 1518 | mechanisms := strings.Split(v, ",")
|
---|
| 1519 | found := false
|
---|
| 1520 | for _, mech := range mechanisms {
|
---|
| 1521 | if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
|
---|
| 1522 | found = true
|
---|
| 1523 | break
|
---|
| 1524 | }
|
---|
| 1525 | }
|
---|
| 1526 | if !found {
|
---|
| 1527 | return false
|
---|
| 1528 | }
|
---|
| 1529 | }
|
---|
| 1530 |
|
---|
| 1531 | return true
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
| 1534 | func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
---|
| 1535 | uc.caps[name] = ok
|
---|
| 1536 |
|
---|
| 1537 | switch name {
|
---|
| 1538 | case "sasl":
|
---|
| 1539 | if !ok {
|
---|
| 1540 | uc.logger.Printf("server refused to acknowledge the SASL capability")
|
---|
| 1541 | return nil
|
---|
| 1542 | }
|
---|
| 1543 |
|
---|
| 1544 | auth := &uc.network.SASL
|
---|
| 1545 | switch auth.Mechanism {
|
---|
| 1546 | case "PLAIN":
|
---|
| 1547 | uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
---|
| 1548 | uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
---|
[307] | 1549 | case "EXTERNAL":
|
---|
| 1550 | uc.logger.Printf("starting SASL EXTERNAL authentication")
|
---|
| 1551 | uc.saslClient = sasl.NewExternalClient("")
|
---|
[282] | 1552 | default:
|
---|
| 1553 | return fmt.Errorf("unsupported SASL mechanism %q", name)
|
---|
| 1554 | }
|
---|
| 1555 |
|
---|
[281] | 1556 | uc.SendMessage(&irc.Message{
|
---|
[282] | 1557 | Command: "AUTHENTICATE",
|
---|
| 1558 | Params: []string{auth.Mechanism},
|
---|
[281] | 1559 | })
|
---|
[282] | 1560 | default:
|
---|
| 1561 | if permanentUpstreamCaps[name] {
|
---|
| 1562 | break
|
---|
| 1563 | }
|
---|
| 1564 | uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
|
---|
[281] | 1565 | }
|
---|
[282] | 1566 | return nil
|
---|
[281] | 1567 | }
|
---|
| 1568 |
|
---|
[174] | 1569 | func splitSpace(s string) []string {
|
---|
| 1570 | return strings.FieldsFunc(s, func(r rune) bool {
|
---|
| 1571 | return r == ' '
|
---|
| 1572 | })
|
---|
| 1573 | }
|
---|
| 1574 |
|
---|
[55] | 1575 | func (uc *upstreamConn) register() {
|
---|
[77] | 1576 | uc.nick = uc.network.Nick
|
---|
[457] | 1577 | uc.username = uc.network.GetUsername()
|
---|
| 1578 | uc.realname = uc.network.GetRealname()
|
---|
[77] | 1579 |
|
---|
[60] | 1580 | uc.SendMessage(&irc.Message{
|
---|
[92] | 1581 | Command: "CAP",
|
---|
| 1582 | Params: []string{"LS", "302"},
|
---|
| 1583 | })
|
---|
| 1584 |
|
---|
[93] | 1585 | if uc.network.Pass != "" {
|
---|
| 1586 | uc.SendMessage(&irc.Message{
|
---|
| 1587 | Command: "PASS",
|
---|
| 1588 | Params: []string{uc.network.Pass},
|
---|
| 1589 | })
|
---|
| 1590 | }
|
---|
| 1591 |
|
---|
[92] | 1592 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1593 | Command: "NICK",
|
---|
[69] | 1594 | Params: []string{uc.nick},
|
---|
[60] | 1595 | })
|
---|
| 1596 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1597 | Command: "USER",
|
---|
[77] | 1598 | Params: []string{uc.username, "0", "*", uc.realname},
|
---|
[60] | 1599 | })
|
---|
[44] | 1600 | }
|
---|
[13] | 1601 |
|
---|
[197] | 1602 | func (uc *upstreamConn) runUntilRegistered() error {
|
---|
| 1603 | for !uc.registered {
|
---|
[212] | 1604 | msg, err := uc.ReadMessage()
|
---|
[197] | 1605 | if err != nil {
|
---|
| 1606 | return fmt.Errorf("failed to read message: %v", err)
|
---|
| 1607 | }
|
---|
| 1608 |
|
---|
| 1609 | if err := uc.handleMessage(msg); err != nil {
|
---|
[399] | 1610 | if _, ok := err.(registrationError); ok {
|
---|
| 1611 | return err
|
---|
| 1612 | } else {
|
---|
| 1613 | msg.Tags = nil // prevent message tags from cluttering logs
|
---|
| 1614 | return fmt.Errorf("failed to handle message %q: %v", msg, err)
|
---|
| 1615 | }
|
---|
[197] | 1616 | }
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
[263] | 1619 | for _, command := range uc.network.ConnectCommands {
|
---|
| 1620 | m, err := irc.ParseMessage(command)
|
---|
| 1621 | if err != nil {
|
---|
| 1622 | uc.logger.Printf("failed to parse connect command %q: %v", command, err)
|
---|
| 1623 | } else {
|
---|
| 1624 | uc.SendMessage(m)
|
---|
| 1625 | }
|
---|
| 1626 | }
|
---|
| 1627 |
|
---|
[197] | 1628 | return nil
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
[165] | 1631 | func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
---|
[13] | 1632 | for {
|
---|
[210] | 1633 | msg, err := uc.ReadMessage()
|
---|
[13] | 1634 | if err == io.EOF {
|
---|
| 1635 | break
|
---|
| 1636 | } else if err != nil {
|
---|
| 1637 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1638 | }
|
---|
| 1639 |
|
---|
[165] | 1640 | ch <- eventUpstreamMessage{msg, uc}
|
---|
[13] | 1641 | }
|
---|
| 1642 |
|
---|
[45] | 1643 | return nil
|
---|
[13] | 1644 | }
|
---|
[60] | 1645 |
|
---|
[303] | 1646 | func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
---|
| 1647 | if !uc.caps["message-tags"] {
|
---|
| 1648 | msg = msg.Copy()
|
---|
| 1649 | msg.Tags = nil
|
---|
| 1650 | }
|
---|
| 1651 |
|
---|
| 1652 | uc.conn.SendMessage(msg)
|
---|
| 1653 | }
|
---|
| 1654 |
|
---|
[176] | 1655 | func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
---|
[278] | 1656 | if uc.caps["labeled-response"] {
|
---|
[155] | 1657 | if msg.Tags == nil {
|
---|
| 1658 | msg.Tags = make(map[string]irc.TagValue)
|
---|
| 1659 | }
|
---|
[176] | 1660 | msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
---|
[161] | 1661 | uc.nextLabelID++
|
---|
[155] | 1662 | }
|
---|
| 1663 | uc.SendMessage(msg)
|
---|
| 1664 | }
|
---|
[178] | 1665 |
|
---|
[428] | 1666 | // appendLog appends a message to the log file.
|
---|
| 1667 | //
|
---|
| 1668 | // The internal message ID is returned. If the message isn't recorded in the
|
---|
| 1669 | // log file, an empty string is returned.
|
---|
| 1670 | func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string) {
|
---|
[423] | 1671 | if uc.user.msgStore == nil {
|
---|
[428] | 1672 | return ""
|
---|
[178] | 1673 | }
|
---|
[215] | 1674 |
|
---|
[284] | 1675 | detached := false
|
---|
| 1676 | if ch, ok := uc.network.channels[entity]; ok {
|
---|
| 1677 | detached = ch.Detached
|
---|
| 1678 | }
|
---|
| 1679 |
|
---|
[451] | 1680 | delivered, ok := uc.network.delivered[entity]
|
---|
[253] | 1681 | if !ok {
|
---|
[423] | 1682 | lastID, err := uc.user.msgStore.LastMsgID(uc.network, entity, time.Now())
|
---|
[409] | 1683 | if err != nil {
|
---|
| 1684 | uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
|
---|
[428] | 1685 | return ""
|
---|
[409] | 1686 | }
|
---|
| 1687 |
|
---|
[451] | 1688 | delivered = make(map[string]string)
|
---|
| 1689 | uc.network.delivered[entity] = delivered
|
---|
[253] | 1690 |
|
---|
| 1691 | for clientName, _ := range uc.network.offlineClients {
|
---|
[451] | 1692 | delivered[clientName] = lastID
|
---|
[253] | 1693 | }
|
---|
[284] | 1694 |
|
---|
| 1695 | if detached {
|
---|
| 1696 | // If the channel is detached, online clients act as offline
|
---|
| 1697 | // clients too
|
---|
| 1698 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[451] | 1699 | delivered[dc.clientName] = lastID
|
---|
[284] | 1700 | })
|
---|
| 1701 | }
|
---|
[253] | 1702 | }
|
---|
| 1703 |
|
---|
[423] | 1704 | msgID, err := uc.user.msgStore.Append(uc.network, entity, msg)
|
---|
[409] | 1705 | if err != nil {
|
---|
| 1706 | uc.logger.Printf("failed to log message: %v", err)
|
---|
[428] | 1707 | return ""
|
---|
[409] | 1708 | }
|
---|
[406] | 1709 |
|
---|
[428] | 1710 | return msgID
|
---|
[253] | 1711 | }
|
---|
| 1712 |
|
---|
[409] | 1713 | // produce appends a message to the logs and forwards it to connected downstream
|
---|
| 1714 | // connections.
|
---|
[245] | 1715 | //
|
---|
| 1716 | // If origin is not nil and origin doesn't support echo-message, the message is
|
---|
| 1717 | // forwarded to all connections except origin.
|
---|
[239] | 1718 | func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
|
---|
[428] | 1719 | var msgID string
|
---|
[239] | 1720 | if target != "" {
|
---|
[428] | 1721 | msgID = uc.appendLog(target, msg)
|
---|
[239] | 1722 | }
|
---|
| 1723 |
|
---|
[284] | 1724 | // Don't forward messages if it's a detached channel
|
---|
| 1725 | if ch, ok := uc.network.channels[target]; ok && ch.Detached {
|
---|
| 1726 | return
|
---|
| 1727 | }
|
---|
| 1728 |
|
---|
[227] | 1729 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[238] | 1730 | if dc != origin || dc.caps["echo-message"] {
|
---|
[428] | 1731 | dc.sendMessageWithID(dc.marshalMessage(msg, uc.network), msgID)
|
---|
| 1732 | } else {
|
---|
| 1733 | dc.advanceMessageWithID(msg, msgID)
|
---|
[238] | 1734 | }
|
---|
[227] | 1735 | })
|
---|
[226] | 1736 | }
|
---|
| 1737 |
|
---|
[198] | 1738 | func (uc *upstreamConn) updateAway() {
|
---|
| 1739 | away := true
|
---|
| 1740 | uc.forEachDownstream(func(*downstreamConn) {
|
---|
| 1741 | away = false
|
---|
| 1742 | })
|
---|
| 1743 | if away == uc.away {
|
---|
| 1744 | return
|
---|
| 1745 | }
|
---|
| 1746 | if away {
|
---|
| 1747 | uc.SendMessage(&irc.Message{
|
---|
| 1748 | Command: "AWAY",
|
---|
| 1749 | Params: []string{"Auto away"},
|
---|
| 1750 | })
|
---|
| 1751 | } else {
|
---|
| 1752 | uc.SendMessage(&irc.Message{
|
---|
| 1753 | Command: "AWAY",
|
---|
| 1754 | })
|
---|
| 1755 | }
|
---|
| 1756 | uc.away = away
|
---|
| 1757 | }
|
---|
[435] | 1758 |
|
---|
| 1759 | func (uc *upstreamConn) updateChannelAutoDetach(name string) {
|
---|
| 1760 | if uch, ok := uc.channels[name]; ok {
|
---|
| 1761 | if ch, ok := uc.network.channels[name]; ok && !ch.Detached {
|
---|
| 1762 | uch.updateAutoDetach(ch.DetachAfter)
|
---|
| 1763 | }
|
---|
| 1764 | }
|
---|
| 1765 | }
|
---|