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