-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
94 lines (84 loc) · 1.79 KB
/
user.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
91
92
93
94
package etcdircd
import (
"bytes"
"encoding/gob"
"strings"
"time"
)
type UserValue struct {
Nick string
User []string
ServerName string
Mode ModeValue
Host string
RealName string
Created time.Time
Channels []string
AwayMsg string
Lease int64 // stm needs to support Lease()
}
func encodeUserValue(uv UserValue) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
enc := gob.NewEncoder(buf)
if err := enc.Encode(&uv); err != nil {
panic(err)
}
return buf.String()
}
func decodeUserValue(uv string) (*UserValue, error) {
dec := gob.NewDecoder(strings.NewReader(uv))
v := &UserValue{}
if err := dec.Decode(v); err != nil {
return nil, err
}
return v, nil
}
func (uv *UserValue) inChan(ch string) bool {
for _, c := range uv.Channels {
if c == ch {
return true
}
}
return false
}
func (uv *UserValue) part(ch string) bool {
for i, uvch := range uv.Channels {
if uvch == ch {
uv.Channels = append(uv.Channels[:i], uv.Channels[i+1:]...)
return true
}
}
return false
}
func stringSliceToMap(ss []string) map[string]struct{} {
m := make(map[string]struct{}, len(ss))
for _, s := range ss {
m[s] = struct{}{}
}
return m
}
func pruneInvisible(users []UserValue, me UserValue) []UserValue {
if me.Mode.has('O') {
return users
}
isLocalOp := me.Mode.has('o')
ret := []UserValue{}
uchan := stringSliceToMap(me.Channels)
for _, user := range users {
if !user.Mode.has('i') || (isLocalOp && user.ServerName == me.ServerName) {
ret = append(ret, user)
continue
}
sharedChannels := []string{}
for _, ch := range user.Channels {
if _, ok := uchan[ch]; ok {
sharedChannels = append(sharedChannels, ch)
}
}
if len(sharedChannels) > 0 {
user.Channels = sharedChannels
ret = append(ret, user)
}
}
return ret
}