File: handledtcalltypemessage.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 (273 lines) | stat: -rw-r--r-- 10,727 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
  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::handleDTCallTypeMessage(SqliteDB const &ddb, std::string const &callid, long long int rowid,
                                           long long int ttid, long long int address, bool insertincompletedataforexport) const
{
  SqliteDB::QueryResults calldetails;
  uint64_t calltype = 0;
  std::any body = nullptr;
  if (ddb.containsTable("callsHistory"))
  {
    // new style! get details from this table
    /*
      sqlite> SELECT * from callsHistory;
    */

    if (!ddb.exec("SELECT "
                  "timestamp AS sent_at, "
                  "mode, "
                  "type, "
                  "peerId, "
                  "direction, "
                  "status"
                  " FROM callsHistory WHERE callId = ?", callid, &calldetails))
    {
      Logger::error("Failed to get call details from desktop database. Skipping.");
      return false;
    }

    if (calldetails.valueAsString(0, "mode") == "Direct")
    {
      /*
        sqlite> SELECT * from callsHistory;
        callId = 4690462596594407498
        peerId = 93722273-78e3-4136-8640-c8261969714c
        ringerId =
        mode = Direct
        type = Audio
        direction = Incoming
        status = Accepted
        timestamp = 1693839545545
      */
      if (calldetails.valueAsString(0, "type") == "Video")
      {
        if (calldetails.valueAsString(0, "direction") == "Incoming")
        {
          if (calldetails.valueAsString(0, "status") == "Accepted")
            calltype = Types::INCOMING_VIDEO_CALL_TYPE;
          else
            calltype = Types::MISSED_VIDEO_CALL_TYPE;
        }
        else // direction = outgoing
          calltype = Types::OUTGOING_VIDEO_CALL_TYPE;
      }
      else // type == "Audio"
      {
        if (calldetails.valueAsString(0, "direction") == "Incoming")
        {
          if (calldetails.valueAsString(0, "status") == "Accepted")
            calltype = Types::INCOMING_CALL_TYPE;
          else
            calltype = Types::MISSED_CALL_TYPE;
        }
        else// direction = outgoing
          calltype = Types::OUTGOING_CALL_TYPE;
      }
    }
    else if (calldetails.valueAsString(0, "mode") == "Group")
    {
      /*
         callId = 13523070632477333339
         peerId = 8FqrnHI/W7ZKnVmfmQEGX3oxRrFeriGy3KT2Ih/dAww=
         ringerId =
         mode = Group
         type = Group
         direction = Incoming
         status = Accepted
         timestamp = 1693840579538
      */


      // NOTE THE ANDROID BACKUP NEEDS THE CALL INITIATOR ADDRESS. THIS IS NOT AVAILABLE IN THE DESKTOP DATABASE ANYMORE
      if (!insertincompletedataforexport)
        return true;

      calltype = Types::GROUP_CALL_TYPE; // always video?
      ProtoBufParser<protobuffer::optional::STRING, protobuffer::optional::STRING, protobuffer::optional::INT64, protobuffer::repeated::STRING, protobuffer::optional::BOOL> groupcallbody;
      groupcallbody.addField<3>(calldetails.getValueAs<long long int>(0, "sent_at")); // cheating
      body = groupcallbody.getDataString();
    }
  }
  else
  {
    if (!ddb.exec("SELECT "
                  "COALESCE(sent_at, json_extract(json, '$.sent_at'), json_extract(json, '$.received_at_ms'), received_at, json_extract(json, '$.received_at')) AS sent_at,"
                  "json_extract(json, '$.callHistoryDetails.callMode') AS mode,"
                  "json_extract(json, '$.callHistoryDetails.creatorUuid') AS creator_uuid,"
                  "json_extract(json, '$.callHistoryDetails.eraId') AS era_id,"
                  "json_extract(json, '$.callHistoryDetails.startedTime') AS started_time,"
                  "IFNULL(json_extract(json, '$.callHistoryDetails.wasIncoming'), false) AS incoming,"
                  "IFNULL(json_extract(json, '$.callHistoryDetails.wasVideoCall'), false) AS video,"
                  "IFNULL(json_extract(json, '$.callHistoryDetails.wasDeclined'), false) AS declined,"
                  "IFNULL(json_extract(json, '$.callHistoryDetails.acceptedTime'), -1) AS accepted"
                  " FROM messages WHERE rowid = ?", rowid, &calldetails))
    {
      Logger::error("Failed to get call details from desktop database. Skipping.");
      return false;
    }

    //calldetails.prettyPrint();

    if (calldetails.valueAsString(0, "mode") == "Direct")
    {
      if (calldetails.getValueAs<long long int>(0, "video"))
      {
        if (calldetails.getValueAs<long long int>(0, "incoming"))
        {
          if (calldetails.getValueAs<long long int>(0, "accepted") >= 0)
            calltype = Types::INCOMING_VIDEO_CALL_TYPE;
          else
            calltype = Types::MISSED_VIDEO_CALL_TYPE;
        }
        else
          calltype = Types::OUTGOING_VIDEO_CALL_TYPE;
      }
      else
      {
        if (calldetails.getValueAs<long long int>(0, "incoming"))
        {
          if (calldetails.getValueAs<long long int>(0, "accepted") >= 0)
            calltype = Types::INCOMING_CALL_TYPE;
          else
            calltype = Types::MISSED_CALL_TYPE;
        }
        else
          calltype = Types::OUTGOING_CALL_TYPE;
      }
    }
    else if (calldetails.valueAsString(0, "mode") == "Group")
    {
      calltype = Types::GROUP_CALL_TYPE; // always video?

      //"callHistoryDetails":{"callMode":"Group","creatorUuid":"93722273-78e3-4136-8640-c8261969714c","eraId":"5d36bc8b0d6a1c5d","startedTime":1669314425500}
      // ->
      //                   _id = 18
      //              thread_id = 4
      //                address = 4
      //      address_device_id = 1
      //                 person =
      //                   date = 1669314409536
      //              date_sent = 1669314409536
      //            date_server = -1
      //               protocol =
      //                   read = 1
      //                 status = -1
      //                   type = 12
      //     reply_path_present =
      // delivery_receipt_count = 0
      //                subject =
      //                   body = ChA1ZDM2YmM4YjBkNmExYzVkEiQ5MzcyMjI3My03OGUzLTQxMzYtODY0MC1jODI2MTk2OTcxNGMYwOiR18ow
      //  mismatched_identities =
      //         service_center =
      //        subscription_id = -1
      //             expires_in = 0
      //         expire_started = 0
      //               notified = 0
      //     read_receipt_count = 0
      //           unidentified = 0
      //       reactions_unread = 0
      //    reactions_last_seen = 1669314426630
      //         remote_deleted = 0
      //     notified_timestamp = 1669314422531
      //            server_guid =
      //      receipt_timestamp = -1
      //           export_state =
      //               exported = 0
      //
      // BODY PROTO
      // message GroupCallUpdateDetails {
      //              string eraId                = 1;
      //              string startedCallUuid      = 2;
      //              int64  startedCallTimestamp = 3;
      //     repeated string inCallUuids          = 4;
      //              bool   isCallFull           = 5;
      // }
      //
      // body =
      // Field #1: 0A String Length = 16, Hex = 10, UTF8 = "5d36bc8b0d6a1c5d"
      // Field #2: 12 String Length = 36, Hex = 24, UTF8 = "93722273-78e3-41 ..." (total 36 chars)
      // Field #3: 18 Varint Value = 1669314409536, Hex = C0-E8-91-D7-CA-30
      ProtoBufParser<protobuffer::optional::STRING, protobuffer::optional::STRING, protobuffer::optional::INT64, protobuffer::repeated::STRING, protobuffer::optional::BOOL> groupcallbody;
      groupcallbody.addField<1>(calldetails.valueAsString(0, "era_id"));
      groupcallbody.addField<2>(calldetails.valueAsString(0, "creator_uuid"));
      groupcallbody.addField<3>(calldetails.getValueAs<long long int>(0, "started_time"));
      body = groupcallbody.getDataString();
    }
  }

  // if (d_databaseversion < 170)
  //{
  if (!d_database.tableContainsColumn(d_mms_table, "to_recipient_id"))
  {
    if (!insertRow(d_database.containsTable("sms") ? "sms" : d_mms_table,
                   {{"thread_id", ttid},
                    {d_database.containsTable("sms") ? d_sms_recipient_id : d_mms_recipient_id, address},
                    {d_database.containsTable("sms") ? d_sms_date_received : "date_received", calldetails.value(0, "sent_at")},
                    {"date_sent", calldetails.value(0, "sent_at")},
                    {"type", calltype},
                    {"body", body}}))
    {
      Logger::warning("Failed inserting into ", (d_database.containsTable("sms") ? "sms" : d_mms_table), ": call type message.");
      return false;
    }
  }
  else
  {
    // newer tables have a unique constraint on date_sent/thread_id/from_recipient_id, so
    // we try to get the first free date_sent
    long long int freedate = getFreeDateForMessage(calldetails.getValueAs<long long int>(0, "sent_at"), ttid, Types::isOutgoing(calltype) ? d_selfid : address);
    if (freedate == -1)
    {
      Logger::error("Getting free date for call type message");
      return false;
    }

    if (!insertRow(d_mms_table,
                   {{"thread_id", ttid},
                    {d_mms_recipient_id, Types::isOutgoing(calltype) ? d_selfid : address},
                    {"to_recipient_id", Types::isOutgoing(calltype) ? address : d_selfid},
                    {"date_received", freedate},//calldetails.value(0, "sent_at")},
                    {"date_sent", freedate},//calldetails.value(0, "sent_at")},
                    {"type", calltype},
                    {"body", body}}))
    {
      Logger::warning("Failed inserting into ", d_mms_table, ": call type message.");
      return false;
    }
  }
  //}
  /*
  else // dbv >=170 -> call into 'call' table???, or also in mms -> just additional details?
  {
  insertRow("call",
            {{call_id, ???},
             {message_id, ???},
             {peer, address?},
             {type, calltype?},
             {direction, ...},
             {event, ???}
            }
  }
  */

  return true;
}