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