source: code/trunk/irc.go@ 109

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

Add CAP support for downstream connections

File size: 2.3 KB
RevLine 
[98]1package soju
[20]2
3import (
4 "fmt"
5 "strings"
[43]6
7 "gopkg.in/irc.v3"
[20]8)
9
10const (
[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
27type modeSet string
28
29func (ms modeSet) Has(c byte) bool {
30 return strings.IndexByte(string(ms), c) >= 0
31}
32
33func (ms *modeSet) Add(c byte) {
34 if !ms.Has(c) {
35 *ms += modeSet(c)
36 }
37}
38
39func (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
46func (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
66type channelStatus byte
67
68const (
69 channelPublic channelStatus = '='
70 channelSecret channelStatus = '@'
71 channelPrivate channelStatus = '*'
72)
73
74func 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
86type membership byte
87
88const (
89 membershipFounder membership = '~'
90 membershipProtected membership = '&'
91 membershipOperator membership = '@'
92 membershipHalfOp membership = '%'
93 membershipVoice membership = '+'
94)
95
96const stdMembershipPrefixes = "~&@%+"
97
98func 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
107func 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}
Note: See TracBrowser for help on using the repository browser.