gomuks/gomuks.go

130 lines
3.1 KiB
Go
Raw Normal View History

// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 Tulir Asokan
//
// 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
// 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.
//
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/>.
package main
import (
"os"
2018-04-24 15:51:40 +02:00
"os/signal"
"syscall"
"time"
2018-03-18 20:24:03 +01:00
"maunium.net/go/gomuks/config"
"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-23 13:44:36 +01:00
// Gomuks is the wrapper for everything.
type Gomuks struct {
ui ifc.GomuksUI
matrix *matrix.Container
config *config.Config
stop chan bool
2018-03-13 20:58:43 +01:00
}
2018-03-23 13:44:36 +01:00
// NewGomuks creates a new Gomuks instance with everything initialized,
// but does not start it.
func NewGomuks(uiProvider ifc.UIProvider, configDir, dataDir, cacheDir, downloadDir string) *Gomuks {
gmx := &Gomuks{
stop: make(chan bool, 1),
2018-03-13 20:58:43 +01:00
}
2018-03-18 20:24:03 +01:00
gmx.config = config.NewConfig(configDir, dataDir, cacheDir, downloadDir)
gmx.ui = uiProvider(gmx)
2018-03-21 22:29:58 +01:00
gmx.matrix = matrix.NewContainer(gmx)
gmx.config.LoadAll()
gmx.ui.Init()
debug.OnRecover = gmx.ui.Finish
2018-03-13 20:58:43 +01:00
return gmx
}
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() {
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() {
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) {
debug.Print("Disconnecting from Matrix...")
2018-03-23 13:44:36 +01:00
gmx.matrix.Stop()
debug.Print("Cleaning up UI...")
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().
func (gmx *Gomuks) Start() {
_ = 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()
if err := gmx.ui.Start(); err != nil {
2018-03-13 20:58:43 +01:00
panic(err)
}
}
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-23 13:44:36 +01:00
// Config returns the Gomuks config instance.
func (gmx *Gomuks) Config() *config.Config {
2018-03-13 20:58:43 +01:00
return gmx.config
}
2018-03-23 13:44:36 +01:00
// UI returns the Gomuks UI instance.
func (gmx *Gomuks) UI() ifc.GomuksUI {
2018-03-13 20:58:43 +01:00
return gmx.ui
}