File: scanmissingattachments.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 (144 lines) | stat: -rw-r--r-- 6,958 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
/*
  Copyright (C) 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"

void SignalBackup::scanMissingAttachments() const
{
  // get 'missing' attachments
  SqliteDB::QueryResults res;
  d_database.exec("SELECT _id," +
                  (d_database.tableContainsColumn(d_part_table, "unique_id") ? "unique_id"s : "-1 AS unique_id"s) +
                  " FROM " + d_part_table, &res);
  std::vector<std::pair<long long int, long long int>> missing;
  for (unsigned int i = 0; i < res.rows(); ++i)
    if (/*true || */d_attachments.find({res.getValueAs<long long int>(i, "_id"), res.getValueAs<long long int>(i, "unique_id")}) == d_attachments.end())
      missing.emplace_back(std::make_pair(res.getValueAs<long long int>(i, "_id"), res.getValueAs<long long int>(i, "unique_id")));

  Logger::message("Got ", missing.size(), " attachments with data not found");

  for (unsigned int i = 0; i < missing.size(); ++i)
  {
    Logger::message_start("Checking ", (i + 1), " of ", missing.size(), ": ",  missing[i].first, ",", missing[i].second, "... ");

    SqliteDB::QueryResults isquote;
    d_database.exec("SELECT " + d_part_mid + " FROM " + d_part_table + " WHERE _id = ?" +
                    (d_database.tableContainsColumn(d_part_table, "unique_id") ?
                     " AND unique_id = " + bepaald::toString(missing[i].second) : "") +
                    " AND quote = 1", missing[i].first, &isquote);
    if (isquote.rows())
    {
      long long int mid = isquote.getValueAs<long long int>(0, d_part_mid);

      d_database.exec("SELECT _id FROM " + d_mms_table + " WHERE quote_missing = 1 AND _id = ?", mid, &res);
      if (res.rows() == 1)
      {
        if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
          Logger::message_end("OK, EXPECTED (quote missing)");
        else
          Logger::message_end("FALSE HIT! (quote missing)");
        continue;
      }

      // quote_missing is not always (often not?) set to 1 even if quote is missing, so manually check
      if (d_database.tableContainsColumn(d_mms_table, "remote_deleted"))
      {
        d_database.exec("SELECT _id FROM " + d_mms_table + " WHERE remote_deleted IS 1 AND " + d_mms_date_sent + " IS (SELECT quote_id FROM " + d_mms_table + " WHERE _id = ?)",
                        mid, &res);
        if (res.rows()) // can be more than 1 row if messages were doubled (before date_sent (=quote_id) had UNIQUE constraint)
        {
          if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
            Logger::message_end("OK, EXPECTED (original message missing (remote deleted))");
          else
            Logger::message_end("FALSE HIT! (remote delete)");
          continue;
        }
      }

      if (d_database.getSingleResultAs<long long int>("SELECT IFNULL(quote_id, 0)_id FROM " + d_mms_table + " WHERE _id = ?", mid, 0) != 0)
      {
        d_database.exec("SELECT _id FROM " + d_mms_table + " WHERE " +
                        d_mms_date_sent + " IS (SELECT quote_id FROM " + d_mms_table + " WHERE _id = ?)"
                        " AND "
                        "thread_id IS (SELECT thread_id FROM " + d_mms_table + " WHERE _id = ?)",
                        {mid, mid}, &res);
        if (res.rows() == 0)
        {
          if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
            Logger::message_end("OK, EXPECTED (original message missing (deleted))");
          else
            Logger::message_end("FALSE HIT! (delete)");
          continue;
        }
      }
    }

    d_database.exec("SELECT " + d_part_ct + " FROM " + d_part_table + " WHERE "
                    "quote = 1 "
                    "AND _id = ?" +
                    (d_database.tableContainsColumn(d_part_table, "unique_id") ? " AND unique_id = " + bepaald::toString(missing[i].second) : ""s) +
                    " AND " + d_part_ct + " NOT LIKE 'image%' AND " + d_part_ct + " NOT LIKE 'video%'",
                    missing[i].first, &res);
    if (res.rows() == 1)
    {
      if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
        Logger::message_end("OK, EXPECTED (type = ",res.valueAsString(0, 0), ")");
      else
        Logger::message_end("FALSE HIT! (type)");
      continue;
    }

    d_database.exec("SELECT " + d_part_pending + " FROM " + d_part_table + " WHERE " + d_part_pending + " IS NOT 0 AND _id = ?" +
                    (d_database.tableContainsColumn(d_part_table, "unique_id") ? " AND unique_id = " + bepaald::toString(missing[i].second) : ""s),
                    missing[i].first, &res);
    if (res.rows() == 1)
    {
      if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
        Logger::message_end("OK, EXPECTED (pending_push = ", res.valueAsString(0, 0), ")");
      else
        Logger::message_end("FALSE HIT! (pending_push)");
      continue;
    }

    d_database.exec("SELECT " + d_part_ct + " FROM " + d_part_table + " WHERE _id = ?" +
                    (d_database.tableContainsColumn(d_part_table, "unique_id") ? " AND unique_id = " + bepaald::toString(missing[i].second) : ""s),
                    missing[i].first, &res);
    if (res.rows() == 1 && res(0, d_part_ct) == "application/x-signal-view-once")
    {
      if (d_attachments.find({missing[i].first, missing[i].second}) == d_attachments.end())
        Logger::message_end("OK, EXPECTED (content_type = application/x-signal-view-once)");
      else
        Logger::message_end("FALSE HIT! (view_once)");
      continue;
    }

    if (d_attachments.find({missing[i].first, missing[i].second}) != d_attachments.end())
    {
      Logger::message_end("OK, EXPECTED (no special circumstances, but not missing)");
      continue;
    }

    Logger::message(Logger::Control::BOLD, "UNEXPECTED!", Logger::Control::NORMAL, " details:");
    d_database.exec("SELECT quote," + d_part_ct + "," + d_part_pending + " FROM " + d_part_table + " WHERE _id = ?" +
                    (d_database.tableContainsColumn(d_part_table, "unique_id") ? " AND unique_id = " + bepaald::toString(missing[i].second) : ""s),
                    missing[i].first, &res);
    res.prettyPrint(d_truncate);
  }

}