gomuks/gomuks.go

127 lines
3.1 KiB
Go
Raw Normal View History

// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"os"
"path/filepath"
"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) *Gomuks {
2018-03-13 20:58:43 +01:00
configDir := filepath.Join(os.Getenv("HOME"), ".config/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)
gmx.ui = uiProvider(gmx)
2018-03-21 22:29:58 +01:00
gmx.matrix = matrix.NewContainer(gmx)
gmx.ui.Init()
debug.OnRecover = gmx.ui.Finish
2018-03-13 20:58:43 +01:00
gmx.config.Load()
2018-03-21 22:29:58 +01:00
if len(gmx.config.UserID) > 0 {
2018-04-01 09:25:15 +02:00
_ = gmx.config.LoadSession(gmx.config.UserID)
}
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() {
2018-03-14 23:14:39 +01:00
if gmx.config.Session != nil {
debug.Print("Saving session...")
2018-04-01 09:25:15 +02:00
_ = gmx.config.Session.Save()
2018-03-14 23:14:39 +01:00
}
debug.Print("Saving history...")
2018-03-22 18:51:20 +01:00
gmx.ui.MainView().SaveAllHistory()
}
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:
gmx.Save()
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).
func (gmx *Gomuks) Stop() {
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
gmx.Save()
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-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
}