source: code/trunk/irc.go@ 70

Last change on this file since 70 was 43, checked in by contact, 5 years ago

Add parseMessageParams helper

File size: 2.0 KB
Line 
1package jounce
2
3import (
4 "fmt"
5 "strings"
6
7 "gopkg.in/irc.v3"
8)
9
10const (
11 rpl_localusers = "265"
12 rpl_globalusers = "266"
13 rpl_topicwhotime = "333"
14)
15
16type modeSet string
17
18func (ms modeSet) Has(c byte) bool {
19 return strings.IndexByte(string(ms), c) >= 0
20}
21
22func (ms *modeSet) Add(c byte) {
23 if !ms.Has(c) {
24 *ms += modeSet(c)
25 }
26}
27
28func (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
35func (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
55type channelStatus byte
56
57const (
58 channelPublic channelStatus = '='
59 channelSecret channelStatus = '@'
60 channelPrivate channelStatus = '*'
61)
62
63func 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
75type membership byte
76
77const (
78 membershipFounder membership = '~'
79 membershipProtected membership = '&'
80 membershipOperator membership = '@'
81 membershipHalfOp membership = '%'
82 membershipVoice membership = '+'
83)
84
85const stdMembershipPrefixes = "~&@%+"
86
87func 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}
95
96func 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}
Note: See TracBrowser for help on using the repository browser.