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