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