File: signalplaintextbackupdatabase.h

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 (227 lines) | stat: -rw-r--r-- 8,572 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
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
/*
  Copyright (C) 2024-2025  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/>.
*/

#ifndef SIGNALPLAINTEXTBACKUPDATABASE_H_
#define SIGNALPLAINTEXTBACKUPDATABASE_H_

#include "../memsqlitedb/memsqlitedb.h"
#include "../logger/logger.h"
#include "../common_be.h"

#if __cpp_lib_span >= 202002L
#include <span>
#endif

class SignalPlaintextBackupDatabase
{
  MemSqliteDB d_database;
  bool d_ok;
  bool d_truncate;
  bool d_verbose;
  //std::set<std::string> d_warningsgiven;
  std::string d_countrycode;
  std::vector<std::pair<std::string, std::string>> d_addressmap;
 public:
#if __cpp_lib_span >= 202002L
  SignalPlaintextBackupDatabase(std::span<std::string const> const &sptbxmls, bool truncate, bool verbose,
                                std::vector<std::pair<std::string, std::string>> namemap, std::string const &namemap_file,
                                std::vector<std::pair<std::string, std::string>> const &addressmap, std::string const &addressmap_file,
                                std::string const &countrycode, bool autogroupnames);
#else
  SignalPlaintextBackupDatabase(std::vector<std::string> const &sptbxmls, bool truncate, bool verbose,
                                std::vector<std::pair<std::string, std::string>> namemap, std::string const &namemap_file,
                                std::vector<std::pair<std::string, std::string>> const &addressmap, std::string const &addressmap_file,
                                std::string const &countrycode, bool autogroupnames);
#endif
  SignalPlaintextBackupDatabase(SignalPlaintextBackupDatabase const &other) = delete;
  SignalPlaintextBackupDatabase(SignalPlaintextBackupDatabase &&other) = delete;
  SignalPlaintextBackupDatabase &operator=(SignalPlaintextBackupDatabase const &other) = delete;
  SignalPlaintextBackupDatabase &operator=(SignalPlaintextBackupDatabase &&other) = delete;
  inline bool ok() const;
  inline bool listContacts() const;

  friend class SignalBackup;
  friend class DummyBackup;

 private:
  inline std::string normalizePhoneNumber(std::string const &in, bool show = true);// const;
  std::set<std::string> norm_shown;
};

inline bool SignalPlaintextBackupDatabase::ok() const
{
  return d_ok;
}

inline bool SignalPlaintextBackupDatabase::listContacts() const
{
  SqliteDB::QueryResults addresses;
  d_database.exec("WITH adrs AS "
                  "("
                  "  SELECT DISTINCT address FROM smses UNION ALL SELECT DISTINCT sourceaddress AS address FROM smses"
                  ") "
                  "SELECT DISTINCT address FROM adrs WHERE address IS NOT NULL ORDER BY address", &addresses);
  //addresses.prettyPrint(d_truncate);

  if (addresses.rows() == 0) [[unlikely]]
    Logger::message("(no contacts found in XML file)");
  else
    Logger::message(" is_chat   ", std::setw(20), std::left, " address", std::setw(0), " :  name");

  for (unsigned int i = 0; i < addresses.rows(); ++i)
  {
    std::string cn = d_database.getSingleResultAs<std::string>("SELECT MAX(contact_name) FROM smses "
                                                               "WHERE contact_name IS NOT '(Unknown)' "
                                                               "  AND contact_name IS NOT NULL "
                                                               "  AND contact_name IS NOT '' "
                                                               "  AND address = ?", addresses.value(i, 0), std::string());
    long long int is_chat = d_database.getSingleResultAs<long long int>("SELECT COUNT(*) FROM smses WHERE address = ? AND skip = 0", addresses.value(i, 0), 0);
    Logger::message((is_chat > 0 ? "   (*)     " : "           "), std::setw(20), std::left, addresses(i, "address"), std::setw(0), " : \"", cn, "\"");
  }
  /*
  std::vector<std::string> numbers
    {
      {"00-1-202-688-5500"},
      {"(202)688-5500"},
      {"+12026885500"},
      {"011-1-202-688-5500"},
      {"011381688-5500"},
      {"00381688-5500"},
      {"+381688-5500"}
    };
  std::cout << std::endl;
  std::cout << std::endl;
  std::cout << std::endl;
  for (auto const &n : numbers)
    std::cout << std::setw(19) << std::left << n << " : " << std::setw(0) << normalizePhoneNumber(n) << std::endl;
  std::cout << std::endl;
  std::cout << std::endl;
  std::cout << std::endl;
  */
  return true;
}

inline std::string SignalPlaintextBackupDatabase::normalizePhoneNumber(std::string const &in, bool show)// const
{
  if (show && norm_shown.find(in) == norm_shown.end())
    Logger::message("normalizePhoneNumber in:  ", in);

  std::string result;

  // if in is group (phone1~phone2~phone3~etc), split and recurse...
  if (std::string::size_type pos = in.find('~');
      pos != std::string::npos &&
      pos != 0 &&
      pos != in.size() - 1)
  {
    std::string::size_type start = 0;
    std::string::size_type end;
    while ((end = in.find('~', start)) != std::string::npos)
    {
      result += normalizePhoneNumber(in.substr(start, end - start), false) + '~';
      start = end + 1;
    }
    // get last bit
    result += normalizePhoneNumber(in.substr(start), false);
  }
  else
  {
    result = in;

    // find in in address-map
    for (auto const &[from, to] : d_addressmap)
    {
      if (from == result)
      {
        if (show && norm_shown.find(in) == norm_shown.end())
        {
          norm_shown.insert(in);
          Logger::message("normalizePhoneNumber out: ", to);
        }
        return to;
      }
    }

#if __cpp_lib_erase_if >= 202002L
    unsigned int removed = std::erase_if(result, [](char c) { return (c < '0' || c > '9') && c != '+'; });
#else
    unsigned int removed = 0;
    result.erase(std::remove_if(result.begin(), result.end(),
                                [&](char c)
                                {
                                  if ((c < '0' || c > '9') && c != '+')
                                  {
                                    ++removed;
                                    return true;
                                  }
                                  return false;
                                }), result.end());
#endif

    if (removed > result.size())
    {
      if (show && norm_shown.find(in) == norm_shown.end())
      {
        norm_shown.insert(in);
        Logger::message("normalizePhoneNumber out: ", in);
      }
      return in;
    }

    if (STRING_STARTS_WITH(result, "00"))
      result = "+" + result.substr(STRLEN("00"));
    else if (STRING_STARTS_WITH(result, "011"))
      result = "+" + result.substr(STRLEN("011"));

    // Special case to deal with numbers that start with _two_ international call prefixes _ countrycodes:
    // eg (with countrycode '1'): 01110019999999999
    if (result.size() >= 15 && // we'll assume max number size of 15 (sources differ), the plus stands for (at least) 2 digits.
        !d_countrycode.empty() &&
        d_countrycode[0] == '+' &&
        STRING_STARTS_WITH(result, d_countrycode) &&
        (result.substr(d_countrycode.size(), (d_countrycode.size() - 1) + 2) == ("00" + d_countrycode.substr(1)) ||
         result.substr(d_countrycode.size(), (d_countrycode.size() - 1) + 3) == ("011" + d_countrycode.substr(1)))) [[unlikely]]
    {
      Logger::warning("Detected doubled prefix and countrycode in phone number (", in, ")");
      result = normalizePhoneNumber(result.substr(d_countrycode.size()), false);
    }

    if (result[0] != '+' && !d_countrycode.empty())
      result = d_countrycode + (result[0] == '0' ? result.substr(1) : result);
  }

  if (result.size() >= 9)
  {
    if (show && norm_shown.find(in) == norm_shown.end())
    {
      norm_shown.insert(in);
      Logger::message("normalizePhoneNumber out: ", result);
    }
    return result;
  }

  if (show && norm_shown.find(in) == norm_shown.end())
  {
    norm_shown.insert(in);
    Logger::message("normalizePhoneNumber out: ", in);
  }
  return in;
}

#endif