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