-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontact.c
394 lines (328 loc) · 7.8 KB
/
contact.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
#include "contact.h"
#include "ecchat.h"
#include "txqueue.h"
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define EOF_PTR (char *)EOF
#define MBOX_DIR_PERM (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH)
#define MBOX_FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
static struct contact *
contact_new(struct contacts *contacts, const char *name)
{
struct contact *c;
if (strlen(name) > ECCHAT_ID_MAXLEN) {
err("contact name to long: %s\n", name);
return NULL;
}
c = calloc(1, sizeof(*c));
if (c == NULL) {
err("%s: out of memory\n", name);
return NULL;
}
ecchat_str2id(&c->id, name);
tx_queue_init(&c->mbox, 128);
c->node.key = &c->id;
avl_insert(&contacts->tree, &c->node);
return c;
}
static void contact_free(struct contacts *contacts, struct contact *c)
{
avl_delete(&contacts->tree, &c->node);
free(c->clist.entries);
tx_queue_deinit(&c->mbox);
free(c);
}
static char * contact_parse_id_line(char *buf, unsigned buflen, FILE *f)
{
char *id = fgets(buf, buflen - 1, f);
unsigned idlen;
if (id == NULL)
return EOF_PTR;
idlen = strlen(id) - 1;
id[idlen--] = 0;
if (idlen == 0)
return NULL;
if (idlen > ECCHAT_ID_MAXLEN) {
err("id exceeds maxlen: %s\n", id);
return NULL;
}
return id;
}
static void contact_store_mbox(struct contact *c,
const char *msg, const unsigned len,
const int append)
{
char path[PATH_MAX + 1];
int flags = O_WRONLY | O_CREAT;
int fd;
if (append)
flags |= O_APPEND;
else
flags |= O_TRUNC;
sprintf(path, "./contacts/mbox/%s", ecchat_idstr(&c->id));
fd = open(path, flags, MBOX_FILE_PERM);
if (fd == -1) {
err_errno("open %s", path);
return;
}
if (write(fd, msg, len) == -1)
err_errno("write %s", path);
close(fd);
}
static void contact_load_mbox(struct contact *c, const char *name)
{
char path[PATH_MAX + 1];
int fd;
sprintf(path, "./contacts/mbox/%s", name);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno != ENOENT)
err_errno("open %s", path);
return;
}
for (;;) {
char buf[2048];
ssize_t len;
len = read(fd, buf, sizeof(buf));
if (len <= 0) {
if (len == -1)
err_errno("read %s", path);
break;
}
tx_queue_add(&c->mbox, buf, len);
}
close(fd);
}
static int contact_load_clist(struct ecchat_clist *cl, const char *name)
{
char path[PATH_MAX + 1];
char line[128];
FILE *f;
char *id = NULL;
unsigned ids = 0;
unsigned i = 0;
int rc = 0;
sprintf(path, "./contacts/%s", name);
f = fopen(path, "r");
if (f == NULL) {
err_errno("open %s", path);
return -1;
}
/* parse contact list
*/
do {
id = contact_parse_id_line(line, sizeof(line), f);
if (id && id != EOF_PTR)
ids++;
} while (id != EOF_PTR);
if (ids == 0) {
err("contact '%s' has empty contact list\n", name);
rc = -1;
goto out;
}
cl->n_entries = ids;
cl->entries = calloc(sizeof(struct ecchat_clist_entry), ids);
if (cl->entries == NULL) {
rc = -1;
goto out;
}
rewind(f);
do {
id = contact_parse_id_line(line, sizeof(line), f);
if (id && id != EOF_PTR)
ecchat_str2id(&cl->entries[i++].id, id);
} while (id != EOF_PTR);
out:
fclose(f);
return rc;
}
struct contact * contact_get(struct contacts *contacts, const ecchat_id_t *id)
{
struct avl_node *n = avl_find(&contacts->tree, id);
if (n == NULL)
return NULL;
return container_of(n, struct contact, node);
}
static int contact_id_is_on_clist(const ecchat_id_t *id,
const struct ecchat_clist *cl)
{
unsigned i;
for (i = 0; i < cl->n_entries; i++) {
const struct ecchat_clist_entry *e = &cl->entries[i];
if (!ecchat_id_cmp(id, &e->id))
return 1;
}
return 0;
}
static int contacts_check_clists(struct contacts *contacts)
{
char idstr[ECCHAT_ID_MAXLEN + 2];
char idstr_ref[ECCHAT_ID_MAXLEN + 2];
struct contact *c;
avl_for_each_element(&contacts->tree, c, node) {
const struct ecchat_clist *cl = &c->clist;
unsigned i;
for (i = 0; i < cl->n_entries; i++) {
const struct ecchat_clist_entry *e = &cl->entries[i];
const struct contact *cl_c = e->ref;
if (!contact_id_is_on_clist(&c->id, &cl_c->clist)) {
ecchat_id2str(idstr, &c->id);
ecchat_id2str(idstr_ref, &e->id);
err("'%s' knows '%s', but not the other way\n",
idstr, idstr_ref);
return -1;
}
}
}
return 0;
}
static int contacts_build_xref(struct contacts *contacts)
{
char idstr[ECCHAT_ID_MAXLEN + 2];
char idstr_cur[ECCHAT_ID_MAXLEN + 2];
struct contact *c;
unsigned n;
avl_for_each_element(&contacts->tree, c, node) {
struct ecchat_clist *cl = &c->clist;
for (n = 0; n < cl->n_entries; n++) {
struct ecchat_clist_entry *e = &cl->entries[n];
struct contact *c_ref = contact_get(contacts, &e->id);
if (c_ref == NULL) {
ecchat_id2str(idstr, &e->id);
ecchat_id2str(idstr_cur, &c->id);
err("contact '%s' clist[%02u]='%s' not found\n",
idstr_cur, n, idstr);
return -1;
}
if (c_ref == c) {
ecchat_id2str(idstr, &e->id);
ecchat_id2str(idstr_cur, &c->id);
err("contact '%s' clist[%02u]='%s' contacts himself\n",
idstr_cur, n, idstr);
return -1;
}
e->ref = c_ref;
}
}
return 0;
}
static void contact_move_state(struct contact *dst, struct contact *src)
{
dst->online = src->online;
src->online = 0;
dst->priv = src->priv;
src->priv = NULL;
tx_queue_move(&dst->mbox, &src->mbox);
}
void contacts_move_state(struct contacts *contacts_new,
struct contacts *contacts_old)
{
struct contact *c_new;
avl_for_each_element(&contacts_new->tree, c_new, node) {
struct contact *c_old = contact_get(contacts_old, &c_new->id);
if (c_old) {
contact_move_state(c_new, c_old);
contacts_new->contact_moved(c_new);
}
}
}
int contacts_load(struct contacts *contacts)
{
DIR *d = opendir("./contacts");
struct dirent *de;
unsigned cnt = 0;
int rc = 0;
if (d == NULL) {
err_errno("open dir");
return -1;
}
while ((de = readdir(d))) {
const char *name = de->d_name;
struct contact *c;
if (name[0] == '.' || de->d_type != DT_REG)
continue;
c = contact_new(contacts, name);
if (c == NULL) {
rc = -1;
break;
}
rc = contact_load_clist(&c->clist, name);
if (rc == -1)
break;
contact_load_mbox(c, name);
contact_print(c);
cnt++;
}
closedir(d);
if (cnt == 0) {
err("no contacts\n");
rc = -1;
} else {
rc = contacts_build_xref(contacts);
if (!rc)
rc = contacts_check_clists(contacts);
}
return rc;
}
static int contact_cmp(const void *k1, const void *k2, __unused void *priv)
{
const struct contact *c1 = k1;
const struct contact *c2 = k2;
return ecchat_id_cmp(&c1->id, &c2->id);
}
void contacts_init()
{
int rc = mkdir("./contacts/mbox", MBOX_DIR_PERM);
if (rc == -1 && errno != EEXIST)
err_errno("mkdir ./contacts/mbox");
}
struct contacts * contacts_new(contact_moved_cb mov_cb,
contact_deleted_cb del_cb)
{
struct contacts *contacts = malloc(sizeof(*contacts));
if (contacts) {
avl_init(&contacts->tree, contact_cmp, false, NULL);
contacts->contact_moved = mov_cb;
contacts->contact_deleted = del_cb;
}
return contacts;
}
void contacts_free(struct contacts *contacts)
{
struct contact *c, *tmp;
avl_for_each_element_safe(&contacts->tree, c, node, tmp) {
contacts->contact_deleted(c);
contact_free(contacts, c);
}
free(contacts);
}
void contact_mbox_add(struct contact *c, const char *msg, const unsigned len)
{
tx_queue_add(&c->mbox, msg, len);
contact_store_mbox(c, msg, len, 1);
}
void contact_mbox_del(struct contact *c)
{
char path[PATH_MAX + 1];
int rc;
sprintf(path, "./contacts/mbox/%s", ecchat_idstr(&c->id));
rc = unlink(path);
if (rc == -1 && errno != ENOENT)
err_errno("unlink %s", path);
}
void contact_mbox_truncate(struct contact *c)
{
struct tx_queue_entry *e;
int first = 1;
list_for_each_entry(e, &c->mbox.head, list) {
if (first) {
contact_store_mbox(c, e->data, e->len, 0);
first = 0;
}
contact_store_mbox(c, e->data, e->len, 1);
}
}