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