forked from mumble-voip/grumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeze.go
302 lines (265 loc) · 6.33 KB
/
freeze.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2011 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.
package main
import (
"compress/gzip"
"fmt"
"gob"
"io"
"io/ioutil"
"os"
)
type frozenServer struct {
Id int "id"
MaxUsers int "max_user"
Channels []frozenChannel "channels"
Users []frozenUser "users"
}
type frozenUser struct {
Id uint32 "id"
Name string "name"
Password string "password"
CertHash string "cert_hash"
Email string "email"
TextureBlob string "texture_blob"
CommentBlob string "comment_blob"
LastChannelId int "last_channel_id"
LastActive uint64 "last_active"
}
type frozenChannel struct {
Id int "id"
Name string "name"
ParentId int "parent_id"
Position int64 "position"
InheritACL bool "inherit_acl"
Links []int "links"
ACL []frozenACL "acl"
Groups []frozenGroup "groups"
DescriptionBlob string "description_blob"
}
type frozenACL struct {
UserId int "user_id"
Group string "group"
ApplyHere bool "apply_here"
ApplySubs bool "apply_subs"
Allow uint32 "allow"
Deny uint32 "deny"
}
type frozenGroup struct {
Name string "name"
Inherit bool "inherit"
Inheritable bool "inheritable"
Add []int "add"
Remove []int "remove"
}
// Freeze a server and write it to a file
func (server *Server) FreezeToFile(filename string) (err os.Error) {
r := server.FreezeServer()
if err != nil {
return err
}
f, err := ioutil.TempFile(*datadir, fmt.Sprintf("%v_", server.Id))
if err != nil {
return err
}
_, err = io.Copy(f, r)
if err != nil {
return err
}
err = r.Close()
if err != nil {
return err
}
err = f.Sync()
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
err = os.Rename(f.Name(), filename)
if err != nil {
return err
}
return
}
// Freeze a server
func (server *Server) Freeze() (fs frozenServer, err os.Error) {
fs.Id = int(server.Id)
fs.MaxUsers = server.MaxUsers
channels := []frozenChannel{}
for _, c := range server.Channels {
fc, err := c.Freeze()
if err != nil {
return
}
channels = append(channels, fc)
}
fs.Channels = channels
users := []frozenUser{}
for _, u := range server.Users {
fu, err := u.Freeze()
if err != nil {
return
}
users = append(users, fu)
}
fs.Users = users
return
}
// Freeze a channel
func (channel *Channel) Freeze() (fc frozenChannel, err os.Error) {
fc.Id = channel.Id
fc.Name = channel.Name
if channel.parent != nil {
fc.ParentId = channel.parent.Id
} else {
fc.ParentId = -1
}
fc.Position = int64(channel.Position)
fc.InheritACL = channel.InheritACL
acls := []frozenACL{}
for _, acl := range channel.ACL {
facl, err := acl.Freeze()
if err != nil {
return
}
acls = append(acls, facl)
}
fc.ACL = acls
groups := []frozenGroup{}
for _, grp := range channel.Groups {
fgrp, err := grp.Freeze()
if err != nil {
return
}
groups = append(groups, fgrp)
}
fc.Groups = groups
links := []int{}
for cid, _ := range channel.Links {
links = append(links, cid)
}
fc.Links = links
return
}
// Freeze a User
func (user *User) Freeze() (fu frozenUser, err os.Error) {
fu.Id = user.Id
fu.Name = user.Name
fu.Password = user.Password
fu.CertHash = user.CertHash
fu.Email = user.Email
fu.TextureBlob = user.TextureBlob
fu.CommentBlob = user.CommentBlob
fu.LastChannelId = user.LastChannelId
fu.LastActive = user.LastActive
return
}
// Freeze a ChannelACL
func (acl *ChannelACL) Freeze() (facl frozenACL, err os.Error) {
facl.UserId = acl.UserId
facl.Group = acl.Group
facl.ApplyHere = acl.ApplyHere
facl.ApplySubs = acl.ApplySubs
facl.Allow = uint32(acl.Allow)
facl.Deny = uint32(acl.Deny)
return
}
// Freeze a Group
func (group *Group) Freeze() (fgrp frozenGroup, err os.Error) {
fgrp.Name = group.Name
fgrp.Inherit = group.Inherit
fgrp.Inheritable = group.Inheritable
fgrp.Add = group.AddUsers()
fgrp.Remove = group.RemoveUsers()
return
}
// Create a new Server from a frozen server
func NewServerFromFrozen(filename string) (s *Server, err os.Error) {
descFile, err := os.Open(filename)
if err != nil {
return nil, err
}
defer descFile.Close()
zr, err := gzip.NewReader(descFile)
if err != nil {
return nil, err
}
fs := new(frozenServer)
decoder := gob.NewDecoder(zr)
decoder.Decode(&fs)
s, err = NewServer(int64(fs.Id), "0.0.0.0", int(DefaultPort+fs.Id-1))
if err != nil {
return nil, err
}
// Add all channels, but don't hook up parent/child relationships
// until all of them are loaded.
for _, fc := range fs.Channels {
c := NewChannel(fc.Id, fc.Name)
c.Position = int(fc.Position)
c.InheritACL = fc.InheritACL
c.DescriptionBlob = fc.DescriptionBlob
for _, facl := range fc.ACL {
acl := NewChannelACL(c)
acl.ApplyHere = facl.ApplyHere
acl.ApplySubs = facl.ApplySubs
acl.UserId = facl.UserId
acl.Group = facl.Group
acl.Deny = Permission(facl.Deny)
acl.Allow = Permission(facl.Allow)
c.ACL = append(c.ACL, acl)
}
for _, fgrp := range fc.Groups {
g := NewGroup(c, fgrp.Name)
g.Inherit = fgrp.Inherit
g.Inheritable = fgrp.Inheritable
for _, uid := range fgrp.Add {
g.Add[uid] = true
}
for _, uid := range fgrp.Remove {
g.Remove[uid] = true
}
c.Groups[g.Name] = g
}
s.Channels[c.Id] = c
}
// Hook up children with their parents.
for _, fc := range fs.Channels {
if fc.Id == 0 {
continue
}
childChan, exists := s.Channels[fc.Id]
if !exists {
return nil, os.NewError("Non-existant child channel")
}
parentChan, exists := s.Channels[fc.ParentId]
if !exists {
return nil, os.NewError("Non-existant parent channel")
}
parentChan.AddChild(childChan)
}
s.root = s.Channels[0]
// Add all users
for _, fu := range fs.Users {
u, err := NewUser(fu.Id, fu.Name)
if err != nil {
return nil, err
}
u.Password = fu.Password
u.CertHash = fu.CertHash
u.Email = fu.Email
u.TextureBlob = fu.TextureBlob
u.CommentBlob = fu.CommentBlob
u.LastChannelId = fu.LastChannelId
u.LastActive = fu.LastActive
s.Users[u.Id] = u
s.UserNameMap[u.Name] = u
if len(u.CertHash) > 0 {
s.UserCertMap[u.CertHash] = u
}
}
return s, nil
}