2015-01-17 01:37:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
2015-04-26 22:45:49 +00:00
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2015-02-07 02:10:58 +00:00
|
|
|
Name string `json:"name"`
|
2015-01-22 00:03:25 +00:00
|
|
|
Address string `json:"address"`
|
2015-02-02 00:54:26 +00:00
|
|
|
TLS bool `json:"tls"`
|
2015-02-07 02:10:58 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
2015-01-22 00:03:25 +00:00
|
|
|
Nick string `json:"nick"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Realname string `json:"realname"`
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Channel struct {
|
|
|
|
Server string `json:"server"`
|
|
|
|
Name string `json:"name"`
|
2015-02-12 01:14:56 +00:00
|
|
|
Users []string `json:"users,omitempty"`
|
2015-01-21 02:06:34 +00:00
|
|
|
Topic string `json:"topic,omitempty"`
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
UUID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUser(uuid string) User {
|
|
|
|
user := User{
|
|
|
|
UUID: uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Users"))
|
|
|
|
data, _ := json.Marshal(user)
|
|
|
|
|
|
|
|
b.Put([]byte(uuid), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadUsers() []User {
|
|
|
|
var users []User
|
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Users"))
|
|
|
|
|
|
|
|
b.ForEach(func(k, v []byte) error {
|
|
|
|
users = append(users, User{string(k)})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) GetServers() []Server {
|
|
|
|
var servers []Server
|
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
|
|
|
c := tx.Bucket([]byte("Servers")).Cursor()
|
|
|
|
prefix := []byte(u.UUID)
|
|
|
|
|
|
|
|
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
|
|
var server Server
|
|
|
|
json.Unmarshal(v, &server)
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) GetChannels() []Channel {
|
|
|
|
var channels []Channel
|
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
|
|
|
c := tx.Bucket([]byte("Channels")).Cursor()
|
|
|
|
|
|
|
|
prefix := []byte(u.UUID)
|
|
|
|
|
|
|
|
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
|
|
var channel Channel
|
|
|
|
json.Unmarshal(v, &channel)
|
|
|
|
channels = append(channels, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) AddServer(server Server) {
|
2015-01-21 02:06:34 +00:00
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
2015-01-17 01:37:21 +00:00
|
|
|
b := tx.Bucket([]byte("Servers"))
|
|
|
|
data, _ := json.Marshal(server)
|
|
|
|
|
|
|
|
b.Put([]byte(u.UUID+":"+server.Address), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) AddChannel(channel Channel) {
|
2015-01-21 02:06:34 +00:00
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
2015-01-17 01:37:21 +00:00
|
|
|
b := tx.Bucket([]byte("Channels"))
|
|
|
|
data, _ := json.Marshal(channel)
|
|
|
|
|
|
|
|
b.Put([]byte(u.UUID+":"+channel.Server+":"+channel.Name), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:43:49 +00:00
|
|
|
func (u User) SetNick(nick, address string) {
|
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Servers"))
|
|
|
|
id := []byte(u.UUID + ":" + address)
|
|
|
|
var server Server
|
|
|
|
|
|
|
|
json.Unmarshal(b.Get(id), &server)
|
|
|
|
server.Nick = nick
|
|
|
|
|
|
|
|
data, _ := json.Marshal(server)
|
|
|
|
b.Put(id, data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
func (u User) RemoveServer(address string) {
|
2015-01-21 02:06:34 +00:00
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
|
|
|
serverID := []byte(u.UUID + ":" + address)
|
|
|
|
|
|
|
|
tx.Bucket([]byte("Servers")).Delete(serverID)
|
|
|
|
|
|
|
|
b := tx.Bucket([]byte("Channels"))
|
|
|
|
c := b.Cursor()
|
|
|
|
|
|
|
|
for k, _ := c.Seek(serverID); bytes.HasPrefix(k, serverID); k, _ = c.Next() {
|
|
|
|
b.Delete(k)
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) RemoveChannel(server, channel string) {
|
2015-01-21 02:06:34 +00:00
|
|
|
go db.Update(func(tx *bolt.Tx) error {
|
|
|
|
tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + server + ":" + channel))
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|