-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
142 lines (117 loc) · 3.04 KB
/
main.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2021, 2023 Tim van der Molen <tim@kariliq.nl>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"bufio"
"log"
"os"
"path/filepath"
"strings"
"github.com/tbvdm/go-cli"
"github.com/tbvdm/go-openbsd"
"github.com/tbvdm/sigtop/getopt"
"github.com/tbvdm/sigtop/safestorage"
"github.com/tbvdm/sigtop/signal"
)
type cmdStatus int
const (
cmdOK cmdStatus = iota
cmdError
cmdUsage
)
type cmdEntry struct {
name string
alias string
usage string
exec func([]string) cmdStatus
}
var cmdEntries = []cmdEntry{
cmdCheckDatabaseEntry,
cmdExportAvatarsEntry,
cmdExportAttachmentsEntry,
cmdExportDatabaseEntry,
cmdExportKeyEntry,
cmdExportMessagesEntry,
cmdQueryDatabaseEntry,
}
func main() {
cli.SetLog()
if len(os.Args) < 2 {
cli.ExitUsage("command", "[argument ...]")
}
cmd := command(os.Args[1])
if cmd == nil {
log.Fatalln("invalid command:", os.Args[1])
}
switch cmd.exec(os.Args[2:]) {
case cmdError:
os.Exit(1)
case cmdUsage:
cli.ExitUsage(cmd.name, cmd.usage)
}
}
func command(name string) *cmdEntry {
for _, cmd := range cmdEntries {
if name == cmd.name || name == cmd.alias {
return &cmd
}
}
return nil
}
func encryptionKeyFromFile(keyfile getopt.Arg) (*safestorage.RawEncryptionKey, error) {
if !keyfile.Set() {
return nil, nil
}
system, file, found := strings.Cut(keyfile.String(), ":")
if !found {
system, file = file, system
}
f := os.Stdin
if file != "-" {
var err error
if f, err = os.Open(file); err != nil {
return nil, err
}
defer f.Close()
}
s := bufio.NewScanner(f)
s.Scan()
if s.Err() != nil {
return nil, s.Err()
}
key := safestorage.RawEncryptionKey{
Key: append([]byte{}, s.Bytes()...),
OS: system,
}
return &key, nil
}
func unveilSignalDir(dir string) error {
if err := openbsd.Unveil(dir, "r"); err != nil {
return err
}
// SQLite/SQLCipher needs to create the WAL and shared-memory files if
// they don't exist already. See https://www.sqlite.org/tempfiles.html.
walFile := filepath.Join(dir, signal.DatabaseFile+"-wal")
shmFile := filepath.Join(dir, signal.DatabaseFile+"-shm")
if err := openbsd.Unveil(walFile, "rwc"); err != nil {
return err
}
if err := openbsd.Unveil(shmFile, "rwc"); err != nil {
return err
}
return nil
}
func recipientFilename(rpt *signal.Recipient, ext string) string {
return sanitiseFilename(rpt.DetailedDisplayName() + ext)
}