File: importwachat.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 (214 lines) | stat: -rw-r--r-- 7,219 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
/*
  Copyright (C) 2021-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"

//DEPRECATED

//#include <iomanip>

/*
  Limitations and requirements

  - no media (yet?)
  - no mentions YET
  - filename must exactly and uniquely identify an existing conversation (group or contact name) in the Signal backup
  - messages have format '<timestamp><author>: <message>' where timestamp is passed as 'fmt', author exactly
    and uniquely identifies an existing contact in the Signal backup and contains no colons (':')
  -
*/

// bool SignalBackup::importWAChat(std::string const &file, std::string const &fmt, std::string const &self)
// {
//   std::ifstream chatfile(file);
//   if (!chatfile.is_open())
//   {
//     std::cout << bepaald::bold_on << "ERROR" << bepaald::bold_off
//               << " opening file '" << file << "' for reading." << std::endl;
//     return false;
//   }


//   // get global address (recipient_id of chat partner for 1-on-1, group_id for groupchats
//   std::string chatname = file.substr(0, file.length() - STRLEN(".txt"));
//   std::string globaladdress;
//   bool isgroup = false;
//   SqliteDB::QueryResults results;
//   std::map<std::string, std::string> name_to_recipientid;

//   std::cout << "Looking for conversation: '" << file.substr(0, file.length() - STRLEN(".txt")) << "'" << std::endl;

//   if (!d_database.exec("SELECT recipient._id, recipient.group_id, COALESCE(groups.title, recipient.system_display_name, recipient.profile_joined_name, recipient.phone) AS 'identifier' "
//                        "FROM recipient "
//                        "LEFT JOIN groups ON recipient.group_id == groups.group_id "
//                        "WHERE identifier == ?", chatname, &results))
//     return false;

//   //results.prettyPrint();

//   if (results.rows() == 1)
//   {
//     globaladdress = results.valueAsString(0, "_id");
//     name_to_recipientid[chatname] = globaladdress;
//     if (results.valueHasType<std::string>(0, "group_id"))
//     {
//       isgroup = true;
//       if (self.empty())
//       {
//         std::cout << "Error: dealing with group chat, but self-id not supplied. No way of determining which messages are outgoing and which are incoming." << std::endl;
//         return false;
//       }
//     }
//   }
//   else
//   {
//     std::cout << "Failed to find conversation partner/group for chat: '" << file << "' : query returned " << results.rows() << " results." << std::endl;
//     return false;
//   }

//   // get thread id from recipient
//   long long int tid = -1;
//   if (!d_database.exec("SELECT _id FROM thread WHERE " + d_thread_recipient_id + " == ?", globaladdress, &results))
//     return false;

//   if (results.rows() != 1)
//   {
//     std::cout << "Failed to find thread for chat. Query returned " << results.rows() << " results." << std::endl;
//     return false;
//   }

//   tid = results.getValueAs<long long int>(0, "_id");

//   std::cout << "Importing messages into thread: " << tid << std::endl;

//   std::string line;
//   std::string author;

//   // scan for chat participants, we need these upfront to reference in mentions and update delevery_receipts for groups
//   while (std::getline(chatfile, line))
//   {
//     std::tm tmb = {};
//     std::istringstream ss(line);
//     ss >> std::get_time(&tmb, fmt.c_str());
//     if (ss.fail())
//       continue;
//     else
//     {
//       std::getline(ss, author, ':');
//       if (ss.eof())
//       {
//         //std::cout << "No colon => some type of status message?" << std::endl;
//         author.clear();
//         continue;
//       }

//       // get the address and save in map.
//       if (name_to_recipientid.find(author) == name_to_recipientid.end())
//       {
//         if (!d_database.exec("SELECT _id, COALESCE(system_display_name, profile_joined_name, phone) AS 'identifier' "
//                              "FROM recipient WHERE identifier == ?", author, &results))
//           return false;
//         if (results.rows() != 1)
//           return false;

//         name_to_recipientid[author] = results.valueAsString(0, "_id");
//       }

//     }
//   }

//   chatfile.clear();
//   chatfile.seekg(0);
//   line.clear();
//   author.clear();
//   long long int time = 0;
//   long long int previous_time = 0;
//   long long int previous_time_adj = 0;
//   std::string message;
//   uint64_t count = 0;

//   // scan for messages
//   while (std::getline(chatfile, line))
//   {
//     std::tm tmb = {};
//     std::istringstream ss(line);
//     ss >> std::get_time(&tmb, fmt.c_str());
//     if (ss.fail())
//     {
//       std::cout << "Invalid timefmt (" << fmt << ") => continue previous message" << std::endl;

//       ss.clear();
//       ss.seekg(0);

//       std::string msg_cnt;
//       if (!std::getline(ss, msg_cnt))
//       {
//         std::cout << "Some error reading the line" << std::endl;
//         continue;
//       }
//       message += "\n" + msg_cnt;
//     }
//     else
//     {
//       // found start of new message, deal with previous
//       if (time != 0 && !author.empty())
//       {
//         if (!handleWAMessage(tid, time, chatname, author, message, self, isgroup, name_to_recipientid))
//           return false;
//         else
//           ++count;
//       }

//       // get a unique sequentially later time
//       time = std::mktime(&tmb) * 1000;
//       if (time == previous_time)
//         time = previous_time_adj + 1;
//       else
//         previous_time = time;
//       previous_time_adj = time;
//       //std::cout << "Time: " << std::put_time(&tmb, "%c") << " (" << time << ")" << std::endl;

//       std::getline(ss, author, ':');
//       if (ss.eof())
//       {
//         std::cout << "No colon => some type of status message? SKIPPING LINE : '" << author << "'" << std::endl;
//         author.clear();
//         continue;
//       }
//       //std::cout << "Author: " << author << std::endl;

//       std::getline(ss, message);
//       message = message.substr(1); // remove the single whitespace after colon??

//       //std::cout << "Message: " << message << std::endl;
//     }
//   }

//   // deal with last message
//   if (time != 0 && !author.empty())
//   {
//     if (!handleWAMessage(tid, time, chatname, author, message, self, isgroup, name_to_recipientid))
//       return false;
//     else
//       ++count;
//   }
//   std::cout << "Imported " << count << " messages from file '" << file << "'" << std::endl;

//   return true;
// }