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-10-15 06:56:17 +00:00
|
|
|
store Store
|
|
|
|
messageLog MessageStore
|
|
|
|
messageIndex MessageSearchProvider
|
|
|
|
clientSettings *ClientSettings
|
|
|
|
lastIP []byte
|
|
|
|
certificate *tls.Certificate
|
|
|
|
lock sync.Mutex
|
2016-01-22 17:30:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func NewUser(store Store) (*User, error) {
|
2018-10-15 06:56:17 +00:00
|
|
|
user := &User{
|
|
|
|
store: store,
|
|
|
|
clientSettings: DefaultClientSettings(),
|
|
|
|
}
|
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
|
|
|
|
2020-04-20 01:02:15 +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()
|
|
|
|
}
|
2020-04-20 01:02:15 +00:00
|
|
|
os.RemoveAll(Path.User(u.Username))
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:24:29 +00:00
|
|
|
func (u *User) GetLastIP() []byte {
|
|
|
|
u.lock.Lock()
|
|
|
|
ip := u.lastIP
|
|
|
|
u.lock.Unlock()
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SetLastIP(ip []byte) error {
|
|
|
|
u.lock.Lock()
|
|
|
|
u.lastIP = ip
|
|
|
|
u.lock.Unlock()
|
|
|
|
|
|
|
|
return u.store.SaveUser(u)
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:56:17 +00:00
|
|
|
//easyjson:json
|
|
|
|
type ClientSettings struct {
|
|
|
|
ColoredNicks bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultClientSettings() *ClientSettings {
|
|
|
|
return &ClientSettings{
|
|
|
|
ColoredNicks: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetClientSettings() *ClientSettings {
|
|
|
|
u.lock.Lock()
|
|
|
|
settings := *u.clientSettings
|
|
|
|
u.lock.Unlock()
|
|
|
|
return &settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SetClientSettings(settings *ClientSettings) error {
|
|
|
|
u.lock.Lock()
|
|
|
|
u.clientSettings = settings
|
|
|
|
u.lock.Unlock()
|
|
|
|
|
|
|
|
return u.store.SaveUser(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) UnmarshalClientSettingsJSON(b []byte) error {
|
|
|
|
u.lock.Lock()
|
|
|
|
err := u.clientSettings.UnmarshalJSON(b)
|
|
|
|
u.lock.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.store.SaveUser(u)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-27 07:53:07 +00:00
|
|
|
func (u *User) GetServer(address string) (*Server, error) {
|
|
|
|
return u.store.GetServer(u, address)
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-01-27 07:53:07 +00:00
|
|
|
return u.store.SaveServer(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 {
|
2019-01-27 07:53:07 +00:00
|
|
|
server, err := u.GetServer(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.Nick = nick
|
|
|
|
return u.AddServer(server)
|
2018-05-31 21:24:59 +00:00
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func (u *User) SetServerName(name, address string) error {
|
2019-01-27 07:53:07 +00:00
|
|
|
server, err := u.GetServer(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.Name = name
|
|
|
|
return u.AddServer(server)
|
2018-05-31 21:24:59 +00:00
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2019-01-27 07:53:07 +00:00
|
|
|
// TODO: Remove topic from disk schema
|
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
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
type Tab struct {
|
|
|
|
Server string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetOpenDMs() ([]Tab, error) {
|
|
|
|
return u.store.GetOpenDMs(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) AddOpenDM(server, nick string) error {
|
|
|
|
return u.store.AddOpenDM(u, server, nick)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) RemoveOpenDM(server, nick string) error {
|
|
|
|
return u.store.RemoveOpenDM(u, server, nick)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|