File: scramble.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (84 lines) | stat: -rw-r--r-- 2,701 bytes parent folder | download
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
/*
  Copyright (C) 2022-2024  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  signalbackup-tools is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "signalbackup.ih"

bool SignalBackup::scrambleHelper(std::string const &table, std::vector<std::string> const &columns) const
{
  Logger::message("Scrambling ", table);

  std::string selectquery = "SELECT _id";
  for (unsigned int i = 0; i < columns.size(); ++i)
    selectquery += "," + columns[i];
  selectquery += " FROM " + table;

  SqliteDB::QueryResults res;
  d_database.exec(selectquery, &res);

  for (unsigned int i = 0; i < res.rows(); ++i)
  {
    std::vector<std::string> str;
    for (unsigned int j = 0; j < columns.size(); ++j)
    {
      str.push_back(res.valueAsString(i, columns[j]));
      std::replace_if(str.back().begin(), str.back().end(), [](char c)
      {
        return c != ' ' && std::islower(c);
      }, 'x');
      std::replace_if(str.back().begin(), str.back().end(), [](char c)
      {
        return c != ' ' && !std::islower(c);
      }, 'X');
    }

    std::string updatequery = "UPDATE " + table + " SET ";
    for (unsigned int j = 0; j < columns.size(); ++j)
      updatequery += columns[j] + " = ?" + ((j == columns.size() - 1) ? " WHERE _id = ?" : ", ");

    std::vector<std::any> values;
    for (unsigned int j = 0; j < str.size(); ++j)
      values.push_back(str[j]);
    values.push_back(res.getValueAs<long long int>(i, "_id"));

    if (!d_database.exec(updatequery, values))
      return false;
  }
  return true;
}

bool SignalBackup::scramble() const
{
  if (d_database.containsTable("sms"))
    if (!scrambleHelper("sms", {"body"}))
      return false;

  if (!scrambleHelper(d_mms_table, {"body", "quote_body"}))
    return false;

  if (!scrambleHelper("recipient", {d_recipient_system_joined_name, "profile_joined_name", d_recipient_profile_given_name, "profile_family_name", "system_family_name", "system_given_name"}))
    return false;

  if (!scrambleHelper("thread", {"snippet"}))
    return false;

  if (!scrambleHelper("groups", {"title"}))
    return false;

  return true;
}