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