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 (
|
2022-11-21 21:42:42 +01:00
|
|
|
"errors"
|
2022-04-17 20:21:10 +02:00
|
|
|
"fmt"
|
2018-03-13 14:27:12 +01:00
|
|
|
"os"
|
2018-04-24 15:51:40 +02:00
|
|
|
"os/signal"
|
2022-11-21 21:42:42 +01:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-04-17 20:21:10 +02:00
|
|
|
"strings"
|
2018-04-24 15:51:40 +02:00
|
|
|
"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"
|
2022-04-15 11:53:09 +02:00
|
|
|
ifc "maunium.net/go/gomuks/interface"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/matrix"
|
2018-03-13 14:27:12 +01:00
|
|
|
)
|
|
|
|
|
2022-04-17 20:21:10 +02:00
|
|
|
// Information to find out exactly which commit gomuks was built from.
|
|
|
|
// These are filled at build time with the -X linker flag.
|
|
|
|
var (
|
|
|
|
Tag = "unknown"
|
|
|
|
Commit = "unknown"
|
|
|
|
BuildTime = "unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Version is the version number of gomuks. Changed manually when making a release.
|
2022-11-19 16:24:57 +01:00
|
|
|
Version = "0.3.0"
|
2022-04-17 20:21:10 +02:00
|
|
|
// VersionString is the gomuks version, plus commit information. Filled in init() using the build-time values.
|
|
|
|
VersionString = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if len(Tag) > 0 && Tag[0] == 'v' {
|
|
|
|
Tag = Tag[1:]
|
|
|
|
}
|
|
|
|
if Tag != Version {
|
|
|
|
suffix := ""
|
|
|
|
if !strings.HasSuffix(Version, "+dev") {
|
|
|
|
suffix = "+dev"
|
|
|
|
}
|
|
|
|
if len(Commit) > 8 {
|
|
|
|
Version = fmt.Sprintf("%s%s.%s", Version, suffix, Commit[:8])
|
|
|
|
} else {
|
|
|
|
Version = fmt.Sprintf("%s%s.unknown", Version, suffix)
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 21:42:42 +01:00
|
|
|
VersionString = fmt.Sprintf("gomuks %s (%s with %s)", Version, BuildTime, runtime.Version())
|
2022-04-17 20:21:10 +02: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 {
|
2022-04-17 20:21:10 +02:00
|
|
|
return Version
|
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) {
|
2022-04-15 21:29:58 +02:00
|
|
|
go gmx.internalStop(save)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gmx *Gomuks) internalStop(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()
|
|
|
|
}
|
2022-11-19 16:14:31 +01:00
|
|
|
debug.Print("Exiting process")
|
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() {
|
2022-11-21 21:42:42 +01:00
|
|
|
err := gmx.matrix.InitClient(true)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, matrix.ErrServerOutdated) {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, strings.Replace(err.Error(), "homeserver", gmx.config.HS, 1))
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr)
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "See `%s --help` if you want to skip this check or clear all data.\n", os.Args[0])
|
|
|
|
os.Exit(4)
|
|
|
|
} else if strings.HasPrefix(err.Error(), "failed to check server versions") {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to check versions supported by server:", errors.Unwrap(err))
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr)
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Modify %s if the server has moved.\n", filepath.Join(gmx.config.Dir, "config.yaml"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "See `%s --help` if you want to skip this check or clear all data.\n", os.Args[0])
|
|
|
|
os.Exit(5)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 17:35:24 +02:00
|
|
|
|
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()
|
2022-11-21 21:42:42 +01: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
|
|
|
}
|