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