gomuks/gomuks.go

166 lines
4.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 (
"fmt"
"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"
"maunium.net/go/gomuks/ui"
2018-03-14 21:19:26 +01:00
"maunium.net/go/tview"
)
2018-03-23 13:44:36 +01:00
// Gomuks is the wrapper for everything.
type Gomuks struct {
app *tview.Application
ui *ui.GomuksUI
matrix *matrix.Container
debugMode bool
config *config.Config
2018-03-22 18:51:20 +01:00
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(enableDebug bool) *Gomuks {
2018-03-13 20:58:43 +01:00
configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
gmx := &Gomuks{
app: tview.NewApplication(),
stop: make(chan bool, 1),
debugMode: enableDebug,
2018-03-13 20:58:43 +01:00
}
2018-03-18 20:24:03 +01:00
gmx.config = config.NewConfig(configDir)
gmx.ui = ui.NewGomuksUI(gmx)
2018-03-21 22:29:58 +01:00
gmx.matrix = matrix.NewContainer(gmx)
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-04-01 09:25:15 +02:00
_ = gmx.matrix.InitClient()
2018-03-13 20:58:43 +01:00
main := gmx.ui.InitViews()
gmx.app.SetRoot(main, true)
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 gmx.Recover()
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...")
2018-03-23 13:44:36 +01:00
gmx.app.Stop()
gmx.stop <- true
gmx.Save()
os.Exit(0)
}
// Recover recovers a panic, closes the tcell screen and either re-panics or
// shows an user-friendly message about the panic depending on whether or not
// the debug mode is enabled.
func (gmx *Gomuks) Recover() {
2018-03-15 17:21:14 +01:00
if p := recover(); p != nil {
if gmx.App().GetScreen() != nil {
gmx.App().GetScreen().Fini()
}
if gmx.debugMode {
panic(p)
} else {
debug.PrettyPanic()
}
2018-03-15 17:21:14 +01:00
}
}
2018-03-23 13:44:36 +01:00
// 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() {
defer gmx.Recover()
2018-03-22 18:51:20 +01:00
go gmx.StartAutosave()
2018-03-13 20:58:43 +01:00
if err := gmx.app.Run(); err != nil {
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
// App returns the tview Application instance.
func (gmx *Gomuks) App() *tview.Application {
2018-03-13 20:58:43 +01:00
return gmx.app
}
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
}
2018-03-13 20:58:43 +01:00
func main() {
enableDebug := len(os.Getenv("DEBUG")) > 0
NewGomuks(enableDebug).Start()
// We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
time.Sleep(5 * time.Second)
fmt.Println("Unexpected exit by return from Gomuks#Start().")
os.Exit(2)
}