summaryrefslogtreecommitdiff
path: root/testing/test_avatars.c
blob: 7e7cdd4923130ad97318bf687818c782aeb6c85c (plain)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*
 * A bot to test Tox avatars
 *
 * Usage: ./test_avatars <data dir>
 *
 * Connects to the Tox network, publishes our avatar, requests our friends
 * avatars and, if available, saves them to a local cache.
 * This bot automatically accepts any friend request.
 *
 *
 * Data dir MUST have:
 *
 *  - A file named "data" (named accordingly to STS Draft v0.1.0) with
 *    user id, friends, bootstrap data, etc. from a previously configured
 *    Tox session; use a client (eg. toxic) to configure it, add friends,
 *    etc.
 *
 * Data dir MAY have:
 *
 *  - A file named avatar.png.  If given, the bot will publish it. Otherwise,
 *    no avatar will be set.
 *
 *  - A directory named "avatars" with the currently cached avatars.
 *
 *
 * The bot will answer to these commands:
 *
 *  !debug-on       - Enable extended debug messages
 *  !debug-off      - Disenable extended debug messages
 *  !set-avatar     - Set our avatar to the contents of the file avatar.*
 *  !remove-avatar  - Remove our avatar
 *
 */

#define DATA_FILE_NAME "data"
#define AVATAR_DIR_NAME "avatars"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "../toxcore/tox.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>



/* Basic debug utils */

#define DEBUG(format, ...) debug_printf("DEBUG: %s:%d %s: " format "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)

static bool print_debug_msgs = true;

static void debug_printf(const char *fmt, ...)
{
    if (print_debug_msgs == true) {
        va_list ap;
        va_start(ap, fmt);
        vprintf(fmt, ap);
        va_end(ap);
    }
}






/* ------------ Avatar cache managenment functions ------------ */

typedef struct {
    uint8_t format;
    char *suffix;
    char *file_name;
} def_avatar_name_t;

static const def_avatar_name_t def_avatar_names[] = {
    /* In order of preference */
    { TOX_AVATAR_FORMAT_PNG,  "png", "avatar.png" },
    { TOX_AVATAR_FORMAT_NONE, NULL, NULL },    /* Must be the last one */
};



static void set_avatar(Tox *tox, const char *base_dir);


static char *get_avatar_suffix_from_format(uint8_t format)
{
    int i;

    for (i = 0; def_avatar_names[i].format != TOX_AVATAR_FORMAT_NONE; i++)
        if (def_avatar_names[i].format == format)
            return def_avatar_names[i].suffix;

    return NULL;
}


/* Load avatar data from a file into a memory buffer 'buf'.
 * buf must have at least TOX_MAX_AVATAR_DATA_LENGTH bytes
 * Returns the length of the data sucess or < 0 on error
 */
static int load_avatar_data(char *fname, uint8_t *buf)
{
    FILE *fp = fopen(fname, "rb");

    if (fp == NULL)
        return -1;  /* Error */

    size_t n = fread(buf, 1, TOX_AVATAR_MAX_DATA_LENGTH, fp);
    int ret;

    if (ferror(fp) != 0 || n == 0)
        ret = -1;   /* Error */
    else
        ret = n;

    fclose(fp);
    return ret;
}


/* Save avatar data to a file */
static int save_avatar_data(char *fname, uint8_t *data, uint32_t len)
{
    FILE *fp = fopen(fname, "wb");

    if (fp == NULL)
        return -1;  /* Error */

    int ret = 0;    /* Ok */

    if (fwrite(data, 1, len, fp) != len)
        ret = -1;   /* Error */

    if (fclose(fp) != 0)
        ret = -1;   /* Error */

    return ret;
}


static void byte_to_hex_str(const uint8_t *buf, const size_t buflen, char *dst)
{
    const char *hex_chars = "0123456789ABCDEF";
    size_t i = 0;
    size_t j = 0;

    while (i < buflen) {
        dst[j++] = hex_chars[(buf[i] >> 4) & 0xf];
        dst[j++] = hex_chars[buf[i] & 0xf];
        i++;
    }

    dst[j++] = '\0';
}

/* Make the cache file name for a avatar of the given format for the given
 * client id.
 */
static int make_avatar_file_name(char *dst, size_t dst_len,
                                 char *base_dir, uint8_t format, uint8_t *client_id)
{
    char client_id_str[2 * TOX_CLIENT_ID_SIZE + 1];
    byte_to_hex_str(client_id, TOX_CLIENT_ID_SIZE, client_id_str);

    const char *suffix = get_avatar_suffix_from_format(format);

    if (suffix == NULL)
        return -1;  /* Error */

    int n = snprintf(dst, dst_len, "%s/%s/%s.%s", base_dir, AVATAR_DIR_NAME,
                     client_id_str, suffix);
    dst[dst_len - 1] = '\0';

    if (n >= dst_len)
        return -1;  /* Error: Output truncated */

    return 0;   /* Ok */
}


/* Load a cached avatar into the buffer 'data' (which must be at least
 * TOX_MAX_AVATAR_DATA_LENGTH bytes long). Gets the file name from client
 * id and the given data format.
 * Returns 0 on success, or -1 on error.
 */
static int load_user_avatar(Tox *tox, char *base_dir, int friendnum,
                            uint8_t format, uint8_t *hash, uint8_t *data, uint32_t *datalen)
{
    uint8_t addr[TOX_CLIENT_ID_SIZE];

    if (tox_get_client_id(tox, friendnum, addr) != 0) {
        DEBUG("Bad client id, friendnumber=%d", friendnum);
        return -1;
    }

    char path[PATH_MAX];
    int ret = make_avatar_file_name(path, sizeof(path), base_dir, format, addr);

    if (ret != 0) {
        DEBUG("Can't create an file name for this user/avatar.");
        return -1;
    }

    ret = load_avatar_data(path, data);

    if (ret < 0) {
        DEBUG("Failed to load avatar data.");
        return -1;
    }

    *datalen = ret;
    tox_hash(tox, hash, data, *datalen);

    return 0;
}

/* Save a user avatar into the cache. Gets the file name from client id and
 * the given data format.
 * Returns 0 on success, or -1 on error.
 */
static int save_user_avatar(Tox *tox, char *base_dir, int friendnum,
                            uint8_t format, uint8_t *data, uint32_t datalen)
{
    uint8_t addr[TOX_CLIENT_ID_SIZE];

    if (tox_get_client_id(tox, friendnum, addr) != 0) {
        DEBUG("Bad client id, friendnumber=%d", friendnum);
        return -1;
    }

    char path[PATH_MAX];
    int ret = make_avatar_file_name(path, sizeof(path), base_dir, format, addr);

    if (ret != 0) {
        DEBUG("Can't create a file name for this user/avatar");
        return -1;
    }

    return save_avatar_data(path, data, datalen);
}

/* Delete all cached avatars for a given user */
static int delete_user_avatar(Tox *tox, char *base_dir, int friendnum)
{
    uint8_t addr[TOX_CLIENT_ID_SIZE];

    if (tox_get_client_id(tox, friendnum, addr) != 0) {
        DEBUG("Bad client id, friendnumber=%d", friendnum);
        return -1;
    }

    char path[PATH_MAX];

    /* This iteration is dumb and inefficient */
    int i;

    for (i = 0; def_avatar_names[i].format != TOX_AVATAR_FORMAT_NONE; i++) {
        int ret = make_avatar_file_name(path, sizeof(path), base_dir,
                                        def_avatar_names[i].format, addr);

        if (ret != 0) {
            DEBUG("Failed to create avatar path for friend #%d, format %d\n",
                  friendnum, def_avatar_names[i].format);
            continue;
        }

        if (unlink(path) == 0)
            printf("Avatar file %s deleted.\n", path);
    }

    return 0;
}




/* ------------ Protocol callbacks ------------ */

static void friend_status_cb(Tox *tox, int n, uint8_t status, void *ud)
{
    uint8_t addr[TOX_CLIENT_ID_SIZE];
    char addr_str[2 * TOX_CLIENT_ID_SIZE + 1];

    if (tox_get_client_id(tox, n, addr) == 0) {
        byte_to_hex_str(addr, TOX_CLIENT_ID_SIZE, addr_str);
        printf("Receiving status from %s: %u\n", addr_str, status);
    }
}

static void friend_avatar_info_cb(Tox *tox, int32_t n, uint8_t format, uint8_t *hash, void *ud)
{
    char *base_dir = (char *) ud;
    uint8_t addr[TOX_CLIENT_ID_SIZE];
    char addr_str[2 * TOX_CLIENT_ID_SIZE + 1];
    char hash_str[2 * TOX_HASH_LENGTH + 1];

    if (tox_get_client_id(tox, n, addr) == 0) {
        byte_to_hex_str(addr, TOX_CLIENT_ID_SIZE, addr_str);
        printf("Receiving avatar information from %s.\n", addr_str);
    } else {
        DEBUG("tox_get_client_id failed");
        printf("Receiving avatar information from friend number %u.\n", n);
    }

    byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
    DEBUG("format=%u, hash=%s", format, hash_str);

    if (format == TOX_AVATAR_FORMAT_NONE) {
        printf(" -> User do not have an avatar.\n");
        /* User have no avatar anymore, delete it from our cache */
        delete_user_avatar(tox, base_dir, n);
    } else {
        /* Check the hash of the currently cached user avatar
         * WARNING: THIS IS ONLY AN EXAMPLE!
         *
         * Real clients should keep the hashes in memory (eg. in the object
         * used to represent a friend in the friend list) and do not access
         * the file system or do anything resource intensive in reply of
         * these events.
         */
        uint32_t cur_av_len;
        uint8_t cur_av_data[TOX_AVATAR_MAX_DATA_LENGTH];
        uint8_t cur_av_hash[TOX_HASH_LENGTH];
        int ret;

        ret = load_user_avatar(tox, base_dir, n, format, cur_av_hash, cur_av_data, &cur_av_len);

        if (ret != 0
                && memcpy(cur_av_hash, hash, TOX_HASH_LENGTH) != 0) {
            printf(" -> Cached avatar is outdated. Requesting avatar data.\n");
            tox_request_avatar_data(tox, n);
        } else {
            printf(" -> Cached avatar is still updated.\n");
        }
    }

}

static void friend_avatar_data_cb(Tox *tox, int32_t n, uint8_t format,
                                  uint8_t *hash, uint8_t *data, uint32_t datalen, void *ud)
{
    char *base_dir = (char *) ud;
    uint8_t addr[TOX_CLIENT_ID_SIZE];
    char addr_str[2 * TOX_CLIENT_ID_SIZE + 1];
    char hash_str[2 * TOX_HASH_LENGTH + 1];

    if (tox_get_client_id(tox, n, addr) == 0) {
        byte_to_hex_str(addr, TOX_CLIENT_ID_SIZE, addr_str);
        printf("Receiving avatar data from %s.\n", addr_str);
    } else {
        DEBUG("tox_get_client_id failed");
        printf("Receiving avatar data from friend number %u.\n", n);
    }

    byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
    DEBUG("format=%u, datalen=%d, hash=%s\n", format, datalen, hash_str);

    delete_user_avatar(tox, base_dir, n);

    if (format != TOX_AVATAR_FORMAT_NONE) {
        int ret = save_user_avatar(tox, base_dir, n, format, data, datalen);

        if (ret == 0)
            printf(" -> Avatar updated in the cache.\n");
        else
            printf(" -> Failed to save user avatar.\n");
    }
}


static void friend_msg_cb(Tox *tox, int n, const uint8_t *msg, uint16_t len, void *ud)
{
    const char *base_dir = (char *) ud;
    const char *msg_str = (char *) msg;
    uint8_t addr[TOX_CLIENT_ID_SIZE];
    char addr_str[2 * TOX_CLIENT_ID_SIZE + 1];

    if (tox_get_client_id(tox, n, addr) == 0) {
        byte_to_hex_str(addr, TOX_FRIEND_ADDRESS_SIZE, addr_str);
        printf("Receiving message from %s:\n   %s\n", addr_str, msg);
    }

    /* Handle bot commands for the tests */
    char *reply_ptr = NULL;

    if (strstr(msg_str, "!debug-on") != NULL) {
        print_debug_msgs = true;
        reply_ptr = "Debug enabled.";
    } else if (strstr(msg_str, "!debug-off") != NULL) {
        print_debug_msgs = false;
        reply_ptr = "Debug disabled.";
    } else if (strstr(msg_str, "!set-avatar") != NULL) {
        set_avatar(tox, base_dir);
        reply_ptr = "Setting image avatar";
    } else if (strstr(msg_str, "!remove-avatar") != NULL) {
        int r = tox_set_avatar(tox, TOX_AVATAR_FORMAT_NONE, NULL, 0);
        DEBUG("tox_set_avatar returned %d", r);
        reply_ptr = "Removing avatar";
    }

    /* Add more useful commands here: add friend, etc. */

    char reply[TOX_MAX_MESSAGE_LENGTH];
    int reply_len;

    if (reply_ptr)
        reply_len = snprintf(reply, sizeof(reply), "%s", reply_ptr);
    else
        reply_len = snprintf(reply, sizeof(reply),
                             "No command found in message: %s", msg);

    reply[sizeof(reply) - 1] = '\0';
    printf(" -> Reply: %s\n", reply);
    tox_send_message(tox, n, (uint8_t *) reply, reply_len);
}


static void friend_request_cb(Tox *tox, const uint8_t *public_key,
                              const uint8_t *data, uint16_t length, void *ud)
{
    char addr_str[2 * TOX_CLIENT_ID_SIZE + 1];
    byte_to_hex_str(public_key, TOX_CLIENT_ID_SIZE, addr_str);
    printf("Accepting friend request from %s.\n   %s\n", addr_str, data);
    tox_add_friend_norequest(tox, public_key);
}


static int try_avatar_file(Tox *tox, const char *base_dir, const def_avatar_name_t *an)
{
    char path[PATH_MAX];
    int n = snprintf(path, sizeof(path), "%s/%s", base_dir, an->file_name);
    path[sizeof(path) - 1] = '\0';

    if (n >= sizeof(path)) {
        DEBUG("error: path %s too big\n", path);
        return -1;
    }

    DEBUG("trying file %s: ", path);
    FILE *fp = fopen(path, "rb");

    if (fp != NULL) {
        uint8_t buf[2 * TOX_AVATAR_MAX_DATA_LENGTH];
        int len = fread(buf, 1, sizeof(buf), fp);

        if (len >= 0 && len <= TOX_AVATAR_MAX_DATA_LENGTH) {
            int r = tox_set_avatar(tox, an->format, buf, len);
            DEBUG("%d bytes, tox_set_avatar returned=%d", len, r);

            if (r == 0)
                printf("Setting avatar file %s\n", path);
            else
                printf("Error setting avatar file %s\n", path);
        } else if (len < 0) {
            DEBUG("read error %d", len);
        } else {
            printf("Avatar file %s if too big (more than %d bytes)",
                   path, TOX_AVATAR_MAX_DATA_LENGTH);
        }

        fclose(fp);
        return 0;
    } else {
        DEBUG("File %s not found", path);
    }

    return -1;
}


static void set_avatar(Tox *tox, const char *base_dir)
{
    int i;

    for (i = 0; i < 4; i++) {
        if (def_avatar_names[i].format == TOX_AVATAR_FORMAT_NONE) {
            tox_set_avatar(tox, TOX_AVATAR_FORMAT_NONE, NULL, 0);
            printf("No avatar file found, setting to NONE.\n");
            return;
        } else {
            if (try_avatar_file(tox, base_dir, &def_avatar_names[i]) == 0)
                return;
        }
    }

    /* Should be unreachable */
    printf("UNEXPECTED CODE PATH\n");
}


static void print_avatar_info(Tox *tox)
{
    uint8_t format;
    uint8_t data[TOX_AVATAR_MAX_DATA_LENGTH];
    uint8_t hash[TOX_HASH_LENGTH];
    uint32_t data_length;
    char hash_str[2 * TOX_HASH_LENGTH + 1];

    int ret = tox_get_self_avatar(tox, &format, data, &data_length, sizeof(data), hash);
    DEBUG("tox_get_self_avatar returned %d", ret);
    DEBUG("format: %d, data_length: %d", format, data_length);
    byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
    DEBUG("hash: %s", hash_str);
}


/* ------------ Initialization functions ------------ */

/* Create directory to store tha avatars. Returns 0 if it was sucessfuly
 * created or already existed. Returns -1 on error.
 */
static int create_avatar_diretory(const char *base_dir)
{
    char path[PATH_MAX];
    int n = snprintf(path, sizeof(path), "%s/%s", base_dir, AVATAR_DIR_NAME);
    path[sizeof(path) - 1] = '\0';

    if (n >= sizeof(path))
        return -1;

    if (mkdir(path, 0755) == 0) {
        return 0;   /* Done */
    } else if (errno == EEXIST) {
        /* Check if the existing path is a directory */
        struct stat st;

        if (stat(path, &st) != 0) {
            perror("stat()ing avatar directory");
            return -1;
        }

        if (S_ISDIR(st.st_mode))
            return 0;
    }

    return -1;  /* Error */
}


static void *load_bootstrap_data(const char *base_dir, uint32_t *len)
{
    char path[PATH_MAX];
    int n = snprintf(path, sizeof(path), "%s/%s", base_dir, DATA_FILE_NAME);
    path[sizeof(path) - 1] = '\0';

    if (n >= sizeof(path)) {
        printf("Load error: path %s too long\n", path);
        return NULL;
    }

    /* We should be using POSIX functions here, but let's try to be
     * compatible with Windows.
     */

    FILE *fp = fopen(path, "rb");

    if (fp == NULL) {
        printf("fatal error: file %s not found.\n", path);
        return NULL;
    }

    if (fseek(fp, 0, SEEK_END) != 0) {
        printf("seek fail\n");
        fclose(fp);
        return NULL;
    }

    int32_t flen = ftell(fp);

    if (flen < 8 || flen > 2e6) {
        printf("Fatal error: file %s have %u bytes. Out of acceptable range.\n", path, flen);
        fclose(fp);
        return NULL;
    }

    if (fseek(fp, 0, SEEK_SET) != 0) {
        printf("seek fail\n");
        fclose(fp);
        return NULL;
    }

    void *buf = malloc(flen);

    if (buf == NULL) {
        printf("malloc failed, %u bytes", flen);
        fclose(fp);
        return NULL;
    }

    *len = fread(buf, 1, flen, fp);
    fclose(fp);

    if (*len != flen) {
        printf("fatal: %s have %u bytes, read only %u\n", path, flen, *len);
        free(buf);
        return NULL;
    }

    printf("bootstrap data loaded from %s (%u bytes)\n", path, flen);
    return buf;
}

static int save_bootstrap_data(Tox *tox, const char *base_dir)
{
    char path[PATH_MAX];
    int n = snprintf(path, sizeof(path), "%s/%s", base_dir, DATA_FILE_NAME);
    path[sizeof(path) - 1] = '\0';

    if (n >= sizeof(path)) {
        printf("Save error: path %s too long\n", path);
        return -1;
    }

    char path_tmp[PATH_MAX];
    n = snprintf(path_tmp, sizeof(path_tmp), "%s.tmp", path);
    path_tmp[sizeof(path_tmp) - 1] = '\0';

    if (n >= sizeof(path_tmp)) {
        printf("error: path %s too long\n", path);
        return -1;
    }

    uint32_t len = tox_size(tox);

    if (len < 8 || len > 2e6) {
        printf("save data length == %u, out of acceptable range\n", len);
        return -1;
    }

    void *buf = malloc(len);

    if (buf == NULL) {
        printf("save data: malloc failed\n");
        return -1;
    }

    tox_save(tox, buf);

    FILE *fp = fopen(path_tmp, "wb");

    if (fp == NULL) {
        printf("Error saving data: can't open %s\n", path_tmp);
        free(buf);
        return -1;
    }

    if (fwrite(buf, 1, len, fp) != len) {
        printf("Error writing data to %s\n", path_tmp);
        free(buf);
        fclose(fp);
        return -1;
    }

    free(buf);

    if (fclose(fp) != 0) {
        printf("Error writing data to %s\n", path_tmp);
        return -1;
    }

    if (rename(path_tmp, path) != 0) {
        printf("Error renaming %s to %s\n", path_tmp, path);
        return -1;
    }

    printf("Bootstrap data saved to %s\n", path);
    return 0;   /* Done */
}




int main(int argc, char *argv[])
{
    int ret;

    if (argc != 2) {
        printf("usage: %s <data dir>\n", argv[0]);
        return 1;
    }

    char *base_dir = argv[1];

    if (create_avatar_diretory(base_dir) != 0)
        printf("Error creating avatar directory.\n");

    Tox *tox = tox_new(NULL);

    uint32_t len;
    void *data = load_bootstrap_data(base_dir, &len);

    if (data == NULL)
        return 1;

    ret = tox_load(tox, data, len);
    free(data);

    if (ret == 0) {
        printf("Tox initialized\n");
    } else {
        printf("Fatal: tox_load returned %d\n", ret);
        return 1;
    }

    tox_callback_connection_status(tox, friend_status_cb, NULL);
    tox_callback_friend_message(tox, friend_msg_cb, base_dir);
    tox_callback_friend_request(tox, friend_request_cb, NULL);
    tox_callback_avatar_info(tox, friend_avatar_info_cb, base_dir);
    tox_callback_avatar_data(tox, friend_avatar_data_cb, base_dir);

    uint8_t addr[TOX_FRIEND_ADDRESS_SIZE];
    char addr_str[2 * TOX_FRIEND_ADDRESS_SIZE + 1];
    tox_get_address(tox, addr);
    byte_to_hex_str(addr, TOX_FRIEND_ADDRESS_SIZE, addr_str);
    printf("Using local tox address: %s\n", addr_str);

#ifdef TEST_SET_RESET_AVATAR
    printf("Printing default avatar information:\n");
    print_avatar_info(tox);

    printf("Setting a new avatar:\n");
    set_avatar(tox, base_dir);
    print_avatar_info(tox);

    printf("Removing the avatar we just set:\n");
    tox_avatar(tox, TOX_AVATARFORMAT_NONE, NULL, 0);
    print_avatar_info(tox);

    printf("Setting that avatar again:\n");
#endif /* TEST_SET_RESET_AVATAR */

    set_avatar(tox, base_dir);
    print_avatar_info(tox);

    bool waiting = true;
    time_t last_save = time(0);

    while (1) {
        if (tox_isconnected(tox) && waiting) {
            printf("DHT connected.\n");
            waiting = false;
        }

        tox_do(tox);

        time_t now = time(0);

        if (now - last_save > 120) {
            save_bootstrap_data(tox, base_dir);
            last_save = now;
        }

        usleep(500000);
    }

    return 0;
}