diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
commit | bcb87139a2d8c5b308cfb82635e9483e867c9dbc (patch) | |
tree | 822a967fe0a795b605f1b97d5cd063d5b4b7179d /testing/toxic/main.c | |
parent | 733066a57e8d5403c91475fb6b095f44a81bf270 (diff) | |
parent | 4d89d63c3f1489e248261983f6c73a8b083b3336 (diff) |
Merge branch 'master' into new-api
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r-- | testing/toxic/main.c | 345 |
1 files changed, 0 insertions, 345 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c deleted file mode 100644 index 2d4a39ad..00000000 --- a/testing/toxic/main.c +++ /dev/null | |||
@@ -1,345 +0,0 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <errno.h> | ||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <stdbool.h> | ||
10 | #include <stdint.h> | ||
11 | #include <signal.h> | ||
12 | #include <locale.h> | ||
13 | |||
14 | #ifdef _win32 | ||
15 | #include <direct.h> | ||
16 | #else | ||
17 | #include <sys/stat.h> | ||
18 | #include <sys/types.h> | ||
19 | #endif | ||
20 | |||
21 | #include "../../core/Messenger.h" | ||
22 | #include "../../core/network.h" | ||
23 | |||
24 | #include "configdir.h" | ||
25 | #include "windows.h" | ||
26 | #include "prompt.h" | ||
27 | #include "friendlist.h" | ||
28 | |||
29 | /* Export for use in Callbacks */ | ||
30 | char *DATA_FILE = NULL; | ||
31 | char *SRVLIST_FILE = NULL; | ||
32 | |||
33 | void on_window_resize(int sig) | ||
34 | { | ||
35 | endwin(); | ||
36 | refresh(); | ||
37 | clear(); | ||
38 | } | ||
39 | |||
40 | static void init_term() | ||
41 | { | ||
42 | /* Setup terminal */ | ||
43 | signal(SIGWINCH, on_window_resize); | ||
44 | setlocale(LC_ALL, ""); | ||
45 | initscr(); | ||
46 | cbreak(); | ||
47 | keypad(stdscr, 1); | ||
48 | noecho(); | ||
49 | timeout(100); | ||
50 | |||
51 | if (has_colors()) { | ||
52 | start_color(); | ||
53 | init_pair(1, COLOR_GREEN, COLOR_BLACK); | ||
54 | init_pair(2, COLOR_CYAN, COLOR_BLACK); | ||
55 | init_pair(3, COLOR_RED, COLOR_BLACK); | ||
56 | init_pair(4, COLOR_BLUE, COLOR_BLACK); | ||
57 | init_pair(5, COLOR_YELLOW, COLOR_BLACK); | ||
58 | init_pair(6, COLOR_MAGENTA, COLOR_BLACK); | ||
59 | init_pair(7, COLOR_BLACK, COLOR_BLACK); | ||
60 | init_pair(8, COLOR_BLACK, COLOR_WHITE); | ||
61 | |||
62 | } | ||
63 | |||
64 | refresh(); | ||
65 | } | ||
66 | |||
67 | static Messenger *init_tox() | ||
68 | { | ||
69 | /* Init core */ | ||
70 | Messenger *m = initMessenger(); | ||
71 | |||
72 | /* Callbacks */ | ||
73 | m_callback_friendrequest(m, on_request, NULL); | ||
74 | m_callback_friendmessage(m, on_message, NULL); | ||
75 | m_callback_namechange(m, on_nickchange, NULL); | ||
76 | m_callback_statusmessage(m, on_statuschange, NULL); | ||
77 | m_callback_action(m, on_action, NULL); | ||
78 | #ifdef __linux__ | ||
79 | setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); | ||
80 | #elif defined(WIN32) | ||
81 | setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); | ||
82 | #elif defined(__APPLE__) | ||
83 | setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters | ||
84 | #else | ||
85 | setname(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4")); | ||
86 | #endif | ||
87 | return m; | ||
88 | } | ||
89 | |||
90 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ | ||
91 | #define MINLINE 70 | ||
92 | #define MAXSERVERS 50 | ||
93 | |||
94 | /* Connects to a random DHT server listed in the DHTservers file */ | ||
95 | int init_connection(Messenger *m) | ||
96 | { | ||
97 | FILE *fp = NULL; | ||
98 | |||
99 | if (DHT_isconnected(m->dht)) | ||
100 | return 0; | ||
101 | |||
102 | fp = fopen(SRVLIST_FILE, "r"); | ||
103 | |||
104 | if (!fp) | ||
105 | return 1; | ||
106 | |||
107 | char servers[MAXSERVERS][MAXLINE]; | ||
108 | char line[MAXLINE]; | ||
109 | int linecnt = 0; | ||
110 | |||
111 | while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { | ||
112 | if (strlen(line) > MINLINE) | ||
113 | strcpy(servers[linecnt++], line); | ||
114 | } | ||
115 | |||
116 | if (linecnt < 1) { | ||
117 | fclose(fp); | ||
118 | return 2; | ||
119 | } | ||
120 | |||
121 | fclose(fp); | ||
122 | |||
123 | char *server = servers[rand() % linecnt]; | ||
124 | char *ip = strtok(server, " "); | ||
125 | char *port = strtok(NULL, " "); | ||
126 | char *key = strtok(NULL, " "); | ||
127 | |||
128 | if (!ip || !port || !key) | ||
129 | return 3; | ||
130 | |||
131 | IP_Port dht; | ||
132 | dht.port = htons(atoi(port)); | ||
133 | uint32_t resolved_address = resolve_addr(ip); | ||
134 | |||
135 | if (resolved_address == 0) | ||
136 | return 0; | ||
137 | |||
138 | dht.ip.i = resolved_address; | ||
139 | unsigned char *binary_string = hex_string_to_bin(key); | ||
140 | DHT_bootstrap(m->dht, dht, binary_string); | ||
141 | free(binary_string); | ||
142 | return 0; | ||
143 | } | ||
144 | |||
145 | static void do_tox(Messenger *m, ToxWindow *prompt) | ||
146 | { | ||
147 | static int conn_try = 0; | ||
148 | static int conn_err = 0; | ||
149 | static bool dht_on = false; | ||
150 | |||
151 | if (!dht_on && !DHT_isconnected(m->dht) && !(conn_try++ % 100)) { | ||
152 | if (!conn_err) { | ||
153 | conn_err = init_connection(m); | ||
154 | wprintw(prompt->window, "\nEstablishing connection...\n"); | ||
155 | |||
156 | if (conn_err) | ||
157 | wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); | ||
158 | } | ||
159 | } else if (!dht_on && DHT_isconnected(m->dht)) { | ||
160 | dht_on = true; | ||
161 | wprintw(prompt->window, "\nDHT connected.\n"); | ||
162 | } else if (dht_on && !DHT_isconnected(m->dht)) { | ||
163 | dht_on = false; | ||
164 | wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); | ||
165 | } | ||
166 | |||
167 | doMessenger(m); | ||
168 | } | ||
169 | |||
170 | int f_loadfromfile; | ||
171 | |||
172 | /* | ||
173 | * Store Messenger to given location | ||
174 | * Return 0 stored successfully | ||
175 | * Return 1 malloc failed | ||
176 | * Return 2 opening path failed | ||
177 | * Return 3 fwrite failed | ||
178 | */ | ||
179 | int store_data(Messenger *m, char *path) | ||
180 | { | ||
181 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ | ||
182 | return 0; | ||
183 | |||
184 | FILE *fd; | ||
185 | size_t len; | ||
186 | uint8_t *buf; | ||
187 | |||
188 | len = Messenger_size(m); | ||
189 | buf = malloc(len); | ||
190 | |||
191 | if (buf == NULL) { | ||
192 | return 1; | ||
193 | } | ||
194 | |||
195 | Messenger_save(m, buf); | ||
196 | |||
197 | fd = fopen(path, "w"); | ||
198 | |||
199 | if (fd == NULL) { | ||
200 | free(buf); | ||
201 | return 2; | ||
202 | } | ||
203 | |||
204 | if (fwrite(buf, len, 1, fd) != 1) { | ||
205 | free(buf); | ||
206 | fclose(fd); | ||
207 | return 3; | ||
208 | } | ||
209 | |||
210 | free(buf); | ||
211 | fclose(fd); | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | static void load_data(Messenger *m, char *path) | ||
216 | { | ||
217 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ | ||
218 | return; | ||
219 | |||
220 | FILE *fd; | ||
221 | size_t len; | ||
222 | uint8_t *buf; | ||
223 | |||
224 | if ((fd = fopen(path, "r")) != NULL) { | ||
225 | fseek(fd, 0, SEEK_END); | ||
226 | len = ftell(fd); | ||
227 | fseek(fd, 0, SEEK_SET); | ||
228 | |||
229 | buf = malloc(len); | ||
230 | |||
231 | if (buf == NULL) { | ||
232 | fprintf(stderr, "malloc() failed.\n"); | ||
233 | fclose(fd); | ||
234 | endwin(); | ||
235 | exit(1); | ||
236 | } | ||
237 | |||
238 | if (fread(buf, len, 1, fd) != 1) { | ||
239 | fprintf(stderr, "fread() failed.\n"); | ||
240 | free(buf); | ||
241 | fclose(fd); | ||
242 | endwin(); | ||
243 | exit(1); | ||
244 | } | ||
245 | |||
246 | Messenger_load(m, buf, len); | ||
247 | |||
248 | uint32_t i; | ||
249 | |||
250 | for (i = 0; i < m->numfriends; i++) { | ||
251 | on_friendadded(m, i); | ||
252 | } | ||
253 | |||
254 | free(buf); | ||
255 | fclose(fd); | ||
256 | } else { | ||
257 | int st; | ||
258 | |||
259 | if ((st = store_data(m, path)) != 0) { | ||
260 | fprintf(stderr, "Store messenger failed with return code: %d\n", st); | ||
261 | endwin(); | ||
262 | exit(1); | ||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | int main(int argc, char *argv[]) | ||
268 | { | ||
269 | char *user_config_dir = get_user_config_dir(); | ||
270 | int config_err = 0; | ||
271 | |||
272 | f_loadfromfile = 1; | ||
273 | int f_flag = 0; | ||
274 | int i = 0; | ||
275 | |||
276 | for (i = 0; i < argc; ++i) { | ||
277 | if (argv[i] == NULL) | ||
278 | break; | ||
279 | else if (argv[i][0] == '-') { | ||
280 | if (argv[i][1] == 'f') { | ||
281 | if (argv[i + 1] != NULL) | ||
282 | DATA_FILE = strdup(argv[i + 1]); | ||
283 | else | ||
284 | f_flag = -1; | ||
285 | } else if (argv[i][1] == 'n') { | ||
286 | f_loadfromfile = 0; | ||
287 | } | ||
288 | } | ||
289 | } | ||
290 | |||
291 | if (DATA_FILE == NULL ) { | ||
292 | config_err = create_user_config_dir(user_config_dir); | ||
293 | |||
294 | if (config_err) { | ||
295 | DATA_FILE = strdup("data"); | ||
296 | SRVLIST_FILE = strdup("../../other/DHTservers"); | ||
297 | } else { | ||
298 | DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); | ||
299 | strcpy(DATA_FILE, user_config_dir); | ||
300 | strcat(DATA_FILE, CONFIGDIR); | ||
301 | strcat(DATA_FILE, "data"); | ||
302 | |||
303 | SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); | ||
304 | strcpy(SRVLIST_FILE, user_config_dir); | ||
305 | strcat(SRVLIST_FILE, CONFIGDIR); | ||
306 | strcat(SRVLIST_FILE, "DHTservers"); | ||
307 | } | ||
308 | } | ||
309 | |||
310 | free(user_config_dir); | ||
311 | |||
312 | init_term(); | ||
313 | Messenger *m = init_tox(); | ||
314 | ToxWindow *prompt = init_windows(m); | ||
315 | |||
316 | if (f_loadfromfile) | ||
317 | load_data(m, DATA_FILE); | ||
318 | |||
319 | if (f_flag == -1) { | ||
320 | attron(COLOR_PAIR(3) | A_BOLD); | ||
321 | wprintw(prompt->window, "You passed '-f' without giving an argument.\n" | ||
322 | "defaulting to 'data' for a keyfile...\n"); | ||
323 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
324 | } | ||
325 | |||
326 | if (config_err) { | ||
327 | attron(COLOR_PAIR(3) | A_BOLD); | ||
328 | wprintw(prompt->window, "Unable to determine configuration directory.\n" | ||
329 | "defaulting to 'data' for a keyfile...\n"); | ||
330 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
331 | } | ||
332 | |||
333 | while (true) { | ||
334 | /* Update tox */ | ||
335 | do_tox(m, prompt); | ||
336 | |||
337 | /* Draw */ | ||
338 | draw_active_window(m); | ||
339 | } | ||
340 | |||
341 | cleanupMessenger(m); | ||
342 | free(DATA_FILE); | ||
343 | free(SRVLIST_FILE); | ||
344 | return 0; | ||
345 | } | ||