2015-01-17 01:37:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-22 17:30:47 +00:00
|
|
|
"crypto/tls"
|
2016-01-19 21:02:12 +00:00
|
|
|
"os"
|
2015-04-29 21:54:44 +00:00
|
|
|
"strconv"
|
2016-01-22 17:30:47 +00:00
|
|
|
"sync"
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-22 17:30:47 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/blevesearch/bleve"
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/boltdb/bolt"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-22 17:30:47 +00:00
|
|
|
type User struct {
|
|
|
|
ID uint64
|
|
|
|
Username string
|
|
|
|
|
|
|
|
id []byte
|
|
|
|
messageLog *bolt.DB
|
|
|
|
messageIndex bleve.Index
|
|
|
|
certificate *tls.Certificate
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
2016-01-26 21:10:44 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port string `json:"port,omitempty"`
|
|
|
|
TLS bool `json:"tls,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Nick string `json:"nick"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Realname string `json:"realname,omitempty"`
|
|
|
|
Connected bool `json:"connected"`
|
2016-01-22 17:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Channel struct {
|
|
|
|
Server string `json:"server"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Topic string `json:"topic,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
func NewUser() (*User, error) {
|
2016-01-15 01:27:30 +00:00
|
|
|
user := &User{}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2016-01-17 22:33:52 +00:00
|
|
|
err := db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketUsers)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
user.ID, _ = b.NextSequence()
|
2016-01-15 01:27:30 +00:00
|
|
|
user.Username = strconv.FormatUint(user.ID, 10)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-22 17:30:47 +00:00
|
|
|
data, err := user.Marshal(nil)
|
2016-01-15 01:27:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
user.id = idToBytes(user.ID)
|
2016-01-15 01:27:30 +00:00
|
|
|
return b.Put(user.id, data)
|
|
|
|
})
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
if err != nil {
|
2016-01-17 20:15:29 +00:00
|
|
|
return nil, err
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
err = user.openMessageLog()
|
|
|
|
if err != nil {
|
2016-01-17 20:15:29 +00:00
|
|
|
return nil, err
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
return user, nil
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func LoadUsers() []*User {
|
|
|
|
var users []*User
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketUsers)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
b.ForEach(func(k, _ []byte) error {
|
|
|
|
id := idFromBytes(k)
|
|
|
|
user := &User{
|
|
|
|
ID: id,
|
|
|
|
Username: strconv.FormatUint(id, 10),
|
|
|
|
id: make([]byte, 8),
|
|
|
|
}
|
|
|
|
copy(user.id, k)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
users = append(users, user)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
for _, user := range users {
|
|
|
|
user.openMessageLog()
|
|
|
|
user.loadCertificate()
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) GetServers() []Server {
|
2015-01-17 01:37:21 +00:00
|
|
|
var servers []Server
|
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
c := tx.Bucket(bucketServers).Cursor()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
for k, v := c.Seek(u.id); bytes.HasPrefix(k, u.id); k, v = c.Next() {
|
2016-01-17 22:33:52 +00:00
|
|
|
server := Server{}
|
2016-01-22 17:30:47 +00:00
|
|
|
server.Unmarshal(v)
|
2015-01-17 01:37:21 +00:00
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) GetChannels() []Channel {
|
2015-01-17 01:37:21 +00:00
|
|
|
var channels []Channel
|
|
|
|
|
|
|
|
db.View(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
c := tx.Bucket(bucketChannels).Cursor()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
for k, v := c.Seek(u.id); bytes.HasPrefix(k, u.id); k, v = c.Next() {
|
2016-01-17 22:33:52 +00:00
|
|
|
channel := Channel{}
|
2016-01-22 17:30:47 +00:00
|
|
|
channel.Unmarshal(v)
|
2015-01-17 01:37:21 +00:00
|
|
|
channels = append(channels, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) AddServer(server Server) {
|
2016-01-17 22:33:52 +00:00
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketServers)
|
2016-01-22 17:30:47 +00:00
|
|
|
data, _ := server.Marshal(nil)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
b.Put(u.serverID(server.Host), data)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) AddChannel(channel Channel) {
|
2016-01-17 22:33:52 +00:00
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketChannels)
|
2016-01-22 17:30:47 +00:00
|
|
|
data, _ := channel.Marshal(nil)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
b.Put(u.channelID(channel.Server, channel.Name), data)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) SetNick(nick, address string) {
|
2016-01-17 22:33:52 +00:00
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketServers)
|
|
|
|
id := u.serverID(address)
|
2015-02-04 01:43:49 +00:00
|
|
|
|
2016-01-17 22:33:52 +00:00
|
|
|
server := Server{}
|
2016-01-22 17:30:47 +00:00
|
|
|
server.Unmarshal(b.Get(id))
|
2015-02-04 01:43:49 +00:00
|
|
|
server.Nick = nick
|
|
|
|
|
2016-01-22 17:30:47 +00:00
|
|
|
data, _ := server.Marshal(nil)
|
2015-02-04 01:43:49 +00:00
|
|
|
b.Put(id, data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) RemoveServer(address string) {
|
2016-01-17 22:33:52 +00:00
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
serverID := u.serverID(address)
|
|
|
|
tx.Bucket(bucketServers).Delete(serverID)
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketChannels)
|
2015-01-21 02:06:34 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) RemoveChannel(server, channel string) {
|
2016-01-17 22:33:52 +00:00
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
2016-01-15 01:27:30 +00:00
|
|
|
b := tx.Bucket(bucketChannels)
|
|
|
|
id := u.channelID(server, channel)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
b.Delete(id)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:02:12 +00:00
|
|
|
func (u *User) Remove() {
|
|
|
|
db.Batch(func(tx *bolt.Tx) error {
|
|
|
|
return tx.Bucket(bucketUsers).Delete(u.id)
|
|
|
|
})
|
|
|
|
u.closeMessageLog()
|
|
|
|
os.RemoveAll(Path.User(u.Username))
|
2015-06-07 04:16:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
func (u *User) serverID(address string) []byte {
|
|
|
|
id := make([]byte, 8+len(address))
|
|
|
|
copy(id, u.id)
|
|
|
|
copy(id[8:], address)
|
|
|
|
return id
|
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
func (u *User) channelID(server, channel string) []byte {
|
|
|
|
id := make([]byte, 8+len(server)+1+len(channel))
|
|
|
|
copy(id, u.id)
|
|
|
|
copy(id[8:], server)
|
|
|
|
copy(id[8+len(server)+1:], channel)
|
|
|
|
return id
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|