| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "os" |
| 23 | "os/signal" |
| 24 | "path/filepath" |
| 25 | "runtime" |
| 26 | "strings" |
| 27 | "syscall" |
| 28 | "time" |
| 29 | |
| 30 | "maunium.net/go/gomuks/config" |
| 31 | "maunium.net/go/gomuks/debug" |
| 32 | ifc "maunium.net/go/gomuks/interface" |
| 33 | "maunium.net/go/gomuks/matrix" |
| 34 | ) |
| 35 | |
| 36 | // Information to find out exactly which commit gomuks was built from. |
| 37 | // These are filled at build time with the -X linker flag. |
| 38 | var ( |
| 39 | Tag = "unknown" |
| 40 | Commit = "unknown" |
| 41 | BuildTime = "unknown" |
| 42 | ) |
| 43 | |
| 44 | var ( |
| 45 | // Version is the version number of gomuks. Changed manually when making a release. |
| 46 | Version = "0.3.1" |
| 47 | // VersionString is the gomuks version, plus commit information. Filled in init() using the build-time values. |
| 48 | VersionString = "" |
| 49 | ) |
| 50 | |
| 51 | func init() { |
| 52 | if len(Tag) > 0 && Tag[0] == 'v' { |
| 53 | Tag = Tag[1:] |
| 54 | } |
| 55 | if Tag != Version { |
| 56 | suffix := "" |
| 57 | if !strings.HasSuffix(Version, "+dev") { |
| 58 | suffix = "+dev" |
| 59 | } |
| 60 | if len(Commit) > 8 { |
| 61 | Version = fmt.Sprintf("%s%s.%s", Version, suffix, Commit[:8]) |
| 62 | } else { |
| 63 | Version = fmt.Sprintf("%s%s.unknown", Version, suffix) |
| 64 | } |
| 65 | } |
| 66 | VersionString = fmt.Sprintf("gomuks %s (%s with %s)", Version, BuildTime, runtime.Version()) |
| 67 | } |
| 68 | |
| 69 | // Gomuks is the wrapper for everything. |
| 70 | type Gomuks struct { |
| 71 | ui ifc.GomuksUI |
| 72 | matrix *matrix.Container |
| 73 | config *config.Config |
| 74 | stop chan bool |
| 75 | } |
| 76 | |
| 77 | // NewGomuks creates a new Gomuks instance with everything initialized, |
| 78 | // but does not start it. |
| 79 | func NewGomuks(uiProvider ifc.UIProvider, configDir, dataDir, cacheDir, downloadDir string) *Gomuks { |
| 80 | gmx := &Gomuks{ |
| 81 | stop: make(chan bool, 1), |
| 82 | } |
| 83 | |
| 84 | gmx.config = config.NewConfig(configDir, dataDir, cacheDir, downloadDir) |
| 85 | gmx.ui = uiProvider(gmx) |
| 86 | gmx.matrix = matrix.NewContainer(gmx) |
| 87 | |
| 88 | gmx.config.LoadAll() |
| 89 | gmx.ui.Init() |
| 90 | |
| 91 | debug.OnRecover = gmx.ui.Finish |
| 92 | |
| 93 | return gmx |
| 94 | } |
| 95 | |
| 96 | func (gmx *Gomuks) Version() string { |
| 97 | return Version |
| 98 | } |
| 99 | |
| 100 | // Save saves the active session and message history. |
| 101 | func (gmx *Gomuks) Save() { |
| 102 | gmx.config.SaveAll() |
| 103 | } |
| 104 | |
| 105 | // StartAutosave calls Save() every minute until it receives a stop signal |
| 106 | // on the Gomuks.stop channel. |
| 107 | func (gmx *Gomuks) StartAutosave() { |
| 108 | defer debug.Recover() |
| 109 | ticker := time.NewTicker(time.Minute) |
| 110 | for { |
| 111 | select { |
| 112 | case <-ticker.C: |
| 113 | if gmx.config.AuthCache.InitialSyncDone { |
| 114 | gmx.Save() |
| 115 | } |
| 116 | case val := <-gmx.stop: |
| 117 | if val { |
| 118 | return |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Stop stops the Matrix syncer, the tview app and the autosave goroutine, |
| 125 | // then saves everything and calls os.Exit(0). |
| 126 | func (gmx *Gomuks) Stop(save bool) { |
| 127 | go gmx.internalStop(save) |
| 128 | } |
| 129 | |
| 130 | func (gmx *Gomuks) internalStop(save bool) { |
| 131 | debug.Print("Disconnecting from Matrix...") |
| 132 | gmx.matrix.Stop() |
| 133 | debug.Print("Cleaning up UI...") |
| 134 | gmx.ui.Stop() |
| 135 | gmx.stop <- true |
| 136 | if save { |
| 137 | gmx.Save() |
| 138 | } |
| 139 | debug.Print("Exiting process") |
| 140 | os.Exit(0) |
| 141 | } |
| 142 | |
| 143 | // Start opens a goroutine for the autosave loop and starts the tview app. |
| 144 | // |
| 145 | // If the tview app returns an error, it will be passed into panic(), which |
| 146 | // will be recovered as specified in Recover(). |
| 147 | func (gmx *Gomuks) Start() { |
| 148 | err := gmx.matrix.InitClient(true) |
| 149 | if err != nil { |
| 150 | if errors.Is(err, matrix.ErrServerOutdated) { |
| 151 | _, _ = fmt.Fprintln(os.Stderr, strings.Replace(err.Error(), "homeserver", gmx.config.HS, 1)) |
| 152 | _, _ = fmt.Fprintln(os.Stderr) |
| 153 | _, _ = fmt.Fprintf(os.Stderr, "See `%s --help` if you want to skip this check or clear all data.\n", os.Args[0]) |
| 154 | os.Exit(4) |
| 155 | } else if strings.HasPrefix(err.Error(), "failed to check server versions") { |
| 156 | _, _ = fmt.Fprintln(os.Stderr, "Failed to check versions supported by server:", errors.Unwrap(err)) |
| 157 | _, _ = fmt.Fprintln(os.Stderr) |
| 158 | _, _ = fmt.Fprintf(os.Stderr, "Modify %s if the server has moved.\n", filepath.Join(gmx.config.Dir, "config.yaml")) |
| 159 | _, _ = fmt.Fprintf(os.Stderr, "See `%s --help` if you want to skip this check or clear all data.\n", os.Args[0]) |
| 160 | os.Exit(5) |
| 161 | } else { |
| 162 | panic(err) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | c := make(chan os.Signal, 1) |
| 167 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 168 | go func() { |
| 169 | <-c |
| 170 | gmx.Stop(true) |
| 171 | }() |
| 172 | |
| 173 | go gmx.StartAutosave() |
| 174 | if err = gmx.ui.Start(); err != nil { |
| 175 | panic(err) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Matrix returns the MatrixContainer instance. |
| 180 | func (gmx *Gomuks) Matrix() ifc.MatrixContainer { |
| 181 | return gmx.matrix |
| 182 | } |
| 183 | |
| 184 | // Config returns the Gomuks config instance. |
| 185 | func (gmx *Gomuks) Config() *config.Config { |
| 186 | return gmx.config |
| 187 | } |
| 188 | |
| 189 | // UI returns the Gomuks UI instance. |
| 190 | func (gmx *Gomuks) UI() ifc.GomuksUI { |
| 191 | return gmx.ui |
| 192 | } |