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