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