diff options
Diffstat (limited to 'other')
-rw-r--r-- | other/fun/BUILD.bazel | 9 | ||||
-rw-r--r-- | other/fun/save-generator.c | 168 |
2 files changed, 177 insertions, 0 deletions
diff --git a/other/fun/BUILD.bazel b/other/fun/BUILD.bazel index 93009543..f66aab25 100644 --- a/other/fun/BUILD.bazel +++ b/other/fun/BUILD.bazel | |||
@@ -25,3 +25,12 @@ cc_binary( | |||
25 | "@libsodium", | 25 | "@libsodium", |
26 | ], | 26 | ], |
27 | ) | 27 | ) |
28 | |||
29 | cc_binary( | ||
30 | name = "save-generator", | ||
31 | srcs = ["save-generator.c"], | ||
32 | deps = [ | ||
33 | "//c-toxcore/testing:misc_tools", | ||
34 | "//c-toxcore/toxcore", | ||
35 | ], | ||
36 | ) | ||
diff --git a/other/fun/save-generator.c b/other/fun/save-generator.c new file mode 100644 index 00000000..82b83e72 --- /dev/null +++ b/other/fun/save-generator.c | |||
@@ -0,0 +1,168 @@ | |||
1 | #include <stdint.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | |||
6 | #include "../../testing/misc_tools.h" | ||
7 | #include "../../toxcore/ccompat.h" | ||
8 | #include "../../toxcore/tox.h" | ||
9 | |||
10 | #define SAVE_FILE "save.tox" | ||
11 | #define STATUS_MESSAGE "Hello World" | ||
12 | #define REQUEST_MESSAGE "Add me." | ||
13 | #define BOOTSTRAP_IP "185.14.30.213" | ||
14 | #define BOOTSTRAP_ADDRESS "2555763C8C460495B14157D234DD56B86300A2395554BCAE4621AC345B8C1B1B" | ||
15 | #define UDP_PORT 443 | ||
16 | |||
17 | static bool write_save(const uint8_t *data, size_t length) | ||
18 | { | ||
19 | FILE *fp = fopen(SAVE_FILE, "w"); | ||
20 | |||
21 | if (!fp) { | ||
22 | return false; | ||
23 | } | ||
24 | |||
25 | if (fwrite(data, length, 1, fp) != 1) { | ||
26 | fclose(fp); | ||
27 | return false; | ||
28 | } | ||
29 | |||
30 | fclose(fp); | ||
31 | return true; | ||
32 | } | ||
33 | |||
34 | static bool bootstrap_tox(Tox *tox) | ||
35 | { | ||
36 | uint8_t *key = hex_string_to_bin(BOOTSTRAP_ADDRESS); | ||
37 | |||
38 | if (!key) { | ||
39 | printf("Could not allocate memory for tox address\n"); | ||
40 | return false; | ||
41 | } | ||
42 | |||
43 | Tox_Err_Bootstrap err; | ||
44 | tox_bootstrap(tox, BOOTSTRAP_IP, UDP_PORT, key, &err); | ||
45 | free(key); | ||
46 | |||
47 | if (err != TOX_ERR_BOOTSTRAP_OK) { | ||
48 | printf("Failed to bootstrap. Error number: %d", err); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | return true; | ||
53 | } | ||
54 | |||
55 | static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *userdata) | ||
56 | { | ||
57 | if (connection == TOX_CONNECTION_UDP) { | ||
58 | printf("Connected to the tox network.\n"); | ||
59 | *(bool *)userdata = true; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | static void print_information(Tox *tox) | ||
64 | { | ||
65 | uint8_t tox_id[TOX_ADDRESS_SIZE]; | ||
66 | char tox_id_str[TOX_ADDRESS_SIZE * 2]; | ||
67 | tox_self_get_address(tox, tox_id); | ||
68 | to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); | ||
69 | |||
70 | char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; | ||
71 | uint32_t nospam = tox_self_get_nospam(tox); | ||
72 | int length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); | ||
73 | nospam_str[length] = '\0'; | ||
74 | |||
75 | uint8_t name[TOX_MAX_NAME_LENGTH]; | ||
76 | tox_self_get_name(tox, name); | ||
77 | |||
78 | printf("INFORMATION\n"); | ||
79 | printf("----------------------------------\n"); | ||
80 | printf("Tox ID: %.*s.\n", (int)TOX_ADDRESS_SIZE * 2, tox_id_str); | ||
81 | printf("Nospam: %s.\n", nospam_str); | ||
82 | printf("Name: %s.\n", name); | ||
83 | printf("Status message: %s.\n", STATUS_MESSAGE); | ||
84 | printf("Number of friends: %zu.\n", tox_self_get_friend_list_size(tox)); | ||
85 | printf("----------------------------------\n"); | ||
86 | } | ||
87 | |||
88 | int main(int argc, char *argv[]) | ||
89 | { | ||
90 | if (argc < 3) { | ||
91 | printf("Usage: ./save-generator <name> <friend id> ...\n"); | ||
92 | return -1; | ||
93 | } | ||
94 | |||
95 | Tox *tox = tox_new(nullptr, nullptr); | ||
96 | |||
97 | if (!tox) { | ||
98 | printf("Failed to create tox.\n"); | ||
99 | return -1; | ||
100 | } | ||
101 | |||
102 | if (!bootstrap_tox(tox)) { | ||
103 | tox_kill(tox); | ||
104 | return -1; | ||
105 | } | ||
106 | |||
107 | tox_callback_self_connection_status(tox, tox_connection_callback); | ||
108 | |||
109 | bool connected = false; | ||
110 | |||
111 | while (!connected) { | ||
112 | tox_iterate(tox, &connected); | ||
113 | c_sleep(tox_iteration_interval(tox)); | ||
114 | } | ||
115 | |||
116 | Tox_Err_Set_Info err; | ||
117 | const uint8_t *name = (uint8_t *)argv[1]; | ||
118 | tox_self_set_name(tox, name, strlen((char *)name), &err); | ||
119 | |||
120 | if (err != TOX_ERR_SET_INFO_OK) { | ||
121 | printf("Failed to set name. Error number %d\n", err); | ||
122 | } | ||
123 | |||
124 | tox_self_set_status_message(tox, (const uint8_t *)STATUS_MESSAGE, strlen(STATUS_MESSAGE), &err); | ||
125 | |||
126 | if (err != TOX_ERR_SET_INFO_OK) { | ||
127 | printf("Failed to set status. Error number: %d\n", err); | ||
128 | } | ||
129 | |||
130 | for (unsigned int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are | ||
131 | uint8_t *address = hex_string_to_bin(argv[i]); | ||
132 | Tox_Err_Friend_Add friend_err; | ||
133 | tox_friend_add(tox, address, (const uint8_t *)REQUEST_MESSAGE, strlen(REQUEST_MESSAGE), &friend_err); | ||
134 | free(address); | ||
135 | |||
136 | if (friend_err != TOX_ERR_FRIEND_ADD_OK) { | ||
137 | printf("Failed to add friend number %u. Error number: %d\n", i - 1, friend_err); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | const size_t length = tox_get_savedata_size(tox); | ||
142 | uint8_t *savedata = (uint8_t *)malloc(length); | ||
143 | |||
144 | if (!savedata) { | ||
145 | printf("Could not allocate memory for savedata.\n"); | ||
146 | tox_kill(tox); | ||
147 | return -1; | ||
148 | } | ||
149 | |||
150 | tox_get_savedata(tox, savedata); | ||
151 | |||
152 | bool ret = write_save(savedata, length); | ||
153 | free(savedata); | ||
154 | |||
155 | if (!ret) { | ||
156 | printf("Failed to write save.\n"); | ||
157 | tox_kill(tox); | ||
158 | return -1; | ||
159 | } | ||
160 | |||
161 | printf("Wrote tox save to %s\n", SAVE_FILE); | ||
162 | |||
163 | print_information(tox); | ||
164 | |||
165 | tox_kill(tox); | ||
166 | |||
167 | return 0; | ||
168 | } | ||