-
Notifications
You must be signed in to change notification settings - Fork 65
/
format.go
226 lines (198 loc) · 5.16 KB
/
format.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
package cmd
import (
"encoding/csv"
"encoding/xml"
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/xeals/signal-back/signal"
"github.com/xeals/signal-back/types"
)
// Format fulfils the `format` subcommand.
var Format = cli.Command{
Name: "format",
Usage: "Read and format the backup file",
UsageText: "Parse and transform the backup file into other formats.\nValid formats include: CSV, XML, RAW.",
CustomHelpTemplate: SubcommandHelp,
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "format, f",
Usage: "output the backup as `FORMAT`",
Value: "xml",
},
cli.StringFlag{
Name: "message, m",
Usage: "format `TYPE` messages",
Value: "sms",
},
cli.StringFlag{
Name: "output, o",
Usage: "write decrypted format to `FILE`",
},
}, coreFlags...),
Action: func(c *cli.Context) error {
bf, err := setup(c)
if err != nil {
return err
}
var out io.Writer
if c.String("output") != "" {
var file *os.File
file, err = os.OpenFile(c.String("output"), os.O_CREATE|os.O_WRONLY, 0644)
out = io.Writer(file)
if err != nil {
return errors.Wrap(err, "unable to open output file")
}
defer func() {
if file.Close() != nil {
log.Fatalf("unable to close output file: %s", err.Error())
}
}()
} else {
out = os.Stdout
}
switch strings.ToLower(c.String("format")) {
case "csv":
err = CSV(bf, strings.ToLower(c.String("message")), out)
case "xml":
err = XML(bf, out)
case "json":
// err = formatJSON(bf, out)
return errors.New("JSON is still TODO")
case "raw":
err = Raw(bf, out)
default:
return errors.Errorf("format %s not recognised", c.String("format"))
}
if err != nil {
return errors.Wrap(err, "failed to format output")
}
return nil
},
}
// JSON <undefined>
func JSON(bf *types.BackupFile, out io.Writer) error {
return nil
}
// CSV dumps the raw backup data into a comma-separated value format.
func CSV(bf *types.BackupFile, message string, out io.Writer) error {
ss := make([][]string, 0)
for {
f, err := bf.Frame()
if err != nil {
break
}
// Attachment needs removing
if a := f.GetAttachment(); a != nil {
err := bf.DecryptAttachment(a, ioutil.Discard)
if err != nil {
return errors.Wrap(err, "unable to chew through attachment")
}
}
if stmt := f.GetStatement(); stmt != nil {
if (*stmt.Statement)[:15] == "INSERT INTO "+message {
ss = append(ss, types.StatementToStringArray(stmt))
}
}
}
w := csv.NewWriter(out)
var headers []string
if message == "mms" {
headers = types.MMSCSVHeaders
} else {
headers = types.SMSCSVHeaders
}
if err := w.Write(headers); err != nil {
return errors.Wrap(err, "unable to write CSV headers")
}
for _, sms := range ss {
if err := w.Write(sms); err != nil {
return errors.Wrap(err, "unable to format CSV")
}
}
w.Flush()
return errors.WithMessage(w.Error(), "unable to end CSV writer or something")
}
// XML formats the backup into the same XML format as SMS Backup & Restore
// uses. Layout described at their website
// http://synctech.com.au/fields-in-xml-backup-files/
func XML(bf *types.BackupFile, out io.Writer) error {
smses := &types.SMSes{}
mmses := map[uint64]types.MMS{}
mmsParts := map[uint64][]types.MMSPart{}
for {
f, err := bf.Frame()
if err != nil {
break
}
// Attachment needs removing
if a := f.GetAttachment(); a != nil {
err := bf.DecryptAttachment(a, ioutil.Discard)
if err != nil {
return errors.Wrap(err, "unable to chew through attachment")
}
}
if stmt := f.GetStatement(); stmt != nil {
// Only use SMS/MMS statements
if strings.HasPrefix(*stmt.Statement, "INSERT INTO sms") {
sms, err := types.NewSMSFromStatement(stmt)
if err == nil {
smses.SMS = append(smses.SMS, *sms)
}
}
if strings.HasPrefix(*stmt.Statement, "INSERT INTO mms") {
// TODO this
log.Println("MMS export not yet supported")
}
if strings.HasPrefix(*stmt.Statement, "INSERT INTO part") {
// TODO also this
}
}
}
for id, p := range mmsParts {
if mms, ok := mmses[id]; ok {
mms.Parts = p
smses.MMS = append(smses.MMS, mms)
}
}
smses.Count = len(smses.SMS)
x, err := xml.MarshalIndent(smses, "", " ")
if err != nil {
return errors.Wrap(err, "unable to format XML")
}
w := types.NewMultiWriter(out)
w.W([]byte("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>\n"))
w.W([]byte("<?xml-stylesheet type=\"text/xsl\" href=\"sms.xsl\" ?>\n"))
w.W(x)
return errors.WithMessage(w.Error(), "failed to write out XML")
}
// Raw performs an ever plainer dump than CSV, and is largely unusable for any purpose outside
// debugging.
func Raw(bf *types.BackupFile, out io.Writer) error {
var (
err error
f *signal.BackupFrame
)
for {
f, err = bf.Frame()
if err != nil {
break
}
// Attachment needs removing
if a := f.GetAttachment(); a != nil {
err := bf.DecryptAttachment(a, ioutil.Discard)
if err != nil {
return errors.Wrap(err, "unable to chew through attachment")
}
}
if stmt := f.GetStatement(); stmt != nil {
out.Write([]byte(stmt.String()))
out.Write([]byte{'\n'})
}
}
return nil
}