summaryrefslogtreecommitdiff
path: root/other/fun/save-generator.c
blob: c0b5a8a5ebf665e44053ff0bed47aa4f0a367526 (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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../../testing/misc_tools.h"
#include "../../toxcore/ccompat.h"
#include "../../toxcore/tox.h"

#define GENERATED_SAVE_FILE "save.tox"
#define GENERATED_STATUS_MESSAGE "Hello World"
#define GENERATED_REQUEST_MESSAGE "Add me."
#define BOOTSTRAP_IP "185.14.30.213"
#define BOOTSTRAP_ADDRESS "2555763C8C460495B14157D234DD56B86300A2395554BCAE4621AC345B8C1B1B"
#define BOOTSTRAP_UDP_PORT 443

static bool write_save(const uint8_t *data, size_t length)
{
    FILE *fp = fopen(GENERATED_SAVE_FILE, "w");

    if (!fp) {
        return false;
    }

    if (fwrite(data, length, 1, fp) != 1) {
        fclose(fp);
        return false;
    }

    fclose(fp);
    return true;
}

static bool bootstrap_tox(Tox *tox)
{
    uint8_t *key = hex_string_to_bin(BOOTSTRAP_ADDRESS);

    if (!key) {
        printf("Could not allocate memory for tox address\n");
        return false;
    }

    Tox_Err_Bootstrap err;
    tox_bootstrap(tox, BOOTSTRAP_IP, BOOTSTRAP_UDP_PORT, key, &err);
    free(key);

    if (err != TOX_ERR_BOOTSTRAP_OK) {
        printf("Failed to bootstrap. Error number: %d", err);
        return false;
    }

    return true;
}

static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *userdata)
{
    if (connection == TOX_CONNECTION_UDP) {
        printf("Connected to the tox network.\n");
        *(bool *)userdata = true;
    }
}

static void print_information(Tox *tox)
{
    uint8_t tox_id[TOX_ADDRESS_SIZE];
    char tox_id_str[TOX_ADDRESS_SIZE * 2];
    tox_self_get_address(tox, tox_id);
    to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE);

    char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1];
    uint32_t nospam = tox_self_get_nospam(tox);
    int length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam);
    nospam_str[length] = '\0';

    uint8_t name[TOX_MAX_NAME_LENGTH];
    tox_self_get_name(tox, name);

    printf("INFORMATION\n");
    printf("----------------------------------\n");
    printf("Tox ID: %.*s.\n", (int)TOX_ADDRESS_SIZE * 2, tox_id_str);
    printf("Nospam: %s.\n", nospam_str);
    printf("Name: %s.\n", name);
    printf("Status message: %s.\n", GENERATED_STATUS_MESSAGE);
    printf("Number of friends: %zu.\n", tox_self_get_friend_list_size(tox));
    printf("----------------------------------\n");
}

int main(int argc, char *argv[])
{
    if (argc < 3) {
        printf("Usage: ./save-generator <name> <friend id> ...\n");
        return -1;
    }

    Tox *tox = tox_new(nullptr, nullptr);

    if (!tox) {
        printf("Failed to create tox.\n");
        return -1;
    }

    if (!bootstrap_tox(tox)) {
        tox_kill(tox);
        return -1;
    }

    tox_callback_self_connection_status(tox, tox_connection_callback);

    bool connected = false;

    while (!connected) {
        tox_iterate(tox, &connected);
        c_sleep(tox_iteration_interval(tox));
    }

    Tox_Err_Set_Info err;
    const uint8_t *name = (uint8_t *)argv[1];
    tox_self_set_name(tox, name, strlen((const char *)name), &err);

    if (err != TOX_ERR_SET_INFO_OK) {
        printf("Failed to set name. Error number %d\n", err);
    }

    tox_self_set_status_message(tox, (const uint8_t *)GENERATED_STATUS_MESSAGE, strlen(GENERATED_STATUS_MESSAGE), &err);

    if (err != TOX_ERR_SET_INFO_OK) {
        printf("Failed to set status. Error number: %d\n", err);
    }

    for (unsigned int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are
        uint8_t *address = hex_string_to_bin(argv[i]);
        Tox_Err_Friend_Add friend_err;
        tox_friend_add(tox, address, (const uint8_t *)GENERATED_REQUEST_MESSAGE, strlen(GENERATED_REQUEST_MESSAGE),
                       &friend_err);
        free(address);

        if (friend_err != TOX_ERR_FRIEND_ADD_OK) {
            printf("Failed to add friend number %u. Error number: %d\n", i - 1, friend_err);
        }
    }

    const size_t length = tox_get_savedata_size(tox);
    uint8_t *savedata = (uint8_t *)malloc(length);

    if (!savedata) {
        printf("Could not allocate memory for savedata.\n");
        tox_kill(tox);
        return -1;
    }

    tox_get_savedata(tox, savedata);

    bool ret = write_save(savedata, length);
    free(savedata);

    if (!ret) {
        printf("Failed to write save.\n");
        tox_kill(tox);
        return -1;
    }

    printf("Wrote tox save to %s\n", GENERATED_SAVE_FILE);

    print_information(tox);

    tox_kill(tox);

    return 0;
}