-
Notifications
You must be signed in to change notification settings - Fork 12
/
cmd_export_messages.go
245 lines (210 loc) · 5.08 KB
/
cmd_export_messages.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// 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 (
"errors"
"io/fs"
"log"
"os"
"github.com/tbvdm/go-openbsd"
"github.com/tbvdm/sigtop/at"
"github.com/tbvdm/sigtop/errio"
"github.com/tbvdm/sigtop/getopt"
"github.com/tbvdm/sigtop/signal"
)
type formatMode int
const (
formatJSON formatMode = iota
formatText
formatTextShort
)
type msgMode struct {
format formatMode
incremental bool
}
var cmdExportMessagesEntry = cmdEntry{
name: "export-messages",
alias: "msg",
usage: "[-i] [-c conversation] [-d signal-directory] [-f format] [-k [system:]keyfile] [-s interval] [directory]",
exec: cmdExportMessages,
}
func cmdExportMessages(args []string) cmdStatus {
mode := msgMode{
format: formatText,
incremental: false,
}
getopt.ParseArgs("c:d:f:ik:p:s:", args)
var dArg, kArg, sArg getopt.Arg
var selectors []string
for getopt.Next() {
switch getopt.Option() {
case 'c':
selectors = append(selectors, getopt.OptionArg().String())
case 'd':
dArg = getopt.OptionArg()
case 'f':
switch arg := getopt.OptionArg().String(); arg {
case "json":
mode.format = formatJSON
case "text":
mode.format = formatText
case "text-short":
mode.format = formatTextShort
default:
log.Fatalf("invalid format: %s", arg)
}
case 'i':
mode.incremental = true
case 'p':
log.Print("-p is deprecated; use -k instead")
fallthrough
case 'k':
kArg = getopt.OptionArg()
case 's':
sArg = getopt.OptionArg()
}
}
if err := getopt.Err(); err != nil {
log.Fatal(err)
}
args = getopt.Args()
var exportDir string
switch len(args) {
case 0:
exportDir = "."
case 1:
exportDir = args[0]
if err := os.Mkdir(exportDir, 0777); err != nil && !errors.Is(err, fs.ErrExist) {
log.Fatal(err)
}
default:
return cmdUsage
}
key, err := encryptionKeyFromFile(kArg)
if err != nil {
log.Fatal(err)
}
var signalDir string
if dArg.Set() {
signalDir = dArg.String()
} else {
var err error
signalDir, err = signal.DesktopDir()
if err != nil {
log.Fatal(err)
}
}
var ival signal.Interval
if sArg.Set() {
var err error
ival, err = parseInterval(sArg.String())
if err != nil {
log.Fatal(err)
}
}
if err := unveilSignalDir(signalDir); err != nil {
log.Fatal(err)
}
if err := openbsd.Unveil(exportDir, "rwc"); err != nil {
log.Fatal(err)
}
// For SQLite/SQLCipher
if err := openbsd.Unveil("/dev/urandom", "r"); err != nil {
log.Fatal(err)
}
if err := openbsd.Pledge("stdio rpath wpath cpath flock"); err != nil {
log.Fatal(err)
}
var ctx *signal.Context
if key == nil {
ctx, err = signal.Open(signalDir)
} else {
ctx, err = signal.OpenWithEncryptionKey(signalDir, key)
}
if err != nil {
log.Fatal(err)
}
defer ctx.Close()
if !exportMessages(ctx, exportDir, mode, selectors, ival) {
return cmdError
}
return cmdOK
}
func exportMessages(ctx *signal.Context, dir string, mode msgMode, selectors []string, ival signal.Interval) bool {
d, err := at.Open(dir)
if err != nil {
log.Print(err)
return false
}
defer d.Close()
convs, err := selectConversations(ctx, selectors)
if err != nil {
log.Print(err)
return false
}
ret := true
for _, conv := range convs {
if err = exportConversationMessages(ctx, d, &conv, mode, ival); err != nil {
log.Print(err)
ret = false
}
}
return ret
}
func exportConversationMessages(ctx *signal.Context, d at.Dir, conv *signal.Conversation, mode msgMode, ival signal.Interval) error {
msgs, err := ctx.ConversationMessages(conv, ival)
if err != nil {
return err
}
if len(msgs) == 0 {
return nil
}
f, err := conversationFile(d, conv, mode)
if err != nil {
return err
}
ew := errio.NewWriter(f)
switch mode.format {
case formatJSON:
err = jsonWriteMessages(ew, msgs)
case formatText:
err = textWriteMessages(ew, msgs)
case formatTextShort:
err = textShortWriteMessages(ew, msgs)
}
if err != nil {
f.Close()
return err
}
return f.Close()
}
func conversationFile(d at.Dir, conv *signal.Conversation, mode msgMode) (*os.File, error) {
var ext string
switch mode.format {
case formatJSON:
ext = ".json"
case formatText, formatTextShort:
ext = ".txt"
}
flags := os.O_WRONLY | os.O_CREATE
if !mode.incremental {
flags |= os.O_EXCL
}
name := recipientFilename(conv.Recipient, ext)
f, err := d.OpenFile(name, flags, 0666)
if err != nil {
return nil, err
}
return f, nil
}