2015-01-17 01:37:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-01-22 17:30:47 +00:00
|
|
|
"crypto/tls"
|
2016-01-19 21:02:12 +00:00
|
|
|
"os"
|
2016-01-22 17:30:47 +00:00
|
|
|
"sync"
|
2018-05-31 21:24:59 +00:00
|
|
|
"time"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-22 17:30:47 +00:00
|
|
|
type User struct {
|
|
|
|
ID uint64
|
2018-05-31 21:24:59 +00:00
|
|
|
IDBytes []byte
|
2016-01-22 17:30:47 +00:00
|
|
|
Username string
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
store Store
|
|
|
|
messageLog MessageStore
|
|
|
|
messageIndex MessageSearchProvider
|
2016-01-22 17:30:47 +00:00
|
|
|
certificate *tls.Certificate
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func NewUser(store Store) (*User, error) {
|
|
|
|
user := &User{store: store}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
err := store.SaveUser(user)
|
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
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
err = os.MkdirAll(Path.User(user.Username), 0700)
|
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-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
|
|
|
}
|
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
func LoadUsers(store Store) ([]*User, error) {
|
2018-05-31 21:24:59 +00:00
|
|
|
users, err := store.GetUsers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
for _, user := range users {
|
|
|
|
user.store = store
|
|
|
|
user.loadCertificate()
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
return users, nil
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SetMessageStore(store MessageStore) {
|
|
|
|
u.messageLog = store
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SetMessageSearchProvider(search MessageSearchProvider) {
|
|
|
|
u.messageIndex = search
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) Remove() {
|
|
|
|
u.store.DeleteUser(u)
|
|
|
|
if u.messageLog != nil {
|
|
|
|
u.messageLog.Close()
|
|
|
|
}
|
|
|
|
if u.messageIndex != nil {
|
|
|
|
u.messageIndex.Close()
|
|
|
|
}
|
|
|
|
os.RemoveAll(Path.User(u.Username))
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
type Server struct {
|
|
|
|
Name string
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
TLS bool
|
|
|
|
Password string
|
|
|
|
Nick string
|
|
|
|
Username string
|
|
|
|
Realname string
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
func (u *User) GetServers() ([]*Server, error) {
|
2018-05-31 21:24:59 +00:00
|
|
|
return u.store.GetServers(u)
|
|
|
|
}
|
2015-02-04 01:43:49 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) AddServer(server *Server) error {
|
|
|
|
return u.store.AddServer(u, server)
|
2015-02-04 01:43:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) RemoveServer(address string) error {
|
|
|
|
return u.store.RemoveServer(u, address)
|
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SetNick(nick, address string) error {
|
|
|
|
return u.store.SetNick(u, nick, address)
|
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SetServerName(name, address string) error {
|
|
|
|
return u.store.SetServerName(u, name, address)
|
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
type Channel struct {
|
|
|
|
Server string
|
|
|
|
Name string
|
|
|
|
Topic string
|
2017-06-12 04:18:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
func (u *User) GetChannels() ([]*Channel, error) {
|
2018-05-31 21:24:59 +00:00
|
|
|
return u.store.GetChannels(u)
|
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) AddChannel(channel *Channel) error {
|
|
|
|
return u.store.AddChannel(u, channel)
|
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) RemoveChannel(server, channel string) error {
|
|
|
|
return u.store.RemoveChannel(u, server, channel)
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
type Message struct {
|
|
|
|
ID string `json:"-" bleve:"-"`
|
|
|
|
Server string `json:"-" bleve:"server"`
|
|
|
|
From string `bleve:"-"`
|
|
|
|
To string `json:"-" bleve:"to"`
|
|
|
|
Content string `bleve:"content"`
|
|
|
|
Time int64 `bleve:"-"`
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (m Message) Type() string {
|
|
|
|
return "message"
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) LogMessage(id, server, from, to, content string) error {
|
|
|
|
message := &Message{
|
|
|
|
ID: id,
|
|
|
|
Server: server,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Content: content,
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
err := u.messageLog.LogMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.messageIndex.Index(id, message)
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) GetMessages(server, channel string, count int, fromID string) ([]Message, bool, error) {
|
|
|
|
return u.messageLog.GetMessages(server, channel, count, fromID)
|
2015-06-07 04:16:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, bool, error) {
|
|
|
|
return u.GetMessages(server, channel, count, "")
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SearchMessages(server, channel, q string) ([]Message, error) {
|
|
|
|
ids, err := u.messageIndex.SearchMessages(server, channel, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.messageLog.GetMessagesByID(server, channel, ids)
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|