[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 |
|
---|
[350] | 382 | type joinSorter struct {
|
---|
| 383 | channels []string
|
---|
| 384 | keys []string
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | func (js *joinSorter) Len() int {
|
---|
| 388 | return len(js.channels)
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | func (js *joinSorter) Less(i, j int) bool {
|
---|
| 392 | if (js.keys[i] != "") != (js.keys[j] != "") {
|
---|
| 393 | // Only one of the channels has a key
|
---|
| 394 | return js.keys[i] != ""
|
---|
| 395 | }
|
---|
| 396 | return js.channels[i] < js.channels[j]
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | func (js *joinSorter) Swap(i, j int) {
|
---|
| 400 | js.channels[i], js.channels[j] = js.channels[j], js.channels[i]
|
---|
| 401 | js.keys[i], js.keys[j] = js.keys[j], js.keys[i]
|
---|
| 402 | }
|
---|
[392] | 403 |
|
---|
| 404 | // parseCTCPMessage parses a CTCP message. CTCP is defined in
|
---|
| 405 | // https://tools.ietf.org/html/draft-oakley-irc-ctcp-02
|
---|
| 406 | func parseCTCPMessage(msg *irc.Message) (cmd string, params string, ok bool) {
|
---|
| 407 | if (msg.Command != "PRIVMSG" && msg.Command != "NOTICE") || len(msg.Params) < 2 {
|
---|
| 408 | return "", "", false
|
---|
| 409 | }
|
---|
| 410 | text := msg.Params[1]
|
---|
| 411 |
|
---|
| 412 | if !strings.HasPrefix(text, "\x01") {
|
---|
| 413 | return "", "", false
|
---|
| 414 | }
|
---|
| 415 | text = strings.Trim(text, "\x01")
|
---|
| 416 |
|
---|
| 417 | words := strings.SplitN(text, " ", 2)
|
---|
| 418 | cmd = strings.ToUpper(words[0])
|
---|
| 419 | if len(words) > 1 {
|
---|
| 420 | params = words[1]
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | return cmd, params, true
|
---|
| 424 | }
|
---|
[478] | 425 |
|
---|
| 426 | type casemapping func(string) string
|
---|
| 427 |
|
---|
| 428 | func casemapNone(name string) string {
|
---|
| 429 | return name
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | // CasemapASCII of name is the canonical representation of name according to the
|
---|
| 433 | // ascii casemapping.
|
---|
| 434 | func casemapASCII(name string) string {
|
---|
[492] | 435 | nameBytes := []byte(name)
|
---|
| 436 | for i, r := range nameBytes {
|
---|
[478] | 437 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 438 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 439 | }
|
---|
| 440 | }
|
---|
[492] | 441 | return string(nameBytes)
|
---|
[478] | 442 | }
|
---|
| 443 |
|
---|
| 444 | // casemapRFC1459 of name is the canonical representation of name according to the
|
---|
| 445 | // rfc1459 casemapping.
|
---|
| 446 | func casemapRFC1459(name string) string {
|
---|
[492] | 447 | nameBytes := []byte(name)
|
---|
| 448 | for i, r := range nameBytes {
|
---|
[478] | 449 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 450 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 451 | } else if r == '{' {
|
---|
[492] | 452 | nameBytes[i] = '['
|
---|
[478] | 453 | } else if r == '}' {
|
---|
[492] | 454 | nameBytes[i] = ']'
|
---|
[478] | 455 | } else if r == '\\' {
|
---|
[492] | 456 | nameBytes[i] = '|'
|
---|
[478] | 457 | } else if r == '~' {
|
---|
[492] | 458 | nameBytes[i] = '^'
|
---|
[478] | 459 | }
|
---|
| 460 | }
|
---|
[492] | 461 | return string(nameBytes)
|
---|
[478] | 462 | }
|
---|
| 463 |
|
---|
| 464 | // casemapRFC1459Strict of name is the canonical representation of name
|
---|
| 465 | // according to the rfc1459-strict casemapping.
|
---|
| 466 | func casemapRFC1459Strict(name string) string {
|
---|
[492] | 467 | nameBytes := []byte(name)
|
---|
| 468 | for i, r := range nameBytes {
|
---|
[478] | 469 | if 'A' <= r && r <= 'Z' {
|
---|
[492] | 470 | nameBytes[i] = r + 'a' - 'A'
|
---|
[478] | 471 | } else if r == '{' {
|
---|
[492] | 472 | nameBytes[i] = '['
|
---|
[478] | 473 | } else if r == '}' {
|
---|
[492] | 474 | nameBytes[i] = ']'
|
---|
[478] | 475 | } else if r == '\\' {
|
---|
[492] | 476 | nameBytes[i] = '|'
|
---|
[478] | 477 | }
|
---|
| 478 | }
|
---|
[492] | 479 | return string(nameBytes)
|
---|
[478] | 480 | }
|
---|
| 481 |
|
---|
| 482 | func parseCasemappingToken(tokenValue string) (casemap casemapping, ok bool) {
|
---|
| 483 | switch tokenValue {
|
---|
| 484 | case "ascii":
|
---|
| 485 | casemap = casemapASCII
|
---|
| 486 | case "rfc1459":
|
---|
| 487 | casemap = casemapRFC1459
|
---|
| 488 | case "rfc1459-strict":
|
---|
| 489 | casemap = casemapRFC1459Strict
|
---|
| 490 | default:
|
---|
| 491 | return nil, false
|
---|
| 492 | }
|
---|
| 493 | return casemap, true
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | func partialCasemap(higher casemapping, name string) string {
|
---|
[492] | 497 | nameFullyCM := []byte(higher(name))
|
---|
| 498 | nameBytes := []byte(name)
|
---|
| 499 | for i, r := range nameBytes {
|
---|
| 500 | if !('A' <= r && r <= 'Z') && !('a' <= r && r <= 'z') {
|
---|
| 501 | nameBytes[i] = nameFullyCM[i]
|
---|
[478] | 502 | }
|
---|
| 503 | }
|
---|
[492] | 504 | return string(nameBytes)
|
---|
[478] | 505 | }
|
---|
| 506 |
|
---|
| 507 | type casemapMap struct {
|
---|
| 508 | innerMap map[string]casemapEntry
|
---|
| 509 | casemap casemapping
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | type casemapEntry struct {
|
---|
| 513 | originalKey string
|
---|
| 514 | value interface{}
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | func newCasemapMap(size int) casemapMap {
|
---|
| 518 | return casemapMap{
|
---|
| 519 | innerMap: make(map[string]casemapEntry, size),
|
---|
| 520 | casemap: casemapNone,
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | func (cm *casemapMap) OriginalKey(name string) (key string, ok bool) {
|
---|
| 525 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 526 | if !ok {
|
---|
| 527 | return "", false
|
---|
| 528 | }
|
---|
| 529 | return entry.originalKey, true
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | func (cm *casemapMap) Has(name string) bool {
|
---|
| 533 | _, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 534 | return ok
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | func (cm *casemapMap) Len() int {
|
---|
| 538 | return len(cm.innerMap)
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | func (cm *casemapMap) SetValue(name string, value interface{}) {
|
---|
| 542 | nameCM := cm.casemap(name)
|
---|
| 543 | entry, ok := cm.innerMap[nameCM]
|
---|
| 544 | if !ok {
|
---|
| 545 | cm.innerMap[nameCM] = casemapEntry{
|
---|
| 546 | originalKey: name,
|
---|
| 547 | value: value,
|
---|
| 548 | }
|
---|
| 549 | return
|
---|
| 550 | }
|
---|
| 551 | entry.value = value
|
---|
| 552 | cm.innerMap[nameCM] = entry
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | func (cm *casemapMap) Delete(name string) {
|
---|
| 556 | delete(cm.innerMap, cm.casemap(name))
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | func (cm *casemapMap) SetCasemapping(newCasemap casemapping) {
|
---|
| 560 | cm.casemap = newCasemap
|
---|
| 561 | newInnerMap := make(map[string]casemapEntry, len(cm.innerMap))
|
---|
| 562 | for _, entry := range cm.innerMap {
|
---|
| 563 | newInnerMap[cm.casemap(entry.originalKey)] = entry
|
---|
| 564 | }
|
---|
| 565 | cm.innerMap = newInnerMap
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | type upstreamChannelCasemapMap struct{ casemapMap }
|
---|
| 569 |
|
---|
| 570 | func (cm *upstreamChannelCasemapMap) Value(name string) *upstreamChannel {
|
---|
| 571 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 572 | if !ok {
|
---|
| 573 | return nil
|
---|
| 574 | }
|
---|
| 575 | return entry.value.(*upstreamChannel)
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | type channelCasemapMap struct{ casemapMap }
|
---|
| 579 |
|
---|
| 580 | func (cm *channelCasemapMap) Value(name string) *Channel {
|
---|
| 581 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 582 | if !ok {
|
---|
| 583 | return nil
|
---|
| 584 | }
|
---|
| 585 | return entry.value.(*Channel)
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | type membershipsCasemapMap struct{ casemapMap }
|
---|
| 589 |
|
---|
| 590 | func (cm *membershipsCasemapMap) Value(name string) *memberships {
|
---|
| 591 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 592 | if !ok {
|
---|
| 593 | return nil
|
---|
| 594 | }
|
---|
| 595 | return entry.value.(*memberships)
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[480] | 598 | type deliveredCasemapMap struct{ casemapMap }
|
---|
[478] | 599 |
|
---|
[480] | 600 | func (cm *deliveredCasemapMap) Value(name string) deliveredClientMap {
|
---|
[478] | 601 | entry, ok := cm.innerMap[cm.casemap(name)]
|
---|
| 602 | if !ok {
|
---|
| 603 | return nil
|
---|
| 604 | }
|
---|
[480] | 605 | return entry.value.(deliveredClientMap)
|
---|
[478] | 606 | }
|
---|
[498] | 607 |
|
---|
| 608 | func isWordBoundary(r rune) bool {
|
---|
| 609 | switch r {
|
---|
| 610 | case '-', '_', '|':
|
---|
| 611 | return false
|
---|
| 612 | case '\u00A0':
|
---|
| 613 | return true
|
---|
| 614 | default:
|
---|
| 615 | return !unicode.IsLetter(r) && !unicode.IsNumber(r)
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | func isHighlight(text, nick string) bool {
|
---|
| 620 | for {
|
---|
| 621 | i := strings.Index(text, nick)
|
---|
| 622 | if i < 0 {
|
---|
| 623 | return false
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | // Detect word boundaries
|
---|
| 627 | var left, right rune
|
---|
| 628 | if i > 0 {
|
---|
| 629 | left, _ = utf8.DecodeLastRuneInString(text[:i])
|
---|
| 630 | }
|
---|
| 631 | if i < len(text) {
|
---|
| 632 | right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
|
---|
| 633 | }
|
---|
| 634 | if isWordBoundary(left) && isWordBoundary(right) {
|
---|
| 635 | return true
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | text = text[i+len(nick):]
|
---|
| 639 | }
|
---|
| 640 | }
|
---|
[516] | 641 |
|
---|
| 642 | // parseChatHistoryBound parses the given CHATHISTORY parameter as a bound.
|
---|
| 643 | // The zero time is returned on error.
|
---|
| 644 | func parseChatHistoryBound(param string) time.Time {
|
---|
| 645 | parts := strings.SplitN(param, "=", 2)
|
---|
| 646 | if len(parts) != 2 {
|
---|
| 647 | return time.Time{}
|
---|
| 648 | }
|
---|
| 649 | switch parts[0] {
|
---|
| 650 | case "timestamp":
|
---|
| 651 | timestamp, err := time.Parse(serverTimeLayout, parts[1])
|
---|
| 652 | if err != nil {
|
---|
| 653 | return time.Time{}
|
---|
| 654 | }
|
---|
| 655 | return timestamp
|
---|
| 656 | default:
|
---|
| 657 | return time.Time{}
|
---|
| 658 | }
|
---|
| 659 | }
|
---|