forked from tbvdm/sigbak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbk-reaction.c
273 lines (218 loc) · 5.89 KB
/
sbk-reaction.c
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) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "sbk-internal.h"
#define SBK_SELECT \
"SELECT " \
"author_id, " \
"emoji, " \
"date_sent, " \
"date_received " \
"FROM reaction "
/* For database versions < SINGLE_MESSAGE_TABLE_MIGRATION */
#define SBK_WHERE_1 \
"WHERE message_id = ? AND is_mms = ? "
/* For database versions >= SINGLE_MESSAGE_TABLE_MIGRATION */
#define SBK_WHERE_2 \
"WHERE message_id = ? "
#define SBK_ORDER \
"ORDER BY date_sent"
/* For database versions < SINGLE_MESSAGE_TABLE_MIGRATION */
#define SBK_QUERY_1 \
SBK_SELECT \
SBK_WHERE_1 \
SBK_ORDER
/* For database versions >= SINGLE_MESSAGE_TABLE_MIGRATION */
#define SBK_QUERY_2 \
SBK_SELECT \
SBK_WHERE_2 \
SBK_ORDER
#define SBK_COLUMN_AUTHOR_ID 0
#define SBK_COLUMN_EMOJI 1
#define SBK_COLUMN_DATE_SENT 2
#define SBK_COLUMN_DATE_RECEIVED 3
static void
sbk_free_reaction(struct sbk_reaction *rct)
{
if (rct != NULL) {
free(rct->emoji);
free(rct);
}
}
void
sbk_free_reaction_list(struct sbk_reaction_list *lst)
{
struct sbk_reaction *rct;
if (lst != NULL) {
while ((rct = SIMPLEQ_FIRST(lst)) != NULL) {
SIMPLEQ_REMOVE_HEAD(lst, entries);
sbk_free_reaction(rct);
}
free(lst);
}
}
/*
* For database versions >= REACTION_REFACTOR
*/
static struct sbk_reaction *
sbk_get_reaction(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
struct sbk_reaction *rct;
if ((rct = calloc(1, sizeof *rct)) == NULL) {
warn(NULL);
return NULL;
}
rct->recipient = sbk_get_recipient_from_id_from_column(ctx, stm,
SBK_COLUMN_AUTHOR_ID);
if (rct->recipient == NULL)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &rct->emoji, stm,
SBK_COLUMN_EMOJI) == -1)
goto error;
rct->time_sent = sqlite3_column_int64(stm, SBK_COLUMN_DATE_SENT);
rct->time_recv = sqlite3_column_int64(stm, SBK_COLUMN_DATE_RECEIVED);
return rct;
error:
sbk_free_reaction(rct);
return NULL;
}
static struct sbk_reaction_list *
sbk_get_reactions(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
struct sbk_reaction_list *lst;
struct sbk_reaction *rct;
int ret;
if ((lst = malloc(sizeof *lst)) == NULL) {
warn(NULL);
goto error;
}
SIMPLEQ_INIT(lst);
while ((ret = sbk_sqlite_step(ctx, stm)) == SQLITE_ROW) {
if ((rct = sbk_get_reaction(ctx, stm)) == NULL)
goto error;
SIMPLEQ_INSERT_TAIL(lst, rct, entries);
}
if (ret != SQLITE_DONE)
goto error;
sqlite3_finalize(stm);
return lst;
error:
sbk_free_reaction_list(lst);
sqlite3_finalize(stm);
return NULL;
}
int
sbk_get_reactions_from_table(struct sbk_ctx *ctx, struct sbk_message *msg)
{
sqlite3_stmt *stm;
const char *query;
if (ctx->db_version >= SBK_DB_VERSION_SINGLE_MESSAGE_TABLE_MIGRATION)
query = SBK_QUERY_2;
else
query = SBK_QUERY_1;
if (sbk_sqlite_prepare(ctx, &stm, query) == -1)
return -1;
if (sbk_sqlite_bind_int(ctx, stm, 1, msg->id.row_id) == -1) {
sqlite3_finalize(stm);
return -1;
}
if (ctx->db_version < SBK_DB_VERSION_SINGLE_MESSAGE_TABLE_MIGRATION)
if (sbk_sqlite_bind_int(ctx, stm, 2,
msg->id.table != SBK_SMS_TABLE) == -1) {
sqlite3_finalize(stm);
return -1;
}
if ((msg->reactions = sbk_get_reactions(ctx, stm)) == NULL)
return -1;
return 0;
}
/*
* For database versions < REACTION_REFACTOR
*/
static Signal__ReactionList *
sbk_unpack_reaction_list_message(const void *buf, size_t len)
{
Signal__ReactionList *msg;
if ((msg = signal__reaction_list__unpack(NULL, len, buf)) == NULL)
warnx("Cannot unpack reaction list");
return msg;
}
static void
sbk_free_reaction_list_message(Signal__ReactionList *msg)
{
if (msg != NULL)
signal__reaction_list__free_unpacked(msg, NULL);
}
int
sbk_get_reactions_from_column(struct sbk_ctx *ctx,
struct sbk_reaction_list **lst, sqlite3_stmt *stm, int idx)
{
struct sbk_reaction *rct;
struct sbk_recipient_id id;
Signal__ReactionList *msg;
const void *blob;
size_t i;
int len;
*lst = NULL;
if (sqlite3_column_type(stm, idx) != SQLITE_BLOB) {
/* No reactions */
return 0;
}
if ((blob = sqlite3_column_blob(stm, idx)) == NULL) {
sbk_sqlite_warn(ctx, "Cannot get reactions column");
return -1;
}
if ((len = sqlite3_column_bytes(stm, idx)) < 0) {
sbk_sqlite_warn(ctx, "Cannot get reactions size");
return -1;
}
if ((msg = sbk_unpack_reaction_list_message(blob, len)) == NULL)
return -1;
if ((*lst = malloc(sizeof **lst)) == NULL) {
warn(NULL);
goto error1;
}
SIMPLEQ_INIT(*lst);
for (i = 0; i < msg->n_reactions; i++) {
if ((rct = malloc(sizeof *rct)) == NULL) {
warn(NULL);
goto error1;
}
id.new = msg->reactions[i]->author;
id.old = NULL;
rct->recipient = sbk_get_recipient_from_id(ctx, &id);
if (rct->recipient == NULL)
goto error2;
if ((rct->emoji = strdup(msg->reactions[i]->emoji)) == NULL) {
warn(NULL);
goto error2;
}
rct->time_sent = msg->reactions[i]->senttime;
rct->time_recv = msg->reactions[i]->receivedtime;
SIMPLEQ_INSERT_TAIL(*lst, rct, entries);
}
sbk_free_reaction_list_message(msg);
return 0;
error2:
free(rct);
error1:
sbk_free_reaction_list(*lst);
sbk_free_reaction_list_message(msg);
*lst = NULL;
return -1;
}