2019-11-10 23:50:50 +00:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"io/ioutil"
|
2019-12-11 22:48:35 +00:00
|
|
|
"time"
|
2019-11-10 23:50:50 +00:00
|
|
|
|
|
|
|
"dev.narayana.im/narayana/telegabber/yamldb"
|
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-11-10 23:50:50 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2019-12-11 22:48:35 +00:00
|
|
|
var zeroLocation *time.Location
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
zeroLocation, err = time.LoadLocation("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Wrong hardcoded timezone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 23:50:50 +00:00
|
|
|
// SessionsYamlDB wraps YamlDB with Session
|
|
|
|
type SessionsYamlDB struct {
|
|
|
|
yamldb.YamlDB
|
|
|
|
Data *SessionsMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionsMap is for :sessions: subtree
|
|
|
|
type SessionsMap struct {
|
|
|
|
Sessions map[string]Session `yaml:":sessions"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session is a key-values subtree
|
|
|
|
type Session struct {
|
2022-02-08 18:49:49 +00:00
|
|
|
Login string `yaml:":login"`
|
|
|
|
Timezone string `yaml:":timezone"`
|
|
|
|
KeepOnline bool `yaml:":keeponline"`
|
|
|
|
RawMessages bool `yaml:":rawmessages"`
|
2022-02-14 00:05:59 +00:00
|
|
|
AsciiArrows bool `yaml:":asciiarrows"`
|
2023-01-16 01:35:13 +00:00
|
|
|
OOBMode bool `yaml:":oobmode"`
|
2023-03-18 21:43:11 +00:00
|
|
|
Carbons bool `yaml:":carbons"`
|
2023-03-19 00:13:00 +00:00
|
|
|
HideIds bool `yaml:":hideids"`
|
2022-01-05 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var configKeys = []string{
|
|
|
|
"timezone",
|
|
|
|
"keeponline",
|
2022-02-08 18:49:49 +00:00
|
|
|
"rawmessages",
|
2022-02-14 00:05:59 +00:00
|
|
|
"asciiarrows",
|
2023-01-16 01:35:13 +00:00
|
|
|
"oobmode",
|
2023-03-18 21:43:11 +00:00
|
|
|
"carbons",
|
2023-03-19 00:13:00 +00:00
|
|
|
"hideids",
|
2019-11-10 23:50:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 15:55:15 +00:00
|
|
|
var sessionDB *SessionsYamlDB
|
2019-11-10 23:50:50 +00:00
|
|
|
|
2019-11-12 15:50:25 +00:00
|
|
|
// SessionMarshaller implementation for YamlDB
|
|
|
|
func SessionMarshaller() ([]byte, error) {
|
2019-12-18 21:00:23 +00:00
|
|
|
cleanedMap := SessionsMap{}
|
|
|
|
emptySessionsMap(&cleanedMap)
|
|
|
|
|
|
|
|
for jid, session := range sessionDB.Data.Sessions {
|
|
|
|
if session.Login != "" {
|
|
|
|
cleanedMap.Sessions[jid] = session
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return yaml.Marshal(&cleanedMap)
|
2019-11-10 23:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadSessions restores TDlib sessions from the previous run
|
2019-12-04 15:55:15 +00:00
|
|
|
func LoadSessions(path string) (*SessionsYamlDB, error) {
|
2019-11-10 23:50:50 +00:00
|
|
|
var sessionData SessionsMap
|
2019-12-04 15:55:15 +00:00
|
|
|
var err error
|
2019-11-10 23:50:50 +00:00
|
|
|
|
2019-12-04 15:55:15 +00:00
|
|
|
sessionDB, err = initYamlDB(path, &sessionData)
|
2019-11-10 23:50:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return sessionDB, errors.Wrap(err, "Sessions restore error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessionDB, nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
func emptySessionsMap(dataPtr *SessionsMap) {
|
|
|
|
dataPtr.Sessions = make(map[string]Session)
|
|
|
|
}
|
|
|
|
|
2019-12-04 15:55:15 +00:00
|
|
|
func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
|
2019-11-10 23:50:50 +00:00
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
err = yaml.Unmarshal(file, dataPtr)
|
|
|
|
if err != nil {
|
2019-12-04 15:55:15 +00:00
|
|
|
return nil, errors.Wrap(err, "YamlDB is corrupted")
|
2019-11-10 23:50:50 +00:00
|
|
|
}
|
2019-11-19 20:25:14 +00:00
|
|
|
|
|
|
|
if dataPtr.Sessions == nil {
|
|
|
|
emptySessionsMap(dataPtr)
|
|
|
|
}
|
|
|
|
log.Debugf("Unmarshalled YAML: %#v", *dataPtr)
|
2019-11-10 23:50:50 +00:00
|
|
|
} else {
|
|
|
|
// DB file does not exist, create an empty DB
|
2019-11-19 20:25:14 +00:00
|
|
|
emptySessionsMap(dataPtr)
|
2019-11-10 23:50:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 15:55:15 +00:00
|
|
|
return &SessionsYamlDB{
|
2019-11-10 23:50:50 +00:00
|
|
|
YamlDB: yamldb.YamlDB{
|
|
|
|
Path: path,
|
|
|
|
PathNew: path + ".new",
|
|
|
|
},
|
|
|
|
Data: dataPtr,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-12-05 19:56:12 +00:00
|
|
|
|
|
|
|
// Get retrieves a session value
|
|
|
|
func (s *Session) Get(key string) (string, error) {
|
|
|
|
switch key {
|
|
|
|
case "timezone":
|
|
|
|
return s.Timezone, nil
|
2022-01-05 21:04:22 +00:00
|
|
|
case "keeponline":
|
|
|
|
return fromBool(s.KeepOnline), nil
|
2022-02-08 18:49:49 +00:00
|
|
|
case "rawmessages":
|
|
|
|
return fromBool(s.RawMessages), nil
|
2022-02-14 00:05:59 +00:00
|
|
|
case "asciiarrows":
|
|
|
|
return fromBool(s.AsciiArrows), nil
|
2023-01-16 01:35:13 +00:00
|
|
|
case "oobmode":
|
|
|
|
return fromBool(s.OOBMode), nil
|
2023-03-18 21:43:11 +00:00
|
|
|
case "carbons":
|
|
|
|
return fromBool(s.Carbons), nil
|
2023-03-19 00:13:00 +00:00
|
|
|
case "hideids":
|
|
|
|
return fromBool(s.HideIds), nil
|
2019-12-05 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("Unknown session property")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap converts the session to a map
|
|
|
|
func (s *Session) ToMap() map[string]string {
|
|
|
|
m := make(map[string]string)
|
2022-01-05 21:04:22 +00:00
|
|
|
for _, configKey := range configKeys {
|
2019-12-05 19:56:12 +00:00
|
|
|
value, _ := s.Get(configKey)
|
|
|
|
m[configKey] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a session value
|
|
|
|
func (s *Session) Set(key string, value string) (string, error) {
|
|
|
|
switch key {
|
|
|
|
case "timezone":
|
|
|
|
s.Timezone = value
|
|
|
|
return value, nil
|
2022-01-05 21:04:22 +00:00
|
|
|
case "keeponline":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.KeepOnline = b
|
|
|
|
return value, nil
|
2022-02-08 18:49:49 +00:00
|
|
|
case "rawmessages":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.RawMessages = b
|
|
|
|
return value, nil
|
2022-02-14 00:05:59 +00:00
|
|
|
case "asciiarrows":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.AsciiArrows = b
|
|
|
|
return value, nil
|
2023-01-16 01:35:13 +00:00
|
|
|
case "oobmode":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.OOBMode = b
|
|
|
|
return value, nil
|
2023-03-18 21:43:11 +00:00
|
|
|
case "carbons":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.Carbons = b
|
|
|
|
return value, nil
|
2023-03-19 00:13:00 +00:00
|
|
|
case "hideids":
|
|
|
|
b, err := toBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s.HideIds = b
|
|
|
|
return value, nil
|
2019-12-05 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("Unknown session property")
|
|
|
|
}
|
2019-12-11 22:48:35 +00:00
|
|
|
|
2019-12-12 00:24:35 +00:00
|
|
|
// TimezoneToLocation tries to convert config timezone to location
|
2019-12-11 22:48:35 +00:00
|
|
|
func (s *Session) TimezoneToLocation() *time.Location {
|
|
|
|
time, err := time.Parse("-07:00", s.Timezone)
|
|
|
|
if err == nil {
|
|
|
|
return time.Location()
|
|
|
|
}
|
|
|
|
|
|
|
|
// default
|
|
|
|
return zeroLocation
|
|
|
|
}
|
2022-01-05 21:04:22 +00:00
|
|
|
|
|
|
|
func fromBool(b bool) string {
|
|
|
|
if b {
|
|
|
|
return "true"
|
|
|
|
} else {
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBool(s string) (bool, error) {
|
|
|
|
switch s {
|
|
|
|
case "true":
|
|
|
|
return true, nil
|
|
|
|
case "false":
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.New("Invalid boolean value")
|
|
|
|
}
|