~eliasnaur/scatter

scatter/cmd/scatter/main.go -rw-r--r-- 2.1 KiB
8050178dElias Naur README.md: note that the project is no longer maintained 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"

	"gioui.org/app"
)

var (
	imapHost = flag.String("imap", "", "specify the IMAP host and port")
	smtpHost = flag.String("smtp", "", "specify the SMTP host and port")
	user     = flag.String("user", "", "specify the username (e.g. user@example.com)")
	pass     = flag.String("pass", "", "specify the password")

	dataDir string
)

var (
	clientOnce   sync.Once
	singleClient *Client
)

func init() {
	log.SetPrefix("scatter: ")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Scatter is a federated implementation of the Signal protocol on email.\n\n")
		fmt.Fprintf(os.Stderr, "Usage:\n\n\tscatter [flags] <pkg>\n\n")
		flag.PrintDefaults()
		os.Exit(2)
	}
	conf, err := os.UserConfigDir()
	if err == nil {
		conf = filepath.Join(conf, "scatter")
	}
	flag.StringVar(&dataDir, "store", conf, "directory for storing configuration and messages")
	flag.Parse()
}

func getClient() *Client {
	clientOnce.Do(func() {
		cl, err := initClient()
		if err != nil {
			errorf("scatter: %v", err)
		}
		singleClient = cl
	})
	return singleClient
}

func initClient() (*Client, error) {
	dir := dataDir
	if dir == "" {
		var err error
		dir, err = app.DataDir()
		if err != nil {
			errorf("scatter: %v", err)
		}
		dir = filepath.Join(dir, "scatter")
	}
	if err := os.MkdirAll(dir, 0700); err != nil {
		return nil, err
	}
	store, err := OpenStore(filepath.Join(dir, "store.db"))
	if err != nil {
		return nil, err
	}
	acc, err := store.GetAccount()
	if err != nil {
		store.Close()
		return nil, err
	}
	if *imapHost != "" {
		acc.IMAPHost = *imapHost
	}
	if *smtpHost != "" {
		acc.SMTPHost = *smtpHost
	}
	if *user != "" {
		acc.User = *user
	}
	if *pass != "" {
		acc.Password = *pass
	}
	store.SetAccount(acc)
	cl, err := NewClient(store)
	if err != nil {
		store.Close()
		return nil, err
	}
	cl.Run()
	singleClient = cl
	return cl, nil
}

func main() {
	uiMain()
}

func errorf(format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, format+"\n", args...)
	time.Sleep(5 * time.Second)
	os.Exit(1)
}