source: code/trunk/irc.go@ 20

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

Split IRC helpers to separate file

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