-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsbk-recipient-tree.c
455 lines (394 loc) · 12 KB
/
sbk-recipient-tree.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* 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"
/* For database versions < RECIPIENT_IDS */
#define SBK_QUERY_1 \
"SELECT " \
"r.recipient_ids, " \
"NULL, " /* e164 */ \
"NULL, " /* aci */ \
"NULL, " /* email */ \
"r.signal_profile_name, " /* profile_given_name */ \
"NULL, " /* profile_family_name */ \
"NULL, " /* profile_joined_name */ \
"r.system_display_name, " /* system_joined_name */ \
"r.system_phone_label, " \
"NULL, " /* nickname_given_name */ \
"NULL, " /* nickname_family_name */ \
"NULL, " /* nickname_joined_name */ \
"g.group_id, " \
"g.title " \
"FROM recipient_preferences AS r " \
"LEFT JOIN groups AS g " \
"ON r.recipient_ids = g.group_id"
/* For database versions [RECIPIENT_IDS, SPLIT_PROFILE_NAMES) */
#define SBK_QUERY_2 \
"SELECT " \
"r._id, " \
"r.phone, " /* e164 */ \
"r.uuid, " /* aci */ \
"r.email, " \
"r.signal_profile_name, " /* profile_given_name */ \
"NULL, " /* profile_family_name */ \
"NULL, " /* profile_joined_name */ \
"r.system_display_name, " /* system_joined_name */ \
"r.system_phone_label, " \
"NULL, " /* nickname_given_name */ \
"NULL, " /* nickname_family_name */ \
"NULL, " /* nickname_joined_name */ \
"g.group_id, " \
"g.title " \
"FROM recipient AS r " \
"LEFT JOIN groups AS g " \
"ON r._id = g.recipient_id"
/* For database versions [SPLIT_PROFILE_NAMES, RESET_PNI_COLUMN) */
#define SBK_QUERY_3 \
"SELECT " \
"r._id, " \
"r.phone, " /* e164 */ \
"r.uuid, " /* aci */ \
"r.email, " \
"r.signal_profile_name, " /* profile_given_name */ \
"r.profile_family_name, " \
"r.profile_joined_name, " \
"r.system_display_name, " /* system_joined_name */ \
"r.system_phone_label, " \
"NULL, " /* nickname_given_name */ \
"NULL, " /* nickname_family_name */ \
"NULL, " /* nickname_joined_name */ \
"g.group_id, " \
"g.title " \
"FROM recipient AS r " \
"LEFT JOIN groups AS g " \
"ON r._id = g.recipient_id"
/* For database versions [RESET_PNI_COLUMN, RECIPIENT_TABLE_VALIDATIONS) */
#define SBK_QUERY_4 \
"SELECT " \
"r._id, " \
"r.phone, " /* e164 */ \
"r.aci, " \
"r.email, " \
"r.signal_profile_name, " /* profile_given_name */ \
"r.profile_family_name, " \
"r.profile_joined_name, " \
"r.system_display_name, " /* system_joined_name */ \
"r.system_phone_label, " \
"NULL, " /* nickname_given_name */ \
"NULL, " /* nickname_family_name */ \
"NULL, " /* nickname_joined_name */ \
"g.group_id, " \
"g.title " \
"FROM recipient AS r " \
"LEFT JOIN groups AS g " \
"ON r._id = g.recipient_id"
/*
* For database versions [RECIPIENT_TABLE_VALIDATIONS,
* ADD_NICKNAME_AND_NOTE_FIELDS_TO_RECIPIENT_TABLE)
*/
#define SBK_QUERY_5 \
"SELECT " \
"r._id, " \
"r.e164, " \
"r.aci, " \
"r.email, " \
"r.profile_given_name, " \
"r.profile_family_name, " \
"r.profile_joined_name, " \
"r.system_joined_name, " \
"r.system_phone_label, " \
"NULL, " /* nickname_given_name */ \
"NULL, " /* nickname_family_name */ \
"NULL, " /* nickname_joined_name */ \
"g.group_id, " \
"g.title " \
"FROM recipient AS r " \
"LEFT JOIN groups AS g " \
"ON r._id = g.recipient_id"
/* For database versions >= ADD_NICKNAME_AND_NOTE_FIELDS_TO_RECIPIENT_TABLE */
#define SBK_QUERY_6 \
"SELECT " \
"r._id, " \
"r.e164, " \
"r.aci, " \
"r.email, " \
"r.profile_given_name, " \
"r.profile_family_name, " \
"r.profile_joined_name, " \
"r.system_joined_name, " \
"r.system_phone_label, " \
"r.nickname_given_name, " \
"r.nickname_family_name, " \
"r.nickname_joined_name, " \
"g.group_id, " \
"g.title " \
"FROM recipient AS r " \
"LEFT JOIN groups AS g " \
"ON r._id = g.recipient_id"
#define SBK_COLUMN__ID 0
#define SBK_COLUMN_E164 1
#define SBK_COLUMN_ACI 2
#define SBK_COLUMN_EMAIL 3
#define SBK_COLUMN_PROFILE_GIVEN_NAME 4
#define SBK_COLUMN_PROFILE_FAMILY_NAME 5
#define SBK_COLUMN_PROFILE_JOINED_NAME 6
#define SBK_COLUMN_SYSTEM_JOINED_NAME 7
#define SBK_COLUMN_SYSTEM_PHONE_LABEL 8
#define SBK_COLUMN_NICKNAME_GIVEN_NAME 9
#define SBK_COLUMN_NICKNAME_FAMILY_NAME 10
#define SBK_COLUMN_NICKNAME_JOINED_NAME 11
#define SBK_COLUMN_GROUP_ID 12
#define SBK_COLUMN_TITLE 13
static int sbk_cmp_recipient_entries(struct sbk_recipient_entry *,
struct sbk_recipient_entry *);
RB_GENERATE_STATIC(sbk_recipient_tree, sbk_recipient_entry, entries,
sbk_cmp_recipient_entries)
static void
sbk_free_recipient_entry(struct sbk_recipient_entry *ent)
{
if (ent == NULL)
return;
switch (ent->recipient.type) {
case SBK_CONTACT:
if (ent->recipient.contact != NULL) {
free(ent->recipient.contact->aci);
free(ent->recipient.contact->phone);
free(ent->recipient.contact->email);
free(ent->recipient.contact->system_joined_name);
free(ent->recipient.contact->system_phone_label);
free(ent->recipient.contact->profile_given_name);
free(ent->recipient.contact->profile_family_name);
free(ent->recipient.contact->profile_joined_name);
free(ent->recipient.contact);
}
break;
case SBK_GROUP:
if (ent->recipient.group != NULL) {
free(ent->recipient.group->name);
free(ent->recipient.group);
}
break;
}
free(ent->id.old);
free(ent);
}
void
sbk_free_recipient_tree(struct sbk_ctx *ctx)
{
struct sbk_recipient_entry *ent;
while ((ent = RB_ROOT(&ctx->recipients)) != NULL) {
RB_REMOVE(sbk_recipient_tree, &ctx->recipients, ent);
sbk_free_recipient_entry(ent);
}
}
static int
sbk_cmp_recipient_entries(struct sbk_recipient_entry *e,
struct sbk_recipient_entry *f)
{
if (e->id.old != NULL)
return strcmp(e->id.old, f->id.old);
else
return (e->id.new < f->id.new) ? -1 : (e->id.new > f->id.new);
}
static int
sbk_get_recipient_id_from_column(struct sbk_ctx *ctx,
struct sbk_recipient_id *id, sqlite3_stmt *stm, int idx)
{
if (ctx->db_version >= SBK_DB_VERSION_RECIPIENT_IDS) {
id->new = sqlite3_column_int(stm, idx);
id->old = NULL;
} else {
id->new = -1;
if (sbk_sqlite_column_text_copy(ctx, &id->old, stm, idx) == -1)
return -1;
if (id->old == NULL) {
warnx("Invalid recipient id");
return -1;
}
}
return 0;
}
static struct sbk_recipient_entry *
sbk_get_recipient_entry(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
struct sbk_recipient_entry *ent;
struct sbk_contact *con;
struct sbk_group *grp;
if ((ent = calloc(1, sizeof *ent)) == NULL) {
warn(NULL);
return NULL;
}
if (sbk_get_recipient_id_from_column(ctx, &ent->id, stm,
SBK_COLUMN__ID) == -1)
goto error;
if (sqlite3_column_type(stm, SBK_COLUMN_GROUP_ID) == SQLITE_NULL)
ent->recipient.type = SBK_CONTACT;
else
ent->recipient.type = SBK_GROUP;
switch (ent->recipient.type) {
case SBK_CONTACT:
con = ent->recipient.contact = calloc(1, sizeof *con);
if (con == NULL) {
warn(NULL);
goto error;
}
if (ctx->db_version >= SBK_DB_VERSION_RECIPIENT_IDS) {
if (sbk_sqlite_column_text_copy(ctx, &con->phone,
stm, SBK_COLUMN_E164) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->email,
stm, SBK_COLUMN_EMAIL) == -1)
goto error;
} else {
if (strchr(ent->id.old, '@') != NULL) {
con->email = strdup(ent->id.old);
if (con->email == NULL) {
warn(NULL);
goto error;
}
} else {
con->phone = strdup(ent->id.old);
if (con->phone == NULL) {
warn(NULL);
goto error;
}
}
}
if (sbk_sqlite_column_text_copy(ctx, &con->aci,
stm, SBK_COLUMN_ACI) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->system_joined_name,
stm, SBK_COLUMN_SYSTEM_JOINED_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->system_phone_label,
stm, SBK_COLUMN_SYSTEM_PHONE_LABEL) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->profile_given_name,
stm, SBK_COLUMN_PROFILE_GIVEN_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->profile_family_name,
stm, SBK_COLUMN_PROFILE_FAMILY_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->profile_joined_name,
stm, SBK_COLUMN_PROFILE_JOINED_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &con->nickname_given_name,
stm, SBK_COLUMN_NICKNAME_GIVEN_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx,
&con->nickname_family_name, stm,
SBK_COLUMN_NICKNAME_FAMILY_NAME) == -1)
goto error;
if (sbk_sqlite_column_text_copy(ctx,
&con->nickname_joined_name, stm,
SBK_COLUMN_NICKNAME_JOINED_NAME) == -1)
goto error;
break;
case SBK_GROUP:
grp = ent->recipient.group = calloc(1, sizeof *grp);
if (grp == NULL) {
warn(NULL);
goto error;
}
if (sbk_sqlite_column_text_copy(ctx, &grp->name,
stm, SBK_COLUMN_TITLE) == -1)
goto error;
break;
}
return ent;
error:
sbk_free_recipient_entry(ent);
return NULL;
}
static int
sbk_build_recipient_tree(struct sbk_ctx *ctx)
{
struct sbk_recipient_entry *ent;
sqlite3_stmt *stm;
const char *query;
int ret;
if (!RB_EMPTY(&ctx->recipients))
return 0;
if (sbk_create_database(ctx) == -1)
return -1;
if (ctx->db_version >=
SBK_DB_VERSION_ADD_NICKNAME_AND_NOTE_FIELDS_TO_RECIPIENT_TABLE)
query = SBK_QUERY_6;
else if (ctx->db_version >= SBK_DB_VERSION_RECIPIENT_TABLE_VALIDATIONS)
query = SBK_QUERY_5;
else if (ctx->db_version >= SBK_DB_VERSION_RESET_PNI_COLUMN)
query = SBK_QUERY_4;
else if (ctx->db_version >= SBK_DB_VERSION_SPLIT_PROFILE_NAMES)
query = SBK_QUERY_3;
else if (ctx->db_version >= SBK_DB_VERSION_RECIPIENT_IDS)
query = SBK_QUERY_2;
else
query = SBK_QUERY_1;
if (sbk_sqlite_prepare(ctx, &stm, query) == -1)
return -1;
while ((ret = sbk_sqlite_step(ctx, stm)) == SQLITE_ROW) {
if ((ent = sbk_get_recipient_entry(ctx, stm)) == NULL)
goto error;
RB_INSERT(sbk_recipient_tree, &ctx->recipients, ent);
}
if (ret != SQLITE_DONE)
goto error;
sqlite3_finalize(stm);
return 0;
error:
sbk_free_recipient_tree(ctx);
sqlite3_finalize(stm);
return -1;
}
struct sbk_recipient *
sbk_get_recipient_from_id(struct sbk_ctx *ctx, struct sbk_recipient_id *id)
{
struct sbk_recipient_entry find, *result;
if (sbk_build_recipient_tree(ctx) == -1)
return NULL;
find.id = *id;
result = RB_FIND(sbk_recipient_tree, &ctx->recipients, &find);
if (result == NULL) {
warnx("Cannot find recipient");
return NULL;
}
return &result->recipient;
}
struct sbk_recipient *
sbk_get_recipient_from_id_from_column(struct sbk_ctx *ctx, sqlite3_stmt *stm,
int idx)
{
struct sbk_recipient *rcp;
struct sbk_recipient_id id;
if (sbk_get_recipient_id_from_column(ctx, &id, stm, idx) == -1)
return NULL;
rcp = sbk_get_recipient_from_id(ctx, &id);
free(id.old);
return rcp;
}
struct sbk_recipient *
sbk_get_recipient_from_aci(struct sbk_ctx *ctx, const char *aci)
{
struct sbk_recipient_entry *ent;
RB_FOREACH(ent, sbk_recipient_tree, &ctx->recipients)
if (ent->recipient.type == SBK_CONTACT &&
ent->recipient.contact->aci != NULL &&
strcmp(aci, ent->recipient.contact->aci) == 0)
return &ent->recipient;
return NULL;
}