[98] | 1 | package soju
|
---|
[20] | 2 |
|
---|
| 3 | import (
|
---|
| 4 | "fmt"
|
---|
[350] | 5 | "sort"
|
---|
[20] | 6 | "strings"
|
---|
[516] | 7 | "time"
|
---|
[498] | 8 | "unicode"
|
---|
| 9 | "unicode/utf8"
|
---|
[43] | 10 |
|
---|
| 11 | "gopkg.in/irc.v3"
|
---|
[20] | 12 | )
|
---|
| 13 |
|
---|
| 14 | const (
|
---|
[108] | 15 | rpl_statsping = "246"
|
---|
| 16 | rpl_localusers = "265"
|
---|
| 17 | rpl_globalusers = "266"
|
---|
[162] | 18 | rpl_creationtime = "329"
|
---|
[108] | 19 | rpl_topicwhotime = "333"
|
---|
| 20 | err_invalidcapcmd = "410"
|
---|
[20] | 21 | )
|
---|
| 22 |
|
---|
[463] | 23 | const (
|
---|
| 24 | maxMessageLength = 512
|
---|
| 25 | maxMessageParams = 15
|
---|
| 26 | )
|
---|
[346] | 27 |
|
---|
[350] | 28 | // The server-time layout, as defined in the IRCv3 spec.
|
---|
| 29 | const serverTimeLayout = "2006-01-02T15:04:05.000Z"
|
---|
| 30 |
|
---|
[139] | 31 | type userModes string
|
---|
[20] | 32 |
|
---|
[139] | 33 | func (ms userModes) Has(c byte) bool {
|
---|
[20] | 34 | return strings.IndexByte(string(ms), c) >= 0
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[139] | 37 | func (ms *userModes) Add(c byte) {
|
---|
[20] | 38 | if !ms.Has(c) {
|
---|
[139] | 39 | *ms += userModes(c)
|
---|
[20] | 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[139] | 43 | func (ms *userModes) Del(c byte) {
|
---|
[20] | 44 | i := strings.IndexByte(string(*ms), c)
|
---|
| 45 | if i >= 0 {
|
---|
| 46 | *ms = (*ms)[:i] + (*ms)[i+1:]
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[139] | 50 | func (ms *userModes) Apply(s string) error {
|
---|
[20] | 51 | var plusMinus byte
|
---|
| 52 | for i := 0; i < len(s); i++ {
|
---|
| 53 | switch c := s[i]; c {
|
---|
| 54 | case '+', '-':
|
---|
| 55 | plusMinus = c
|
---|
| 56 | default:
|
---|
| 57 | switch plusMinus {
|
---|
| 58 | case '+':
|
---|
| 59 | ms.Add(c)
|
---|
| 60 | case '-':
|
---|
| 61 | ms.Del(c)
|
---|
| 62 | default:
|
---|
| 63 | return fmt.Errorf("malformed modestring %q: missing plus/minus", s)
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return nil
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[139] | 70 | type channelModeType byte
|
---|
| 71 |
|
---|
| 72 | // standard channel mode types, as explained in https://modern.ircdocs.horse/#mode-message
|
---|
| 73 | const (
|
---|
| 74 | // modes that add or remove an address to or from a list
|
---|
| 75 | modeTypeA channelModeType = iota
|
---|
| 76 | // modes that change a setting on a channel, and must always have a parameter
|
---|
| 77 | modeTypeB
|
---|
| 78 | // modes that change a setting on a channel, and must have a parameter when being set, and no parameter when being unset
|
---|
| 79 | modeTypeC
|
---|
| 80 | // modes that change a setting on a channel, and must not have a parameter
|
---|
| 81 | modeTypeD
|
---|
| 82 | )
|
---|
| 83 |
|
---|
| 84 | var stdChannelModes = map[byte]channelModeType{
|
---|
| 85 | 'b': modeTypeA, // ban list
|
---|
| 86 | 'e': modeTypeA, // ban exception list
|
---|
| 87 | 'I': modeTypeA, // invite exception list
|
---|
| 88 | 'k': modeTypeB, // channel key
|
---|
| 89 | 'l': modeTypeC, // channel user limit
|
---|
| 90 | 'i': modeTypeD, // channel is invite-only
|
---|
| 91 | 'm': modeTypeD, // channel is moderated
|
---|
| 92 | 'n': modeTypeD, // channel has no external messages
|
---|
| 93 | 's': modeTypeD, // channel is secret
|
---|
| 94 | 't': modeTypeD, // channel has protected topic
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | type channelModes map[byte]string
|
---|
| 98 |
|
---|
[293] | 99 | // applyChannelModes parses a mode string and mode arguments from a MODE message,
|
---|
| 100 | // and applies the corresponding channel mode and user membership changes on that channel.
|
---|
| 101 | //
|
---|
| 102 | // If ch.modes is nil, channel modes are not updated.
|
---|
| 103 | //
|
---|
| 104 | // needMarshaling is a list of indexes of mode arguments that represent entities
|
---|
| 105 | // that must be marshaled when sent downstream.
|
---|
| 106 | func applyChannelModes(ch *upstreamChannel, modeStr string, arguments []string) (needMarshaling map[int]struct{}, err error) {
|
---|
| 107 | needMarshaling = make(map[int]struct{}, len(arguments))
|
---|
[139] | 108 | nextArgument := 0
|
---|
| 109 | var plusMinus byte
|
---|
[293] | 110 | outer:
|
---|
[139] | 111 | for i := 0; i < len(modeStr); i++ {
|
---|
| 112 | mode := modeStr[i]
|
---|
| 113 | if mode == '+' || mode == '-' {
|
---|
| 114 | plusMinus = mode
|
---|
| 115 | continue
|
---|
| 116 | }
|
---|
| 117 | if plusMinus != '+' && plusMinus != '-' {
|
---|
[293] | 118 | return nil, fmt.Errorf("malformed modestring %q: missing plus/minus", modeStr)
|
---|
[139] | 119 | }
|
---|
| 120 |
|
---|
[293] | 121 | for _, membership := range ch.conn.availableMemberships {
|
---|
| 122 | if membership.Mode == mode {
|
---|
| 123 | if nextArgument >= len(arguments) {
|
---|
| 124 | return nil, fmt.Errorf("malformed modestring %q: missing mode argument for %c%c", modeStr, plusMinus, mode)
|
---|
| 125 | }
|
---|
| 126 | member := arguments[nextArgument]
|
---|
[478] | 127 | m := ch.Members.Value(member)
|
---|
| 128 | if m != nil {
|
---|
[293] | 129 | if plusMinus == '+' {
|
---|
[478] | 130 | m.Add(ch.conn.availableMemberships, membership)
|
---|
[293] | 131 | } else {
|
---|
| 132 | // TODO: for upstreams without multi-prefix, query the user modes again
|
---|
[478] | 133 | m.Remove(membership)
|
---|
[293] | 134 | }
|
---|
| 135 | }
|
---|
| 136 | needMarshaling[nextArgument] = struct{}{}
|
---|
| 137 | nextArgument++
|
---|
| 138 | continue outer
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | mt, ok := ch.conn.availableChannelModes[mode]
|
---|
[139] | 143 | if !ok {
|
---|
| 144 | continue
|
---|
| 145 | }
|
---|
| 146 | if mt == modeTypeB || (mt == modeTypeC && plusMinus == '+') {
|
---|
| 147 | if plusMinus == '+' {
|
---|
| 148 | var argument string
|
---|
| 149 | // some sentitive arguments (such as channel keys) can be omitted for privacy
|
---|
| 150 | // (this will only happen for RPL_CHANNELMODEIS, never for MODE messages)
|
---|
| 151 | if nextArgument < len(arguments) {
|
---|
| 152 | argument = arguments[nextArgument]
|
---|
| 153 | }
|
---|
[293] | 154 | if ch.modes != nil {
|
---|
| 155 | ch.modes[mode] = argument
|
---|
| 156 | }
|
---|
[139] | 157 | } else {
|
---|
[293] | 158 | delete(ch.modes, mode)
|
---|
[139] | 159 | }
|
---|
| 160 | nextArgument++
|
---|
| 161 | } else if mt == modeTypeC || mt == modeTypeD {
|
---|
| 162 | if plusMinus == '+' {
|
---|
[293] | 163 | if ch.modes != nil {
|
---|
| 164 | ch.modes[mode] = ""
|
---|
| 165 | }
|
---|
[139] | 166 | } else {
|
---|
[293] | 167 | delete(ch.modes, mode)
|
---|
[139] | 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
[293] | 171 | return needMarshaling, nil
|
---|
[139] | 172 | }
|
---|
| 173 |
|
---|
| 174 | func (cm channelModes) Format() (modeString string, parameters []string) {
|
---|
| 175 | var modesWithValues strings.Builder
|
---|
| 176 | var modesWithoutValues strings.Builder
|
---|
| 177 | parameters = make([]string, 0, 16)
|
---|
| 178 | for mode, value := range cm {
|
---|
| 179 | if value != "" {
|
---|
| 180 | modesWithValues.WriteString(string(mode))
|
---|
| 181 | parameters = append(parameters, value)
|
---|
| 182 | } else {
|
---|
| 183 | modesWithoutValues.WriteString(string(mode))
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | modeString = "+" + modesWithValues.String() + modesWithoutValues.String()
|
---|
| 187 | return
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | const stdChannelTypes = "#&+!"
|
---|
| 191 |
|
---|
[20] | 192 | type channelStatus byte
|
---|
| 193 |
|
---|
| 194 | const (
|
---|
| 195 | channelPublic channelStatus = '='
|
---|
| 196 | channelSecret channelStatus = '@'
|
---|
| 197 | channelPrivate channelStatus = '*'
|
---|
| 198 | )
|
---|
| 199 |
|
---|
| 200 | func parseChannelStatus(s string) (channelStatus, error) {
|
---|
| 201 | if len(s) > 1 {
|
---|
| 202 | return 0, fmt.Errorf("invalid channel status %q: more than one character", s)
|
---|
| 203 | }
|
---|
| 204 | switch cs := channelStatus(s[0]); cs {
|
---|
| 205 | case channelPublic, channelSecret, channelPrivate:
|
---|
| 206 | return cs, nil
|
---|
| 207 | default:
|
---|
| 208 | return 0, fmt.Errorf("invalid channel status %q: unknown status", s)
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[139] | 212 | type membership struct {
|
---|
| 213 | Mode byte
|
---|
| 214 | Prefix byte
|
---|
| 215 | }
|
---|
[20] | 216 |
|
---|
[139] | 217 | var stdMemberships = []membership{
|
---|
| 218 | {'q', '~'}, // founder
|
---|
| 219 | {'a', '&'}, // protected
|
---|
| 220 | {'o', '@'}, // operator
|
---|
| 221 | {'h', '%'}, // halfop
|
---|
| 222 | {'v', '+'}, // voice
|
---|
| 223 | }
|
---|
[20] | 224 |
|
---|
[292] | 225 | // memberships always sorted by descending membership rank
|
---|
| 226 | type memberships []membership
|
---|
| 227 |
|
---|
| 228 | func (m *memberships) Add(availableMemberships []membership, newMembership membership) {
|
---|
| 229 | l := *m
|
---|
| 230 | i := 0
|
---|
| 231 | for _, availableMembership := range availableMemberships {
|
---|
| 232 | if i >= len(l) {
|
---|
| 233 | break
|
---|
| 234 | }
|
---|
| 235 | if l[i] == availableMembership {
|
---|
| 236 | if availableMembership == newMembership {
|
---|
| 237 | // we already have this membership
|
---|
| 238 | return
|
---|
| 239 | }
|
---|
| 240 | i++
|
---|
| 241 | continue
|
---|
| 242 | }
|
---|
| 243 | if availableMembership == newMembership {
|
---|
| 244 | break
|
---|
| 245 | }
|
---|
[128] | 246 | }
|
---|
[292] | 247 | // insert newMembership at i
|
---|
| 248 | l = append(l, membership{})
|
---|
| 249 | copy(l[i+1:], l[i:])
|
---|
| 250 | l[i] = newMembership
|
---|
| 251 | *m = l
|
---|
[128] | 252 | }
|
---|
| 253 |
|
---|
[292] | 254 | func (m *memberships) Remove(oldMembership membership) {
|
---|
| 255 | l := *m
|
---|
| 256 | for i, currentMembership := range l {
|
---|
| 257 | if currentMembership == oldMembership {
|
---|
| 258 | *m = append(l[:i], l[i+1:]...)
|
---|
| 259 | return
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | func (m memberships) Format(dc *downstreamConn) string {
|
---|
| 265 | if !dc.caps["multi-prefix"] {
|
---|
| 266 | if len(m) == 0 {
|
---|
| 267 | return ""
|
---|
| 268 | }
|
---|
| 269 | return string(m[0].Prefix)
|
---|
| 270 | }
|
---|
| 271 | prefixes := make([]byte, len(m))
|
---|
| 272 | for i, membership := range m {
|
---|
| 273 | prefixes[i] = membership.Prefix
|
---|
| 274 | }
|
---|
| 275 | return string(prefixes)
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[43] | 278 | func parseMessageParams(msg *irc.Message, out ...*string) error {
|
---|
| 279 | if len(msg.Params) < len(out) {
|
---|
| 280 | return newNeedMoreParamsError(msg.Command)
|
---|
| 281 | }
|
---|
| 282 | for i := range out {
|
---|
| 283 | if out[i] != nil {
|
---|
| 284 | *out[i] = msg.Params[i]
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | return nil
|
---|
| 288 | }
|
---|
[153] | 289 |
|
---|
[303] | 290 | func copyClientTags(tags irc.Tags) irc.Tags {
|
---|
| 291 | t := make(irc.Tags, len(tags))
|
---|
| 292 | for k, v := range tags {
|
---|
| 293 | if strings.HasPrefix(k, "+") {
|
---|
| 294 | t[k] = v
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | return t
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[153] | 300 | type batch struct {
|
---|
| 301 | Type string
|
---|
| 302 | Params []string
|
---|
| 303 | Outer *batch // if not-nil, this batch is nested in Outer
|
---|
[155] | 304 | Label string
|
---|
[153] | 305 | }
|
---|
[193] | 306 |
|
---|
[350] | 307 | func join(channels, keys []string) []*irc.Message {
|
---|
| 308 | // Put channels with a key first
|
---|
| 309 | js := joinSorter{channels, keys}
|
---|
| 310 | sort.Sort(&js)
|
---|
| 311 |
|
---|
| 312 | // Two spaces because there are three words (JOIN, channels and keys)
|
---|
| 313 | maxLength := maxMessageLength - (len("JOIN") + 2)
|
---|
| 314 |
|
---|
| 315 | var msgs []*irc.Message
|
---|
| 316 | var channelsBuf, keysBuf strings.Builder
|
---|
| 317 | for i, channel := range channels {
|
---|
| 318 | key := keys[i]
|
---|
| 319 |
|
---|
| 320 | n := channelsBuf.Len() + keysBuf.Len() + 1 + len(channel)
|
---|
| 321 | if key != "" {
|
---|
| 322 | n += 1 + len(key)
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | if channelsBuf.Len() > 0 && n > maxLength {
|
---|
| 326 | // No room for the new channel in this message
|
---|
| 327 | params := []string{channelsBuf.String()}
|
---|
| 328 | if keysBuf.Len() > 0 {
|
---|
| 329 | params = append(params, keysBuf.String())
|
---|
| 330 | }
|
---|
| 331 | msgs = append(msgs, &irc.Message{Command: "JOIN", Params: params})
|
---|
| 332 | channelsBuf.Reset()
|
---|
| 333 | keysBuf.Reset()
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | if channelsBuf.Len() > 0 {
|
---|
| 337 | channelsBuf.WriteByte(',')
|
---|
| 338 | }
|
---|
| 339 | channelsBuf.WriteString(channel)
|
---|
| 340 | if key != "" {
|
---|
| 341 | if keysBuf.Len() > 0 {
|
---|
| 342 | keysBuf.WriteByte(',')
|
---|
| 343 | }
|
---|
| 344 | keysBuf.WriteString(key)
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | if channelsBuf.Len() > 0 {
|
---|
| 348 | params := []string{channelsBuf.String()}
|
---|
| 349 | if keysBuf.Len() > 0 {
|
---|
| 350 | params = append(params, keysBuf.String())
|
---|
| 351 | }
|
---|
| 352 | msgs = append(msgs, &irc.Message{Command: "JOIN", Params: params})
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | return msgs
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[463] | 358 | func generateIsupport(prefix *irc.Prefix, nick string, tokens []string) []*irc.Message {
|
---|
| 359 | maxTokens := maxMessageParams - 2 // 2 reserved params: nick + text
|
---|
| 360 |
|
---|
| 361 | var msgs []*irc.Message
|
---|
| 362 | for len(tokens) > 0 {
|
---|
| 363 | var msgTokens []string
|
---|
| 364 | if len(tokens) > maxTokens {
|
---|
| 365 | msgTokens = tokens[:maxTokens]
|
---|
| 366 | tokens = tokens[maxTokens:]
|
---|
| 367 | } else {
|
---|
| 368 | msgTokens = tokens
|
---|
| 369 | tokens = nil
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | msgs = append(msgs, &irc.Message{
|
---|
| 373 | Prefix: prefix,
|
---|
| 374 | Command: irc.RPL_ISUPPORT,
|
---|
| 375 | Params: append(append([]string{nick}, msgTokens...), "are supported"),
|
---|
| 376 | })
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | return msgs
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[636] | 382 | func generateMOTD(prefix *irc.Prefix, nick string, motd string) []*irc.Message {
|
---|
| 383 | var msgs []*irc.Message
|
---|
| 384 | msgs = append(msgs, &irc.Message{
|
---|
| 385 | Prefix: prefix,
|
---|
| 386 | Command: irc.RPL_MOTDSTART,
|
---|
| 387 | Params: []string{nick, fmt.Sprintf("- Message of the Day -")},
|
---|
| 388 | })
|
---|
| 389 |
|
---|
| 390 | for _, l := range strings.Split(motd, "\n") {
|
---|
| 391 | msgs = append(msgs, &irc.Message{
|
---|
| 392 | Prefix: prefix,
|
---|
| 393 | Command: irc.RPL_MOTD,
|
---|
| 394 | Params: []string{nick, l},
|
---|
| 395 | })
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | msgs = append(msgs, &irc.Message{
|
---|
| 399 | Prefix: prefix,
|
---|
| 400 | Command: irc.RPL_ENDOFMOTD,
|
---|
| 401 | Params: []string{nick, "End of /MOTD command."},
|
---|
| 402 | })
|
---|
| 403 |
|
---|
| 404 | return msgs
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[350] | 407 | type joinSorter struct {
|
---|
| 408 | channels []string
|
---|
| 409 | keys []string
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | func (js *joinSorter) Len() int {
|
---|
| 413 | return len(js.channels)
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | func (js *joinSorter) Less(i, j int) bool {
|
---|
| 417 | if (js.keys[i] != "") != (js.keys[j] != "") {
|
---|
| 418 | // Only one of the channels has a key
|
---|
| 419 | return js.keys[i] != ""
|
---|
| 420 | }
|
---|
| 421 | return js.channels[i] < js.channels[j]
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | func (js *joinSorter) Swap(i, j int) {
|
---|
| 425 | js.channels[i], js.channels[j] = js.channels[j], js.channels[i]
|
---|
| 426 | js.keys[i], js.keys[j] = js.keys[j], js.keys[i]
|
---|
| 427 | }
|
---|
[392] | 428 |
|
---|
| 429 | // parseCTCPMessage parses a CTCP message. CTCP is defined in
|
---|
| 430 | // https://tools.ietf.org/html/draft-oakley-irc-ctcp-02
|
---|
| 431 | func parseCTCPMessage(msg *irc.Message) (cmd string, params string, ok bool) {
|
---|
| 432 | if (msg.Command != "PRIVMSG" && msg.Command != "NOTICE") || len(msg.Params) < 2 {
|
---|
| 433 | return "", "", false
|
---|
| 434 | }
|
---|
| 435 | text := msg.Params[1]
|
---|
| 436 |
|
---|
| 437 | if !strings.HasPrefix(text, "\x01") {
|
---|
| 438 | return "", "", false
|
---|
| 439 | }
|
---|
| 440 | text = strings.Trim(text, "\x01")
|
---|
| 441 |
|
---|
| 442 | words := strings.SplitN(text, " ", 2)
|
---|
| 443 | cmd = strings.ToUpper(words[0])
|
---|
| 444 | if len(words) > 1 {
|
---|
| 445 | params = words[1]
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | return cmd, params, true
|
---|
| 449 | }
|
---|
[478] | 450 |
|
---|
| 451 | type casemapping func(string) string
|
---|
| 452 |
|
---|
| 453 | func casemapNone(name string) string {
|
---|
| 454 | return name
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | // CasemapASCII of name is the canonical representation of name according to the
|
---|
| 458 | // ascii casemapping.
|
---|
| 459 | func casemapASCII(name string) string {
|
---|
[492] | 460 | nameBytes := []byte(name)
|
---|
| 461 | for i, r := range nameBytes {
|
---|
[478] | 462 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 463 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 464 | }
|
---|
| 465 | }
|
---|
[492] | 466 | return string(nameBytes)
|
---|
[478] | 467 | }
|
---|
| 468 |
|
---|
| 469 | // casemapRFC1459 of name is the canonical representation of name according to the
|
---|
| 470 | // rfc1459 casemapping.
|
---|
| 471 | func casemapRFC1459(name string) string {
|
---|
[492] | 472 | nameBytes := []byte(name)
|
---|
| 473 | for i, r := range nameBytes {
|
---|
[478] | 474 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 475 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 476 | } else if r == '{' {
|
---|
[492] | 477 | nameBytes[i] = '['
|
---|
[478] | 478 | } else if r == '}' {
|
---|
[492] | 479 | nameBytes[i] = ']'
|
---|
[478] | 480 | } else if r == '\\' {
|
---|
[492] | 481 | nameBytes[i] = '|'
|
---|
[478] | 482 | } else if r == '~' {
|
---|
[492] | 483 | nameBytes[i] = '^'
|
---|
[478] | 484 | }
|
---|
| 485 | }
|
---|
[492] | 486 | return string(nameBytes)
|
---|
[478] | 487 | }
|
---|
| 488 |
|
---|
| 489 | // casemapRFC1459Strict of name is the canonical representation of name
|
---|
| 490 | // according to the rfc1459-strict casemapping.
|
---|
| 491 | func casemapRFC1459Strict(name string) string {
|
---|
[492] | 492 | nameBytes := []byte(name)
|
---|
| 493 | for i, r := range nameBytes {
|
---|
[478] | 494 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 495 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 496 | } else if r == '{' {
|
---|
[492] | 497 | nameBytes[i] = '['
|
---|
[478] | 498 | } else if r == '}' {
|
---|
[492] | 499 | nameBytes[i] = ']'
|
---|
[478] | 500 | } else if r == '\\' {
|
---|
[492] | 501 | nameBytes[i] = '|'
|
---|
[478] | 502 | }
|
---|
| 503 | }
|
---|
[492] | 504 | return string(nameBytes)
|
---|
[478] | 505 | }
|
---|
| 506 |
|
---|
| 507 | func parseCasemappingToken(tokenValue string) (casemap casemapping, ok bool) {
|
---|
| 508 | switch tokenValue {
|
---|
| 509 | case "ascii":
|
---|
| 510 | casemap = casemapASCII
|
---|
| 511 | case "rfc1459":
|
---|
| 512 | casemap = casemapRFC1459
|
---|
| 513 | case "rfc1459-strict":
|
---|
| 514 | casemap = casemapRFC1459Strict
|
---|
| 515 | default:
|
---|
| 516 | return nil, false
|
---|
| 517 | }
|
---|
| 518 | return casemap, true
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | func partialCasemap(higher casemapping, name string) string {
|
---|
[492] | 522 | nameFullyCM := []byte(higher(name))
|
---|
| 523 | nameBytes := []byte(name)
|
---|
| 524 | for i, r := range nameBytes {
|
---|
| 525 | if !('A' <= r && r <= 'Z') && !('a' <= r && r <= 'z') {
|
---|
| 526 | nameBytes[i] = nameFullyCM[i]
|
---|
[478] | 527 | }
|
---|
| 528 | }
|
---|
[492] | 529 | return string(nameBytes)
|
---|
[478] | 530 | }
|
---|
| 531 |
|
---|
| 532 | type casemapMap struct {
|
---|
| 533 | innerMap map[string]casemapEntry
|
---|
| 534 | casemap casemapping
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | type casemapEntry struct {
|
---|
| 538 | originalKey string
|
---|
| 539 | value interface{}
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | func newCasemapMap(size int) casemapMap {
|
---|
| 543 | return casemapMap{
|
---|
| 544 | innerMap: make(map[string]casemapEntry, size),
|
---|
| 545 | casemap: casemapNone,
|
---|
| 546 | }
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | func (cm *casemapMap) OriginalKey(name string) (key string, ok bool) {
|
---|
| 550 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 551 | if !ok {
|
---|
| 552 | return "", false
|
---|
| 553 | }
|
---|
| 554 | return entry.originalKey, true
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | func (cm *casemapMap) Has(name string) bool {
|
---|
| 558 | _, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 559 | return ok
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | func (cm *casemapMap) Len() int {
|
---|
| 563 | return len(cm.innerMap)
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | func (cm *casemapMap) SetValue(name string, value interface{}) {
|
---|
| 567 | nameCM := cm.casemap(name)
|
---|
| 568 | entry, ok := cm.innerMap[nameCM]
|
---|
| 569 | if !ok {
|
---|
| 570 | cm.innerMap[nameCM] = casemapEntry{
|
---|
| 571 | originalKey: name,
|
---|
| 572 | value: value,
|
---|
| 573 | }
|
---|
| 574 | return
|
---|
| 575 | }
|
---|
| 576 | entry.value = value
|
---|
| 577 | cm.innerMap[nameCM] = entry
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | func (cm *casemapMap) Delete(name string) {
|
---|
| 581 | delete(cm.innerMap, cm.casemap(name))
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | func (cm *casemapMap) SetCasemapping(newCasemap casemapping) {
|
---|
| 585 | cm.casemap = newCasemap
|
---|
| 586 | newInnerMap := make(map[string]casemapEntry, len(cm.innerMap))
|
---|
| 587 | for _, entry := range cm.innerMap {
|
---|
| 588 | newInnerMap[cm.casemap(entry.originalKey)] = entry
|
---|
| 589 | }
|
---|
| 590 | cm.innerMap = newInnerMap
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | type upstreamChannelCasemapMap struct{ casemapMap }
|
---|
| 594 |
|
---|
| 595 | func (cm *upstreamChannelCasemapMap) Value(name string) *upstreamChannel {
|
---|
| 596 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 597 | if !ok {
|
---|
| 598 | return nil
|
---|
| 599 | }
|
---|
| 600 | return entry.value.(*upstreamChannel)
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | type channelCasemapMap struct{ casemapMap }
|
---|
| 604 |
|
---|
| 605 | func (cm *channelCasemapMap) Value(name string) *Channel {
|
---|
| 606 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 607 | if !ok {
|
---|
| 608 | return nil
|
---|
| 609 | }
|
---|
| 610 | return entry.value.(*Channel)
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | type membershipsCasemapMap struct{ casemapMap }
|
---|
| 614 |
|
---|
| 615 | func (cm *membershipsCasemapMap) Value(name string) *memberships {
|
---|
| 616 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 617 | if !ok {
|
---|
| 618 | return nil
|
---|
| 619 | }
|
---|
| 620 | return entry.value.(*memberships)
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[480] | 623 | type deliveredCasemapMap struct{ casemapMap }
|
---|
[478] | 624 |
|
---|
[480] | 625 | func (cm *deliveredCasemapMap) Value(name string) deliveredClientMap {
|
---|
[478] | 626 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 627 | if !ok {
|
---|
| 628 | return nil
|
---|
| 629 | }
|
---|
[480] | 630 | return entry.value.(deliveredClientMap)
|
---|
[478] | 631 | }
|
---|
[498] | 632 |
|
---|
| 633 | func isWordBoundary(r rune) bool {
|
---|
| 634 | switch r {
|
---|
| 635 | case '-', '_', '|':
|
---|
| 636 | return false
|
---|
| 637 | case '\u00A0':
|
---|
| 638 | return true
|
---|
| 639 | default:
|
---|
| 640 | return !unicode.IsLetter(r) && !unicode.IsNumber(r)
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | func isHighlight(text, nick string) bool {
|
---|
| 645 | for {
|
---|
| 646 | i := strings.Index(text, nick)
|
---|
| 647 | if i < 0 {
|
---|
| 648 | return false
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | // Detect word boundaries
|
---|
| 652 | var left, right rune
|
---|
| 653 | if i > 0 {
|
---|
| 654 | left, _ = utf8.DecodeLastRuneInString(text[:i])
|
---|
| 655 | }
|
---|
| 656 | if i < len(text) {
|
---|
| 657 | right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
|
---|
| 658 | }
|
---|
| 659 | if isWordBoundary(left) && isWordBoundary(right) {
|
---|
| 660 | return true
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | text = text[i+len(nick):]
|
---|
| 664 | }
|
---|
| 665 | }
|
---|
[516] | 666 |
|
---|
| 667 | // parseChatHistoryBound parses the given CHATHISTORY parameter as a bound.
|
---|
| 668 | // The zero time is returned on error.
|
---|
| 669 | func parseChatHistoryBound(param string) time.Time {
|
---|
| 670 | parts := strings.SplitN(param, "=", 2)
|
---|
| 671 | if len(parts) != 2 {
|
---|
| 672 | return time.Time{}
|
---|
| 673 | }
|
---|
| 674 | switch parts[0] {
|
---|
| 675 | case "timestamp":
|
---|
| 676 | timestamp, err := time.Parse(serverTimeLayout, parts[1])
|
---|
| 677 | if err != nil {
|
---|
| 678 | return time.Time{}
|
---|
| 679 | }
|
---|
| 680 | return timestamp
|
---|
| 681 | default:
|
---|
| 682 | return time.Time{}
|
---|
| 683 | }
|
---|
| 684 | }
|
---|