Skip to content

Commit

Permalink
Support CSV MMS output
Browse files Browse the repository at this point in the history
Closes #5. Closes #8.

Also clean up types/raw a fair bit because it ended up not being used.
  • Loading branch information
xeals committed Apr 26, 2018
1 parent 9561bbd commit d0e70d0
Show file tree
Hide file tree
Showing 2 changed files with 330 additions and 201 deletions.
44 changes: 16 additions & 28 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ var Format = cli.Command{
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",
Expand Down Expand Up @@ -64,7 +69,7 @@ var Format = cli.Command{

switch strings.ToLower(c.String("format")) {
case "csv":
err = CSV(bf, out)
err = CSV(bf, strings.ToLower(c.String("message")), out)
case "xml":
err = XML(bf, out)
case "json":
Expand All @@ -86,8 +91,8 @@ func JSON(bf *types.BackupFile, out io.Writer) error {
return nil
}

// CSV <undefined>
func CSV(bf *types.BackupFile, out io.Writer) error {
// 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()
Expand All @@ -104,38 +109,21 @@ func CSV(bf *types.BackupFile, out io.Writer) error {
}

if stmt := f.GetStatement(); stmt != nil {
if strings.HasPrefix(*stmt.Statement, "INSERT INTO sms") {
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([]string{
"ID",
"THREAD_ID",
"ADDRESS",
"ADDRESS_DEVICE_ID",
"PERSON",
"DATE_RECEIVED",
"DATE_SENT",
"PROTOCOL",
"READ",
"STATUS",
"TYPE",
"REPLY_PATH_PRESENT",
"DELIVERY_RECEIPT_COUNT",
"SUBJECT",
"BODY",
"MISMATCHED_IDENTITIES",
"SERVICE_CENTER",
"SUBSCRIPTION_ID",
"EXPIRES_IN",
"EXPIRE_STARTED",
"NOTIFIED",
"READ_RECEIPT_COUNT",
}); err != nil {
if err := w.Write(headers); err != nil {
return errors.Wrap(err, "unable to write CSV headers")
}

Expand Down
Loading

0 comments on commit d0e70d0

Please sign in to comment.