-
Notifications
You must be signed in to change notification settings - Fork 2
/
channel.go
90 lines (79 loc) · 1.74 KB
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package etcdircd
import (
"bytes"
"encoding/gob"
"strings"
"time"
)
// ChannelCtl represents a channel in etcd.
type ChannelCtl struct {
Name string
Topic string
Mode ModeValue
Created time.Time
}
// ChannelNick contains per-channel information for a nick.
type ChannelNick struct {
Nick string
Mode ModeValue
}
// ChannelNicks is one per server session; holds all nick data for a channel.
type ChannelNicks struct {
Nicks []ChannelNick
}
func (cns *ChannelNicks) nicks() []string {
ret := make([]string, len(cns.Nicks))
for i, cn := range cns.Nicks {
ret[i] = cn.Nick
}
return ret
}
func (cns *ChannelNicks) find(nick string) *ChannelNick {
for i, cn := range cns.Nicks {
if cn.Nick == nick {
return &cns.Nicks[i]
}
}
return nil
}
func (cu *ChannelNicks) del(nick string) bool {
for i, u := range cu.Nicks {
if u.Nick == nick {
cu.Nicks = append(cu.Nicks[:i], cu.Nicks[i+1:]...)
return true
}
}
return false
}
func encodeChannelCtl(cc ChannelCtl) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
enc := gob.NewEncoder(buf)
if err := enc.Encode(&cc); err != nil {
panic(err)
}
return buf.String()
}
func decodeChannelCtl(cc string) (*ChannelCtl, error) {
dec := gob.NewDecoder(strings.NewReader(cc))
v := &ChannelCtl{}
if err := dec.Decode(v); err != nil {
return nil, err
}
return v, nil
}
func encodeChannelNicks(cn ChannelNicks) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
enc := gob.NewEncoder(buf)
if err := enc.Encode(&cn); err != nil {
panic(err)
}
return buf.String()
}
func decodeChannelNicks(cn string) (*ChannelNicks, error) {
dec := gob.NewDecoder(strings.NewReader(cn))
v := &ChannelNicks{}
if err := dec.Decode(v); err != nil {
return nil, err
}
return v, nil
}