/
database.go
139 lines (121 loc) · 2.58 KB
/
database.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package bot
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/steveyen/gkvlite"
"os"
"path/filepath"
"reflect"
"strings"
)
var (
kvFile *os.File
KV *gkvlite.Store
Users *gkvlite.Collection
Channels *gkvlite.Collection
)
func initkv() {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
kvFile, _ = os.OpenFile(fmt.Sprintf("%s/database.gkvlite", dir), os.O_RDWR|os.O_CREATE, 0666)
KV, _ = gkvlite.NewStore(kvFile)
Users = KV.SetCollection("users", nil)
Channels = KV.SetCollection("channels", nil)
}
func getInterface(bts []byte, data interface{}) error {
buf := bytes.NewBuffer(bts)
dec := gob.NewDecoder(buf)
err := dec.Decode(data)
if err != nil {
return err
}
return nil
}
func getBytes(key interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(key)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func getKeyString(nick string) map[string]string {
decoded := make(map[string]string)
user, _ := Users.Get([]byte(nick))
if user == nil {
return nil
}
err := getInterface(user, &decoded)
if err != nil {
return nil
}
return decoded
}
func GetUserKey(nick string, key string) string {
nick = strings.ToLower(nick)
user := getKeyString(nick)
return user[key]
}
func SetUserKey(nick string, key string, value string) {
nick = strings.ToLower(nick)
user := getKeyString(nick)
if user == nil {
user = map[string]string{
key: value,
}
} else {
user[key] = value
}
val := reflect.ValueOf(user)
in := val.Interface()
byt, _ := getBytes(in)
Users.Set([]byte(nick), byt)
KV.Flush()
}
func DeleteUserKey(nick string, key string) {
nick = strings.ToLower(nick)
user := getKeyString(nick)
if user == nil {
return
}
delete(user, key)
val := reflect.ValueOf(user)
in := val.Interface()
byt, _ := getBytes(in)
Users.Set([]byte(nick), byt)
KV.Flush()
}
func getKeyBool(channel string) map[string]bool {
decoded := make(map[string]bool)
chann, _ := Channels.Get([]byte(channel))
if chann == nil {
return nil
}
err := getInterface(chann, &decoded)
if err != nil {
return nil
}
return decoded
}
func GetChannelKey(channel string, key string) bool {
channel = strings.ToLower(channel)
chann := getKeyBool(channel)
return chann[key]
}
func SetChannelKey(channel string, key string, value bool) {
channel = strings.ToLower(channel)
chann := getKeyBool(channel)
if chann == nil {
chann = map[string]bool{
key: value,
}
} else {
chann[key] = value
}
val := reflect.ValueOf(chann)
in := val.Interface()
byt, _ := getBytes(in)
Channels.Set([]byte(channel), byt)
KV.Flush()
}