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