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