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