2015-02-04 14:56:31 +01:00
|
|
|
/* toxic.c
|
2014-02-25 08:28:24 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2014-06-22 02:18:23 +02:00
|
|
|
#include <getopt.h>
|
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>
|
2014-08-27 05:21:32 +02:00
|
|
|
#include <limits.h>
|
2014-09-14 23:46:28 +02:00
|
|
|
#include <termios.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
#ifdef NO_GETTEXT
|
|
|
|
#define gettext(A) (A)
|
|
|
|
#else
|
|
|
|
#include <libintl.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2014-09-14 23:46:28 +02:00
|
|
|
#include <tox/toxencryptsave.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-08 10:15:07 +02:00
|
|
|
#include "configdir.h"
|
2014-06-22 02:18:23 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "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"
|
2015-03-28 07:56:54 +01:00
|
|
|
#include "file_transfers.h"
|
2014-03-25 08:17:22 +01:00
|
|
|
#include "line_info.h"
|
2014-04-07 10:42:10 +02:00
|
|
|
#include "settings.h"
|
2014-06-22 03:41:38 +02:00
|
|
|
#include "log.h"
|
2014-07-21 01:12:13 +02:00
|
|
|
#include "notify.h"
|
2015-05-25 23:59:06 +02:00
|
|
|
#include "audio_device.h"
|
2014-09-06 19:18:42 +02:00
|
|
|
#include "message_queue.h"
|
2014-09-26 09:10:44 +02:00
|
|
|
#include "execute.h"
|
2015-02-26 22:51:20 +01:00
|
|
|
#include "term_mplex.h"
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2014-10-01 23:28:34 +02:00
|
|
|
#ifdef X11
|
|
|
|
#include "xtra.h"
|
|
|
|
#endif
|
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2014-04-19 23:58:13 +02:00
|
|
|
#include "audio_call.h"
|
2015-06-12 17:50:26 +02:00
|
|
|
ToxAV *av;
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2014-03-07 03:14:04 +01:00
|
|
|
|
2013-08-25 01:16:43 +02:00
|
|
|
#ifndef PACKAGE_DATADIR
|
2014-10-01 23:28:34 +02:00
|
|
|
#define PACKAGE_DATADIR "."
|
2013-08-25 01:16:43 +02:00
|
|
|
#endif
|
2013-09-09 06:56:47 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
/* Export for use in Callbacks */
|
|
|
|
char *DATA_FILE = NULL;
|
2014-07-31 18:48:49 +02:00
|
|
|
char *BLOCK_FILE = NULL;
|
2013-09-07 01:59:45 +02:00
|
|
|
ToxWindow *prompt = NULL;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2014-07-08 20:46:50 +02:00
|
|
|
#define AUTOSAVE_FREQ 60
|
2015-03-28 07:56:54 +01:00
|
|
|
#define MIN_PASSWORD_LEN 6
|
|
|
|
#define MAX_PASSWORD_LEN 64
|
2014-07-08 20:46:50 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
struct Winthread Winthread;
|
|
|
|
struct cqueue_thread cqueue_thread;
|
2014-11-29 05:47:22 +01:00
|
|
|
struct audio_thread audio_thread;
|
2014-07-31 18:48:49 +02:00
|
|
|
struct arg_opts arg_opts;
|
2014-09-23 03:24:45 +02:00
|
|
|
struct user_settings *user_settings = NULL;
|
2014-03-13 11:06:53 +01:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
static struct user_password {
|
2014-09-14 23:46:28 +02:00
|
|
|
bool data_is_encrypted;
|
2014-09-15 03:49:38 +02:00
|
|
|
char pass[MAX_PASSWORD_LEN + 1];
|
2014-09-14 23:46:28 +02:00
|
|
|
int len;
|
|
|
|
} user_password;
|
|
|
|
|
2014-07-09 02:21:51 +02:00
|
|
|
static void catch_SIGINT(int sig)
|
2013-08-11 02:59:16 +02:00
|
|
|
{
|
2014-09-22 20:27:33 +02:00
|
|
|
Winthread.sig_exit_toxic = 1;
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 06:26:44 +02:00
|
|
|
static void catch_SIGSEGV(int sig)
|
|
|
|
{
|
2015-03-26 03:56:45 +01:00
|
|
|
freopen("/dev/tty", "w", stderr); // make sure stderr is enabled since we may have disabled it
|
2014-08-03 06:26:44 +02:00
|
|
|
endwin();
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("Caught SIGSEGV: Aborting toxic session.\n"));
|
2014-08-03 06:26:44 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-07-01 05:56:47 +02:00
|
|
|
static void flag_window_resize(int sig)
|
|
|
|
{
|
2014-09-22 20:27:33 +02:00
|
|
|
Winthread.flag_resize = 1;
|
2014-07-01 05:56:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 06:26:44 +02:00
|
|
|
static void init_signal_catchers(void)
|
|
|
|
{
|
|
|
|
signal(SIGWINCH, flag_window_resize);
|
|
|
|
signal(SIGINT, catch_SIGINT);
|
|
|
|
signal(SIGSEGV, catch_SIGSEGV);
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
void exit_toxic_success(Tox *m)
|
|
|
|
{
|
|
|
|
store_data(m, DATA_FILE);
|
2014-09-23 03:24:45 +02:00
|
|
|
memset(&user_password, 0, sizeof(struct user_password));
|
2015-04-04 09:26:38 +02:00
|
|
|
kill_all_file_transfers(m);
|
2015-04-05 03:15:34 +02:00
|
|
|
kill_all_windows(m);
|
2014-10-02 08:07:54 +02:00
|
|
|
terminate_notify();
|
|
|
|
|
|
|
|
#ifdef AUDIO
|
|
|
|
terminate_audio();
|
|
|
|
#endif /* AUDIO */
|
2014-07-09 01:24:44 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
free(DATA_FILE);
|
2014-08-02 05:00:52 +02:00
|
|
|
free(BLOCK_FILE);
|
2014-09-23 03:24:45 +02:00
|
|
|
free(user_settings);
|
2014-07-09 01:24:44 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
tox_kill(m);
|
|
|
|
endwin();
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-10-12 22:54:18 +02:00
|
|
|
#ifdef X11
|
|
|
|
/* We have to terminate xtra last coz reasons
|
|
|
|
* Please don't call this anywhere else coz trust me
|
|
|
|
*/
|
|
|
|
terminate_xtra();
|
|
|
|
#endif /* X11 */
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void exit_toxic_err(const char *errmsg, int errcode)
|
|
|
|
{
|
2014-08-03 21:31:56 +02:00
|
|
|
freopen("/dev/tty", "w", stderr);
|
2014-06-22 02:18:23 +02:00
|
|
|
endwin();
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("Toxic session aborted with error code %d (%s)\n"), errcode, errmsg);
|
2014-06-22 02:18:23 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-11 02:59:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-24 03:19:59 +01:00
|
|
|
static void init_term(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2014-06-22 02:18:23 +02:00
|
|
|
#if HAVE_WIDECHAR
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-06-30 00:14:28 +02:00
|
|
|
if (!arg_opts.default_locale) {
|
|
|
|
if (setlocale(LC_ALL, "") == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("Could not set your locale, please check your locale settings or "
|
|
|
|
"disable unicode support with the -d flag."), FATALERR_LOCALE_NOT_SET);
|
2014-06-30 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2013-08-25 00:59:57 +02:00
|
|
|
#endif
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
#ifndef NO_GETTEXT
|
|
|
|
textdomain("toxic");
|
|
|
|
#endif
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
keypad(stdscr, 1);
|
|
|
|
noecho();
|
|
|
|
timeout(100);
|
|
|
|
|
|
|
|
if (has_colors()) {
|
2014-04-02 20:41:19 +02:00
|
|
|
short bg_color = COLOR_BLACK;
|
2013-08-16 19:11:09 +02:00
|
|
|
start_color();
|
2014-04-12 09:10:01 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
if (user_settings->colour_theme == NATIVE_COLS) {
|
2014-04-19 23:58:13 +02:00
|
|
|
if (assume_default_colors(-1, -1) == OK)
|
2014-04-12 09:10:01 +02:00
|
|
|
bg_color = -1;
|
|
|
|
}
|
|
|
|
|
2013-09-05 06:47:33 +02:00
|
|
|
init_pair(0, COLOR_WHITE, COLOR_BLACK);
|
2014-04-02 20:41:19 +02:00
|
|
|
init_pair(1, COLOR_GREEN, bg_color);
|
|
|
|
init_pair(2, COLOR_CYAN, bg_color);
|
|
|
|
init_pair(3, COLOR_RED, bg_color);
|
|
|
|
init_pair(4, COLOR_BLUE, bg_color);
|
|
|
|
init_pair(5, COLOR_YELLOW, bg_color);
|
|
|
|
init_pair(6, COLOR_MAGENTA, bg_color);
|
2013-08-18 04:43:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-26 01:26:41 +02:00
|
|
|
static struct _init_messages {
|
|
|
|
char **msgs;
|
|
|
|
int num;
|
|
|
|
} init_messages;
|
|
|
|
|
2014-08-27 05:21:32 +02:00
|
|
|
/* One-time queue for messages created during init. Do not use after program init. */
|
2014-09-19 05:50:51 +02:00
|
|
|
static void queue_init_message(const char *msg, ...)
|
2014-08-26 01:26:41 +02:00
|
|
|
{
|
2014-09-19 05:50:51 +02:00
|
|
|
char frmt_msg[MAX_STR_SIZE] = {0};
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2014-08-26 01:26:41 +02:00
|
|
|
int i = init_messages.num;
|
|
|
|
++init_messages.num;
|
|
|
|
|
|
|
|
char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num);
|
|
|
|
|
|
|
|
if (new_msgs == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("Failed in queue_init_message"), FATALERR_MEMORY);
|
2014-08-26 01:26:41 +02:00
|
|
|
|
|
|
|
new_msgs[i] = malloc(MAX_STR_SIZE);
|
|
|
|
|
|
|
|
if (new_msgs[i] == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("Failed in queue_init_message"), FATALERR_MEMORY);
|
2014-08-26 01:26:41 +02:00
|
|
|
|
2014-09-19 05:50:51 +02:00
|
|
|
snprintf(new_msgs[i], MAX_STR_SIZE, "%s", frmt_msg);
|
2014-08-26 01:26:41 +02:00
|
|
|
init_messages.msgs = new_msgs;
|
|
|
|
}
|
|
|
|
|
2014-09-28 23:49:48 +02:00
|
|
|
/* called after messages have been printed to prompt and are no longer needed */
|
2014-08-26 01:26:41 +02:00
|
|
|
static void cleanup_init_messages(void)
|
|
|
|
{
|
|
|
|
if (init_messages.num <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < init_messages.num; ++i)
|
|
|
|
free(init_messages.msgs[i]);
|
|
|
|
|
|
|
|
free(init_messages.msgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_init_messages(ToxWindow *toxwin)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < init_messages.num; ++i)
|
|
|
|
line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]);
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:40:19 +02:00
|
|
|
#define MIN_NODE_LINE 50 /* IP: 7 + port: 5 + key: 38 + spaces: 2 = 70. ! (& e.g. tox.im = 6) */
|
|
|
|
#define MAX_NODE_LINE 256 /* Approx max number of chars in a sever line (name + port + key) */
|
2014-03-03 16:55:10 +01:00
|
|
|
#define MAXNODES 50
|
2015-02-01 21:09:48 +01:00
|
|
|
#define NODELEN (MAX_NODE_LINE - TOX_PUBLIC_KEY_SIZE - 7)
|
2013-08-10 21:46:29 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
static struct toxNodes {
|
2014-06-22 02:18:23 +02:00
|
|
|
int lines;
|
|
|
|
char nodes[MAXNODES][NODELEN];
|
|
|
|
uint16_t ports[MAXNODES];
|
2015-02-01 21:09:48 +01:00
|
|
|
char keys[MAXNODES][TOX_PUBLIC_KEY_SIZE];
|
2014-06-22 02:18:23 +02:00
|
|
|
} toxNodes;
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-08-11 03:40:19 +02:00
|
|
|
static int load_nodelist(const 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;
|
|
|
|
|
2014-08-11 03:40:19 +02:00
|
|
|
char line[MAX_NODE_LINE];
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
|
2014-08-11 03:40:19 +02:00
|
|
|
if (strlen(line) > MIN_NODE_LINE) {
|
2014-06-22 02:18:23 +02:00
|
|
|
const char *name = strtok(line, " ");
|
|
|
|
const char *port = strtok(NULL, " ");
|
|
|
|
const char *key_ascii = strtok(NULL, " ");
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2013-09-17 03:21:54 +02:00
|
|
|
/* invalid line */
|
|
|
|
if (name == NULL || port == NULL || key_ascii == NULL)
|
|
|
|
continue;
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name);
|
|
|
|
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
|
2014-08-15 02:34:20 +02:00
|
|
|
toxNodes.ports[toxNodes.lines] = atoi(port);
|
2013-09-17 03:21:54 +02:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char *key_binary = hex_string_to_bin(key_ascii);
|
2015-02-01 21:09:48 +01:00
|
|
|
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE);
|
2013-09-17 03:21:54 +02:00
|
|
|
free(key_binary);
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
toxNodes.lines++;
|
2013-09-15 14:31:59 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
2013-08-10 21:46:29 +02:00
|
|
|
fclose(fp);
|
2014-08-11 03:40:19 +02:00
|
|
|
|
|
|
|
if (toxNodes.lines < 1)
|
|
|
|
return 1;
|
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-05-24 23:56:30 +02:00
|
|
|
/* Bootstraps and adds as TCP relay.
|
|
|
|
* Returns 0 if both actions are successful.
|
|
|
|
* Returns -1 otherwise.
|
|
|
|
*/
|
2014-02-25 01:18:43 +01:00
|
|
|
int init_connection_helper(Tox *m, int line)
|
2013-09-15 14:31:59 +02:00
|
|
|
{
|
2015-05-24 23:56:30 +02:00
|
|
|
TOX_ERR_BOOTSTRAP err;
|
|
|
|
tox_bootstrap(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line], &err);
|
|
|
|
|
|
|
|
if (err != TOX_ERR_BOOTSTRAP_OK) {
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("Failed to bootstrap %s:%d\n"), toxNodes.nodes[line], toxNodes.ports[line]);
|
2015-05-24 23:56:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tox_add_tcp_relay(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line], &err);
|
|
|
|
|
|
|
|
if (err != TOX_ERR_BOOTSTRAP_OK) {
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("Failed to add TCP relay %s:%d\n"), toxNodes.nodes[line], toxNodes.ports[line]);
|
2015-05-24 23:56:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
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:
|
2015-05-24 23:56:30 +02:00
|
|
|
* 0: success
|
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)
|
|
|
|
{
|
2015-05-24 23:56:30 +02:00
|
|
|
if (toxNodes.lines > 0) { /* already loaded nodelist */
|
|
|
|
init_connection_helper(m, rand() % toxNodes.lines);
|
|
|
|
return 0;
|
|
|
|
}
|
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-06-22 02:18:23 +02:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!arg_opts.nodes_path[0])
|
2014-08-11 03:40:19 +02:00
|
|
|
res = load_nodelist(PACKAGE_DATADIR "/DHTnodes");
|
2014-06-22 02:18:23 +02:00
|
|
|
else
|
2014-08-11 03:40:19 +02:00
|
|
|
res = load_nodelist(arg_opts.nodes_path);
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-08-11 03:40:19 +02:00
|
|
|
if (res != 0)
|
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;
|
2014-06-22 02:18:23 +02:00
|
|
|
int n = MIN(NUM_INIT_NODES, toxNodes.lines);
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
for (i = 0; i < n; ++i) {
|
2015-05-24 23:56:30 +02:00
|
|
|
if (init_connection_helper(m, rand() % toxNodes.lines) == 0)
|
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-03-05 14:01:12 +01:00
|
|
|
static void load_friendlist(Tox *m)
|
|
|
|
{
|
2015-03-28 07:56:54 +01:00
|
|
|
size_t i;
|
|
|
|
size_t numfriends = tox_self_get_friend_list_size(m);
|
2014-03-05 14:01:12 +01:00
|
|
|
|
|
|
|
for (i = 0; i < numfriends; ++i)
|
|
|
|
friendlist_onFriendAdded(NULL, m, i, false);
|
2014-07-31 18:48:49 +02:00
|
|
|
|
|
|
|
sort_friendlist_index();
|
2014-03-05 14:01:12 +01:00
|
|
|
}
|
2013-08-16 02:56:22 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
/* return length of password on success, 0 on failure */
|
|
|
|
static int password_prompt(char *buf, int size)
|
|
|
|
{
|
2014-09-15 10:13:12 +02:00
|
|
|
buf[0] = '\0';
|
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
/* disable terminal echo */
|
|
|
|
struct termios oflags, nflags;
|
|
|
|
tcgetattr(fileno(stdin), &oflags);
|
|
|
|
nflags = oflags;
|
|
|
|
nflags.c_lflag &= ~ECHO;
|
|
|
|
nflags.c_lflag |= ECHONL;
|
|
|
|
|
|
|
|
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-09-15 10:13:12 +02:00
|
|
|
const char *p = fgets(buf, size, stdin);
|
2014-09-14 23:46:28 +02:00
|
|
|
int len = strlen(buf);
|
|
|
|
|
|
|
|
/* re-enable terminal echo */
|
2014-09-15 10:13:12 +02:00
|
|
|
tcsetattr(fileno(stdin), TCSANOW, &oflags);
|
|
|
|
|
2014-09-17 19:52:27 +02:00
|
|
|
if (p == NULL || len <= 1)
|
2014-09-15 10:13:12 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* eat overflowed stdin and return error */
|
|
|
|
if (buf[--len] != '\n') {
|
|
|
|
int ch;
|
|
|
|
while ((ch = getchar()) != '\n' && ch > 0)
|
|
|
|
;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
2014-09-14 23:46:28 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-09-17 19:52:27 +02:00
|
|
|
/* Ask user if they would like to encrypt the data file and set password */
|
2014-09-15 22:17:58 +02:00
|
|
|
static void first_time_encrypt(const char *msg)
|
2014-09-14 23:46:28 +02:00
|
|
|
{
|
2014-09-16 01:00:36 +02:00
|
|
|
char ch[256] = {0};
|
2014-09-14 23:46:28 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
system("clear");
|
2014-09-16 01:00:36 +02:00
|
|
|
printf("%s ", msg);
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
if (!strcasecmp(ch, "y\n") || !strcasecmp(ch, "n\n") || !strcasecmp(ch, "yes\n")
|
2014-09-17 19:52:27 +02:00
|
|
|
|| !strcasecmp(ch, "no\n") || !strcasecmp(ch, "q\n"))
|
2014-09-14 23:46:28 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
} while (fgets(ch, sizeof(ch), stdin));
|
|
|
|
|
2014-09-16 01:00:36 +02:00
|
|
|
printf("\n");
|
|
|
|
|
2014-09-15 03:49:38 +02:00
|
|
|
if (ch[0] == 'q' || ch[0] == 'Q')
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
if (ch[0] == 'y' || ch[0] == 'Y') {
|
|
|
|
int len = 0;
|
2014-09-16 01:00:36 +02:00
|
|
|
bool valid_password = false;
|
|
|
|
char passconfirm[MAX_PASSWORD_LEN + 1] = {0};
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Enter a new password (must be at least %d characters) "), MIN_PASSWORD_LEN);
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2014-09-16 01:00:36 +02:00
|
|
|
while (valid_password == false) {
|
2014-09-14 23:46:28 +02:00
|
|
|
len = password_prompt(user_password.pass, sizeof(user_password.pass));
|
|
|
|
user_password.len = len;
|
2014-09-15 03:49:38 +02:00
|
|
|
|
2014-09-16 01:00:36 +02:00
|
|
|
if (strcasecmp(user_password.pass, "q") == 0)
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
if (string_is_empty(passconfirm) && (len < MIN_PASSWORD_LEN || len > MAX_PASSWORD_LEN)) {
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Password must be between %d and %d characters long. "), MIN_PASSWORD_LEN, MAX_PASSWORD_LEN);
|
2014-09-16 01:00:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_is_empty(passconfirm)) {
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Enter password again "));
|
2014-09-16 01:00:36 +02:00
|
|
|
snprintf(passconfirm, sizeof(passconfirm), "%s", user_password.pass);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(user_password.pass, passconfirm) != 0) {
|
|
|
|
memset(passconfirm, 0, sizeof(passconfirm));
|
|
|
|
memset(user_password.pass, 0, sizeof(user_password.pass));
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Passwords don't match. Try again. "));
|
2014-09-16 01:00:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid_password = true;
|
2014-09-15 03:49:38 +02:00
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Data file '%s' is encrypted"), DATA_FILE);
|
2014-09-16 01:00:36 +02:00
|
|
|
memset(passconfirm, 0, sizeof(passconfirm));
|
2014-09-14 23:46:28 +02:00
|
|
|
user_password.data_is_encrypted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
system("clear");
|
|
|
|
}
|
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
/* Store Tox data to given location
|
|
|
|
*
|
|
|
|
* Return 0 if stored successfully or ignoring data file.
|
|
|
|
* Return -1 on error
|
2013-08-15 12:11:48 +02:00
|
|
|
*/
|
2014-09-14 23:46:28 +02:00
|
|
|
int store_data(Tox *m, const char *path)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-09 06:56:47 +02:00
|
|
|
if (path == NULL)
|
2014-07-31 18:48:49 +02:00
|
|
|
return -1;
|
2013-09-09 06:56:47 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
FILE *fp = fopen(path, "wb");
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size_t data_len = tox_get_savedata_size(m);
|
|
|
|
char data[data_len];
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
tox_get_savedata(m, (uint8_t *) data);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-10-02 08:07:54 +02:00
|
|
|
if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) {
|
2015-04-01 03:56:11 +02:00
|
|
|
size_t enc_len = data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
|
|
|
char enc_data[enc_len];
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
TOX_ERR_ENCRYPTION err;
|
|
|
|
tox_pass_encrypt((uint8_t *) data, data_len, (uint8_t *) user_password.pass, user_password.len,
|
|
|
|
(uint8_t *) enc_data, &err);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
if (err != TOX_ERR_ENCRYPTION_OK) {
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("tox_pass_encrypt() failed with error %d\n"), err);
|
2015-04-01 03:56:11 +02:00
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
if (fwrite(enc_data, enc_len, 1, fp) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
} else { /* data will not be encrypted */
|
2015-04-01 03:56:11 +02:00
|
|
|
if (fwrite(data, data_len, 1, fp) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2013-08-15 12:11:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
static void init_tox_callbacks(Tox *m)
|
2013-08-15 12:11:48 +02:00
|
|
|
{
|
2015-03-28 07:56:54 +01:00
|
|
|
tox_callback_self_connection_status(m, prompt_onSelfConnectionChange, NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
tox_callback_friend_connection_status(m, on_connectionchange, NULL);
|
|
|
|
tox_callback_friend_typing(m, on_typing_change, NULL);
|
|
|
|
tox_callback_friend_request(m, on_request, NULL);
|
|
|
|
tox_callback_friend_message(m, on_message, NULL);
|
|
|
|
tox_callback_friend_name(m, on_nickchange, NULL);
|
|
|
|
tox_callback_friend_status(m, on_statuschange, NULL);
|
|
|
|
tox_callback_friend_status_message(m, on_statusmessagechange, NULL);
|
|
|
|
tox_callback_friend_read_receipt(m, on_read_receipt, NULL);
|
|
|
|
tox_callback_group_invite(m, on_groupinvite, NULL);
|
|
|
|
tox_callback_group_message(m, on_groupmessage, NULL);
|
|
|
|
tox_callback_group_action(m, on_groupaction, NULL);
|
|
|
|
tox_callback_group_namelist_change(m, on_group_namelistchange, NULL);
|
|
|
|
tox_callback_group_title(m, on_group_titlechange, NULL);
|
2015-03-28 07:56:54 +01:00
|
|
|
tox_callback_file_recv(m, on_file_recv, NULL);
|
|
|
|
tox_callback_file_chunk_request(m, on_file_chunk_request, NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
tox_callback_file_recv_control(m, on_file_control, NULL);
|
2015-03-28 07:56:54 +01:00
|
|
|
tox_callback_file_recv_chunk(m, on_file_recv_chunk, NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
}
|
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
static void init_tox_options(struct Tox_Options *tox_opts)
|
2015-03-26 03:56:45 +01:00
|
|
|
{
|
2015-05-19 01:52:22 +02:00
|
|
|
tox_options_default(tox_opts);
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
tox_opts->ipv6_enabled = !arg_opts.use_ipv4;
|
|
|
|
tox_opts->udp_enabled = !arg_opts.force_tcp;
|
|
|
|
tox_opts->proxy_type = arg_opts.proxy_type;
|
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
if (!tox_opts->ipv6_enabled)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Forcing IPv4 connection"));
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
if (tox_opts->proxy_type != TOX_PROXY_TYPE_NONE) {
|
|
|
|
tox_opts->proxy_port = arg_opts.proxy_port;
|
2015-03-28 07:56:54 +01:00
|
|
|
tox_opts->proxy_host = arg_opts.proxy_address;
|
|
|
|
const char *ps = tox_opts->proxy_type == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP";
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
char tmp[48];
|
2015-05-25 16:38:52 +02:00
|
|
|
snprintf(tmp, sizeof(tmp), gettext("Using %s proxy %s : %d"), ps, arg_opts.proxy_address, arg_opts.proxy_port);
|
2015-03-26 03:56:45 +01:00
|
|
|
queue_init_message("%s", tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tox_opts->udp_enabled) {
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("UDP disabled"));
|
2015-03-26 03:56:45 +01:00
|
|
|
} else if (tox_opts->proxy_type != TOX_PROXY_TYPE_NONE) {
|
2015-05-25 16:38:52 +02:00
|
|
|
const char *msg = gettext("WARNING: Using a proxy without disabling UDP may leak your real IP address.");
|
2015-03-26 03:56:45 +01:00
|
|
|
queue_init_message("%s", msg);
|
2015-05-25 16:38:52 +02:00
|
|
|
msg = gettext("Use the -t option to disable UDP.");
|
2015-03-26 03:56:45 +01:00
|
|
|
queue_init_message("%s", msg);
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
/* Returns a new Tox object on success.
|
|
|
|
* If object fails to initialize the toxic process will terminate.
|
|
|
|
*/
|
2015-04-13 06:32:35 +02:00
|
|
|
static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW *new_err)
|
2015-03-26 03:56:45 +01:00
|
|
|
{
|
2015-03-28 07:56:54 +01:00
|
|
|
Tox *m = NULL;
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
FILE *fp = fopen(data_path, "rb");
|
2013-08-15 12:11:48 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
if (fp != NULL) { /* Data file exists */
|
2015-03-26 03:56:45 +01:00
|
|
|
off_t len = file_size(data_path);
|
2014-09-24 04:51:56 +02:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
if (len == 0) {
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_toxic"), FATALERR_FILEOP);
|
2014-09-24 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
char data[len];
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
if (fread(data, sizeof(data), 1, fp) != 1) {
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_toxic"), FATALERR_FILEOP);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
bool is_encrypted = tox_is_data_encrypted((uint8_t *) data);
|
2014-09-15 22:17:58 +02:00
|
|
|
|
|
|
|
/* attempt to encrypt an already encrypted data file */
|
2015-04-13 06:32:35 +02:00
|
|
|
if (arg_opts.encrypt_data && is_encrypted) {
|
|
|
|
fclose(fp);
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_toxic"), FATALERR_ENCRYPT);
|
2015-04-13 06:32:35 +02:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
|
|
|
if (arg_opts.unencrypt_data && is_encrypted)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Data file '%s' has been unencrypted"), data_path);
|
2014-09-19 05:50:51 +02:00
|
|
|
else if (arg_opts.unencrypt_data)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Warning: passed --unencrypt-data option with unencrypted data file '%s'"), data_path);
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2014-09-15 22:17:58 +02:00
|
|
|
if (is_encrypted) {
|
2014-09-19 05:50:51 +02:00
|
|
|
if (!arg_opts.unencrypt_data)
|
|
|
|
user_password.data_is_encrypted = true;
|
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
size_t pwlen = 0;
|
2015-03-26 03:56:45 +01:00
|
|
|
system("clear"); // TODO: is this portable?
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Enter password (\"%s\" to quit) "), "q");
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
|
|
|
char plain[plain_len];
|
|
|
|
|
2014-09-15 03:49:38 +02:00
|
|
|
while (true) {
|
2014-09-14 23:46:28 +02:00
|
|
|
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
|
|
|
|
user_password.len = pwlen;
|
|
|
|
|
2015-04-13 06:32:35 +02:00
|
|
|
if (strcasecmp(user_password.pass, "q") == 0) {
|
|
|
|
fclose(fp);
|
2014-09-15 03:49:38 +02:00
|
|
|
exit(0);
|
2015-04-13 06:32:35 +02:00
|
|
|
}
|
2014-09-15 03:49:38 +02:00
|
|
|
|
2014-09-16 01:00:36 +02:00
|
|
|
if (pwlen < MIN_PASSWORD_LEN) {
|
|
|
|
system("clear");
|
|
|
|
sleep(1);
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Invalid password. Try again. "));
|
2014-09-15 22:17:58 +02:00
|
|
|
continue;
|
2014-09-16 01:00:36 +02:00
|
|
|
}
|
2014-09-15 22:17:58 +02:00
|
|
|
|
2015-04-01 03:56:11 +02:00
|
|
|
TOX_ERR_DECRYPTION pwerr;
|
|
|
|
tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) user_password.pass, pwlen,
|
|
|
|
(uint8_t *) plain, &pwerr);
|
|
|
|
|
|
|
|
if (pwerr == TOX_ERR_DECRYPTION_OK) {
|
2015-05-24 23:36:13 +02:00
|
|
|
tox_opts->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
|
|
|
|
tox_opts->savedata_data = (uint8_t *) plain;
|
|
|
|
tox_opts->savedata_length = plain_len;
|
|
|
|
|
|
|
|
m = tox_new(tox_opts, new_err);
|
2015-04-01 03:56:11 +02:00
|
|
|
|
2015-04-14 23:18:09 +02:00
|
|
|
if (m == NULL) {
|
2015-04-13 06:32:35 +02:00
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
break;
|
2015-04-01 03:56:11 +02:00
|
|
|
} else if (pwerr == TOX_ERR_DECRYPTION_FAILED) {
|
2014-09-15 03:49:38 +02:00
|
|
|
system("clear");
|
2014-09-15 10:13:12 +02:00
|
|
|
sleep(1);
|
2015-05-25 16:38:52 +02:00
|
|
|
printf(gettext("Invalid password. Try again. "));
|
2015-03-26 03:56:45 +01:00
|
|
|
} else {
|
2015-04-13 06:32:35 +02:00
|
|
|
fclose(fp);
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("tox_pass_decrypt() failed"), pwerr);
|
2014-09-14 23:46:28 +02:00
|
|
|
}
|
2014-09-15 03:49:38 +02:00
|
|
|
}
|
2015-04-01 03:56:11 +02:00
|
|
|
} else { /* data is not encrypted */
|
2015-05-24 23:36:13 +02:00
|
|
|
tox_opts->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
|
|
|
|
tox_opts->savedata_data = (uint8_t *) data;
|
|
|
|
tox_opts->savedata_length = len;
|
|
|
|
|
|
|
|
m = tox_new(tox_opts, new_err);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-04-14 23:18:09 +02:00
|
|
|
if (m == NULL) {
|
2015-04-13 06:32:35 +02:00
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2015-04-01 03:56:11 +02:00
|
|
|
} else { /* Data file does not/should not exist */
|
2015-03-26 03:56:45 +01:00
|
|
|
if (file_exists(data_path))
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_toxic"), FATALERR_FILEOP);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-05-24 23:36:13 +02:00
|
|
|
tox_opts->savedata_type = TOX_SAVEDATA_TYPE_NONE;
|
|
|
|
|
|
|
|
m = tox_new(tox_opts, new_err);
|
2014-10-02 08:07:54 +02:00
|
|
|
|
2015-04-14 23:18:09 +02:00
|
|
|
if (m == NULL)
|
2015-04-13 06:32:35 +02:00
|
|
|
return NULL;
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
if (store_data(m, data_path) == -1)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_toxic"), FATALERR_FILEOP);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
static Tox *load_toxic(char *data_path)
|
2015-03-26 03:56:45 +01:00
|
|
|
{
|
2015-03-28 07:56:54 +01:00
|
|
|
struct Tox_Options tox_opts;
|
2015-03-26 03:56:45 +01:00
|
|
|
init_tox_options(&tox_opts);
|
|
|
|
|
2015-04-13 06:32:35 +02:00
|
|
|
TOX_ERR_NEW new_err;
|
|
|
|
Tox *m = load_tox(data_path, &tox_opts, &new_err);
|
|
|
|
|
2015-04-14 23:18:09 +02:00
|
|
|
if (new_err == TOX_ERR_NEW_PORT_ALLOC && tox_opts.ipv6_enabled) {
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Falling back to ipv4"));
|
2015-04-13 06:32:35 +02:00
|
|
|
tox_opts.ipv6_enabled = false;
|
|
|
|
m = load_tox(data_path, &tox_opts, &new_err);
|
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-04-14 23:18:09 +02:00
|
|
|
if (!m)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("tox_new returned fatal error"), new_err);
|
2015-04-14 23:18:09 +02:00
|
|
|
|
2015-04-13 06:58:33 +02:00
|
|
|
if (new_err != TOX_ERR_NEW_OK)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("tox_new returned non-fatal error %d"), new_err);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
init_tox_callbacks(m);
|
2015-03-28 07:56:54 +01:00
|
|
|
load_friendlist(m);
|
|
|
|
load_blocklist(BLOCK_FILE);
|
|
|
|
|
|
|
|
if (tox_self_get_name_size(m) == 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
tox_self_set_name(m, (uint8_t *) gettext("Toxic User"), strlen(gettext("Toxic User")), NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
return m;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2015-03-30 01:05:24 +02:00
|
|
|
#define TRY_BOOTSTRAP_INTERVAL 5
|
|
|
|
static uint64_t last_bootstrap_time = 0;
|
|
|
|
|
|
|
|
static void do_bootstrap(Tox *m)
|
2014-02-22 23:58:36 +01:00
|
|
|
{
|
2015-03-30 01:05:24 +02:00
|
|
|
static int conn_err = 0;
|
|
|
|
uint64_t curtime = get_unix_time();
|
|
|
|
|
|
|
|
if (!timed_out(last_bootstrap_time, curtime, TRY_BOOTSTRAP_INTERVAL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tox_self_get_connection_status(m) != TOX_CONNECTION_NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (conn_err != 0)
|
|
|
|
return;
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-03-30 01:05:24 +02:00
|
|
|
last_bootstrap_time = curtime;
|
|
|
|
conn_err = init_connection(m);
|
2014-11-26 06:48:58 +01:00
|
|
|
|
2015-03-30 01:05:24 +02:00
|
|
|
if (conn_err != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, gettext("Auto-connect failed with error code %d"), conn_err);
|
2015-03-30 01:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_toxic(Tox *m, ToxWindow *prompt)
|
|
|
|
{
|
|
|
|
if (arg_opts.no_connect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
tox_iterate(m);
|
|
|
|
do_bootstrap(m);
|
2014-07-09 06:05:13 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2014-03-13 11:06:53 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 22:18:34 +02:00
|
|
|
#define INACTIVE_WIN_REFRESH_RATE 10
|
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
void *thread_winref(void *data)
|
|
|
|
{
|
|
|
|
Tox *m = (Tox *) data;
|
2014-07-06 22:18:34 +02:00
|
|
|
uint8_t draw_count = 0;
|
2014-09-23 01:58:08 +02:00
|
|
|
init_signal_catchers();
|
2014-03-13 11:06:53 +01:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
while (true) {
|
2014-03-13 11:06:53 +01:00
|
|
|
draw_active_window(m);
|
2014-07-06 22:18:34 +02:00
|
|
|
draw_count++;
|
2014-07-01 05:56:47 +02:00
|
|
|
|
|
|
|
if (Winthread.flag_resize) {
|
|
|
|
on_window_resize();
|
2014-09-22 20:27:33 +02:00
|
|
|
Winthread.flag_resize = 0;
|
2014-07-06 22:18:34 +02:00
|
|
|
} else if (draw_count >= INACTIVE_WIN_REFRESH_RATE) {
|
2014-07-01 05:56:47 +02:00
|
|
|
refresh_inactive_windows();
|
2014-07-06 22:18:34 +02:00
|
|
|
draw_count = 0;
|
2014-07-01 05:56:47 +02:00
|
|
|
}
|
2014-07-09 06:05:13 +02:00
|
|
|
|
|
|
|
if (Winthread.sig_exit_toxic) {
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
exit_toxic_success(m);
|
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
2014-02-22 23:58:36 +01:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:18:42 +02:00
|
|
|
void *thread_cqueue(void *data)
|
|
|
|
{
|
|
|
|
Tox *m = (Tox *) data;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
|
|
|
size_t i;
|
2014-09-06 19:18:42 +02:00
|
|
|
|
2014-09-19 05:50:51 +02:00
|
|
|
for (i = 2; i < MAX_WINDOWS_NUM; ++i) {
|
2014-09-06 19:18:42 +02:00
|
|
|
ToxWindow *toxwin = get_window_ptr(i);
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
if (toxwin != NULL && toxwin->is_chat
|
|
|
|
&& tox_friend_get_connection_status(m, toxwin->num, NULL) != TOX_CONNECTION_NONE)
|
2014-09-10 22:18:37 +02:00
|
|
|
cqueue_try_send(toxwin, m);
|
2014-09-06 19:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2014-12-04 04:10:21 +01:00
|
|
|
usleep(4000);
|
2014-09-06 19:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
#ifdef AUDIO
|
|
|
|
void *thread_audio(void *data)
|
|
|
|
{
|
2015-06-12 17:50:26 +02:00
|
|
|
ToxAV *av = (ToxAV *) data;
|
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
while (true) {
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2015-06-12 17:50:26 +02:00
|
|
|
toxav_iterate(av);
|
2014-11-29 05:47:22 +01:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2015-07-06 22:43:05 +02:00
|
|
|
usleep(toxav_iteration_interval(av) * 1000);
|
2014-11-29 05:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* AUDIO */
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void print_usage(void)
|
2014-06-21 01:58:00 +02:00
|
|
|
{
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("usage: toxic [OPTION] [FILE ...]\n"));
|
|
|
|
fprintf(stderr, " -4, --ipv4 ");
|
|
|
|
fprintf(stderr, gettext("Force IPv4 connection\n"));
|
|
|
|
fprintf(stderr, " -b, --debug ");
|
|
|
|
fprintf(stderr, gettext("Enable stderr for debugging\n"));
|
|
|
|
fprintf(stderr, " -c, --config ");
|
|
|
|
fprintf(stderr, gettext("Use specified config file\n"));
|
|
|
|
fprintf(stderr, " -d, --default-locale ");
|
|
|
|
fprintf(stderr, gettext("Use default POSIX locale\n"));
|
|
|
|
fprintf(stderr, " -e, --encrypt-data ");
|
|
|
|
fprintf(stderr, gettext("Encrypt an unencrypted data file\n"));
|
|
|
|
fprintf(stderr, " -f, --file ");
|
|
|
|
fprintf(stderr, gettext("Use specified data file\n"));
|
|
|
|
fprintf(stderr, " -h, --help ");
|
|
|
|
fprintf(stderr, gettext("Show this message and exit\n"));
|
|
|
|
fprintf(stderr, " -n, --nodes ");
|
|
|
|
fprintf(stderr, gettext("Use specified DHTnodes file\n"));
|
|
|
|
fprintf(stderr, " -o, --noconnect ");
|
|
|
|
fprintf(stderr, gettext("Do not connect to the DHT network\n"));
|
|
|
|
fprintf(stderr, " -p, --SOCKS5-proxy ");
|
|
|
|
fprintf(stderr, gettext("Use SOCKS5 proxy: Requires [IP] [port]\n"));
|
|
|
|
fprintf(stderr, " -P, --HTTP-proxy ");
|
|
|
|
fprintf(stderr, gettext("Use HTTP proxy: Requires [IP] [port]\n"));
|
|
|
|
fprintf(stderr, " -r, --dnslist ");
|
|
|
|
fprintf(stderr, gettext("Use specified DNSservers file\n"));
|
|
|
|
fprintf(stderr, " -t, --force-tcp ");
|
|
|
|
fprintf(stderr, gettext("Force TCP connection (use this with proxies)\n"));
|
|
|
|
fprintf(stderr, " -u, --unencrypt-data ");
|
|
|
|
fprintf(stderr, gettext("Unencrypt an encrypted data file\n"));
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_default_opts(void)
|
|
|
|
{
|
2014-09-19 05:50:51 +02:00
|
|
|
memset(&arg_opts, 0, sizeof(struct arg_opts));
|
|
|
|
|
|
|
|
/* set any non-zero defaults here*/
|
2015-03-26 03:56:45 +01:00
|
|
|
arg_opts.proxy_type = TOX_PROXY_TYPE_NONE;
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
set_default_opts();
|
|
|
|
|
|
|
|
static struct option long_opts[] = {
|
|
|
|
{"file", required_argument, 0, 'f'},
|
|
|
|
{"ipv4", no_argument, 0, '4'},
|
2014-08-16 01:04:39 +02:00
|
|
|
{"debug", no_argument, 0, 'b'},
|
2014-08-15 00:45:41 +02:00
|
|
|
{"default-locale", no_argument, 0, 'd'},
|
2014-06-22 02:18:23 +02:00
|
|
|
{"config", required_argument, 0, 'c'},
|
2014-09-15 22:17:58 +02:00
|
|
|
{"encrypt-data", no_argument, 0, 'e'},
|
2014-06-22 02:18:23 +02:00
|
|
|
{"nodes", required_argument, 0, 'n'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2014-08-13 00:59:27 +02:00
|
|
|
{"noconnect", no_argument, 0, 'o'},
|
2014-08-13 01:13:57 +02:00
|
|
|
{"dnslist", required_argument, 0, 'r'},
|
2014-08-15 00:45:41 +02:00
|
|
|
{"force-tcp", no_argument, 0, 't'},
|
2014-12-27 19:12:16 +01:00
|
|
|
{"SOCKS5-proxy", required_argument, 0, 'p'},
|
|
|
|
{"HTTP-proxy", required_argument, 0, 'P'},
|
2014-09-19 05:50:51 +02:00
|
|
|
{"unencrypt-data", no_argument, 0, 'u'},
|
2014-08-27 20:30:06 +02:00
|
|
|
{NULL, no_argument, NULL, 0},
|
2014-06-22 02:18:23 +02:00
|
|
|
};
|
|
|
|
|
2014-12-27 19:12:16 +01:00
|
|
|
const char *opts_str = "4bdehotuxc:f:n:r:p:P:";
|
2014-06-22 02:18:23 +02:00
|
|
|
int opt, indexptr;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
|
|
|
|
switch (opt) {
|
2014-08-13 00:59:27 +02:00
|
|
|
case '4':
|
|
|
|
arg_opts.use_ipv4 = 1;
|
|
|
|
break;
|
|
|
|
|
2014-08-16 01:04:39 +02:00
|
|
|
case 'b':
|
|
|
|
arg_opts.debug = 1;
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("stderr enabled"));
|
2014-08-16 01:04:39 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-13 00:59:27 +02:00
|
|
|
case 'c':
|
|
|
|
snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-08-28 04:45:11 +02:00
|
|
|
if (!file_exists(arg_opts.config_path))
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Config file not found"));
|
2014-08-27 05:21:32 +02:00
|
|
|
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
arg_opts.default_locale = 1;
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Using default POSIX locale"));
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
|
|
|
|
2014-09-15 22:17:58 +02:00
|
|
|
case 'e':
|
|
|
|
arg_opts.encrypt_data = 1;
|
|
|
|
break;
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
case 'f':
|
2014-08-02 19:04:29 +02:00
|
|
|
arg_opts.use_custom_data = 1;
|
2014-06-22 02:18:23 +02:00
|
|
|
DATA_FILE = strdup(optarg);
|
2014-08-02 05:00:52 +02:00
|
|
|
BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2014-07-31 18:48:49 +02:00
|
|
|
if (DATA_FILE == NULL || BLOCK_FILE == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in parse_args"), FATALERR_MEMORY);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2014-08-02 05:00:52 +02:00
|
|
|
strcpy(BLOCK_FILE, optarg);
|
2014-07-31 18:48:49 +02:00
|
|
|
strcat(BLOCK_FILE, "-blocklist");
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Using '%s' data file"), DATA_FILE);
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-08-28 04:45:11 +02:00
|
|
|
if (!file_exists(arg_opts.nodes_path))
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("DHTnodes file not found"));
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-13 00:59:27 +02:00
|
|
|
case 'o':
|
|
|
|
arg_opts.no_connect = 1;
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("DHT disabled"));
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-15 02:31:52 +02:00
|
|
|
case 'p':
|
2015-03-26 03:56:45 +01:00
|
|
|
arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5;
|
2014-12-27 19:12:16 +01:00
|
|
|
snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);
|
|
|
|
|
|
|
|
if (++optind > argc || argv[optind-1][0] == '-')
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("Proxy error"), FATALERR_PROXY);
|
2014-12-27 19:12:16 +01:00
|
|
|
|
|
|
|
arg_opts.proxy_port = (uint16_t) atoi(argv[optind-1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2015-03-26 03:56:45 +01:00
|
|
|
arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP;
|
2014-08-15 02:31:52 +02:00
|
|
|
snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);
|
2014-08-15 21:13:35 +02:00
|
|
|
|
|
|
|
if (++optind > argc || argv[optind-1][0] == '-')
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("Proxy error"), FATALERR_PROXY);
|
2014-08-15 21:13:35 +02:00
|
|
|
|
|
|
|
arg_opts.proxy_port = (uint16_t) atoi(argv[optind-1]);
|
2014-08-15 02:31:52 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-13 01:13:57 +02:00
|
|
|
case 'r':
|
|
|
|
snprintf(arg_opts.dns_path, sizeof(arg_opts.dns_path), "%s", optarg);
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-08-28 04:45:11 +02:00
|
|
|
if (!file_exists(arg_opts.dns_path))
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("DNSservers file not found"));
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-08-13 01:13:57 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-15 00:45:41 +02:00
|
|
|
case 't':
|
|
|
|
arg_opts.force_tcp = 1;
|
|
|
|
break;
|
|
|
|
|
2014-09-19 05:50:51 +02:00
|
|
|
case 'u':
|
|
|
|
arg_opts.unencrypt_data = 1;
|
|
|
|
break;
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
print_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2014-06-21 01:58:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:48:49 +02:00
|
|
|
#define DATANAME "data"
|
|
|
|
#define BLOCKNAME "data-blocklist"
|
2014-09-14 23:46:28 +02:00
|
|
|
static int init_default_data_files(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2014-08-02 19:04:29 +02:00
|
|
|
if (arg_opts.use_custom_data)
|
|
|
|
return 0;
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
char *user_config_dir = get_user_config_dir();
|
2014-08-03 03:01:40 +02:00
|
|
|
int config_err = create_user_config_dirs(user_config_dir);
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
if (config_err) {
|
|
|
|
DATA_FILE = strdup(DATANAME);
|
|
|
|
BLOCK_FILE = strdup(BLOCKNAME);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
if (DATA_FILE == NULL || BLOCK_FILE == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_data_structures"), FATALERR_MEMORY);
|
2014-09-14 23:46:28 +02:00
|
|
|
} else {
|
|
|
|
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(DATANAME) + 1);
|
|
|
|
BLOCK_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(BLOCKNAME) + 1);
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
if (DATA_FILE == NULL || BLOCK_FILE == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in load_data_structures"), FATALERR_MEMORY);
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
strcpy(DATA_FILE, user_config_dir);
|
|
|
|
strcat(DATA_FILE, CONFIGDIR);
|
|
|
|
strcat(DATA_FILE, DATANAME);
|
2014-07-31 18:48:49 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
strcpy(BLOCK_FILE, user_config_dir);
|
|
|
|
strcat(BLOCK_FILE, CONFIGDIR);
|
|
|
|
strcat(BLOCK_FILE, BLOCKNAME);
|
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);
|
2014-07-31 18:48:49 +02:00
|
|
|
return config_err;
|
|
|
|
}
|
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
#define REC_TOX_DO_LOOPS_PER_SEC 25
|
|
|
|
|
|
|
|
/* Adjusts usleep value so that tox_do runs close to the recommended number of times per second */
|
|
|
|
static useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, uint64_t cur_time, useconds_t msleepval)
|
|
|
|
{
|
|
|
|
useconds_t new_sleep = msleepval;
|
|
|
|
++(*loopcount);
|
|
|
|
|
|
|
|
if (*looptimer == cur_time)
|
|
|
|
return new_sleep;
|
|
|
|
|
|
|
|
if (*loopcount != REC_TOX_DO_LOOPS_PER_SEC)
|
|
|
|
new_sleep *= (double) *loopcount / REC_TOX_DO_LOOPS_PER_SEC;
|
|
|
|
|
|
|
|
*looptimer = cur_time;
|
|
|
|
*loopcount = 0;
|
|
|
|
return new_sleep;
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:26:28 +02:00
|
|
|
#ifdef X11
|
2015-03-26 03:56:45 +01:00
|
|
|
// FIXME
|
2014-12-27 19:12:16 +01:00
|
|
|
void DnD_callback(const char* asdv, DropType dt)
|
2014-10-11 23:53:02 +02:00
|
|
|
{
|
|
|
|
if (dt != DT_plain)
|
2014-12-27 19:12:16 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv);
|
2014-10-11 23:53:02 +02:00
|
|
|
}
|
2014-10-12 23:26:28 +02:00
|
|
|
#endif /* X11 */
|
2014-10-11 23:53:02 +02:00
|
|
|
|
2014-07-31 18:48:49 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
parse_args(argc, argv);
|
2014-08-03 06:26:44 +02:00
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
/* Use the -b flag to enable stderr */
|
|
|
|
if (!arg_opts.debug)
|
|
|
|
freopen("/dev/null", "w", stderr);
|
|
|
|
|
2014-09-19 06:29:30 +02:00
|
|
|
if (arg_opts.encrypt_data && arg_opts.unencrypt_data) {
|
|
|
|
arg_opts.encrypt_data = 0;
|
|
|
|
arg_opts.unencrypt_data = 0;
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Warning: Using \"%s\" and \"%s\" simultaneously has no effect"), "--unencrypt-data", "--encrypt-data");
|
2014-09-19 06:29:30 +02:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2014-07-31 18:48:49 +02:00
|
|
|
/* Make sure all written files are read/writeable only by the current user. */
|
|
|
|
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
2014-09-15 22:17:58 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
int config_err = init_default_data_files();
|
2014-09-15 22:17:58 +02:00
|
|
|
bool datafile_exists = file_exists(DATA_FILE);
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2015-03-30 01:05:24 +02:00
|
|
|
if (datafile_exists)
|
|
|
|
last_bootstrap_time = get_unix_time();
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
if (!datafile_exists && !arg_opts.unencrypt_data)
|
2015-05-25 16:38:52 +02:00
|
|
|
first_time_encrypt(gettext("Creating new data file. Would you like to encrypt it? Y/n (q to quit)"));
|
2015-03-26 03:56:45 +01:00
|
|
|
else if (arg_opts.encrypt_data)
|
2015-05-25 16:38:52 +02:00
|
|
|
first_time_encrypt(gettext("Encrypt existing data file? Y/n (q to quit)"));
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-04-07 10:42:10 +02:00
|
|
|
/* init user_settings struct and load settings from conf file */
|
2014-09-23 03:24:45 +02:00
|
|
|
user_settings = calloc(1, sizeof(struct user_settings));
|
2014-04-07 10:42:10 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
if (user_settings == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in main"), FATALERR_MEMORY);
|
2014-04-07 10:42:10 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
const char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL;
|
2014-09-23 03:24:45 +02:00
|
|
|
int settings_err = settings_load(user_settings, p);
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-10-01 23:28:34 +02:00
|
|
|
#ifdef X11
|
2014-12-27 19:12:16 +01:00
|
|
|
if (init_xtra(DnD_callback) == -1)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("X failed to initialize"));
|
2014-10-01 23:28:34 +02:00
|
|
|
#endif
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
Tox *m = load_toxic(DATA_FILE);
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
if (arg_opts.encrypt_data && !datafile_exists)
|
|
|
|
arg_opts.encrypt_data = 0;
|
2015-03-11 23:38:07 +01:00
|
|
|
|
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
init_term();
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
prompt = init_windows(m);
|
2014-07-26 01:55:16 +02:00
|
|
|
prompt_init_statusbar(prompt, m);
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
/* thread for ncurses stuff */
|
2014-06-22 02:18:23 +02:00
|
|
|
if (pthread_mutex_init(&Winthread.lock, NULL) != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in main"), FATALERR_MUTEX_INIT);
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in main"), FATALERR_THREAD_CREATE);
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-09-06 19:18:42 +02:00
|
|
|
/* thread for message queue */
|
|
|
|
if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) m) != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in main"), FATALERR_THREAD_CREATE);
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-03-08 16:36:42 +01:00
|
|
|
av = init_audio(prompt, m);
|
2014-07-26 01:55:16 +02:00
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
/* audio thread */
|
|
|
|
if (pthread_create(&audio_thread.tid, NULL, thread_audio, (void *) av) != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in main"), FATALERR_THREAD_CREATE);
|
2014-11-29 05:47:22 +01:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
set_primary_device(input, user_settings->audio_in_dev);
|
|
|
|
set_primary_device(output, user_settings->audio_out_dev);
|
2014-08-26 01:26:41 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
#elif SOUND_NOTIFY
|
2014-07-21 03:25:21 +02:00
|
|
|
if ( init_devices() == de_InternalError )
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Failed to init audio devices"));
|
2014-07-22 20:38:32 +02:00
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-07-27 01:49:59 +02:00
|
|
|
init_notify(60, 3000);
|
2014-07-22 20:38:32 +02:00
|
|
|
|
2014-07-26 01:55:16 +02:00
|
|
|
const char *msg;
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2013-08-14 19:21:36 +02:00
|
|
|
if (config_err) {
|
2015-05-25 16:38:52 +02:00
|
|
|
msg = gettext("Unable to determine configuration directory. Defaulting to 'data' for data file...");
|
2014-09-19 05:50:51 +02:00
|
|
|
queue_init_message("%s", msg);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:31:52 +02:00
|
|
|
if (settings_err == -1)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Failed to load user settings"));
|
2014-08-15 02:31:52 +02:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
/* screen/tmux auto-away timer */
|
2015-03-26 03:56:45 +01:00
|
|
|
if (init_mplex_away_timer(m) == -1)
|
2015-05-25 16:38:52 +02:00
|
|
|
queue_init_message(gettext("Failed to init mplex auto-away."));
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-08-26 01:26:41 +02:00
|
|
|
print_init_messages(prompt);
|
2014-08-27 05:21:32 +02:00
|
|
|
cleanup_init_messages();
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-09-29 07:30:22 +02:00
|
|
|
/* set user avatar from config file. if no path is supplied tox_unset_avatar is called */
|
2014-09-26 09:10:44 +02:00
|
|
|
char avatarstr[MAX_STR_SIZE];
|
|
|
|
snprintf(avatarstr, sizeof(avatarstr), "/avatar \"%s\"", user_settings->avatar_path);
|
|
|
|
execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE);
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
uint64_t last_save = (uint64_t) time(NULL);
|
2014-11-29 05:47:22 +01:00
|
|
|
uint64_t looptimer = last_save;
|
|
|
|
useconds_t msleepval = 40000;
|
|
|
|
uint64_t loopcount = 0;
|
2014-07-08 20:46:50 +02:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
while (true) {
|
2014-03-14 04:56:46 +01:00
|
|
|
update_unix_time();
|
2014-02-21 06:45:29 +01:00
|
|
|
do_toxic(m, prompt);
|
2014-07-08 20:46:50 +02:00
|
|
|
uint64_t cur_time = get_unix_time();
|
|
|
|
|
|
|
|
if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) {
|
2014-07-09 06:05:13 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2014-10-02 08:07:54 +02:00
|
|
|
if (store_data(m, DATA_FILE) != 0)
|
2015-05-25 16:38:52 +02:00
|
|
|
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, gettext("WARNING: Failed to save to data file"));
|
2014-07-09 06:05:13 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
2014-07-08 20:46:50 +02:00
|
|
|
last_save = cur_time;
|
|
|
|
}
|
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
msleepval = optimal_msleepval(&looptimer, &loopcount, cur_time, msleepval);
|
|
|
|
usleep(msleepval);
|
2014-03-13 11:06:53 +01:00
|
|
|
}
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return 0;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|