2014-02-25 08:28:24 +01:00
|
|
|
/* main.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2013-07-30 01:32:39 +02:00
|
|
|
*/
|
|
|
|
|
2013-08-23 00:37:19 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 09:45:21 +02:00
|
|
|
#ifndef SIGWINCH
|
|
|
|
#define SIGWINCH 28
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <curses.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <errno.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2013-08-11 02:59:16 +02:00
|
|
|
#include <signal.h>
|
2013-08-21 23:19:35 +02:00
|
|
|
#include <locale.h>
|
2013-08-23 23:03:44 +02:00
|
|
|
#include <string.h>
|
2013-11-15 01:14:54 +01:00
|
|
|
#include <time.h>
|
2014-03-13 11:06:53 +01:00
|
|
|
#include <pthread.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-28 19:04:54 +02:00
|
|
|
#ifdef _WIN32
|
2013-08-27 09:45:21 +02:00
|
|
|
#include <direct.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#else
|
2013-08-27 09:45:21 +02:00
|
|
|
#include <netdb.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2013-08-23 23:03:44 +02:00
|
|
|
#include <sys/socket.h>
|
2014-03-07 03:14:04 +01:00
|
|
|
#include <unistd.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#endif
|
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-08 10:15:07 +02:00
|
|
|
#include "configdir.h"
|
2013-08-23 00:18:53 +02:00
|
|
|
#include "toxic_windows.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "prompt.h"
|
|
|
|
#include "misc_tools.h"
|
2014-03-05 11:06:21 +01:00
|
|
|
#include "file_senders.h"
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
#include "audio_call.h"
|
|
|
|
#endif /* _SUPPORT_AUDIO */
|
|
|
|
|
2013-08-25 01:16:43 +02:00
|
|
|
#ifndef PACKAGE_DATADIR
|
|
|
|
#define PACKAGE_DATADIR "."
|
|
|
|
#endif
|
2013-09-09 06:56:47 +02:00
|
|
|
|
2014-03-08 16:36:42 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
ToxAv* av;
|
|
|
|
#endif /* _SUPPORT_AUDIO */
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
/* Export for use in Callbacks */
|
|
|
|
char *DATA_FILE = NULL;
|
2013-09-07 01:59:45 +02:00
|
|
|
ToxWindow *prompt = NULL;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2014-02-25 08:28:24 +01:00
|
|
|
static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */
|
2013-11-30 00:52:21 +01:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
struct _Winthread Winthread;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
void on_window_resize(int sig)
|
2013-08-11 02:59:16 +02:00
|
|
|
{
|
|
|
|
endwin();
|
|
|
|
refresh();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2013-11-24 03:19:59 +01:00
|
|
|
static void init_term(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
/* Setup terminal */
|
|
|
|
signal(SIGWINCH, on_window_resize);
|
2013-08-25 00:59:57 +02:00
|
|
|
#if HAVE_WIDECHAR
|
|
|
|
if (setlocale(LC_ALL, "") == NULL) {
|
2013-09-11 06:02:27 +02:00
|
|
|
fprintf(stderr, "Could not set your locale, plese check your locale settings or"
|
2013-08-25 00:59:57 +02:00
|
|
|
"disable wide char support\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-25 00:59:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
2013-08-16 19:11:09 +02:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
keypad(stdscr, 1);
|
|
|
|
noecho();
|
|
|
|
timeout(100);
|
|
|
|
|
|
|
|
if (has_colors()) {
|
|
|
|
start_color();
|
2013-09-05 06:47:33 +02:00
|
|
|
init_pair(0, COLOR_WHITE, COLOR_BLACK);
|
2013-08-16 19:11:09 +02:00
|
|
|
init_pair(1, COLOR_GREEN, COLOR_BLACK);
|
|
|
|
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
|
|
|
init_pair(3, COLOR_RED, COLOR_BLACK);
|
|
|
|
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
|
2013-08-18 04:43:10 +02:00
|
|
|
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
init_pair(7, COLOR_BLACK, COLOR_BLACK);
|
|
|
|
init_pair(8, COLOR_BLACK, COLOR_WHITE);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh();
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 15:02:34 +01:00
|
|
|
static Tox *init_tox(int ipv4)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
/* Init core */
|
2013-12-02 15:02:34 +01:00
|
|
|
int ipv6 = !ipv4;
|
|
|
|
Tox *m = tox_new(ipv6);
|
2013-10-10 10:52:05 +02:00
|
|
|
|
2014-03-05 16:17:57 +01:00
|
|
|
/*
|
|
|
|
* TOX_ENABLE_IPV6_DEFAULT is always 1.
|
|
|
|
* Checking it is redundant, this *should* be doing ipv4 fallback
|
|
|
|
*/
|
2014-03-05 07:01:10 +01:00
|
|
|
if (ipv6 && m == NULL) {
|
|
|
|
fprintf(stderr, "IPv6 didn't initialize, trying IPv4\n");
|
2013-12-07 03:43:57 +01:00
|
|
|
m = tox_new(0);
|
|
|
|
}
|
|
|
|
|
2013-09-11 06:02:27 +02:00
|
|
|
if (m == NULL)
|
2013-09-11 01:34:29 +02:00
|
|
|
return NULL;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
/* Callbacks */
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_callback_connection_status(m, on_connectionchange, NULL);
|
2014-02-23 10:28:33 +01:00
|
|
|
tox_callback_typing_change(m, on_typing_change, NULL);
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_callback_friend_request(m, on_request, NULL);
|
|
|
|
tox_callback_friend_message(m, on_message, NULL);
|
|
|
|
tox_callback_name_change(m, on_nickchange, NULL);
|
|
|
|
tox_callback_user_status(m, on_statuschange, NULL);
|
|
|
|
tox_callback_status_message(m, on_statusmessagechange, NULL);
|
2013-12-11 01:51:40 +01:00
|
|
|
tox_callback_friend_action(m, on_action, NULL);
|
2013-09-15 22:38:38 +02:00
|
|
|
tox_callback_group_invite(m, on_groupinvite, NULL);
|
|
|
|
tox_callback_group_message(m, on_groupmessage, NULL);
|
2013-12-14 02:57:32 +01:00
|
|
|
tox_callback_group_action(m, on_groupaction, NULL);
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_callback_group_namelist_change(m, on_group_namelistchange, NULL);
|
|
|
|
tox_callback_file_send_request(m, on_file_sendrequest, NULL);
|
2013-10-10 10:52:05 +02:00
|
|
|
tox_callback_file_control(m, on_file_control, NULL);
|
|
|
|
tox_callback_file_data(m, on_file_data, NULL);
|
|
|
|
|
2013-08-18 17:59:34 +02:00
|
|
|
#ifdef __linux__
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
|
2013-08-28 19:04:54 +02:00
|
|
|
#elif defined(_WIN32)
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux"));
|
2013-08-19 09:23:40 +02:00
|
|
|
#elif defined(__APPLE__)
|
2014-03-11 23:22:27 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Hipster", sizeof("Hipster")); /* This used to users of other Unixes are hipsters */
|
2013-08-18 17:59:34 +02:00
|
|
|
#else
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4"));
|
2013-08-18 17:59:34 +02:00
|
|
|
#endif
|
2013-10-10 10:52:05 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return m;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
#define MINLINE 50 /* IP: 7 + port: 5 + key: 38 + spaces: 2 = 70. ! (& e.g. tox.im = 6) */
|
|
|
|
#define MAXLINE 256 /* Approx max number of chars in a sever line (name + port + key) */
|
2014-03-03 16:55:10 +01:00
|
|
|
#define MAXNODES 50
|
|
|
|
#define NODELEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
|
2013-08-10 21:46:29 +02:00
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
static int linecnt = 0;
|
2014-03-03 16:55:10 +01:00
|
|
|
static char nodes[MAXNODES][NODELEN];
|
|
|
|
static uint16_t ports[MAXNODES];
|
|
|
|
static uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE];
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-03-04 09:10:07 +01:00
|
|
|
static int nodelist_load(char *filename)
|
2013-08-10 21:46:29 +02:00
|
|
|
{
|
2014-02-23 10:49:41 +01:00
|
|
|
if (!filename)
|
|
|
|
return 1;
|
2013-08-21 01:37:05 +02:00
|
|
|
|
2014-03-04 09:10:07 +01:00
|
|
|
FILE *fp = fopen(filename, "r");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-11 06:02:27 +02:00
|
|
|
if (fp == NULL)
|
2013-08-16 19:11:09 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
char line[MAXLINE];
|
2014-03-03 16:55:10 +01:00
|
|
|
while (fgets(line, sizeof(line), fp) && linecnt < MAXNODES) {
|
2013-09-15 14:31:59 +02:00
|
|
|
if (strlen(line) > MINLINE) {
|
2013-09-17 03:21:54 +02:00
|
|
|
char *name = strtok(line, " ");
|
|
|
|
char *port = strtok(NULL, " ");
|
|
|
|
char *key_ascii = strtok(NULL, " ");
|
|
|
|
/* invalid line */
|
|
|
|
if (name == NULL || port == NULL || key_ascii == NULL)
|
|
|
|
continue;
|
|
|
|
|
2014-03-03 16:55:10 +01:00
|
|
|
strncpy(nodes[linecnt], name, NODELEN);
|
|
|
|
nodes[linecnt][NODELEN - 1] = 0;
|
2013-09-17 03:21:54 +02:00
|
|
|
ports[linecnt] = htons(atoi(port));
|
|
|
|
|
|
|
|
uint8_t *key_binary = hex_string_to_bin(key_ascii);
|
|
|
|
memcpy(keys[linecnt], key_binary, TOX_CLIENT_ID_SIZE);
|
|
|
|
free(key_binary);
|
|
|
|
|
|
|
|
linecnt++;
|
2013-09-15 14:31:59 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (linecnt < 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return 2;
|
|
|
|
}
|
2013-08-10 21:46:29 +02:00
|
|
|
|
|
|
|
fclose(fp);
|
2013-09-15 14:31:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-02-25 01:18:43 +01:00
|
|
|
int init_connection_helper(Tox *m, int line)
|
2013-09-15 14:31:59 +02:00
|
|
|
{
|
2014-03-03 16:55:10 +01:00
|
|
|
return tox_bootstrap_from_address(m, nodes[line], TOX_ENABLE_IPV6_DEFAULT,
|
2014-02-25 01:18:43 +01:00
|
|
|
ports[line], keys[line]);
|
2013-09-15 14:31:59 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-03-03 16:55:10 +01:00
|
|
|
/* Connects to a random DHT node listed in the DHTnodes file
|
2013-09-15 14:31:59 +02:00
|
|
|
*
|
|
|
|
* return codes:
|
2014-03-03 16:55:10 +01:00
|
|
|
* 1: failed to open node file
|
|
|
|
* 2: no line of sufficient length in node file
|
2014-02-25 01:41:02 +01:00
|
|
|
* 3: failed to resolve name to IP
|
2014-03-03 16:55:10 +01:00
|
|
|
* 4: nodelist file contains no acceptable line
|
2013-09-15 14:31:59 +02:00
|
|
|
*/
|
2014-02-25 01:18:43 +01:00
|
|
|
static bool srvlist_loaded = false;
|
|
|
|
|
2014-02-25 02:08:51 +01:00
|
|
|
#define NUM_INIT_NODES 5
|
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
int init_connection(Tox *m)
|
|
|
|
{
|
2014-03-03 16:55:10 +01:00
|
|
|
if (linecnt > 0) /* already loaded nodelist */
|
2014-02-25 01:41:02 +01:00
|
|
|
return init_connection_helper(m, rand() % linecnt) ? 0 : 3;
|
2013-09-15 14:31:59 +02:00
|
|
|
|
|
|
|
/* only once:
|
2014-03-03 16:55:10 +01:00
|
|
|
* - load the nodelist
|
2013-09-15 14:31:59 +02:00
|
|
|
* - connect to "everyone" inside
|
|
|
|
*/
|
2014-02-25 01:18:43 +01:00
|
|
|
if (!srvlist_loaded) {
|
|
|
|
srvlist_loaded = true;
|
2014-03-04 09:10:07 +01:00
|
|
|
int res = nodelist_load(PACKAGE_DATADIR "/DHTnodes");
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-03-04 09:10:07 +01:00
|
|
|
if (linecnt < 1)
|
2013-09-15 14:31:59 +02:00
|
|
|
return res;
|
|
|
|
|
2014-02-25 01:41:02 +01:00
|
|
|
res = 3;
|
2014-02-25 02:08:51 +01:00
|
|
|
int i;
|
|
|
|
int n = MIN(NUM_INIT_NODES, linecnt);
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
for(i = 0; i < n; ++i) {
|
2014-02-25 02:08:51 +01:00
|
|
|
if (init_connection_helper(m, rand() % linecnt))
|
2013-09-17 03:21:54 +02:00
|
|
|
res = 0;
|
2014-03-13 11:06:53 +01:00
|
|
|
}
|
2013-09-15 14:31:59 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-03-03 16:55:10 +01:00
|
|
|
/* empty nodelist file */
|
2014-02-25 01:41:02 +01:00
|
|
|
return 4;
|
2013-08-10 21:46:29 +02:00
|
|
|
}
|
|
|
|
|
2014-02-22 23:58:36 +01:00
|
|
|
static void do_connection(Tox *m, ToxWindow *prompt)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
static int conn_try = 0;
|
|
|
|
static int conn_err = 0;
|
|
|
|
static bool dht_on = false;
|
|
|
|
|
2014-02-22 23:58:36 +01:00
|
|
|
bool is_connected = tox_isconnected(m);
|
|
|
|
|
|
|
|
if (!dht_on && !is_connected && !(conn_try++ % 100)) {
|
2013-08-16 19:11:09 +02:00
|
|
|
if (!conn_err) {
|
2014-03-11 23:57:32 +01:00
|
|
|
if ((conn_err = init_connection(m))) {
|
|
|
|
prep_prompt_win();
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
|
2014-03-11 23:57:32 +01:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
2014-02-22 23:58:36 +01:00
|
|
|
} else if (!dht_on && is_connected) {
|
2013-08-16 19:11:09 +02:00
|
|
|
dht_on = true;
|
2013-09-07 03:37:16 +02:00
|
|
|
prompt_update_connectionstatus(prompt, dht_on);
|
2013-12-06 11:07:35 +01:00
|
|
|
prep_prompt_win();
|
2014-03-11 23:57:32 +01:00
|
|
|
wprintw(prompt->window, "DHT connected.\n");
|
2014-02-22 23:58:36 +01:00
|
|
|
} else if (dht_on && !is_connected) {
|
2013-08-16 19:11:09 +02:00
|
|
|
dht_on = false;
|
2013-09-07 03:37:16 +02:00
|
|
|
prompt_update_connectionstatus(prompt, dht_on);
|
2013-12-06 11:07:35 +01:00
|
|
|
prep_prompt_win();
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
2013-08-13 04:04:07 +02:00
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 14:01:12 +01:00
|
|
|
static void load_friendlist(Tox *m)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t numfriends = tox_count_friendlist(m);
|
|
|
|
|
|
|
|
for (i = 0; i < numfriends; ++i)
|
|
|
|
friendlist_onFriendAdded(NULL, m, i, false);
|
|
|
|
}
|
2013-08-16 02:56:22 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
/*
|
|
|
|
* Store Messenger to given location
|
|
|
|
* Return 0 stored successfully
|
2013-09-09 06:56:47 +02:00
|
|
|
* Return 1 file path is NULL
|
|
|
|
* Return 2 malloc failed
|
|
|
|
* Return 3 opening path failed
|
|
|
|
* Return 4 fwrite failed
|
2013-08-15 12:11:48 +02:00
|
|
|
*/
|
2013-08-23 23:03:44 +02:00
|
|
|
int store_data(Tox *m, char *path)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 02:56:22 +02:00
|
|
|
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
|
|
|
return 0;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-09 06:56:47 +02:00
|
|
|
if (path == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
FILE *fd;
|
|
|
|
size_t len;
|
|
|
|
uint8_t *buf;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
len = tox_size(m);
|
2013-07-30 01:32:39 +02:00
|
|
|
buf = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-09 21:33:15 +02:00
|
|
|
if (buf == NULL)
|
2013-09-09 06:56:47 +02:00
|
|
|
return 2;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
tox_save(m, buf);
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-09-24 23:47:42 +02:00
|
|
|
fd = fopen(path, "wb");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
if (fd == NULL) {
|
2013-08-15 12:11:48 +02:00
|
|
|
free(buf);
|
2013-09-09 06:56:47 +02:00
|
|
|
return 3;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (fwrite(buf, len, 1, fd) != 1) {
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
2013-09-09 06:56:47 +02:00
|
|
|
return 4;
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void load_data(Tox *m, char *path)
|
2013-08-15 12:11:48 +02:00
|
|
|
{
|
2013-08-16 02:56:22 +02:00
|
|
|
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
|
|
|
return;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
FILE *fd;
|
|
|
|
size_t len;
|
|
|
|
uint8_t *buf;
|
|
|
|
|
2013-09-24 21:23:09 +02:00
|
|
|
if ((fd = fopen(path, "rb")) != NULL) {
|
2013-08-15 12:11:48 +02:00
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
len = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
buf = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
fclose(fd);
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (fread(buf, len, 1, fd) != 1) {
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "fread() failed. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
tox_load(m, buf, len);
|
2014-03-05 14:01:12 +01:00
|
|
|
load_friendlist(m);
|
2013-08-15 12:11:48 +02:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
} else {
|
|
|
|
int st;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if ((st = store_data(m, path)) != 0) {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:58:36 +01:00
|
|
|
void exit_toxic(Tox *m)
|
2013-11-29 04:28:40 +01:00
|
|
|
{
|
2014-02-22 23:58:36 +01:00
|
|
|
store_data(m, DATA_FILE);
|
2014-03-05 11:06:21 +01:00
|
|
|
close_all_file_senders();
|
2014-02-26 11:23:11 +01:00
|
|
|
kill_all_windows();
|
2014-03-05 14:01:12 +01:00
|
|
|
log_disable(prompt->promptbuf->log);
|
2013-09-12 00:07:26 +02:00
|
|
|
free(DATA_FILE);
|
2013-09-13 08:02:49 +02:00
|
|
|
free(prompt->stb);
|
2014-03-02 00:06:35 +01:00
|
|
|
free(prompt->promptbuf->log);
|
2013-12-06 04:55:14 +01:00
|
|
|
free(prompt->promptbuf);
|
2013-09-11 06:12:03 +02:00
|
|
|
tox_kill(m);
|
2014-03-08 16:36:42 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
terminate_audio(prompt, av);
|
|
|
|
#endif /* _SUPPORT_AUDIO */
|
2013-09-11 06:12:03 +02:00
|
|
|
endwin();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:58:36 +01:00
|
|
|
static void do_toxic(Tox *m, ToxWindow *prompt)
|
|
|
|
{
|
2014-03-13 11:06:53 +01:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
|
2014-02-22 23:58:36 +01:00
|
|
|
do_connection(m, prompt);
|
|
|
|
do_file_senders(m);
|
|
|
|
|
|
|
|
/* main tox-core loop */
|
|
|
|
tox_do(m);
|
2014-03-13 11:06:53 +01:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *thread_winref(void *data)
|
|
|
|
{
|
|
|
|
Tox *m = (Tox *) data;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
draw_active_window(m);
|
2014-02-22 23:58:36 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
char *user_config_dir = get_user_config_dir();
|
|
|
|
int config_err = 0;
|
|
|
|
|
|
|
|
f_loadfromfile = 1;
|
|
|
|
int f_flag = 0;
|
|
|
|
int i = 0;
|
2013-12-02 15:02:34 +01:00
|
|
|
int f_use_ipv4 = 0;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-02-25 08:28:24 +01:00
|
|
|
/* Make sure all written files are read/writeable only by the current user. */
|
2014-02-22 23:58:36 +01:00
|
|
|
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
if (argv[i] == NULL)
|
|
|
|
break;
|
|
|
|
else if (argv[i][0] == '-') {
|
|
|
|
if (argv[i][1] == 'f') {
|
|
|
|
if (argv[i + 1] != NULL)
|
|
|
|
DATA_FILE = strdup(argv[i + 1]);
|
|
|
|
else
|
|
|
|
f_flag = -1;
|
|
|
|
} else if (argv[i][1] == 'n') {
|
|
|
|
f_loadfromfile = 0;
|
2013-12-02 15:02:34 +01:00
|
|
|
} else if (argv[i][1] == '4') {
|
|
|
|
f_use_ipv4 = 1;
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 11:21:38 +02:00
|
|
|
config_err = create_user_config_dir(user_config_dir);
|
2013-08-16 19:11:09 +02:00
|
|
|
if (DATA_FILE == NULL ) {
|
|
|
|
if (config_err) {
|
|
|
|
DATA_FILE = strdup("data");
|
|
|
|
} else {
|
|
|
|
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
2013-09-14 11:21:38 +02:00
|
|
|
if (DATA_FILE != NULL) {
|
2013-09-09 06:56:47 +02:00
|
|
|
strcpy(DATA_FILE, user_config_dir);
|
|
|
|
strcat(DATA_FILE, CONFIGDIR);
|
2014-03-04 09:10:07 +01:00
|
|
|
strcat(DATA_FILE, "data");
|
2013-09-12 00:07:26 +02:00
|
|
|
} else {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
2013-09-12 00:07:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-09 06:56:47 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 11:21:38 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
free(user_config_dir);
|
|
|
|
|
|
|
|
init_term();
|
2013-12-02 15:02:34 +01:00
|
|
|
Tox *m = init_tox(f_use_ipv4);
|
2013-09-11 06:02:27 +02:00
|
|
|
|
|
|
|
if (m == NULL) {
|
2013-09-11 01:34:29 +02:00
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "Failed to initialize network. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-11 01:34:29 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
prompt = init_windows(m);
|
2014-03-13 11:06:53 +01:00
|
|
|
|
|
|
|
/* create new thread for ncurses stuff */
|
2014-03-14 04:30:44 +01:00
|
|
|
if (pthread_mutex_init(&Winthread.lock, NULL) != 0) {
|
2014-03-13 11:06:53 +01:00
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "Mutex init failed. Aborting...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) {
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "Thread creation failed. Aborting...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
attron(COLOR_PAIR(RED) | A_BOLD);
|
|
|
|
wprintw(prompt->window, "Starting audio...\n");
|
|
|
|
attroff(COLOR_PAIR(RED) | A_BOLD);
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2014-03-08 16:36:42 +01:00
|
|
|
av = init_audio(prompt, m);
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
if ( errors() == NoError )
|
|
|
|
wprintw(prompt->window, "Audio started with no problems.\n");
|
|
|
|
else /* Get error code and stuff */
|
|
|
|
wprintw(prompt->window, "Error starting audio!\n");
|
2014-03-14 04:30:44 +01:00
|
|
|
|
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
#endif /* _SUPPORT_AUDIO */
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (f_loadfromfile)
|
|
|
|
load_data(m, DATA_FILE);
|
|
|
|
|
|
|
|
if (f_flag == -1) {
|
2013-09-05 06:47:33 +02:00
|
|
|
attron(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
|
|
|
|
"defaulting to 'data' for a keyfile...\n");
|
2013-09-05 06:47:33 +02:00
|
|
|
attroff(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-03 21:12:02 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 19:21:36 +02:00
|
|
|
if (config_err) {
|
2013-09-05 06:47:33 +02:00
|
|
|
attron(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "Unable to determine configuration directory.\n"
|
|
|
|
"defaulting to 'data' for a keyfile...\n");
|
2013-09-05 06:47:33 +02:00
|
|
|
attroff(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-14 04:30:44 +01:00
|
|
|
sort_friendlist_index();
|
2014-03-13 12:34:14 +01:00
|
|
|
prompt_init_statusbar(prompt, m);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
while (true) {
|
2014-02-21 06:45:29 +01:00
|
|
|
do_toxic(m, prompt);
|
2014-03-13 11:06:53 +01:00
|
|
|
usleep(10000);
|
|
|
|
}
|
2014-03-08 16:36:42 +01:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return 0;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|