2018-03-13 14:27:12 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-13 14:27:12 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-13 14:27:12 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-13 14:27:12 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-13 14:27:12 +01:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-04-24 15:51:40 +02:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-03-19 00:18:36 +01:00
|
|
|
"time"
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/config"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/interface"
|
|
|
|
"maunium.net/go/gomuks/matrix"
|
2018-03-13 14:27:12 +01:00
|
|
|
)
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// Gomuks is the wrapper for everything.
|
2018-03-19 00:18:36 +01:00
|
|
|
type Gomuks struct {
|
2018-04-18 17:35:24 +02:00
|
|
|
ui ifc.GomuksUI
|
|
|
|
matrix *matrix.Container
|
|
|
|
config *config.Config
|
|
|
|
stop chan bool
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// NewGomuks creates a new Gomuks instance with everything initialized,
|
|
|
|
// but does not start it.
|
2020-04-28 21:00:37 +02:00
|
|
|
func NewGomuks(uiProvider ifc.UIProvider, configDir, dataDir, cacheDir, downloadDir string) *Gomuks {
|
2018-03-19 00:18:36 +01:00
|
|
|
gmx := &Gomuks{
|
2018-04-18 17:35:24 +02:00
|
|
|
stop: make(chan bool, 1),
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-18 20:24:03 +01:00
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
gmx.config = config.NewConfig(configDir, dataDir, cacheDir, downloadDir)
|
2018-04-18 17:35:24 +02:00
|
|
|
gmx.ui = uiProvider(gmx)
|
2018-03-21 22:29:58 +01:00
|
|
|
gmx.matrix = matrix.NewContainer(gmx)
|
2018-05-16 20:58:04 +02:00
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
gmx.config.LoadAll()
|
2018-04-18 17:35:24 +02:00
|
|
|
gmx.ui.Init()
|
|
|
|
|
|
|
|
debug.OnRecover = gmx.ui.Finish
|
2018-03-13 20:58:43 +01:00
|
|
|
|
|
|
|
return gmx
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 19:09:44 +02:00
|
|
|
func (gmx *Gomuks) Version() string {
|
2020-06-24 15:18:46 +02:00
|
|
|
return "v0.1.1"
|
2020-05-10 19:09:44 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// Save saves the active session and message history.
|
2018-03-22 18:51:20 +01:00
|
|
|
func (gmx *Gomuks) Save() {
|
2018-05-17 15:29:15 +02:00
|
|
|
gmx.config.SaveAll()
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// StartAutosave calls Save() every minute until it receives a stop signal
|
|
|
|
// on the Gomuks.stop channel.
|
2018-03-22 18:51:20 +01:00
|
|
|
func (gmx *Gomuks) StartAutosave() {
|
2018-04-18 17:35:24 +02:00
|
|
|
defer debug.Recover()
|
2018-03-22 18:51:20 +01:00
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-06-15 16:04:08 +02:00
|
|
|
if gmx.config.AuthCache.InitialSyncDone {
|
|
|
|
gmx.Save()
|
|
|
|
}
|
2018-03-22 18:51:20 +01:00
|
|
|
case val := <-gmx.stop:
|
|
|
|
if val {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// Stop stops the Matrix syncer, the tview app and the autosave goroutine,
|
|
|
|
// then saves everything and calls os.Exit(0).
|
2019-06-15 00:11:51 +02:00
|
|
|
func (gmx *Gomuks) Stop(save bool) {
|
2018-04-09 22:45:54 +02:00
|
|
|
debug.Print("Disconnecting from Matrix...")
|
2018-03-23 13:44:36 +01:00
|
|
|
gmx.matrix.Stop()
|
2018-04-09 22:45:54 +02:00
|
|
|
debug.Print("Cleaning up UI...")
|
2018-04-18 17:35:24 +02:00
|
|
|
gmx.ui.Stop()
|
2018-03-23 13:44:36 +01:00
|
|
|
gmx.stop <- true
|
2019-06-15 00:11:51 +02:00
|
|
|
if save {
|
|
|
|
gmx.Save()
|
|
|
|
}
|
2018-03-23 13:44:36 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start opens a goroutine for the autosave loop and starts the tview app.
|
|
|
|
//
|
|
|
|
// If the tview app returns an error, it will be passed into panic(), which
|
|
|
|
// will be recovered as specified in Recover().
|
2018-03-19 00:18:36 +01:00
|
|
|
func (gmx *Gomuks) Start() {
|
2018-04-18 17:35:24 +02:00
|
|
|
_ = gmx.matrix.InitClient()
|
|
|
|
|
2018-04-24 15:51:40 +02:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-c
|
2019-06-15 00:11:51 +02:00
|
|
|
gmx.Stop(true)
|
2018-04-24 15:51:40 +02:00
|
|
|
}()
|
|
|
|
|
2018-03-22 18:51:20 +01:00
|
|
|
go gmx.StartAutosave()
|
2018-04-18 17:35:24 +02:00
|
|
|
if err := gmx.ui.Start(); err != nil {
|
2018-03-13 20:58:43 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// Matrix returns the MatrixContainer instance.
|
|
|
|
func (gmx *Gomuks) Matrix() ifc.MatrixContainer {
|
2018-03-13 20:58:43 +01:00
|
|
|
return gmx.matrix
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// Config returns the Gomuks config instance.
|
2018-03-19 00:18:36 +01:00
|
|
|
func (gmx *Gomuks) Config() *config.Config {
|
2018-03-13 20:58:43 +01:00
|
|
|
return gmx.config
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// UI returns the Gomuks UI instance.
|
2018-03-19 00:18:36 +01:00
|
|
|
func (gmx *Gomuks) UI() ifc.GomuksUI {
|
2018-03-13 20:58:43 +01:00
|
|
|
return gmx.ui
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|