This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
forked from xeals/signal-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
246 lines (219 loc) · 6.15 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package cmd
import (
"encoding/csv"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/urfave/cli"
"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.",
CustomHelpTemplate: SubcommandHelp,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format, f",
Usage: "output the backup as `FORMAT`",
Value: "xml",
},
cli.StringFlag{
Name: "log, l",
Usage: "write logging output to `FILE`",
},
cli.StringFlag{
Name: "message, m",
Usage: "format `TYPE` messages",
Value: "sms",
},
cli.StringFlag{
Name: "password, p",
Usage: "use `PASS` as password for backup file",
},
cli.StringFlag{
Name: "pwdfile, P",
Usage: "read password from `FILE`",
},
cli.StringFlag{
Name: "output, o",
Usage: "write decrypted format to `FILE`",
},
},
Action: func(c *cli.Context) error {
bf, err := setup(c)
if err != nil {
return err
}
var out io.Writer
if c.String("output") != "" {
out, err = os.OpenFile(c.String("output"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return errors.Wrap(err, "unable to open output file")
}
} 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")
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{}
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") {
ps := stmt.Parameters
unix := time.Unix(int64(*ps[5].IntegerParameter)/1000, 0)
readable := unix.Format("Jan 02, 2006 3:04:05 PM")
sms := types.SMS{
Protocol: ps[7].IntegerParameter,
Address: ps[2].GetStringParamter(),
Date: strconv.FormatUint(ps[5].GetIntegerParameter(), 10),
Type: translateSMSType(ps[10].GetIntegerParameter()),
Subject: ps[13].StringParamter,
Body: ps[14].GetStringParamter(),
ServiceCenter: ps[16].StringParamter,
Read: ps[8].GetIntegerParameter(),
Status: int64(*ps[9].IntegerParameter),
DateSent: ps[6].IntegerParameter,
ReadableDate: &readable,
ContactName: ps[4].StringParamter,
}
smses.SMS = append(smses.SMS, sms)
}
if strings.HasPrefix(*stmt.Statement, "INSERT INTO mms") {
// TODO this
log.Println("MMS export not yet supported")
}
}
}
smses.Count = len(smses.SMS)
x, err := xml.Marshal(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' ?>`))
w.W([]byte(`<?xml-stylesheet type="text/xsl" href="sms.xsl"?>`))
w.W(x)
return errors.WithMessage(w.Error(), "failed to write out XML")
}
func translateSMSType(t uint64) types.SMSType {
// Just get the lower 8 bits, because everything else is masking.
// https://github.com/signalapp/Signal-Android/blob/master/src/org/thoughtcrime/securesms/database/MmsSmsColumns.java
v := uint8(t)
switch v {
// STANDARD
case 1: // standard standard
return types.SMSReceived
case 2: // standard sent
return types.SMSSent
case 3: // standard draft
return types.SMSDraft
case 4: // standard outbox
return types.SMSOutbox
case 5: // standard failed
return types.SMSFailed
case 6: // standard queued
return types.SMSQueued
// SIGNAL
case 20: // signal received
return types.SMSReceived
case 21: // signal outbox
return types.SMSOutbox
case 22: // signal sending
return types.SMSQueued
case 23: // signal sent
return types.SMSSent
case 24: // signal failed
return types.SMSFailed
case 25: // pending secure SMS fallback
return types.SMSQueued
case 26: // pending insecure SMS fallback
return types.SMSQueued
case 27: // signal draft
return types.SMSDraft
default:
panic(fmt.Sprintf("undefined SMS type: %#v\nplease report this issue, as well as (if possible) details about the SMS,\nsuch as whether it was sent, received, drafted, etc.", t))
}
}