[20] | 1 | package jounce
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "fmt"
|
---|
| 5 | "strings"
|
---|
| 6 | )
|
---|
| 7 |
|
---|
| 8 | const (
|
---|
| 9 | rpl_localusers = "265"
|
---|
| 10 | rpl_globalusers = "266"
|
---|
| 11 | rpl_topicwhotime = "333"
|
---|
| 12 | )
|
---|
| 13 |
|
---|
| 14 | type modeSet string
|
---|
| 15 |
|
---|
| 16 | func (ms modeSet) Has(c byte) bool {
|
---|
| 17 | return strings.IndexByte(string(ms), c) >= 0
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | func (ms *modeSet) Add(c byte) {
|
---|
| 21 | if !ms.Has(c) {
|
---|
| 22 | *ms += modeSet(c)
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | func (ms *modeSet) Del(c byte) {
|
---|
| 27 | i := strings.IndexByte(string(*ms), c)
|
---|
| 28 | if i >= 0 {
|
---|
| 29 | *ms = (*ms)[:i] + (*ms)[i+1:]
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | func (ms *modeSet) Apply(s string) error {
|
---|
| 34 | var plusMinus byte
|
---|
| 35 | for i := 0; i < len(s); i++ {
|
---|
| 36 | switch c := s[i]; c {
|
---|
| 37 | case '+', '-':
|
---|
| 38 | plusMinus = c
|
---|
| 39 | default:
|
---|
| 40 | switch plusMinus {
|
---|
| 41 | case '+':
|
---|
| 42 | ms.Add(c)
|
---|
| 43 | case '-':
|
---|
| 44 | ms.Del(c)
|
---|
| 45 | default:
|
---|
| 46 | return fmt.Errorf("malformed modestring %q: missing plus/minus", s)
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | return nil
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | type channelStatus byte
|
---|
| 54 |
|
---|
| 55 | const (
|
---|
| 56 | channelPublic channelStatus = '='
|
---|
| 57 | channelSecret channelStatus = '@'
|
---|
| 58 | channelPrivate channelStatus = '*'
|
---|
| 59 | )
|
---|
| 60 |
|
---|
| 61 | func parseChannelStatus(s string) (channelStatus, error) {
|
---|
| 62 | if len(s) > 1 {
|
---|
| 63 | return 0, fmt.Errorf("invalid channel status %q: more than one character", s)
|
---|
| 64 | }
|
---|
| 65 | switch cs := channelStatus(s[0]); cs {
|
---|
| 66 | case channelPublic, channelSecret, channelPrivate:
|
---|
| 67 | return cs, nil
|
---|
| 68 | default:
|
---|
| 69 | return 0, fmt.Errorf("invalid channel status %q: unknown status", s)
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | type membership byte
|
---|
| 74 |
|
---|
| 75 | const (
|
---|
| 76 | membershipFounder membership = '~'
|
---|
| 77 | membershipProtected membership = '&'
|
---|
| 78 | membershipOperator membership = '@'
|
---|
| 79 | membershipHalfOp membership = '%'
|
---|
| 80 | membershipVoice membership = '+'
|
---|
| 81 | )
|
---|
| 82 |
|
---|
| 83 | const stdMembershipPrefixes = "~&@%+"
|
---|
| 84 |
|
---|
| 85 | func parseMembershipPrefix(s string) (prefix membership, nick string) {
|
---|
| 86 | // TODO: any prefix from PREFIX RPL_ISUPPORT
|
---|
| 87 | if strings.IndexByte(stdMembershipPrefixes, s[0]) >= 0 {
|
---|
| 88 | return membership(s[0]), s[1:]
|
---|
| 89 | } else {
|
---|
| 90 | return 0, s
|
---|
| 91 | }
|
---|
| 92 | }
|
---|