[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 |
|
---|
[305] | 555 | // TODO: split this into multiple messages if need be
|
---|
| 556 | var names, keys []string
|
---|
[267] | 557 | for _, ch := range uc.network.channels {
|
---|
[305] | 558 | names = append(names, ch.Name)
|
---|
| 559 | keys = append(keys, ch.Key)
|
---|
[19] | 560 | }
|
---|
[305] | 561 | uc.SendMessage(&irc.Message{
|
---|
| 562 | Command: "JOIN",
|
---|
| 563 | Params: []string{
|
---|
| 564 | strings.Join(names, ","),
|
---|
| 565 | strings.Join(keys, ","),
|
---|
| 566 | },
|
---|
| 567 | })
|
---|
[16] | 568 | case irc.RPL_MYINFO:
|
---|
[139] | 569 | if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, nil); err != nil {
|
---|
[43] | 570 | return err
|
---|
[16] | 571 | }
|
---|
[139] | 572 | case irc.RPL_ISUPPORT:
|
---|
| 573 | if err := parseMessageParams(msg, nil, nil); err != nil {
|
---|
| 574 | return err
|
---|
[16] | 575 | }
|
---|
[139] | 576 | for _, token := range msg.Params[1 : len(msg.Params)-1] {
|
---|
| 577 | negate := false
|
---|
| 578 | parameter := token
|
---|
| 579 | value := ""
|
---|
| 580 | if strings.HasPrefix(token, "-") {
|
---|
| 581 | negate = true
|
---|
| 582 | token = token[1:]
|
---|
| 583 | } else {
|
---|
| 584 | if i := strings.IndexByte(token, '='); i >= 0 {
|
---|
| 585 | parameter = token[:i]
|
---|
| 586 | value = token[i+1:]
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
| 589 | if !negate {
|
---|
| 590 | switch parameter {
|
---|
| 591 | case "CHANMODES":
|
---|
| 592 | parts := strings.SplitN(value, ",", 5)
|
---|
| 593 | if len(parts) < 4 {
|
---|
| 594 | return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", value)
|
---|
| 595 | }
|
---|
| 596 | modes := make(map[byte]channelModeType)
|
---|
| 597 | for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
---|
| 598 | for j := 0; j < len(parts[i]); j++ {
|
---|
| 599 | mode := parts[i][j]
|
---|
| 600 | modes[mode] = mt
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 | uc.availableChannelModes = modes
|
---|
| 604 | case "CHANTYPES":
|
---|
| 605 | uc.availableChannelTypes = value
|
---|
| 606 | case "PREFIX":
|
---|
| 607 | if value == "" {
|
---|
| 608 | uc.availableMemberships = nil
|
---|
| 609 | } else {
|
---|
| 610 | if value[0] != '(' {
|
---|
| 611 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
---|
| 612 | }
|
---|
| 613 | sep := strings.IndexByte(value, ')')
|
---|
| 614 | if sep < 0 || len(value) != sep*2 {
|
---|
| 615 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
---|
| 616 | }
|
---|
| 617 | memberships := make([]membership, len(value)/2-1)
|
---|
| 618 | for i := range memberships {
|
---|
| 619 | memberships[i] = membership{
|
---|
| 620 | Mode: value[i+1],
|
---|
| 621 | Prefix: value[sep+i+1],
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 | uc.availableMemberships = memberships
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 | } else {
|
---|
| 628 | // TODO: handle ISUPPORT negations
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
[153] | 631 | case "BATCH":
|
---|
| 632 | var tag string
|
---|
| 633 | if err := parseMessageParams(msg, &tag); err != nil {
|
---|
| 634 | return err
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | if strings.HasPrefix(tag, "+") {
|
---|
| 638 | tag = tag[1:]
|
---|
| 639 | if _, ok := uc.batches[tag]; ok {
|
---|
| 640 | return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
---|
| 641 | }
|
---|
| 642 | var batchType string
|
---|
| 643 | if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
---|
| 644 | return err
|
---|
| 645 | }
|
---|
[155] | 646 | label := label
|
---|
| 647 | if label == "" && msgBatch != nil {
|
---|
| 648 | label = msgBatch.Label
|
---|
| 649 | }
|
---|
[153] | 650 | uc.batches[tag] = batch{
|
---|
| 651 | Type: batchType,
|
---|
| 652 | Params: msg.Params[2:],
|
---|
| 653 | Outer: msgBatch,
|
---|
[155] | 654 | Label: label,
|
---|
[153] | 655 | }
|
---|
| 656 | } else if strings.HasPrefix(tag, "-") {
|
---|
| 657 | tag = tag[1:]
|
---|
| 658 | if _, ok := uc.batches[tag]; !ok {
|
---|
| 659 | return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
---|
| 660 | }
|
---|
| 661 | delete(uc.batches, tag)
|
---|
| 662 | } else {
|
---|
| 663 | return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
---|
| 664 | }
|
---|
[42] | 665 | case "NICK":
|
---|
[83] | 666 | if msg.Prefix == nil {
|
---|
| 667 | return fmt.Errorf("expected a prefix")
|
---|
| 668 | }
|
---|
| 669 |
|
---|
[43] | 670 | var newNick string
|
---|
| 671 | if err := parseMessageParams(msg, &newNick); err != nil {
|
---|
| 672 | return err
|
---|
[42] | 673 | }
|
---|
| 674 |
|
---|
[244] | 675 | me := false
|
---|
[55] | 676 | if msg.Prefix.Name == uc.nick {
|
---|
| 677 | uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
---|
[244] | 678 | me = true
|
---|
[55] | 679 | uc.nick = newNick
|
---|
[42] | 680 | }
|
---|
| 681 |
|
---|
[55] | 682 | for _, ch := range uc.channels {
|
---|
[292] | 683 | if memberships, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
[42] | 684 | delete(ch.Members, msg.Prefix.Name)
|
---|
[292] | 685 | ch.Members[newNick] = memberships
|
---|
[215] | 686 | uc.appendLog(ch.Name, msg)
|
---|
[253] | 687 | uc.appendHistory(ch.Name, msg)
|
---|
[42] | 688 | }
|
---|
| 689 | }
|
---|
[82] | 690 |
|
---|
[244] | 691 | if !me {
|
---|
[82] | 692 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 693 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[82] | 694 | })
|
---|
[296] | 695 | } else {
|
---|
| 696 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 697 | dc.updateNick()
|
---|
| 698 | })
|
---|
[82] | 699 | }
|
---|
[69] | 700 | case "JOIN":
|
---|
| 701 | if msg.Prefix == nil {
|
---|
| 702 | return fmt.Errorf("expected a prefix")
|
---|
| 703 | }
|
---|
[42] | 704 |
|
---|
[43] | 705 | var channels string
|
---|
| 706 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 707 | return err
|
---|
[19] | 708 | }
|
---|
[34] | 709 |
|
---|
[43] | 710 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 711 | if msg.Prefix.Name == uc.nick {
|
---|
| 712 | uc.logger.Printf("joined channel %q", ch)
|
---|
| 713 | uc.channels[ch] = &upstreamChannel{
|
---|
[34] | 714 | Name: ch,
|
---|
[55] | 715 | conn: uc,
|
---|
[292] | 716 | Members: make(map[string]*memberships),
|
---|
[34] | 717 | }
|
---|
[139] | 718 |
|
---|
| 719 | uc.SendMessage(&irc.Message{
|
---|
| 720 | Command: "MODE",
|
---|
| 721 | Params: []string{ch},
|
---|
| 722 | })
|
---|
[34] | 723 | } else {
|
---|
[55] | 724 | ch, err := uc.getChannel(ch)
|
---|
[34] | 725 | if err != nil {
|
---|
| 726 | return err
|
---|
| 727 | }
|
---|
[294] | 728 | ch.Members[msg.Prefix.Name] = &memberships{}
|
---|
[19] | 729 | }
|
---|
[69] | 730 |
|
---|
[245] | 731 | chMsg := msg.Copy()
|
---|
| 732 | chMsg.Params[0] = ch
|
---|
| 733 | uc.produce(ch, chMsg, nil)
|
---|
[19] | 734 | }
|
---|
[69] | 735 | case "PART":
|
---|
| 736 | if msg.Prefix == nil {
|
---|
| 737 | return fmt.Errorf("expected a prefix")
|
---|
| 738 | }
|
---|
[34] | 739 |
|
---|
[43] | 740 | var channels string
|
---|
| 741 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 742 | return err
|
---|
[34] | 743 | }
|
---|
| 744 |
|
---|
[43] | 745 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 746 | if msg.Prefix.Name == uc.nick {
|
---|
| 747 | uc.logger.Printf("parted channel %q", ch)
|
---|
| 748 | delete(uc.channels, ch)
|
---|
[34] | 749 | } else {
|
---|
[55] | 750 | ch, err := uc.getChannel(ch)
|
---|
[34] | 751 | if err != nil {
|
---|
| 752 | return err
|
---|
| 753 | }
|
---|
| 754 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 755 | }
|
---|
[69] | 756 |
|
---|
[245] | 757 | chMsg := msg.Copy()
|
---|
| 758 | chMsg.Params[0] = ch
|
---|
| 759 | uc.produce(ch, chMsg, nil)
|
---|
[34] | 760 | }
|
---|
[159] | 761 | case "KICK":
|
---|
| 762 | if msg.Prefix == nil {
|
---|
| 763 | return fmt.Errorf("expected a prefix")
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | var channel, user string
|
---|
| 767 | if err := parseMessageParams(msg, &channel, &user); err != nil {
|
---|
| 768 | return err
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | if user == uc.nick {
|
---|
| 772 | uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
---|
| 773 | delete(uc.channels, channel)
|
---|
| 774 | } else {
|
---|
| 775 | ch, err := uc.getChannel(channel)
|
---|
| 776 | if err != nil {
|
---|
| 777 | return err
|
---|
| 778 | }
|
---|
| 779 | delete(ch.Members, user)
|
---|
| 780 | }
|
---|
| 781 |
|
---|
[245] | 782 | uc.produce(channel, msg, nil)
|
---|
[83] | 783 | case "QUIT":
|
---|
| 784 | if msg.Prefix == nil {
|
---|
| 785 | return fmt.Errorf("expected a prefix")
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | if msg.Prefix.Name == uc.nick {
|
---|
| 789 | uc.logger.Printf("quit")
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | for _, ch := range uc.channels {
|
---|
[178] | 793 | if _, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
| 794 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 795 |
|
---|
[215] | 796 | uc.appendLog(ch.Name, msg)
|
---|
[253] | 797 | uc.appendHistory(ch.Name, msg)
|
---|
[178] | 798 | }
|
---|
[83] | 799 | }
|
---|
| 800 |
|
---|
| 801 | if msg.Prefix.Name != uc.nick {
|
---|
| 802 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[261] | 803 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[83] | 804 | })
|
---|
| 805 | }
|
---|
[19] | 806 | case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
---|
[43] | 807 | var name, topic string
|
---|
| 808 | if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
---|
| 809 | return err
|
---|
[19] | 810 | }
|
---|
[55] | 811 | ch, err := uc.getChannel(name)
|
---|
[19] | 812 | if err != nil {
|
---|
| 813 | return err
|
---|
| 814 | }
|
---|
| 815 | if msg.Command == irc.RPL_TOPIC {
|
---|
[43] | 816 | ch.Topic = topic
|
---|
[19] | 817 | } else {
|
---|
| 818 | ch.Topic = ""
|
---|
| 819 | }
|
---|
| 820 | case "TOPIC":
|
---|
[43] | 821 | var name string
|
---|
[74] | 822 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
[43] | 823 | return err
|
---|
[19] | 824 | }
|
---|
[55] | 825 | ch, err := uc.getChannel(name)
|
---|
[19] | 826 | if err != nil {
|
---|
| 827 | return err
|
---|
| 828 | }
|
---|
| 829 | if len(msg.Params) > 1 {
|
---|
| 830 | ch.Topic = msg.Params[1]
|
---|
| 831 | } else {
|
---|
| 832 | ch.Topic = ""
|
---|
| 833 | }
|
---|
[245] | 834 | uc.produce(ch.Name, msg, nil)
|
---|
[139] | 835 | case "MODE":
|
---|
| 836 | var name, modeStr string
|
---|
| 837 | if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
---|
| 838 | return err
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | if !uc.isChannel(name) { // user mode change
|
---|
| 842 | if name != uc.nick {
|
---|
| 843 | return fmt.Errorf("received MODE message for unknown nick %q", name)
|
---|
| 844 | }
|
---|
| 845 | return uc.modes.Apply(modeStr)
|
---|
| 846 | // TODO: notify downstreams about user mode change?
|
---|
| 847 | } else { // channel mode change
|
---|
| 848 | ch, err := uc.getChannel(name)
|
---|
| 849 | if err != nil {
|
---|
| 850 | return err
|
---|
| 851 | }
|
---|
| 852 |
|
---|
[293] | 853 | needMarshaling, err := applyChannelModes(ch, modeStr, msg.Params[2:])
|
---|
| 854 | if err != nil {
|
---|
| 855 | return err
|
---|
[139] | 856 | }
|
---|
| 857 |
|
---|
[293] | 858 | uc.appendLog(ch.Name, msg)
|
---|
| 859 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 860 | params := make([]string, len(msg.Params))
|
---|
| 861 | params[0] = dc.marshalEntity(uc.network, name)
|
---|
| 862 | params[1] = modeStr
|
---|
| 863 |
|
---|
| 864 | copy(params[2:], msg.Params[2:])
|
---|
| 865 | for i, modeParam := range params[2:] {
|
---|
| 866 | if _, ok := needMarshaling[i]; ok {
|
---|
| 867 | params[2+i] = dc.marshalEntity(uc.network, modeParam)
|
---|
| 868 | }
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | dc.SendMessage(&irc.Message{
|
---|
| 872 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
| 873 | Command: "MODE",
|
---|
| 874 | Params: params,
|
---|
| 875 | })
|
---|
| 876 | })
|
---|
[139] | 877 | }
|
---|
| 878 | case irc.RPL_UMODEIS:
|
---|
| 879 | if err := parseMessageParams(msg, nil); err != nil {
|
---|
| 880 | return err
|
---|
| 881 | }
|
---|
| 882 | modeStr := ""
|
---|
| 883 | if len(msg.Params) > 1 {
|
---|
| 884 | modeStr = msg.Params[1]
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | uc.modes = ""
|
---|
| 888 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
| 889 | return err
|
---|
| 890 | }
|
---|
| 891 | // TODO: send RPL_UMODEIS to downstream connections when applicable
|
---|
| 892 | case irc.RPL_CHANNELMODEIS:
|
---|
| 893 | var channel string
|
---|
| 894 | if err := parseMessageParams(msg, nil, &channel); err != nil {
|
---|
| 895 | return err
|
---|
| 896 | }
|
---|
| 897 | modeStr := ""
|
---|
| 898 | if len(msg.Params) > 2 {
|
---|
| 899 | modeStr = msg.Params[2]
|
---|
| 900 | }
|
---|
| 901 |
|
---|
| 902 | ch, err := uc.getChannel(channel)
|
---|
| 903 | if err != nil {
|
---|
| 904 | return err
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | firstMode := ch.modes == nil
|
---|
| 908 | ch.modes = make(map[byte]string)
|
---|
[293] | 909 | if _, err := applyChannelModes(ch, modeStr, msg.Params[3:]); err != nil {
|
---|
[139] | 910 | return err
|
---|
| 911 | }
|
---|
| 912 | if firstMode {
|
---|
| 913 | modeStr, modeParams := ch.modes.Format()
|
---|
| 914 |
|
---|
| 915 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[260] | 916 | params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
|
---|
[139] | 917 | params = append(params, modeParams...)
|
---|
| 918 |
|
---|
| 919 | dc.SendMessage(&irc.Message{
|
---|
| 920 | Prefix: dc.srv.prefix(),
|
---|
| 921 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 922 | Params: params,
|
---|
| 923 | })
|
---|
| 924 | })
|
---|
| 925 | }
|
---|
[162] | 926 | case rpl_creationtime:
|
---|
| 927 | var channel, creationTime string
|
---|
| 928 | if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
---|
| 929 | return err
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | ch, err := uc.getChannel(channel)
|
---|
| 933 | if err != nil {
|
---|
| 934 | return err
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | firstCreationTime := ch.creationTime == ""
|
---|
| 938 | ch.creationTime = creationTime
|
---|
| 939 | if firstCreationTime {
|
---|
| 940 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 941 | dc.SendMessage(&irc.Message{
|
---|
| 942 | Prefix: dc.srv.prefix(),
|
---|
| 943 | Command: rpl_creationtime,
|
---|
| 944 | Params: []string{dc.nick, channel, creationTime},
|
---|
| 945 | })
|
---|
| 946 | })
|
---|
| 947 | }
|
---|
[19] | 948 | case rpl_topicwhotime:
|
---|
[43] | 949 | var name, who, timeStr string
|
---|
| 950 | if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
---|
| 951 | return err
|
---|
[19] | 952 | }
|
---|
[55] | 953 | ch, err := uc.getChannel(name)
|
---|
[19] | 954 | if err != nil {
|
---|
| 955 | return err
|
---|
| 956 | }
|
---|
[43] | 957 | ch.TopicWho = who
|
---|
| 958 | sec, err := strconv.ParseInt(timeStr, 10, 64)
|
---|
[19] | 959 | if err != nil {
|
---|
| 960 | return fmt.Errorf("failed to parse topic time: %v", err)
|
---|
| 961 | }
|
---|
| 962 | ch.TopicTime = time.Unix(sec, 0)
|
---|
[177] | 963 | case irc.RPL_LIST:
|
---|
| 964 | var channel, clients, topic string
|
---|
| 965 | if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
---|
| 966 | return err
|
---|
| 967 | }
|
---|
| 968 |
|
---|
[181] | 969 | pl := uc.getPendingLIST()
|
---|
[177] | 970 | if pl == nil {
|
---|
| 971 | return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
---|
| 975 | dc.SendMessage(&irc.Message{
|
---|
| 976 | Prefix: dc.srv.prefix(),
|
---|
| 977 | Command: irc.RPL_LIST,
|
---|
[260] | 978 | Params: []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
|
---|
[177] | 979 | })
|
---|
| 980 | })
|
---|
| 981 | case irc.RPL_LISTEND:
|
---|
[181] | 982 | ok := uc.endPendingLISTs(false)
|
---|
[177] | 983 | if !ok {
|
---|
| 984 | return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
---|
| 985 | }
|
---|
[19] | 986 | case irc.RPL_NAMREPLY:
|
---|
[43] | 987 | var name, statusStr, members string
|
---|
| 988 | if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
---|
| 989 | return err
|
---|
[19] | 990 | }
|
---|
[140] | 991 |
|
---|
| 992 | ch, ok := uc.channels[name]
|
---|
| 993 | if !ok {
|
---|
| 994 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 995 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 996 | channel := dc.marshalEntity(uc.network, name)
|
---|
[174] | 997 | members := splitSpace(members)
|
---|
[140] | 998 | for i, member := range members {
|
---|
[292] | 999 | memberships, nick := uc.parseMembershipPrefix(member)
|
---|
| 1000 | members[i] = memberships.Format(dc) + dc.marshalEntity(uc.network, nick)
|
---|
[140] | 1001 | }
|
---|
| 1002 | memberStr := strings.Join(members, " ")
|
---|
| 1003 |
|
---|
| 1004 | dc.SendMessage(&irc.Message{
|
---|
| 1005 | Prefix: dc.srv.prefix(),
|
---|
| 1006 | Command: irc.RPL_NAMREPLY,
|
---|
| 1007 | Params: []string{dc.nick, statusStr, channel, memberStr},
|
---|
| 1008 | })
|
---|
| 1009 | })
|
---|
| 1010 | return nil
|
---|
[19] | 1011 | }
|
---|
| 1012 |
|
---|
[43] | 1013 | status, err := parseChannelStatus(statusStr)
|
---|
[19] | 1014 | if err != nil {
|
---|
| 1015 | return err
|
---|
| 1016 | }
|
---|
| 1017 | ch.Status = status
|
---|
| 1018 |
|
---|
[174] | 1019 | for _, s := range splitSpace(members) {
|
---|
[292] | 1020 | memberships, nick := uc.parseMembershipPrefix(s)
|
---|
| 1021 | ch.Members[nick] = memberships
|
---|
[19] | 1022 | }
|
---|
| 1023 | case irc.RPL_ENDOFNAMES:
|
---|
[43] | 1024 | var name string
|
---|
| 1025 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1026 | return err
|
---|
[25] | 1027 | }
|
---|
[140] | 1028 |
|
---|
| 1029 | ch, ok := uc.channels[name]
|
---|
| 1030 | if !ok {
|
---|
| 1031 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1032 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1033 | channel := dc.marshalEntity(uc.network, name)
|
---|
[140] | 1034 |
|
---|
| 1035 | dc.SendMessage(&irc.Message{
|
---|
| 1036 | Prefix: dc.srv.prefix(),
|
---|
| 1037 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1038 | Params: []string{dc.nick, channel, "End of /NAMES list"},
|
---|
| 1039 | })
|
---|
| 1040 | })
|
---|
| 1041 | return nil
|
---|
[25] | 1042 | }
|
---|
| 1043 |
|
---|
[34] | 1044 | if ch.complete {
|
---|
| 1045 | return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
---|
| 1046 | }
|
---|
[25] | 1047 | ch.complete = true
|
---|
[27] | 1048 |
|
---|
[73] | 1049 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[27] | 1050 | forwardChannel(dc, ch)
|
---|
[40] | 1051 | })
|
---|
[127] | 1052 | case irc.RPL_WHOREPLY:
|
---|
| 1053 | var channel, username, host, server, nick, mode, trailing string
|
---|
| 1054 | if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
---|
| 1055 | return err
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
| 1058 | parts := strings.SplitN(trailing, " ", 2)
|
---|
| 1059 | if len(parts) != 2 {
|
---|
| 1060 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
---|
| 1061 | }
|
---|
| 1062 | realname := parts[1]
|
---|
| 1063 | hops, err := strconv.Atoi(parts[0])
|
---|
| 1064 | if err != nil {
|
---|
| 1065 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
---|
| 1066 | }
|
---|
| 1067 | hops++
|
---|
| 1068 |
|
---|
| 1069 | trailing = strconv.Itoa(hops) + " " + realname
|
---|
| 1070 |
|
---|
[161] | 1071 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1072 | channel := channel
|
---|
| 1073 | if channel != "*" {
|
---|
[260] | 1074 | channel = dc.marshalEntity(uc.network, channel)
|
---|
[127] | 1075 | }
|
---|
[260] | 1076 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[127] | 1077 | dc.SendMessage(&irc.Message{
|
---|
| 1078 | Prefix: dc.srv.prefix(),
|
---|
| 1079 | Command: irc.RPL_WHOREPLY,
|
---|
| 1080 | Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
---|
| 1081 | })
|
---|
| 1082 | })
|
---|
| 1083 | case irc.RPL_ENDOFWHO:
|
---|
| 1084 | var name string
|
---|
| 1085 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1086 | return err
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
[161] | 1089 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1090 | name := name
|
---|
| 1091 | if name != "*" {
|
---|
| 1092 | // TODO: support WHO masks
|
---|
[260] | 1093 | name = dc.marshalEntity(uc.network, name)
|
---|
[127] | 1094 | }
|
---|
| 1095 | dc.SendMessage(&irc.Message{
|
---|
| 1096 | Prefix: dc.srv.prefix(),
|
---|
| 1097 | Command: irc.RPL_ENDOFWHO,
|
---|
[142] | 1098 | Params: []string{dc.nick, name, "End of /WHO list"},
|
---|
[127] | 1099 | })
|
---|
| 1100 | })
|
---|
[128] | 1101 | case irc.RPL_WHOISUSER:
|
---|
| 1102 | var nick, username, host, realname string
|
---|
| 1103 | if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
---|
| 1104 | return err
|
---|
| 1105 | }
|
---|
| 1106 |
|
---|
[161] | 1107 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1108 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1109 | dc.SendMessage(&irc.Message{
|
---|
| 1110 | Prefix: dc.srv.prefix(),
|
---|
| 1111 | Command: irc.RPL_WHOISUSER,
|
---|
| 1112 | Params: []string{dc.nick, nick, username, host, "*", realname},
|
---|
| 1113 | })
|
---|
| 1114 | })
|
---|
| 1115 | case irc.RPL_WHOISSERVER:
|
---|
| 1116 | var nick, server, serverInfo string
|
---|
| 1117 | if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
---|
| 1118 | return err
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
[161] | 1121 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1122 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1123 | dc.SendMessage(&irc.Message{
|
---|
| 1124 | Prefix: dc.srv.prefix(),
|
---|
| 1125 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1126 | Params: []string{dc.nick, nick, server, serverInfo},
|
---|
| 1127 | })
|
---|
| 1128 | })
|
---|
| 1129 | case irc.RPL_WHOISOPERATOR:
|
---|
| 1130 | var nick string
|
---|
| 1131 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1132 | return err
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
[161] | 1135 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1136 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1137 | dc.SendMessage(&irc.Message{
|
---|
| 1138 | Prefix: dc.srv.prefix(),
|
---|
| 1139 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 1140 | Params: []string{dc.nick, nick, "is an IRC operator"},
|
---|
| 1141 | })
|
---|
| 1142 | })
|
---|
| 1143 | case irc.RPL_WHOISIDLE:
|
---|
| 1144 | var nick string
|
---|
| 1145 | if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
---|
| 1146 | return err
|
---|
| 1147 | }
|
---|
| 1148 |
|
---|
[161] | 1149 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1150 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1151 | params := []string{dc.nick, nick}
|
---|
| 1152 | params = append(params, msg.Params[2:]...)
|
---|
| 1153 | dc.SendMessage(&irc.Message{
|
---|
| 1154 | Prefix: dc.srv.prefix(),
|
---|
| 1155 | Command: irc.RPL_WHOISIDLE,
|
---|
| 1156 | Params: params,
|
---|
| 1157 | })
|
---|
| 1158 | })
|
---|
| 1159 | case irc.RPL_WHOISCHANNELS:
|
---|
| 1160 | var nick, channelList string
|
---|
| 1161 | if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
---|
| 1162 | return err
|
---|
| 1163 | }
|
---|
[174] | 1164 | channels := splitSpace(channelList)
|
---|
[128] | 1165 |
|
---|
[161] | 1166 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1167 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1168 | channelList := make([]string, len(channels))
|
---|
| 1169 | for i, channel := range channels {
|
---|
[139] | 1170 | prefix, channel := uc.parseMembershipPrefix(channel)
|
---|
[260] | 1171 | channel = dc.marshalEntity(uc.network, channel)
|
---|
[292] | 1172 | channelList[i] = prefix.Format(dc) + channel
|
---|
[128] | 1173 | }
|
---|
| 1174 | channels := strings.Join(channelList, " ")
|
---|
| 1175 | dc.SendMessage(&irc.Message{
|
---|
| 1176 | Prefix: dc.srv.prefix(),
|
---|
| 1177 | Command: irc.RPL_WHOISCHANNELS,
|
---|
| 1178 | Params: []string{dc.nick, nick, channels},
|
---|
| 1179 | })
|
---|
| 1180 | })
|
---|
| 1181 | case irc.RPL_ENDOFWHOIS:
|
---|
| 1182 | var nick string
|
---|
| 1183 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1184 | return err
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
[161] | 1187 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[260] | 1188 | nick := dc.marshalEntity(uc.network, nick)
|
---|
[128] | 1189 | dc.SendMessage(&irc.Message{
|
---|
| 1190 | Prefix: dc.srv.prefix(),
|
---|
| 1191 | Command: irc.RPL_ENDOFWHOIS,
|
---|
[142] | 1192 | Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
---|
[128] | 1193 | })
|
---|
| 1194 | })
|
---|
[115] | 1195 | case "INVITE":
|
---|
[273] | 1196 | var nick, channel string
|
---|
[115] | 1197 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
| 1198 | return err
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1202 | dc.SendMessage(&irc.Message{
|
---|
[260] | 1203 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
[115] | 1204 | Command: "INVITE",
|
---|
[260] | 1205 | Params: []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[115] | 1206 | })
|
---|
| 1207 | })
|
---|
[163] | 1208 | case irc.RPL_INVITING:
|
---|
[273] | 1209 | var nick, channel string
|
---|
[304] | 1210 | if err := parseMessageParams(msg, nil, &nick, &channel); err != nil {
|
---|
[163] | 1211 | return err
|
---|
| 1212 | }
|
---|
| 1213 |
|
---|
| 1214 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1215 | dc.SendMessage(&irc.Message{
|
---|
| 1216 | Prefix: dc.srv.prefix(),
|
---|
| 1217 | Command: irc.RPL_INVITING,
|
---|
[260] | 1218 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
---|
[163] | 1219 | })
|
---|
| 1220 | })
|
---|
[272] | 1221 | case irc.RPL_AWAY:
|
---|
| 1222 | var nick, reason string
|
---|
| 1223 | if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
|
---|
| 1224 | return err
|
---|
| 1225 | }
|
---|
| 1226 |
|
---|
[274] | 1227 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[272] | 1228 | dc.SendMessage(&irc.Message{
|
---|
| 1229 | Prefix: dc.srv.prefix(),
|
---|
| 1230 | Command: irc.RPL_AWAY,
|
---|
| 1231 | Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), reason},
|
---|
| 1232 | })
|
---|
| 1233 | })
|
---|
[276] | 1234 | case "AWAY":
|
---|
| 1235 | if msg.Prefix == nil {
|
---|
| 1236 | return fmt.Errorf("expected a prefix")
|
---|
| 1237 | }
|
---|
| 1238 |
|
---|
| 1239 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1240 | if !dc.caps["away-notify"] {
|
---|
| 1241 | return
|
---|
| 1242 | }
|
---|
| 1243 | dc.SendMessage(&irc.Message{
|
---|
| 1244 | Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
---|
| 1245 | Command: "AWAY",
|
---|
| 1246 | Params: msg.Params,
|
---|
| 1247 | })
|
---|
| 1248 | })
|
---|
[300] | 1249 | case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST:
|
---|
| 1250 | var channel, mask string
|
---|
| 1251 | if err := parseMessageParams(msg, nil, &channel, &mask); err != nil {
|
---|
| 1252 | return err
|
---|
| 1253 | }
|
---|
| 1254 | var addNick, addTime string
|
---|
| 1255 | if len(msg.Params) >= 5 {
|
---|
| 1256 | addNick = msg.Params[3]
|
---|
| 1257 | addTime = msg.Params[4]
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
| 1260 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1261 | channel := dc.marshalEntity(uc.network, channel)
|
---|
| 1262 |
|
---|
| 1263 | var params []string
|
---|
| 1264 | if addNick != "" && addTime != "" {
|
---|
| 1265 | addNick := dc.marshalEntity(uc.network, addNick)
|
---|
| 1266 | params = []string{dc.nick, channel, mask, addNick, addTime}
|
---|
| 1267 | } else {
|
---|
| 1268 | params = []string{dc.nick, channel, mask}
|
---|
| 1269 | }
|
---|
| 1270 |
|
---|
| 1271 | dc.SendMessage(&irc.Message{
|
---|
| 1272 | Prefix: dc.srv.prefix(),
|
---|
| 1273 | Command: msg.Command,
|
---|
| 1274 | Params: params,
|
---|
| 1275 | })
|
---|
| 1276 | })
|
---|
| 1277 | case irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
|
---|
| 1278 | var channel, trailing string
|
---|
| 1279 | if err := parseMessageParams(msg, nil, &channel, &trailing); err != nil {
|
---|
| 1280 | return err
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
| 1283 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1284 | upstreamChannel := dc.marshalEntity(uc.network, channel)
|
---|
| 1285 | dc.SendMessage(&irc.Message{
|
---|
| 1286 | Prefix: dc.srv.prefix(),
|
---|
| 1287 | Command: msg.Command,
|
---|
| 1288 | Params: []string{dc.nick, upstreamChannel, trailing},
|
---|
| 1289 | })
|
---|
| 1290 | })
|
---|
[302] | 1291 | case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
---|
| 1292 | var command, reason string
|
---|
| 1293 | if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
---|
| 1294 | return err
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 | if command == "LIST" {
|
---|
| 1298 | ok := uc.endPendingLISTs(false)
|
---|
| 1299 | if !ok {
|
---|
| 1300 | return fmt.Errorf("unexpected response for LIST: %q: no matching pending LIST", msg.Command)
|
---|
| 1301 | }
|
---|
| 1302 | }
|
---|
| 1303 |
|
---|
| 1304 | if downstreamID != 0 {
|
---|
| 1305 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1306 | dc.SendMessage(&irc.Message{
|
---|
| 1307 | Prefix: uc.srv.prefix(),
|
---|
| 1308 | Command: msg.Command,
|
---|
| 1309 | Params: []string{dc.nick, command, reason},
|
---|
| 1310 | })
|
---|
| 1311 | })
|
---|
| 1312 | }
|
---|
[155] | 1313 | case "ACK":
|
---|
| 1314 | // Ignore
|
---|
[198] | 1315 | case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
|
---|
| 1316 | // Ignore
|
---|
[16] | 1317 | case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
---|
[14] | 1318 | // Ignore
|
---|
| 1319 | case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
---|
| 1320 | // Ignore
|
---|
| 1321 | case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
|
---|
| 1322 | // Ignore
|
---|
[177] | 1323 | case irc.RPL_LISTSTART:
|
---|
| 1324 | // Ignore
|
---|
[14] | 1325 | case rpl_localusers, rpl_globalusers:
|
---|
| 1326 | // Ignore
|
---|
[96] | 1327 | case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
---|
[14] | 1328 | // Ignore
|
---|
[13] | 1329 | default:
|
---|
[95] | 1330 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
[302] | 1331 | if downstreamID != 0 {
|
---|
| 1332 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1333 | // best effort marshaling for unknown messages, replies and errors:
|
---|
| 1334 | // most numerics start with the user nick, marshal it if that's the case
|
---|
| 1335 | // otherwise, conservately keep the params without marshaling
|
---|
| 1336 | params := msg.Params
|
---|
| 1337 | if _, err := strconv.Atoi(msg.Command); err == nil { // numeric
|
---|
| 1338 | if len(msg.Params) > 0 && isOurNick(uc.network, msg.Params[0]) {
|
---|
| 1339 | params[0] = dc.nick
|
---|
| 1340 | }
|
---|
| 1341 | }
|
---|
| 1342 | dc.SendMessage(&irc.Message{
|
---|
| 1343 | Prefix: uc.srv.prefix(),
|
---|
| 1344 | Command: msg.Command,
|
---|
| 1345 | Params: params,
|
---|
| 1346 | })
|
---|
| 1347 | })
|
---|
| 1348 | }
|
---|
[13] | 1349 | }
|
---|
[14] | 1350 | return nil
|
---|
[13] | 1351 | }
|
---|
| 1352 |
|
---|
[281] | 1353 | func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
|
---|
| 1354 | caps := strings.Fields(capsStr)
|
---|
| 1355 | for _, s := range caps {
|
---|
| 1356 | kv := strings.SplitN(s, "=", 2)
|
---|
| 1357 | k := strings.ToLower(kv[0])
|
---|
| 1358 | var v string
|
---|
| 1359 | if len(kv) == 2 {
|
---|
| 1360 | v = kv[1]
|
---|
| 1361 | }
|
---|
| 1362 | uc.supportedCaps[k] = v
|
---|
| 1363 | }
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 | func (uc *upstreamConn) requestCaps() {
|
---|
| 1367 | var requestCaps []string
|
---|
[282] | 1368 | for c := range permanentUpstreamCaps {
|
---|
[281] | 1369 | if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
|
---|
| 1370 | requestCaps = append(requestCaps, c)
|
---|
| 1371 | }
|
---|
| 1372 | }
|
---|
| 1373 |
|
---|
| 1374 | if uc.requestSASL() && !uc.caps["sasl"] {
|
---|
| 1375 | requestCaps = append(requestCaps, "sasl")
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
[282] | 1378 | if len(requestCaps) == 0 {
|
---|
| 1379 | return
|
---|
| 1380 | }
|
---|
| 1381 |
|
---|
| 1382 | uc.SendMessage(&irc.Message{
|
---|
| 1383 | Command: "CAP",
|
---|
| 1384 | Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
---|
| 1385 | })
|
---|
| 1386 | }
|
---|
| 1387 |
|
---|
| 1388 | func (uc *upstreamConn) requestSASL() bool {
|
---|
| 1389 | if uc.network.SASL.Mechanism == "" {
|
---|
| 1390 | return false
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
| 1393 | v, ok := uc.supportedCaps["sasl"]
|
---|
| 1394 | if !ok {
|
---|
| 1395 | return false
|
---|
| 1396 | }
|
---|
| 1397 | if v != "" {
|
---|
| 1398 | mechanisms := strings.Split(v, ",")
|
---|
| 1399 | found := false
|
---|
| 1400 | for _, mech := range mechanisms {
|
---|
| 1401 | if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
|
---|
| 1402 | found = true
|
---|
| 1403 | break
|
---|
| 1404 | }
|
---|
| 1405 | }
|
---|
| 1406 | if !found {
|
---|
| 1407 | return false
|
---|
| 1408 | }
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
| 1411 | return true
|
---|
| 1412 | }
|
---|
| 1413 |
|
---|
| 1414 | func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
---|
| 1415 | uc.caps[name] = ok
|
---|
| 1416 |
|
---|
| 1417 | switch name {
|
---|
| 1418 | case "sasl":
|
---|
| 1419 | if !ok {
|
---|
| 1420 | uc.logger.Printf("server refused to acknowledge the SASL capability")
|
---|
| 1421 | return nil
|
---|
| 1422 | }
|
---|
| 1423 |
|
---|
| 1424 | auth := &uc.network.SASL
|
---|
| 1425 | switch auth.Mechanism {
|
---|
| 1426 | case "PLAIN":
|
---|
| 1427 | uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
---|
| 1428 | uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
---|
[307] | 1429 | case "EXTERNAL":
|
---|
| 1430 | uc.logger.Printf("starting SASL EXTERNAL authentication")
|
---|
| 1431 | uc.saslClient = sasl.NewExternalClient("")
|
---|
[282] | 1432 | default:
|
---|
| 1433 | return fmt.Errorf("unsupported SASL mechanism %q", name)
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
[281] | 1436 | uc.SendMessage(&irc.Message{
|
---|
[282] | 1437 | Command: "AUTHENTICATE",
|
---|
| 1438 | Params: []string{auth.Mechanism},
|
---|
[281] | 1439 | })
|
---|
[282] | 1440 | default:
|
---|
| 1441 | if permanentUpstreamCaps[name] {
|
---|
| 1442 | break
|
---|
| 1443 | }
|
---|
| 1444 | uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
|
---|
[281] | 1445 | }
|
---|
[282] | 1446 | return nil
|
---|
[281] | 1447 | }
|
---|
| 1448 |
|
---|
[174] | 1449 | func splitSpace(s string) []string {
|
---|
| 1450 | return strings.FieldsFunc(s, func(r rune) bool {
|
---|
| 1451 | return r == ' '
|
---|
| 1452 | })
|
---|
| 1453 | }
|
---|
| 1454 |
|
---|
[55] | 1455 | func (uc *upstreamConn) register() {
|
---|
[77] | 1456 | uc.nick = uc.network.Nick
|
---|
| 1457 | uc.username = uc.network.Username
|
---|
| 1458 | if uc.username == "" {
|
---|
| 1459 | uc.username = uc.nick
|
---|
| 1460 | }
|
---|
| 1461 | uc.realname = uc.network.Realname
|
---|
| 1462 | if uc.realname == "" {
|
---|
| 1463 | uc.realname = uc.nick
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
[60] | 1466 | uc.SendMessage(&irc.Message{
|
---|
[92] | 1467 | Command: "CAP",
|
---|
| 1468 | Params: []string{"LS", "302"},
|
---|
| 1469 | })
|
---|
| 1470 |
|
---|
[93] | 1471 | if uc.network.Pass != "" {
|
---|
| 1472 | uc.SendMessage(&irc.Message{
|
---|
| 1473 | Command: "PASS",
|
---|
| 1474 | Params: []string{uc.network.Pass},
|
---|
| 1475 | })
|
---|
| 1476 | }
|
---|
| 1477 |
|
---|
[92] | 1478 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1479 | Command: "NICK",
|
---|
[69] | 1480 | Params: []string{uc.nick},
|
---|
[60] | 1481 | })
|
---|
| 1482 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1483 | Command: "USER",
|
---|
[77] | 1484 | Params: []string{uc.username, "0", "*", uc.realname},
|
---|
[60] | 1485 | })
|
---|
[44] | 1486 | }
|
---|
[13] | 1487 |
|
---|
[197] | 1488 | func (uc *upstreamConn) runUntilRegistered() error {
|
---|
| 1489 | for !uc.registered {
|
---|
[212] | 1490 | msg, err := uc.ReadMessage()
|
---|
[197] | 1491 | if err != nil {
|
---|
| 1492 | return fmt.Errorf("failed to read message: %v", err)
|
---|
| 1493 | }
|
---|
| 1494 |
|
---|
| 1495 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 1496 | return fmt.Errorf("failed to handle message %q: %v", msg, err)
|
---|
| 1497 | }
|
---|
| 1498 | }
|
---|
| 1499 |
|
---|
[263] | 1500 | for _, command := range uc.network.ConnectCommands {
|
---|
| 1501 | m, err := irc.ParseMessage(command)
|
---|
| 1502 | if err != nil {
|
---|
| 1503 | uc.logger.Printf("failed to parse connect command %q: %v", command, err)
|
---|
| 1504 | } else {
|
---|
| 1505 | uc.SendMessage(m)
|
---|
| 1506 | }
|
---|
| 1507 | }
|
---|
| 1508 |
|
---|
[197] | 1509 | return nil
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
[165] | 1512 | func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
---|
[13] | 1513 | for {
|
---|
[210] | 1514 | msg, err := uc.ReadMessage()
|
---|
[13] | 1515 | if err == io.EOF {
|
---|
| 1516 | break
|
---|
| 1517 | } else if err != nil {
|
---|
| 1518 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1519 | }
|
---|
| 1520 |
|
---|
[165] | 1521 | ch <- eventUpstreamMessage{msg, uc}
|
---|
[13] | 1522 | }
|
---|
| 1523 |
|
---|
[45] | 1524 | return nil
|
---|
[13] | 1525 | }
|
---|
[60] | 1526 |
|
---|
[303] | 1527 | func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
---|
| 1528 | if !uc.caps["message-tags"] {
|
---|
| 1529 | msg = msg.Copy()
|
---|
| 1530 | msg.Tags = nil
|
---|
| 1531 | }
|
---|
| 1532 |
|
---|
| 1533 | uc.conn.SendMessage(msg)
|
---|
| 1534 | }
|
---|
| 1535 |
|
---|
[176] | 1536 | func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
---|
[278] | 1537 | if uc.caps["labeled-response"] {
|
---|
[155] | 1538 | if msg.Tags == nil {
|
---|
| 1539 | msg.Tags = make(map[string]irc.TagValue)
|
---|
| 1540 | }
|
---|
[176] | 1541 | msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
---|
[161] | 1542 | uc.nextLabelID++
|
---|
[155] | 1543 | }
|
---|
| 1544 | uc.SendMessage(msg)
|
---|
| 1545 | }
|
---|
[178] | 1546 |
|
---|
| 1547 | // TODO: handle moving logs when a network name changes, when support for this is added
|
---|
[215] | 1548 | func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) {
|
---|
[178] | 1549 | if uc.srv.LogPath == "" {
|
---|
| 1550 | return
|
---|
| 1551 | }
|
---|
[215] | 1552 |
|
---|
| 1553 | ml, ok := uc.messageLoggers[entity]
|
---|
| 1554 | if !ok {
|
---|
[248] | 1555 | ml = newMessageLogger(uc.network, entity)
|
---|
[215] | 1556 | uc.messageLoggers[entity] = ml
|
---|
[178] | 1557 | }
|
---|
| 1558 |
|
---|
[215] | 1559 | if err := ml.Append(msg); err != nil {
|
---|
| 1560 | uc.logger.Printf("failed to log message: %v", err)
|
---|
[178] | 1561 | }
|
---|
| 1562 | }
|
---|
[198] | 1563 |
|
---|
[253] | 1564 | // appendHistory appends a message to the history. entity can be empty.
|
---|
| 1565 | func (uc *upstreamConn) appendHistory(entity string, msg *irc.Message) {
|
---|
[284] | 1566 | detached := false
|
---|
| 1567 | if ch, ok := uc.network.channels[entity]; ok {
|
---|
| 1568 | detached = ch.Detached
|
---|
| 1569 | }
|
---|
| 1570 |
|
---|
[253] | 1571 | // If no client is offline, no need to append the message to the buffer
|
---|
[284] | 1572 | if len(uc.network.offlineClients) == 0 && !detached {
|
---|
[253] | 1573 | return
|
---|
| 1574 | }
|
---|
| 1575 |
|
---|
| 1576 | history, ok := uc.network.history[entity]
|
---|
| 1577 | if !ok {
|
---|
| 1578 | history = &networkHistory{
|
---|
| 1579 | offlineClients: make(map[string]uint64),
|
---|
| 1580 | ring: NewRing(uc.srv.RingCap),
|
---|
| 1581 | }
|
---|
| 1582 | uc.network.history[entity] = history
|
---|
| 1583 |
|
---|
| 1584 | for clientName, _ := range uc.network.offlineClients {
|
---|
| 1585 | history.offlineClients[clientName] = 0
|
---|
| 1586 | }
|
---|
[284] | 1587 |
|
---|
| 1588 | if detached {
|
---|
| 1589 | // If the channel is detached, online clients act as offline
|
---|
| 1590 | // clients too
|
---|
| 1591 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1592 | history.offlineClients[dc.clientName] = 0
|
---|
| 1593 | })
|
---|
| 1594 | }
|
---|
[253] | 1595 | }
|
---|
| 1596 |
|
---|
| 1597 | history.ring.Produce(msg)
|
---|
| 1598 | }
|
---|
| 1599 |
|
---|
[245] | 1600 | // produce appends a message to the logs, adds it to the history and forwards
|
---|
| 1601 | // it to connected downstream connections.
|
---|
| 1602 | //
|
---|
| 1603 | // If origin is not nil and origin doesn't support echo-message, the message is
|
---|
| 1604 | // forwarded to all connections except origin.
|
---|
[239] | 1605 | func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
|
---|
| 1606 | if target != "" {
|
---|
| 1607 | uc.appendLog(target, msg)
|
---|
| 1608 | }
|
---|
| 1609 |
|
---|
[253] | 1610 | uc.appendHistory(target, msg)
|
---|
[227] | 1611 |
|
---|
[284] | 1612 | // Don't forward messages if it's a detached channel
|
---|
| 1613 | if ch, ok := uc.network.channels[target]; ok && ch.Detached {
|
---|
| 1614 | return
|
---|
| 1615 | }
|
---|
| 1616 |
|
---|
[227] | 1617 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[238] | 1618 | if dc != origin || dc.caps["echo-message"] {
|
---|
[261] | 1619 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
[238] | 1620 | }
|
---|
[227] | 1621 | })
|
---|
[226] | 1622 | }
|
---|
| 1623 |
|
---|
[198] | 1624 | func (uc *upstreamConn) updateAway() {
|
---|
| 1625 | away := true
|
---|
| 1626 | uc.forEachDownstream(func(*downstreamConn) {
|
---|
| 1627 | away = false
|
---|
| 1628 | })
|
---|
| 1629 | if away == uc.away {
|
---|
| 1630 | return
|
---|
| 1631 | }
|
---|
| 1632 | if away {
|
---|
| 1633 | uc.SendMessage(&irc.Message{
|
---|
| 1634 | Command: "AWAY",
|
---|
| 1635 | Params: []string{"Auto away"},
|
---|
| 1636 | })
|
---|
| 1637 | } else {
|
---|
| 1638 | uc.SendMessage(&irc.Message{
|
---|
| 1639 | Command: "AWAY",
|
---|
| 1640 | })
|
---|
| 1641 | }
|
---|
| 1642 | uc.away = away
|
---|
| 1643 | }
|
---|