2018-04-18 17:35:24 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-04-18 17:35:24 +02: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-04-18 17:35:24 +02: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-04-18 17:35:24 +02: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-04-18 17:35:24 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-04-06 09:57:24 +02:00
|
|
|
"errors"
|
2018-04-18 17:35:24 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-09-20 05:46:33 +02:00
|
|
|
"os/exec"
|
2019-04-06 09:57:24 +02:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-04-27 14:02:52 +02:00
|
|
|
"strings"
|
2018-04-18 17:35:24 +02:00
|
|
|
"time"
|
|
|
|
|
2023-10-05 22:33:21 +02:00
|
|
|
pasteclip "golang.design/x/clipboard"
|
2022-11-21 21:42:42 +01:00
|
|
|
flag "maunium.net/go/mauflag"
|
|
|
|
|
2018-04-18 17:35:24 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2022-04-15 11:53:09 +02:00
|
|
|
ifc "maunium.net/go/gomuks/interface"
|
2022-11-21 21:42:42 +01:00
|
|
|
"maunium.net/go/gomuks/matrix"
|
2018-04-18 17:35:24 +02:00
|
|
|
"maunium.net/go/gomuks/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
var MainUIProvider ifc.UIProvider = ui.NewGomuksUI
|
|
|
|
|
2022-11-21 21:42:42 +01:00
|
|
|
var wantVersion = flag.MakeFull("v", "version", "Show the version of gomuks", "false").Bool()
|
|
|
|
var clearCache = flag.MakeFull("c", "clear-cache", "Clear the cache directory instead of starting", "false").Bool()
|
|
|
|
var clearData = flag.Make().LongKey("clear-all-data").Usage("Clear all data instead of starting").Default("false").Bool()
|
|
|
|
var skipVersionCheck = flag.MakeFull("s", "skip-version-check", "Skip the homeserver version checks at startup and login", "false").Bool()
|
|
|
|
var wantHelp, _ = flag.MakeHelpFlag()
|
|
|
|
|
2018-04-18 17:35:24 +02:00
|
|
|
func main() {
|
2022-11-21 21:42:42 +01:00
|
|
|
flag.SetHelpTitles(
|
|
|
|
"gomuks - A terminal Matrix client written in Go.",
|
|
|
|
"gomuks [-vch] [--clear-all-data]",
|
|
|
|
)
|
|
|
|
err := flag.Parse()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else if *wantHelp {
|
|
|
|
flag.PrintHelp()
|
|
|
|
return
|
|
|
|
} else if *wantVersion {
|
|
|
|
fmt.Println(VersionString)
|
|
|
|
return
|
|
|
|
}
|
2019-04-27 14:02:52 +02:00
|
|
|
debugDir := os.Getenv("DEBUG_DIR")
|
|
|
|
if len(debugDir) > 0 {
|
|
|
|
debug.LogDirectory = debugDir
|
|
|
|
}
|
|
|
|
debugLevel := strings.ToLower(os.Getenv("DEBUG"))
|
|
|
|
if debugLevel == "1" || debugLevel == "t" || debugLevel == "true" {
|
|
|
|
debug.RecoverPrettyPanic = false
|
|
|
|
debug.DeadlockDetection = true
|
2023-07-06 20:26:08 +02:00
|
|
|
debug.WriteLogs = true
|
2019-04-27 14:02:52 +02:00
|
|
|
}
|
|
|
|
debug.Initialize()
|
2018-04-18 17:35:24 +02:00
|
|
|
defer debug.Recover()
|
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
var configDir, dataDir, cacheDir, downloadDir string
|
|
|
|
|
|
|
|
configDir, err = UserConfigDir()
|
2019-04-06 09:57:24 +02:00
|
|
|
if err != nil {
|
2020-04-28 21:00:37 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to get config directory:", err)
|
2019-04-06 09:57:24 +02:00
|
|
|
os.Exit(3)
|
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
dataDir, err = UserDataDir()
|
2019-04-06 09:57:24 +02:00
|
|
|
if err != nil {
|
2020-04-28 21:00:37 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to get data directory:", err)
|
2019-04-06 09:57:24 +02:00
|
|
|
os.Exit(3)
|
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
cacheDir, err = UserCacheDir()
|
2020-04-08 14:49:42 +02:00
|
|
|
if err != nil {
|
2020-04-28 21:00:37 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to get cache directory:", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
downloadDir, err = UserDownloadDir()
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to get download directory:", err)
|
2020-04-08 14:49:42 +02:00
|
|
|
os.Exit(3)
|
|
|
|
}
|
2019-04-06 09:57:24 +02:00
|
|
|
|
2021-03-11 21:53:13 +01:00
|
|
|
debug.Print("Config directory:", configDir)
|
|
|
|
debug.Print("Data directory:", dataDir)
|
|
|
|
debug.Print("Cache directory:", cacheDir)
|
|
|
|
debug.Print("Download directory:", downloadDir)
|
2020-04-08 14:49:42 +02:00
|
|
|
|
2022-11-21 21:42:42 +01:00
|
|
|
matrix.SkipVersionCheck = *skipVersionCheck
|
2020-04-28 21:00:37 +02:00
|
|
|
gmx := NewGomuks(MainUIProvider, configDir, dataDir, cacheDir, downloadDir)
|
2020-06-14 06:36:43 +02:00
|
|
|
|
2023-10-05 22:33:21 +02:00
|
|
|
// initialize clipboard
|
|
|
|
err = pasteclip.Init()
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to initialize clipboard")
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:42:42 +01:00
|
|
|
if *clearCache {
|
|
|
|
debug.Print("Clearing cache as requested by CLI flag")
|
|
|
|
gmx.config.Clear()
|
|
|
|
fmt.Printf("Cleared cache at %s\n", gmx.config.CacheDir)
|
|
|
|
return
|
|
|
|
} else if *clearData {
|
|
|
|
debug.Print("Clearing all data as requested by CLI flag")
|
|
|
|
gmx.config.Clear()
|
|
|
|
gmx.config.ClearData()
|
|
|
|
_ = os.RemoveAll(gmx.config.Dir)
|
|
|
|
fmt.Printf("Cleared cache at %s, data at %s and config at %s\n", gmx.config.CacheDir, gmx.config.DataDir, gmx.config.Dir)
|
|
|
|
return
|
2020-06-14 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 17:35:24 +02:00
|
|
|
gmx.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 gmx.Start().")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2019-04-06 09:57:24 +02:00
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
func getRootDir(subdir string) string {
|
|
|
|
rootDir := os.Getenv("GOMUKS_ROOT")
|
|
|
|
if rootDir == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return filepath.Join(rootDir, subdir)
|
|
|
|
}
|
|
|
|
|
2019-04-06 09:57:24 +02:00
|
|
|
func UserCacheDir() (dir string, err error) {
|
|
|
|
dir = os.Getenv("GOMUKS_CACHE_HOME")
|
2020-04-28 21:00:37 +02:00
|
|
|
if dir == "" {
|
|
|
|
dir = getRootDir("cache")
|
|
|
|
}
|
2019-04-06 09:57:24 +02:00
|
|
|
if dir == "" {
|
|
|
|
dir, err = os.UserCacheDir()
|
|
|
|
dir = filepath.Join(dir, "gomuks")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
func UserDataDir() (dir string, err error) {
|
|
|
|
dir = os.Getenv("GOMUKS_DATA_HOME")
|
2019-04-06 09:57:24 +02:00
|
|
|
if dir != "" {
|
|
|
|
return
|
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
|
|
|
|
return UserConfigDir()
|
|
|
|
}
|
2022-02-16 18:37:11 +01:00
|
|
|
dir = getRootDir("data")
|
2020-04-28 21:00:37 +02:00
|
|
|
if dir == "" {
|
2022-02-16 18:37:11 +01:00
|
|
|
dir = os.Getenv("XDG_DATA_HOME")
|
2020-04-28 21:00:37 +02:00
|
|
|
}
|
|
|
|
if dir == "" {
|
|
|
|
dir = os.Getenv("HOME")
|
2019-04-06 09:57:24 +02:00
|
|
|
if dir == "" {
|
2021-09-20 05:46:33 +02:00
|
|
|
return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
|
2019-04-06 09:57:24 +02:00
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
dir = filepath.Join(dir, ".local", "share")
|
2019-04-06 09:57:24 +02:00
|
|
|
}
|
|
|
|
dir = filepath.Join(dir, "gomuks")
|
|
|
|
return
|
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
|
2021-09-20 05:46:33 +02:00
|
|
|
func getXDGUserDir(name string) (dir string, err error) {
|
|
|
|
cmd := exec.Command("xdg-user-dir", name)
|
|
|
|
var out strings.Builder
|
|
|
|
cmd.Stdout = &out
|
|
|
|
err = cmd.Run()
|
|
|
|
dir = strings.TrimSpace(out.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:00:37 +02:00
|
|
|
func UserDownloadDir() (dir string, err error) {
|
2021-09-20 05:46:33 +02:00
|
|
|
dir = os.Getenv("GOMUKS_DOWNLOAD_HOME")
|
|
|
|
if dir != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir, _ = getXDGUserDir("DOWNLOAD")
|
|
|
|
if dir != "" {
|
|
|
|
return
|
|
|
|
}
|
2020-04-28 21:00:37 +02:00
|
|
|
dir, err = os.UserHomeDir()
|
|
|
|
dir = filepath.Join(dir, "Downloads")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserConfigDir() (dir string, err error) {
|
|
|
|
dir = os.Getenv("GOMUKS_CONFIG_HOME")
|
|
|
|
if dir == "" {
|
2020-05-12 14:35:05 +02:00
|
|
|
dir = getRootDir("config")
|
2020-04-28 21:00:37 +02:00
|
|
|
}
|
|
|
|
if dir == "" {
|
|
|
|
dir, err = os.UserConfigDir()
|
|
|
|
dir = filepath.Join(dir, "gomuks")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|