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
|
|
|
*/
|
|
|
|
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <ctype.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <curses.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <errno.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-08-23 23:03:44 +02:00
|
|
|
#include <string.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <sys/socket.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-09-14 23:46:28 +02:00
|
|
|
#include <termios.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
#include <curl/curl.h>
|
2014-09-14 23:46:28 +02:00
|
|
|
#include <tox/toxencryptsave.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "audio_device.h"
|
|
|
|
#include "bootstrap.h"
|
2020-11-08 16:08:24 +01:00
|
|
|
#include "conference.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "configdir.h"
|
|
|
|
#include "execute.h"
|
2015-03-28 07:56:54 +01:00
|
|
|
#include "file_transfers.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "friendlist.h"
|
2014-03-25 08:17:22 +01:00
|
|
|
#include "line_info.h"
|
2014-06-22 03:41:38 +02:00
|
|
|
#include "log.h"
|
2014-09-06 19:18:42 +02:00
|
|
|
#include "message_queue.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "misc_tools.h"
|
2015-10-28 22:14:17 +01:00
|
|
|
#include "name_lookup.h"
|
2022-01-30 19:18:14 +01:00
|
|
|
#include "netprof.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "notify.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "term_mplex.h"
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2014-10-01 23:28:34 +02:00
|
|
|
#ifdef X11
|
2020-11-25 01:31:51 +01:00
|
|
|
#include "x11focus.h"
|
2014-10-01 23:28:34 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2014-04-19 23:58:13 +02:00
|
|
|
#include "audio_call.h"
|
2015-08-10 13:14:38 +02:00
|
|
|
#ifdef VIDEO
|
|
|
|
#include "video_call.h"
|
|
|
|
#endif /* VIDEO */
|
2021-12-11 23:35:51 +01:00
|
|
|
static ToxAV *av;
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2014-03-07 03:14:04 +01:00
|
|
|
|
2017-05-16 02:17:23 +02:00
|
|
|
#ifdef PYTHON
|
2017-05-17 02:31:23 +02:00
|
|
|
#include "api.h"
|
2017-05-16 02:17:23 +02:00
|
|
|
#include "python_api.h"
|
|
|
|
#endif
|
|
|
|
|
2013-08-25 01:16:43 +02:00
|
|
|
#ifndef PACKAGE_DATADIR
|
2016-09-25 03:07:04 +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
|
|
|
|
2015-10-15 05:09:11 +02:00
|
|
|
#define DATANAME "toxic_profile.tox"
|
|
|
|
#define BLOCKNAME "toxic_blocklist"
|
2015-08-27 09:38:08 +02:00
|
|
|
|
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;
|
2021-12-11 23:35:51 +01:00
|
|
|
static struct cqueue_thread cqueue_thread;
|
2014-07-31 18:48:49 +02:00
|
|
|
struct arg_opts arg_opts;
|
2021-12-05 23:03:42 +01:00
|
|
|
|
2021-12-13 16:56:53 +01:00
|
|
|
#ifdef AUDIO
|
|
|
|
static struct av_thread av_thread;
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 23:03:42 +01:00
|
|
|
// This struct is not thread safe. It should only ever be written to from the main thread
|
|
|
|
// before any other thread that uses it is initialized.
|
2014-09-23 03:24:45 +02:00
|
|
|
struct user_settings *user_settings = NULL;
|
2014-03-13 11:06:53 +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;
|
|
|
|
|
2017-05-19 20:36:43 +02:00
|
|
|
static time_t last_signal_time;
|
|
|
|
|
2014-07-09 02:21:51 +02:00
|
|
|
static void catch_SIGINT(int sig)
|
2013-08-11 02:59:16 +02:00
|
|
|
{
|
2020-03-15 19:57:00 +01:00
|
|
|
UNUSED_VAR(sig);
|
|
|
|
|
2017-05-19 20:36:43 +02:00
|
|
|
time_t cur_time = get_unix_time();
|
2017-06-01 22:46:12 +02:00
|
|
|
|
2017-05-19 20:36:43 +02:00
|
|
|
if (difftime(cur_time, last_signal_time) <= 1) {
|
|
|
|
Winthread.sig_exit_toxic = 1;
|
|
|
|
} else {
|
|
|
|
last_signal_time = cur_time;
|
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 06:26:44 +02:00
|
|
|
static void catch_SIGSEGV(int sig)
|
|
|
|
{
|
2020-03-15 19:57:00 +01:00
|
|
|
UNUSED_VAR(sig);
|
|
|
|
|
2020-11-01 17:15:41 +01:00
|
|
|
if (freopen("/dev/tty", "w", stderr)) { // make sure stderr is enabled since we may have disabled it
|
|
|
|
fprintf(stderr, "Caught SIGSEGV: Aborting toxic session.\n");
|
2020-03-15 19:57:00 +01:00
|
|
|
}
|
2020-04-23 03:13:28 +02:00
|
|
|
|
2014-08-03 06:26:44 +02:00
|
|
|
endwin();
|
2020-11-01 17:15:41 +01:00
|
|
|
|
2014-08-03 06:26:44 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:18:14 +01:00
|
|
|
void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func,
|
|
|
|
const char *message, void *user_data)
|
|
|
|
{
|
|
|
|
UNUSED_VAR(user_data);
|
|
|
|
UNUSED_VAR(file);
|
|
|
|
UNUSED_VAR(m);
|
|
|
|
|
|
|
|
if (user_data) {
|
|
|
|
FILE *fp = (FILE *)user_data;
|
|
|
|
fprintf(fp, "[%d] %u:%s() - %s\n", level, line, func, message);
|
|
|
|
fflush(fp);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "[%d] %u:%s() - %s\n", level, line, func, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 05:56:47 +02:00
|
|
|
static void flag_window_resize(int sig)
|
|
|
|
{
|
2020-03-15 19:57:00 +01:00
|
|
|
UNUSED_VAR(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);
|
|
|
|
}
|
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
void free_global_data(void)
|
|
|
|
{
|
|
|
|
if (DATA_FILE) {
|
|
|
|
free(DATA_FILE);
|
|
|
|
DATA_FILE = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BLOCK_FILE) {
|
|
|
|
free(BLOCK_FILE);
|
|
|
|
BLOCK_FILE = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_settings) {
|
|
|
|
free(user_settings);
|
|
|
|
user_settings = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
void exit_toxic_success(Tox *m)
|
|
|
|
{
|
2022-01-30 19:18:14 +01:00
|
|
|
if (arg_opts.netprof_log_dump) {
|
2022-01-31 18:45:35 +01:00
|
|
|
netprof_log_dump(m, arg_opts.netprof_fp, get_unix_time() - arg_opts.netprof_start_time);
|
2022-01-30 19:18:14 +01:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
store_data(m, DATA_FILE);
|
2020-11-11 03:25:27 +01:00
|
|
|
|
|
|
|
user_password = (struct user_password) {
|
|
|
|
0
|
|
|
|
};
|
2020-04-23 02:54:53 +02:00
|
|
|
|
|
|
|
terminate_notify();
|
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef AUDIO
|
2015-08-14 03:01:05 +02:00
|
|
|
#ifdef VIDEO
|
|
|
|
terminate_video();
|
|
|
|
#endif /* VIDEO */
|
2014-10-02 08:07:54 +02:00
|
|
|
terminate_audio();
|
|
|
|
#endif /* AUDIO */
|
2014-07-09 01:24:44 +02:00
|
|
|
|
2017-05-16 02:17:23 +02:00
|
|
|
#ifdef PYTHON
|
|
|
|
terminate_python();
|
|
|
|
#endif /* PYTHON */
|
|
|
|
|
2020-05-18 19:58:55 +02:00
|
|
|
free_global_data();
|
|
|
|
tox_kill(m);
|
|
|
|
|
|
|
|
if (arg_opts.log_fp != NULL) {
|
2020-05-17 05:12:54 +02:00
|
|
|
fclose(arg_opts.log_fp);
|
2020-05-18 19:58:55 +02:00
|
|
|
arg_opts.log_fp = NULL;
|
2020-05-17 05:12:54 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 19:18:14 +01:00
|
|
|
if (arg_opts.netprof_fp != NULL) {
|
|
|
|
fclose(arg_opts.netprof_fp);
|
|
|
|
arg_opts.netprof_fp = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
endwin();
|
2016-09-20 06:00:09 +02:00
|
|
|
curl_global_cleanup();
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2014-10-12 22:54:18 +02:00
|
|
|
#ifdef X11
|
2020-11-25 01:31:51 +01:00
|
|
|
terminate_x11focus();
|
2014-10-12 22:54:18 +02:00
|
|
|
#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)
|
|
|
|
{
|
2015-08-27 09:38:08 +02:00
|
|
|
free_global_data();
|
2020-03-15 19:57:00 +01:00
|
|
|
|
2020-11-01 17:15:41 +01:00
|
|
|
if (freopen("/dev/tty", "w", stderr)) {
|
|
|
|
fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg);
|
2020-03-15 19:57:00 +01:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
endwin();
|
|
|
|
exit(EXIT_FAILURE);
|
2013-08-11 02:59:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-20 15:49:04 +01:00
|
|
|
/* Sets ncurses refresh rate. Lower values make it refresh more often. */
|
|
|
|
void set_window_refresh_rate(size_t refresh_rate)
|
|
|
|
{
|
|
|
|
timeout(refresh_rate);
|
|
|
|
}
|
|
|
|
|
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-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("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
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
keypad(stdscr, 1);
|
|
|
|
noecho();
|
2015-12-04 20:15:31 +01:00
|
|
|
nonl();
|
2021-11-20 15:49:04 +01:00
|
|
|
set_window_refresh_rate(NCURSES_DEFAULT_REFRESH_RATE);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
if (has_colors()) {
|
2014-04-02 20:41:19 +02:00
|
|
|
short bg_color = COLOR_BLACK;
|
2020-12-05 21:39:33 +01:00
|
|
|
short bar_bg_color = COLOR_BLUE;
|
|
|
|
short bar_fg_color = COLOR_WHITE;
|
|
|
|
short bar_accent_color = COLOR_CYAN;
|
|
|
|
short bar_notify_color = COLOR_YELLOW;
|
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) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (assume_default_colors(-1, -1) == OK) {
|
2014-04-12 09:10:01 +02:00
|
|
|
bg_color = -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-04-12 09:10:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-05 21:39:33 +01:00
|
|
|
if (!string_is_empty(user_settings->color_bar_bg)) {
|
|
|
|
if (strcmp(user_settings->color_bar_bg, "black") == 0) {
|
|
|
|
bar_bg_color = COLOR_BLACK;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "red") == 0) {
|
|
|
|
bar_bg_color = COLOR_RED;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "blue") == 0) {
|
|
|
|
bar_bg_color = COLOR_BLUE;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "cyan") == 0) {
|
|
|
|
bar_bg_color = COLOR_CYAN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "green") == 0) {
|
|
|
|
bar_bg_color = COLOR_GREEN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "yellow") == 0) {
|
|
|
|
bar_bg_color = COLOR_YELLOW;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "magenta") == 0) {
|
|
|
|
bar_bg_color = COLOR_MAGENTA;
|
|
|
|
} else if (strcmp(user_settings->color_bar_bg, "white") == 0) {
|
|
|
|
bar_bg_color = COLOR_WHITE;
|
|
|
|
} else {
|
|
|
|
bar_bg_color = COLOR_BLUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_bg_color = COLOR_BLUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string_is_empty(user_settings->color_bar_fg)) {
|
|
|
|
if (strcmp(user_settings->color_bar_fg, "black") == 0) {
|
|
|
|
bar_fg_color = COLOR_BLACK;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "red") == 0) {
|
|
|
|
bar_fg_color = COLOR_RED;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "blue") == 0) {
|
|
|
|
bar_fg_color = COLOR_BLUE;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "cyan") == 0) {
|
|
|
|
bar_fg_color = COLOR_CYAN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "green") == 0) {
|
|
|
|
bar_fg_color = COLOR_GREEN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "yellow") == 0) {
|
|
|
|
bar_fg_color = COLOR_YELLOW;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "magenta") == 0) {
|
|
|
|
bar_fg_color = COLOR_MAGENTA;
|
|
|
|
} else if (strcmp(user_settings->color_bar_fg, "white") == 0) {
|
|
|
|
bar_fg_color = COLOR_WHITE;
|
|
|
|
} else {
|
|
|
|
bar_fg_color = COLOR_WHITE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_fg_color = COLOR_WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string_is_empty(user_settings->color_bar_accent)) {
|
|
|
|
if (strcmp(user_settings->color_bar_accent, "black") == 0) {
|
|
|
|
bar_accent_color = COLOR_BLACK;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "red") == 0) {
|
|
|
|
bar_accent_color = COLOR_RED;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "blue") == 0) {
|
|
|
|
bar_accent_color = COLOR_BLUE;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "cyan") == 0) {
|
|
|
|
bar_accent_color = COLOR_CYAN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "green") == 0) {
|
|
|
|
bar_accent_color = COLOR_GREEN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "yellow") == 0) {
|
|
|
|
bar_accent_color = COLOR_YELLOW;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "magenta") == 0) {
|
|
|
|
bar_accent_color = COLOR_MAGENTA;
|
|
|
|
} else if (strcmp(user_settings->color_bar_accent, "white") == 0) {
|
|
|
|
bar_accent_color = COLOR_WHITE;
|
|
|
|
} else {
|
|
|
|
bar_accent_color = COLOR_CYAN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_accent_color = COLOR_CYAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string_is_empty(user_settings->color_bar_notify)) {
|
|
|
|
if (strcmp(user_settings->color_bar_notify, "black") == 0) {
|
|
|
|
bar_notify_color = COLOR_BLACK;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "red") == 0) {
|
|
|
|
bar_notify_color = COLOR_RED;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "blue") == 0) {
|
|
|
|
bar_notify_color = COLOR_BLUE;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "cyan") == 0) {
|
|
|
|
bar_notify_color = COLOR_CYAN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "green") == 0) {
|
|
|
|
bar_notify_color = COLOR_GREEN;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "yellow") == 0) {
|
|
|
|
bar_notify_color = COLOR_YELLOW;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "magenta") == 0) {
|
|
|
|
bar_notify_color = COLOR_MAGENTA;
|
|
|
|
} else if (strcmp(user_settings->color_bar_notify, "white") == 0) {
|
|
|
|
bar_notify_color = COLOR_WHITE;
|
|
|
|
} else {
|
|
|
|
bar_notify_color = COLOR_YELLOW;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_notify_color = COLOR_YELLOW;
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:26:51 +01:00
|
|
|
init_pair(WHITE, COLOR_WHITE, COLOR_BLACK);
|
|
|
|
init_pair(GREEN, COLOR_GREEN, bg_color);
|
|
|
|
init_pair(CYAN, COLOR_CYAN, bg_color);
|
|
|
|
init_pair(RED, COLOR_RED, bg_color);
|
|
|
|
init_pair(BLUE, COLOR_BLUE, bg_color);
|
|
|
|
init_pair(YELLOW, COLOR_YELLOW, bg_color);
|
|
|
|
init_pair(MAGENTA, COLOR_MAGENTA, bg_color);
|
|
|
|
init_pair(BLACK, COLOR_BLACK, COLOR_BLACK);
|
2020-12-11 07:46:01 +01:00
|
|
|
init_pair(WHITE_BLUE, COLOR_WHITE, COLOR_BLUE);
|
2020-11-30 05:26:51 +01:00
|
|
|
init_pair(BLACK_WHITE, COLOR_BLACK, COLOR_WHITE);
|
2020-12-11 07:46:01 +01:00
|
|
|
init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
|
2020-12-19 21:40:00 +01:00
|
|
|
init_pair(WHITE_GREEN, COLOR_WHITE, COLOR_GREEN);
|
2020-12-05 21:39:33 +01:00
|
|
|
init_pair(BLACK_BG, COLOR_BLACK, bar_bg_color);
|
|
|
|
init_pair(PURPLE_BG, COLOR_MAGENTA, bar_bg_color);
|
|
|
|
init_pair(BAR_TEXT, bar_fg_color, bar_bg_color);
|
2021-07-27 20:15:36 +02:00
|
|
|
init_pair(BAR_SOLID, bar_bg_color, bar_bg_color);
|
2020-12-05 21:39:33 +01:00
|
|
|
init_pair(BAR_ACCENT, bar_accent_color, bar_bg_color);
|
|
|
|
init_pair(BAR_NOTIFY, bar_notify_color, bar_bg_color);
|
|
|
|
init_pair(STATUS_ONLINE, COLOR_GREEN, bar_bg_color);
|
|
|
|
init_pair(STATUS_AWAY, COLOR_YELLOW, bar_bg_color);
|
|
|
|
init_pair(STATUS_BUSY, COLOR_RED, bar_bg_color);
|
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);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (new_msgs == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-26 01:26:41 +02:00
|
|
|
|
|
|
|
new_msgs[i] = malloc(MAX_STR_SIZE);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (new_msgs[i] == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (init_messages.num <= 0) {
|
2014-08-26 01:26:41 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-26 01:26:41 +02:00
|
|
|
|
2021-11-20 15:49:04 +01:00
|
|
|
for (int i = 0; i < init_messages.num; ++i) {
|
2014-08-26 01:26:41 +02:00
|
|
|
free(init_messages.msgs[i]);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-26 01:26:41 +02:00
|
|
|
|
|
|
|
free(init_messages.msgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_init_messages(ToxWindow *toxwin)
|
|
|
|
{
|
2021-11-20 15:49:04 +01:00
|
|
|
for (int i = 0; i < init_messages.num; ++i) {
|
2014-08-26 01:26:41 +02:00
|
|
|
line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-26 01:26:41 +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 numfriends = tox_self_get_friend_list_size(m);
|
2014-03-05 14:01:12 +01:00
|
|
|
|
2021-11-20 15:49:04 +01:00
|
|
|
for (size_t i = 0; i < numfriends; ++i) {
|
2014-03-05 14:01:12 +01:00
|
|
|
friendlist_onFriendAdded(NULL, m, i, false);
|
2018-02-25 06:00:06 +01:00
|
|
|
}
|
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
|
|
|
|
2020-05-20 03:44:35 +02:00
|
|
|
static void load_conferences(Tox *m)
|
2018-02-25 06:00:06 +01:00
|
|
|
{
|
|
|
|
size_t num_chats = tox_conference_get_chatlist_size(m);
|
|
|
|
|
2020-05-20 03:44:35 +02:00
|
|
|
if (num_chats == 0) {
|
|
|
|
return;
|
2018-02-28 00:46:56 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
uint32_t *chatlist = malloc(num_chats * sizeof(uint32_t));
|
|
|
|
|
|
|
|
if (chatlist == NULL) {
|
|
|
|
fprintf(stderr, "malloc() failed in load_conferences()\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:44:35 +02:00
|
|
|
tox_conference_get_chatlist(m, chatlist);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_chats; ++i) {
|
2020-11-08 16:08:24 +01:00
|
|
|
uint32_t conferencenum = chatlist[i];
|
2018-02-25 06:00:06 +01:00
|
|
|
|
|
|
|
if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
|
2020-11-08 16:08:24 +01:00
|
|
|
tox_conference_delete(m, conferencenum, NULL);
|
2018-02-25 06:00:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_Conference_Get_Type err;
|
2020-11-08 16:08:24 +01:00
|
|
|
Tox_Conference_Type type = tox_conference_get_type(m, conferencenum, &err);
|
2018-02-25 06:00:06 +01:00
|
|
|
|
|
|
|
if (err != TOX_ERR_CONFERENCE_GET_TYPE_OK) {
|
2020-11-08 16:08:24 +01:00
|
|
|
tox_conference_delete(m, conferencenum, NULL);
|
2018-02-25 06:00:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-30 20:51:13 +02:00
|
|
|
Tox_Err_Conference_Title t_err;
|
2020-11-08 16:08:24 +01:00
|
|
|
size_t length = tox_conference_get_title_size(m, conferencenum, &t_err);
|
2019-06-30 20:51:13 +02:00
|
|
|
uint8_t title[MAX_STR_SIZE];
|
|
|
|
|
|
|
|
if (t_err != TOX_ERR_CONFERENCE_TITLE_OK || length >= sizeof(title)) {
|
|
|
|
length = 0;
|
|
|
|
} else {
|
2020-11-08 16:08:24 +01:00
|
|
|
tox_conference_get_title(m, conferencenum, title, &t_err);
|
2019-06-30 20:51:13 +02:00
|
|
|
|
|
|
|
if (t_err != TOX_ERR_CONFERENCE_TITLE_OK) {
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
title[length] = 0;
|
|
|
|
|
2020-05-07 02:00:00 +02:00
|
|
|
int win_idx = init_conference_win(m, conferencenum, type, (const char *) title, length);
|
|
|
|
|
|
|
|
if (win_idx == -1) {
|
2020-11-08 16:08:24 +01:00
|
|
|
tox_conference_delete(m, conferencenum, NULL);
|
2018-02-25 06:00:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-07 02:00:00 +02:00
|
|
|
|
|
|
|
if (type == TOX_CONFERENCE_TYPE_AV) {
|
|
|
|
line_info_add(get_window_ptr(win_idx), NULL, NULL, NULL, SYS_MSG, 0, 0,
|
|
|
|
#ifdef AUDIO
|
|
|
|
"Use \"/audio on\" to enable audio in this conference."
|
|
|
|
#else
|
|
|
|
"Audio support disabled by compile-time option."
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
2018-02-25 06:00:06 +01:00
|
|
|
}
|
2020-10-28 05:54:06 +01:00
|
|
|
|
|
|
|
free(chatlist);
|
2018-02-25 06:00:06 +01: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;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
|
2014-09-14 23:46:28 +02:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2014-09-15 10:13:12 +02:00
|
|
|
const char *p = fgets(buf, size, stdin);
|
2014-09-14 23:46:28 +02:00
|
|
|
|
|
|
|
/* re-enable terminal echo */
|
2014-09-15 10:13:12 +02:00
|
|
|
tcsetattr(fileno(stdin), TCSANOW, &oflags);
|
|
|
|
|
2020-11-03 06:59:28 +01:00
|
|
|
if (p == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = strlen(buf);
|
|
|
|
|
|
|
|
if (len <= 1) {
|
2014-09-15 10:13:12 +02:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-15 10:13:12 +02:00
|
|
|
|
|
|
|
/* eat overflowed stdin and return error */
|
|
|
|
if (buf[--len] != '\n') {
|
|
|
|
int ch;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:56:21 +02:00
|
|
|
while ((ch = getchar()) != '\n' && ch > 0) {
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2014-09-15 10:13:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
2014-09-14 23:46:28 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
/* Get the password from the eval command.
|
|
|
|
* return length of password on success, 0 on failure
|
|
|
|
*/
|
|
|
|
static int password_eval(char *buf, int size)
|
|
|
|
{
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
/* Run password_eval command */
|
|
|
|
FILE *f = popen(user_settings->password_eval, "r");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
if (f == NULL) {
|
|
|
|
fprintf(stderr, "Executing password_eval failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get output from command */
|
|
|
|
char *ret = fgets(buf, size, f);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
if (ret == NULL) {
|
|
|
|
fprintf(stderr, "Reading password from password_eval command failed\n");
|
|
|
|
pclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exit status */
|
|
|
|
int status = pclose(f);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
if (status != 0) {
|
|
|
|
fprintf(stderr, "password_eval command returned error %d\n", status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Removez whitespace or \n at end */
|
|
|
|
int i, len = strlen(buf);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
for (i = len - 1; i > 0 && isspace(buf[i]); i--) {
|
|
|
|
buf[i] = 0;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-15 19:57:00 +01:00
|
|
|
clear_screen();
|
2014-09-16 01:00:36 +02:00
|
|
|
printf("%s ", msg);
|
2017-06-01 22:46:12 +02:00
|
|
|
fflush(stdout);
|
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")
|
2018-07-18 17:33:16 +02:00
|
|
|
|| !strcasecmp(ch, "no\n") || !strcasecmp(ch, "q\n")) {
|
2014-09-14 23:46:28 +02:00
|
|
|
break;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
|
|
|
|
} while (fgets(ch, sizeof(ch), stdin));
|
|
|
|
|
2014-09-16 01:00:36 +02:00
|
|
|
printf("\n");
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ch[0] == 'q' || ch[0] == 'Q') {
|
2014-09-15 03:49:38 +02:00
|
|
|
exit(0);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-15 03:49:38 +02:00
|
|
|
|
|
|
|
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-07-04 07:19:16 +02:00
|
|
|
printf("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) {
|
2017-06-01 22:46:12 +02:00
|
|
|
fflush(stdout); // Flush all before user input
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (strcasecmp(user_password.pass, "q") == 0) {
|
2014-09-16 01:00:36 +02:00
|
|
|
exit(0);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-16 01:00:36 +02:00
|
|
|
|
|
|
|
if (string_is_empty(passconfirm) && (len < MIN_PASSWORD_LEN || len > MAX_PASSWORD_LEN)) {
|
2015-07-04 07:19:16 +02:00
|
|
|
printf("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-07-04 07:19:16 +02:00
|
|
|
printf("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-07-04 07:19:16 +02:00
|
|
|
printf("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-07-04 07:19:16 +02:00
|
|
|
queue_init_message("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;
|
|
|
|
}
|
|
|
|
|
2020-03-15 19:57:00 +01:00
|
|
|
clear_screen();
|
2014-09-14 23:46:28 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 03:38:32 +01:00
|
|
|
/* Store Tox profile data to path.
|
2015-04-01 03:56:11 +02:00
|
|
|
*
|
2015-11-05 23:46:00 +01:00
|
|
|
* Return 0 if stored successfully.
|
|
|
|
* Return -1 on error.
|
2013-08-15 12:11:48 +02:00
|
|
|
*/
|
2015-11-08 03:38:32 +01:00
|
|
|
#define TEMP_PROFILE_EXT ".tmp"
|
2014-09-14 23:46:28 +02:00
|
|
|
int store_data(Tox *m, const char *path)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2016-05-03 20:13:16 +02:00
|
|
|
if (path == NULL) {
|
2014-07-31 18:48:49 +02:00
|
|
|
return -1;
|
2016-05-03 20:13:16 +02:00
|
|
|
}
|
2013-09-09 06:56:47 +02:00
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
size_t temp_buf_size = strlen(path) + strlen(TEMP_PROFILE_EXT) + 1;
|
|
|
|
char *temp_path = malloc(temp_buf_size);
|
|
|
|
|
|
|
|
if (temp_path == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(temp_path, temp_buf_size, "%s%s", path, TEMP_PROFILE_EXT);
|
2015-11-08 03:38:32 +01:00
|
|
|
|
|
|
|
FILE *fp = fopen(temp_path, "wb");
|
2015-04-01 03:56:11 +02:00
|
|
|
|
2016-05-03 20:13:16 +02:00
|
|
|
if (fp == NULL) {
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2015-04-01 03:56:11 +02:00
|
|
|
return -1;
|
2016-05-03 20:13:16 +02:00
|
|
|
}
|
2015-04-01 03:56:11 +02:00
|
|
|
|
|
|
|
size_t data_len = tox_get_savedata_size(m);
|
2016-05-03 20:13:16 +02:00
|
|
|
char *data = malloc(data_len * sizeof(char));
|
|
|
|
|
|
|
|
if (data == NULL) {
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
2016-05-03 20:13:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
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;
|
2016-05-03 20:13:16 +02:00
|
|
|
char *enc_data = malloc(enc_len * sizeof(char));
|
|
|
|
|
|
|
|
if (enc_data == NULL) {
|
2016-09-20 06:00:09 +02:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2016-05-03 20:13:16 +02:00
|
|
|
free(data);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2020-03-07 18:59:02 +01:00
|
|
|
Tox_Err_Encryption err;
|
2015-04-01 03:56:11 +02:00
|
|
|
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-07-04 07:19:16 +02:00
|
|
|
fprintf(stderr, "tox_pass_encrypt() failed with error %d\n", err);
|
2015-04-01 03:56:11 +02:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2016-05-03 20:13:16 +02:00
|
|
|
free(data);
|
|
|
|
free(enc_data);
|
2015-04-01 03:56:11 +02:00
|
|
|
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) {
|
2015-11-08 03:38:32 +01:00
|
|
|
fprintf(stderr, "Failed to write profile data.\n");
|
2015-04-01 03:56:11 +02:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2016-05-03 20:13:16 +02:00
|
|
|
free(data);
|
|
|
|
free(enc_data);
|
2015-04-01 03:56:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-30 03:36:54 +02:00
|
|
|
|
|
|
|
free(enc_data);
|
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) {
|
2015-11-08 03:38:32 +01:00
|
|
|
fprintf(stderr, "Failed to write profile data.\n");
|
2015-04-01 03:56:11 +02:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2016-05-03 20:13:16 +02:00
|
|
|
free(data);
|
2015-04-01 03:56:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2016-05-03 20:13:16 +02:00
|
|
|
free(data);
|
2015-11-05 23:46:00 +01:00
|
|
|
|
2016-05-03 20:13:16 +02:00
|
|
|
if (rename(temp_path, path) != 0) {
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
2015-11-05 23:46:00 +01:00
|
|
|
return -1;
|
2016-05-03 20:13:16 +02:00
|
|
|
}
|
2015-11-05 23:46:00 +01:00
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
free(temp_path);
|
|
|
|
|
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
|
|
|
{
|
2018-07-18 17:41:17 +02:00
|
|
|
tox_callback_self_connection_status(m, on_self_connection_status);
|
|
|
|
tox_callback_friend_connection_status(m, on_friend_connection_status);
|
|
|
|
tox_callback_friend_typing(m, on_friend_typing);
|
|
|
|
tox_callback_friend_request(m, on_friend_request);
|
|
|
|
tox_callback_friend_message(m, on_friend_message);
|
|
|
|
tox_callback_friend_name(m, on_friend_name);
|
|
|
|
tox_callback_friend_status(m, on_friend_status);
|
|
|
|
tox_callback_friend_status_message(m, on_friend_status_message);
|
|
|
|
tox_callback_friend_read_receipt(m, on_friend_read_receipt);
|
|
|
|
tox_callback_conference_invite(m, on_conference_invite);
|
|
|
|
tox_callback_conference_message(m, on_conference_message);
|
|
|
|
tox_callback_conference_peer_list_changed(m, on_conference_peer_list_changed);
|
|
|
|
tox_callback_conference_peer_name(m, on_conference_peer_name);
|
|
|
|
tox_callback_conference_title(m, on_conference_title);
|
2016-12-18 06:01:51 +01:00
|
|
|
tox_callback_file_recv(m, on_file_recv);
|
|
|
|
tox_callback_file_chunk_request(m, on_file_chunk_request);
|
2018-07-18 17:41:17 +02:00
|
|
|
tox_callback_file_recv_control(m, on_file_recv_control);
|
2016-12-18 06:01:51 +01:00
|
|
|
tox_callback_file_recv_chunk(m, on_file_recv_chunk);
|
2021-01-17 19:29:13 +01:00
|
|
|
tox_callback_friend_lossless_packet(m, on_lossless_custom_packet);
|
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);
|
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_set_ipv6_enabled(tox_opts, !arg_opts.use_ipv4);
|
|
|
|
tox_options_set_udp_enabled(tox_opts, !arg_opts.force_tcp);
|
|
|
|
tox_options_set_proxy_type(tox_opts, arg_opts.proxy_type);
|
|
|
|
tox_options_set_tcp_port(tox_opts, arg_opts.tcp_port);
|
2020-08-11 03:56:45 +02:00
|
|
|
tox_options_set_local_discovery_enabled(tox_opts, !arg_opts.disable_local_discovery);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2020-05-04 22:15:24 +02:00
|
|
|
if (arg_opts.logging) {
|
|
|
|
tox_options_set_log_callback(tox_opts, cb_toxcore_logger);
|
2020-05-17 05:12:54 +02:00
|
|
|
|
|
|
|
if (arg_opts.log_fp != NULL) {
|
|
|
|
tox_options_set_log_user_data(tox_opts, arg_opts.log_fp);
|
|
|
|
}
|
2020-05-04 22:15:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!tox_options_get_ipv6_enabled(tox_opts)) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Forcing IPv4 connection");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (tox_options_get_tcp_port(tox_opts)) {
|
2018-01-20 20:49:36 +01:00
|
|
|
queue_init_message("TCP relaying enabled on port %d", tox_options_get_tcp_port(tox_opts));
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-09 03:01:32 +02:00
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) {
|
|
|
|
tox_options_set_proxy_port(tox_opts, arg_opts.proxy_port);
|
|
|
|
tox_options_set_proxy_host(tox_opts, arg_opts.proxy_address);
|
|
|
|
const char *ps = tox_options_get_proxy_type(tox_opts) == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP";
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2017-09-01 23:36:17 +02:00
|
|
|
char tmp[sizeof(arg_opts.proxy_address) + MAX_STR_SIZE];
|
2015-07-04 07:19:16 +02:00
|
|
|
snprintf(tmp, sizeof(tmp), "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);
|
|
|
|
}
|
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
if (!tox_options_get_udp_enabled(tox_opts)) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("UDP disabled");
|
2018-01-20 20:49:36 +01:00
|
|
|
} else if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) {
|
2015-07-04 07:19:16 +02:00
|
|
|
const char *msg = "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-07-04 07:19:16 +02:00
|
|
|
msg = "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.
|
|
|
|
*/
|
2018-10-10 05:06:57 +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);
|
2016-09-20 06:00:09 +02:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_FILEOP);
|
2014-09-24 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
char *data = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
if (data == NULL) {
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_MEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(data, len, 1, fp) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
free(data);
|
2016-09-20 06:00:09 +02:00
|
|
|
exit_toxic_err("failed in load_tox", 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);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(data);
|
2016-09-20 06:00:09 +02:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_ENCRYPT);
|
2015-04-13 06:32:35 +02:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (arg_opts.unencrypt_data && is_encrypted) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Data file '%s' has been unencrypted", data_path);
|
2018-07-18 17:33:16 +02:00
|
|
|
} else if (arg_opts.unencrypt_data) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Warning: passed --unencrypt-data option with unencrypted data file '%s'", data_path);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
|
2014-09-15 22:17:58 +02:00
|
|
|
if (is_encrypted) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!arg_opts.unencrypt_data) {
|
2014-09-19 05:50:51 +02:00
|
|
|
user_password.data_is_encrypted = true;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
size_t pwlen = 0;
|
2016-01-19 22:45:52 +01:00
|
|
|
int pweval = user_settings->password_eval[0];
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
if (!pweval) {
|
2020-03-15 19:57:00 +01:00
|
|
|
clear_screen();
|
2016-01-19 22:45:52 +01:00
|
|
|
printf("Enter password (q to quit) ");
|
|
|
|
}
|
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;
|
2020-10-28 05:54:06 +01:00
|
|
|
char *plain = malloc(plain_len); // must be freed after tox_new()
|
|
|
|
|
|
|
|
if (plain == NULL) {
|
|
|
|
fclose(fp);
|
2020-10-30 01:28:09 +01:00
|
|
|
free(data);
|
2020-10-28 05:54:06 +01:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_MEMORY);
|
|
|
|
}
|
2015-04-01 03:56:11 +02:00
|
|
|
|
2014-09-15 03:49:38 +02:00
|
|
|
while (true) {
|
2017-06-01 22:46:12 +02:00
|
|
|
fflush(stdout); // Flush before prompts so the user sees the question/message
|
|
|
|
|
2016-01-19 22:45:52 +01:00
|
|
|
if (pweval) {
|
|
|
|
pwlen = password_eval(user_password.pass, sizeof(user_password.pass));
|
|
|
|
} else {
|
|
|
|
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2014-09-14 23:46:28 +02:00
|
|
|
user_password.len = pwlen;
|
|
|
|
|
2015-04-13 06:32:35 +02:00
|
|
|
if (strcasecmp(user_password.pass, "q") == 0) {
|
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(plain);
|
2020-10-30 01:28:09 +01:00
|
|
|
free(data);
|
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) {
|
2020-03-15 19:57:00 +01:00
|
|
|
clear_screen();
|
2014-09-16 01:00:36 +02:00
|
|
|
sleep(1);
|
2015-07-04 07:19:16 +02:00
|
|
|
printf("Invalid password. Try again. ");
|
2016-01-19 22:45:52 +01:00
|
|
|
pweval = 0;
|
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
|
|
|
|
2020-03-07 18:59:02 +01:00
|
|
|
Tox_Err_Decryption pwerr;
|
2015-04-01 03:56:11 +02:00
|
|
|
tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) user_password.pass, pwlen,
|
|
|
|
(uint8_t *) plain, &pwerr);
|
|
|
|
|
|
|
|
if (pwerr == TOX_ERR_DECRYPTION_OK) {
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE);
|
|
|
|
tox_options_set_savedata_data(tox_opts, (uint8_t *) plain, plain_len);
|
2015-05-24 23:36:13 +02:00
|
|
|
|
|
|
|
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);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(data);
|
|
|
|
free(plain);
|
2015-04-13 06:32:35 +02:00
|
|
|
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) {
|
2020-03-15 19:57:00 +01:00
|
|
|
clear_screen();
|
2014-09-15 10:13:12 +02:00
|
|
|
sleep(1);
|
2015-07-04 07:19:16 +02:00
|
|
|
printf("Invalid password. Try again. ");
|
2016-01-19 22:45:52 +01:00
|
|
|
pweval = 0;
|
2015-03-26 03:56:45 +01:00
|
|
|
} else {
|
2015-04-13 06:32:35 +02:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(data);
|
|
|
|
free(plain);
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("tox_pass_decrypt() failed", pwerr);
|
2014-09-14 23:46:28 +02:00
|
|
|
}
|
2014-09-15 03:49:38 +02:00
|
|
|
}
|
2020-10-28 05:54:06 +01:00
|
|
|
|
|
|
|
free(plain);
|
2015-04-01 03:56:11 +02:00
|
|
|
} else { /* data is not encrypted */
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE);
|
|
|
|
tox_options_set_savedata_data(tox_opts, (uint8_t *) data, len);
|
2015-05-24 23:36:13 +02:00
|
|
|
|
|
|
|
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);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(data);
|
2015-04-13 06:32:35 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-14 23:46:28 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
fclose(fp);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(data);
|
2015-04-01 03:56:11 +02:00
|
|
|
} else { /* Data file does not/should not exist */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (file_exists(data_path)) {
|
2016-09-20 06:00:09 +02:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_FILEOP);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_NONE);
|
2015-05-24 23:36:13 +02:00
|
|
|
|
|
|
|
m = tox_new(tox_opts, new_err);
|
2014-10-02 08:07:54 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (m == NULL) {
|
2015-04-13 06:32:35 +02:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (store_data(m, data_path) == -1) {
|
2016-09-20 06:00:09 +02:00
|
|
|
exit_toxic_err("failed in load_tox", FATALERR_FILEOP);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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
|
|
|
{
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_Options_New options_new_err;
|
2018-01-20 20:49:36 +01:00
|
|
|
struct Tox_Options *tox_opts = tox_options_new(&options_new_err);
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!tox_opts) {
|
2018-01-20 20:49:36 +01:00
|
|
|
exit_toxic_err("tox_options_new returned fatal error", options_new_err);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
init_tox_options(tox_opts);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_New new_err;
|
2018-01-20 20:49:36 +01:00
|
|
|
Tox *m = load_tox(data_path, tox_opts, &new_err);
|
2015-04-13 06:32:35 +02:00
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
if (new_err == TOX_ERR_NEW_PORT_ALLOC && tox_options_get_ipv6_enabled(tox_opts)) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Falling back to ipv4");
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_set_ipv6_enabled(tox_opts, false);
|
|
|
|
m = load_tox(data_path, tox_opts, &new_err);
|
2015-04-13 06:32:35 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!m) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("tox_new returned fatal error", new_err);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-14 23:18:09 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (new_err != TOX_ERR_NEW_OK) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("tox_new returned non-fatal error %d", new_err);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (tox_self_get_name_size(m) == 0) {
|
2015-07-04 07:19:16 +02:00
|
|
|
tox_self_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2018-01-20 20:49:36 +01:00
|
|
|
tox_options_free(tox_opts);
|
2015-03-26 03:56:45 +01:00
|
|
|
return m;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 20:26:23 +02:00
|
|
|
static void do_toxic(Tox *m)
|
2015-03-30 01:05:24 +02:00
|
|
|
{
|
2015-08-28 03:29:34 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
|
|
|
|
if (arg_opts.no_connect) {
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-03-30 01:05:24 +02:00
|
|
|
return;
|
2015-08-28 03:29:34 +02:00
|
|
|
}
|
2015-03-30 01:05:24 +02:00
|
|
|
|
2016-12-18 06:01:51 +01:00
|
|
|
tox_iterate(m, NULL);
|
2016-09-17 20:26:23 +02:00
|
|
|
do_tox_connection(m);
|
2020-11-11 22:28:50 +01:00
|
|
|
|
2014-07-09 06:05:13 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2014-03-13 11:06:53 +01:00
|
|
|
}
|
|
|
|
|
2021-11-19 23:54:35 +01:00
|
|
|
/* Set interface refresh flag. This should be called whenever the interface changes.
|
|
|
|
*
|
|
|
|
* This function is not thread safe.
|
|
|
|
*/
|
|
|
|
void flag_interface_refresh(void)
|
|
|
|
{
|
|
|
|
Winthread.flag_refresh = 1;
|
|
|
|
Winthread.last_refresh_flag = get_unix_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* How long we wait to idle interface refreshing after last flag set. Should be no less than 2. */
|
|
|
|
#define ACTIVE_WIN_REFRESH_TIMEOUT 2
|
|
|
|
|
|
|
|
static void poll_interface_refresh_flag(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
|
|
|
|
bool flag = Winthread.flag_refresh;
|
|
|
|
time_t t = Winthread.last_refresh_flag;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
|
|
|
if (flag == 1 && timed_out(t, ACTIVE_WIN_REFRESH_TIMEOUT)) {
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
Winthread.flag_refresh = 0;
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* How often we refresh windows that aren't focused */
|
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;
|
2015-08-28 03:29:34 +02:00
|
|
|
|
2014-07-06 22:18:34 +02:00
|
|
|
uint8_t draw_count = 0;
|
2021-11-19 23:54:35 +01:00
|
|
|
|
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-07-06 22:18:34 +02:00
|
|
|
draw_count++;
|
2021-11-19 23:54:35 +01:00
|
|
|
draw_active_window(m);
|
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);
|
|
|
|
}
|
2021-11-19 23:54:35 +01:00
|
|
|
|
|
|
|
poll_interface_refresh_flag();
|
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
|
|
|
|
2020-11-11 22:28:50 +01:00
|
|
|
for (size_t i = 2; i < MAX_WINDOWS_NUM; ++i) {
|
2014-09-06 19:18:42 +02:00
|
|
|
ToxWindow *toxwin = get_window_ptr(i);
|
|
|
|
|
2021-11-20 04:54:35 +01:00
|
|
|
if ((toxwin != NULL) && (toxwin->type == WINDOW_TYPE_CHAT)) {
|
|
|
|
cqueue_check_unread(toxwin);
|
|
|
|
|
|
|
|
if (get_friend_connection_status(toxwin->num) != TOX_CONNECTION_NONE) {
|
|
|
|
cqueue_try_send(toxwin, m);
|
|
|
|
}
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-09-06 19:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2020-11-11 22:28:50 +01:00
|
|
|
sleep_thread(750000L); // 0.75 seconds
|
2014-09-06 19:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 05:47:22 +01:00
|
|
|
#ifdef AUDIO
|
2015-08-10 13:14:38 +02:00
|
|
|
void *thread_av(void *data)
|
2014-11-29 05:47:22 +01:00
|
|
|
{
|
2015-06-12 17:50:26 +02:00
|
|
|
ToxAV *av = (ToxAV *) data;
|
2015-11-05 22:15:59 +01:00
|
|
|
|
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
|
|
|
|
2020-11-03 18:17:48 +01:00
|
|
|
long int sleep_duration = toxav_iteration_interval(av) * 1000;
|
|
|
|
sleep_thread(sleep_duration);
|
2014-11-29 05:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:39:04 +02:00
|
|
|
#endif /* AUDIO */
|
2014-11-29 05:47:22 +01:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
static void print_usage(void)
|
2014-06-21 01:58:00 +02:00
|
|
|
{
|
2015-07-04 07:19:16 +02:00
|
|
|
fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n");
|
|
|
|
fprintf(stderr, " -4, --ipv4 Force IPv4 connection\n");
|
|
|
|
fprintf(stderr, " -b, --debug Enable stderr for debugging\n");
|
|
|
|
fprintf(stderr, " -c, --config Use specified config file\n");
|
|
|
|
fprintf(stderr, " -d, --default-locale Use default POSIX locale\n");
|
|
|
|
fprintf(stderr, " -e, --encrypt-data Encrypt an unencrypted data file\n");
|
|
|
|
fprintf(stderr, " -f, --file Use specified data file\n");
|
|
|
|
fprintf(stderr, " -h, --help Show this message and exit\n");
|
2020-05-17 05:12:54 +02:00
|
|
|
fprintf(stderr, " -l, --logging Enable toxcore logging: Requires [log_path | stderr]\n");
|
2020-08-11 03:56:45 +02:00
|
|
|
fprintf(stderr, " -L, --no-lan Disable local discovery\n");
|
2015-07-04 07:19:16 +02:00
|
|
|
fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n");
|
|
|
|
fprintf(stderr, " -o, --noconnect Do not connect to the DHT network\n");
|
|
|
|
fprintf(stderr, " -p, --SOCKS5-proxy Use SOCKS5 proxy: Requires [IP] [port]\n");
|
|
|
|
fprintf(stderr, " -P, --HTTP-proxy Use HTTP proxy: Requires [IP] [port]\n");
|
2015-10-28 22:14:17 +01:00
|
|
|
fprintf(stderr, " -r, --namelist Use specified name lookup server list\n");
|
2022-01-30 19:18:14 +01:00
|
|
|
fprintf(stderr, " -s, --netstats Dump network statistic to log on exit: Requires [path]\n");
|
2015-07-09 03:01:32 +02:00
|
|
|
fprintf(stderr, " -t, --force-tcp Force toxic to use a TCP connection (use with proxies)\n");
|
|
|
|
fprintf(stderr, " -T, --tcp-server Act as a TCP relay server: Requires [port]\n");
|
2015-07-04 07:19:16 +02:00
|
|
|
fprintf(stderr, " -u, --unencrypt-data Unencrypt an encrypted data file\n");
|
2017-01-26 04:13:59 +01:00
|
|
|
fprintf(stderr, " -v, --version Print the version\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_version(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Toxic version %s\n", TOXICVER);
|
|
|
|
fprintf(stderr, "Toxcore version %d.%d.%d\n", tox_version_major(), tox_version_minor(), tox_version_patch());
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_default_opts(void)
|
|
|
|
{
|
2020-11-11 03:25:27 +01:00
|
|
|
arg_opts = (struct arg_opts) {
|
|
|
|
0
|
|
|
|
};
|
2014-09-19 05:50:51 +02:00
|
|
|
|
|
|
|
/* 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[] = {
|
|
|
|
{"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'},
|
2020-08-11 03:56:45 +02:00
|
|
|
{"file", required_argument, 0, 'f'},
|
2020-05-17 05:12:54 +02:00
|
|
|
{"logging", required_argument, 0, 'l'},
|
2020-08-11 03:56:45 +02:00
|
|
|
{"no-lan", no_argument, 0, 'L'},
|
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'},
|
2015-10-28 22:14:17 +01:00
|
|
|
{"namelist", required_argument, 0, 'r'},
|
2022-01-30 19:18:14 +01:00
|
|
|
{"netstats", required_argument, 0, 's'},
|
2014-08-15 00:45:41 +02:00
|
|
|
{"force-tcp", no_argument, 0, 't'},
|
2015-07-09 03:01:32 +02:00
|
|
|
{"tcp-server", required_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'},
|
2017-01-26 04:13:59 +01:00
|
|
|
{"version", no_argument, 0, 'v'},
|
2014-08-27 20:30:06 +02:00
|
|
|
{NULL, no_argument, NULL, 0},
|
2014-06-22 02:18:23 +02:00
|
|
|
};
|
|
|
|
|
2022-01-30 19:18:14 +01:00
|
|
|
const char *opts_str = "4bdehLotuxvc:f:l:n:r:s:p:P:T:";
|
2021-11-03 21:23:38 +01:00
|
|
|
int opt = 0;
|
|
|
|
int indexptr = 0;
|
2014-06-22 02:18:23 +02:00
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
|
|
|
|
switch (opt) {
|
2021-11-03 21:23:38 +01:00
|
|
|
case '4': {
|
2014-08-13 00:59:27 +02:00
|
|
|
arg_opts.use_ipv4 = 1;
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-13 00:59:27 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'b': {
|
2014-08-16 01:04:39 +02:00
|
|
|
arg_opts.debug = 1;
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("stderr enabled");
|
2014-08-16 01:04:39 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-16 01:04:39 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'c': {
|
2020-11-03 03:15:32 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:59:27 +02:00
|
|
|
snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!file_exists(arg_opts.config_path)) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Config file not found");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-27 05:21:32 +02:00
|
|
|
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-13 00:59:27 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'd': {
|
2014-08-13 00:59:27 +02:00
|
|
|
arg_opts.default_locale = 1;
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Using default POSIX locale");
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-13 00:59:27 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'e': {
|
2014-09-15 22:17:58 +02:00
|
|
|
arg_opts.encrypt_data = 1;
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-09-15 22:17:58 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'f': {
|
2020-11-03 00:08:54 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:04:29 +02:00
|
|
|
arg_opts.use_custom_data = 1;
|
2015-08-27 09:38:08 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (DATA_FILE) {
|
2015-08-27 09:38:08 +02:00
|
|
|
free(DATA_FILE);
|
2020-11-03 00:08:54 +01:00
|
|
|
DATA_FILE = NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-27 09:38:08 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (BLOCK_FILE) {
|
2015-08-27 09:38:08 +02:00
|
|
|
free(BLOCK_FILE);
|
2020-11-03 00:08:54 +01:00
|
|
|
BLOCK_FILE = NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-27 09:38:08 +02:00
|
|
|
|
|
|
|
DATA_FILE = malloc(strlen(optarg) + 1);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (DATA_FILE == NULL) {
|
2015-08-27 09:38:08 +02:00
|
|
|
exit_toxic_err("failed in parse_args", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-27 09:38:08 +02:00
|
|
|
|
2020-11-03 00:08:54 +01:00
|
|
|
strcpy(DATA_FILE, 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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (BLOCK_FILE == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in parse_args", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Using '%s' data file", DATA_FILE);
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'l': {
|
2020-05-17 05:12:54 +02:00
|
|
|
if (optarg) {
|
|
|
|
arg_opts.logging = true;
|
|
|
|
|
|
|
|
if (strcmp(optarg, "stderr") != 0) {
|
|
|
|
arg_opts.log_fp = fopen(optarg, "w");
|
|
|
|
|
|
|
|
if (arg_opts.log_fp != NULL) {
|
|
|
|
queue_init_message("Toxcore logging enabled to %s", optarg);
|
|
|
|
} else {
|
|
|
|
arg_opts.debug = true;
|
|
|
|
queue_init_message("Failed to open log file %s. Falling back to stderr.", optarg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arg_opts.debug = true;
|
|
|
|
queue_init_message("Toxcore logging enabled to stderr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:15:24 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2020-05-04 22:15:24 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'L': {
|
2020-08-11 03:56:45 +02:00
|
|
|
arg_opts.disable_local_discovery = 1;
|
|
|
|
queue_init_message("Local discovery disabled");
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2020-08-11 03:56:45 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'n': {
|
2020-11-03 03:15:32 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'o': {
|
2014-08-13 00:59:27 +02:00
|
|
|
arg_opts.no_connect = 1;
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("DHT disabled");
|
2014-08-13 00:59:27 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-13 00:59:27 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'p': {
|
2015-03-26 03:56:45 +01:00
|
|
|
arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2015-09-03 01:41:21 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
// Intentional fallthrough
|
2014-12-27 19:12:16 +01:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'P': {
|
2020-11-03 03:15:32 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
2021-11-03 21:23:38 +01:00
|
|
|
arg_opts.proxy_type = TOX_PROXY_TYPE_NONE;
|
2020-11-03 03:15:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
if (arg_opts.proxy_type == TOX_PROXY_TYPE_NONE) {
|
|
|
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (++optind > argc || argv[optind - 1][0] == '-') {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("Proxy error", FATALERR_PROXY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-15 21:13:35 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
long int port = strtol(argv[optind - 1], NULL, 10);
|
2015-09-03 01:41:21 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (port <= 0 || port > MAX_PORT_RANGE) {
|
2015-09-03 01:41:21 +02:00
|
|
|
exit_toxic_err("Proxy error", FATALERR_PROXY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-09-03 01:41:21 +02:00
|
|
|
|
|
|
|
arg_opts.proxy_port = port;
|
2014-08-15 02:31:52 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-15 02:31:52 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'r': {
|
2020-11-03 03:15:32 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:14:17 +01:00
|
|
|
snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", optarg);
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!file_exists(arg_opts.nameserver_path)) {
|
2015-10-28 22:14:17 +01:00
|
|
|
queue_init_message("nameserver list not found");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-08-26 08:28:05 +02:00
|
|
|
|
2014-08-13 01:13:57 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-13 01:13:57 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 't': {
|
2014-08-15 00:45:41 +02:00
|
|
|
arg_opts.force_tcp = 1;
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-08-15 00:45:41 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'T': {
|
2020-11-03 03:15:32 +01:00
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
long int port = strtol(optarg, NULL, 10);
|
2015-09-03 01:41:21 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (port <= 0 || port > MAX_PORT_RANGE) {
|
2020-11-03 03:15:32 +01:00
|
|
|
port = MAX_PORT_RANGE;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-09-03 01:41:21 +02:00
|
|
|
|
|
|
|
arg_opts.tcp_port = port;
|
2015-07-09 03:01:32 +02:00
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2015-07-09 03:01:32 +02:00
|
|
|
|
2022-01-31 18:45:35 +01:00
|
|
|
case 's': {
|
|
|
|
if (optarg == NULL) {
|
|
|
|
queue_init_message("Invalid argument for option: %d", opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_opts.netprof_fp = fopen(optarg, "w");
|
|
|
|
|
|
|
|
if (arg_opts.netprof_fp != NULL) {
|
|
|
|
queue_init_message("Network profile logging enabled. Logging to file: '%s'", optarg);
|
|
|
|
arg_opts.netprof_log_dump = true;
|
|
|
|
arg_opts.netprof_start_time = time(NULL);
|
|
|
|
} else {
|
|
|
|
queue_init_message("Failed to open file '%s' for network profile logging.", optarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'u': {
|
2014-09-19 05:50:51 +02:00
|
|
|
arg_opts.unencrypt_data = 1;
|
|
|
|
break;
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2021-11-03 21:23:38 +01:00
|
|
|
case 'v': {
|
2017-01-26 04:13:59 +01:00
|
|
|
print_version();
|
|
|
|
exit(EXIT_SUCCESS);
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2017-06-01 22:46:12 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
case 'h':
|
2020-11-03 03:15:32 +01:00
|
|
|
|
|
|
|
// Intentional fallthrough
|
2021-11-03 21:23:38 +01:00
|
|
|
default: {
|
2014-06-22 02:18:23 +02:00
|
|
|
print_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
2021-11-03 21:23:38 +01:00
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
}
|
2014-06-21 01:58:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 05:09:11 +02:00
|
|
|
/* Initializes the default config directory and data files used by toxic.
|
|
|
|
*
|
|
|
|
* Exits the process with an error on failure.
|
|
|
|
*/
|
|
|
|
static void init_default_data_files(void)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (arg_opts.use_custom_data) {
|
2015-10-15 05:09:11 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-10-15 05:09:11 +02:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
char *user_config_dir = get_user_config_dir();
|
2015-10-15 05:09:11 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (user_config_dir == NULL) {
|
2015-10-15 05:09:11 +02:00
|
|
|
exit_toxic_err("failed in init_default_data_files()", FATALERR_FILEOP);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-10-15 05:09:11 +02:00
|
|
|
|
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
|
|
|
|
2015-10-15 05:09:11 +02:00
|
|
|
if (config_err == -1) {
|
2014-09-14 23:46:28 +02:00
|
|
|
DATA_FILE = strdup(DATANAME);
|
|
|
|
BLOCK_FILE = strdup(BLOCKNAME);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (DATA_FILE == NULL || BLOCK_FILE == NULL) {
|
2015-10-15 05:09:11 +02:00
|
|
|
exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (DATA_FILE == NULL || BLOCK_FILE == NULL) {
|
2015-10-15 05:09:11 +02:00
|
|
|
exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-10-15 05:09:11 +02:00
|
|
|
int main(int argc, char **argv)
|
2014-07-31 18:48:49 +02:00
|
|
|
{
|
2020-05-04 22:15:24 +02:00
|
|
|
/* Make sure all written files are read/writeable only by the current user. */
|
|
|
|
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
|
2020-12-11 07:46:01 +01:00
|
|
|
srand(time(NULL)); // We use rand() for trivial/non-security related things
|
|
|
|
|
2014-07-31 18:48:49 +02:00
|
|
|
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 */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!arg_opts.debug) {
|
2020-03-15 19:57:00 +01:00
|
|
|
if (!freopen("/dev/null", "w", stderr)) {
|
|
|
|
fprintf(stderr, "Warning: failed to enable stderr\n");
|
|
|
|
}
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-26 03:56:45 +01:00
|
|
|
|
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-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Warning: Using --unencrypt-data and --encrypt-data simultaneously has no effect");
|
2014-09-19 06:29:30 +02:00
|
|
|
}
|
2014-09-19 05:50:51 +02:00
|
|
|
|
2015-10-15 05:09:11 +02:00
|
|
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!datafile_exists && !arg_opts.unencrypt_data) {
|
2015-07-04 07:19:16 +02:00
|
|
|
first_time_encrypt("Creating new data file. Would you like to encrypt it? Y/n (q to quit)");
|
2018-07-18 17:33:16 +02:00
|
|
|
} else if (arg_opts.encrypt_data) {
|
2015-07-04 07:19:16 +02:00
|
|
|
first_time_encrypt("Encrypt existing data file? Y/n (q to quit)");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (user_settings == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in main", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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;
|
2015-10-28 22:14:17 +01:00
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
if (settings_load(user_settings, p) == -1) {
|
2016-09-25 03:07:04 +02:00
|
|
|
queue_init_message("Failed to load user settings");
|
2016-09-17 20:26:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 06:00:09 +02:00
|
|
|
int curl_init = curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
int nameserver_ret = name_lookup_init(curl_init);
|
2015-10-28 22:14:17 +01:00
|
|
|
|
2016-09-17 20:26:23 +02:00
|
|
|
if (nameserver_ret == -1) {
|
2015-10-28 22:14:17 +01:00
|
|
|
queue_init_message("curl failed to initialize; name lookup service is disabled.");
|
2016-09-17 20:26:23 +02:00
|
|
|
} else if (nameserver_ret == -2) {
|
2015-10-28 22:14:17 +01:00
|
|
|
queue_init_message("Name lookup server list could not be found.");
|
2016-09-17 20:26:23 +02:00
|
|
|
} else if (nameserver_ret == -3) {
|
2015-10-28 22:14:17 +01:00
|
|
|
queue_init_message("Name lookup server list does not contain any valid entries.");
|
2016-09-17 20:26:23 +02:00
|
|
|
}
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-10-01 23:28:34 +02:00
|
|
|
#ifdef X11
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2020-11-25 01:31:51 +01:00
|
|
|
if (init_x11focus() == -1) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("X failed to initialize");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-10-08 19:39:04 +02:00
|
|
|
#endif /* X11 */
|
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
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (arg_opts.encrypt_data && !datafile_exists) {
|
2015-03-26 03:56:45 +01:00
|
|
|
arg_opts.encrypt_data = 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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);
|
2018-10-04 18:43:43 +02:00
|
|
|
prompt_init_statusbar(prompt, m, !datafile_exists);
|
2020-05-20 03:44:35 +02:00
|
|
|
load_conferences(m);
|
2020-11-30 05:26:51 +01:00
|
|
|
set_active_window_index(0);
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_mutex_init(&Winthread.lock, NULL) != 0) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in main", FATALERR_MUTEX_INIT);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-11 23:38:07 +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);
|
2015-11-05 22:15:59 +01:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
#ifdef VIDEO
|
2015-08-11 13:11:09 +02:00
|
|
|
init_video(prompt, m);
|
2015-08-10 13:14:38 +02:00
|
|
|
|
|
|
|
#endif /* VIDEO */
|
2014-07-26 01:55:16 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* AV thread */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_create(&av_thread.tid, NULL, thread_av, (void *) av) != 0) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-11-29 05:47:22 +01:00
|
|
|
|
2020-04-04 02:00:00 +02:00
|
|
|
set_al_device(input, user_settings->audio_in_dev);
|
|
|
|
set_al_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
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2020-05-07 02:00:00 +02:00
|
|
|
if (init_devices() == de_InternalError) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Failed to init audio devices");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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
|
|
|
|
2020-05-07 02:00:00 +02:00
|
|
|
/* thread for ncurses stuff */
|
|
|
|
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) {
|
|
|
|
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread for message queue */
|
|
|
|
if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) m) != 0) {
|
|
|
|
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
|
|
|
|
}
|
|
|
|
|
2017-05-16 02:17:23 +02:00
|
|
|
#ifdef PYTHON
|
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
init_python(m);
|
2017-05-17 02:31:23 +02:00
|
|
|
invoke_autoruns(prompt->chatwin->history, prompt);
|
2017-05-16 02:17:23 +02:00
|
|
|
|
|
|
|
#endif /* PYTHON */
|
|
|
|
|
2018-11-06 04:40:51 +01:00
|
|
|
init_notify(60, user_settings->notification_timeout);
|
2014-07-22 20:38:32 +02:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
/* screen/tmux auto-away timer */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (init_mplex_away_timer(m) == -1) {
|
2015-07-04 07:19:16 +02:00
|
|
|
queue_init_message("Failed to init mplex auto-away.");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2016-09-22 20:09:07 +02:00
|
|
|
int nodeslist_ret = load_DHT_nodeslist();
|
|
|
|
|
|
|
|
if (nodeslist_ret != 0) {
|
|
|
|
queue_init_message("DHT nodeslist failed to load (error %d)", nodeslist_ret);
|
|
|
|
}
|
|
|
|
|
2015-08-28 03:29:34 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2014-08-26 01:26:41 +02:00
|
|
|
print_init_messages(prompt);
|
2021-11-22 02:16:02 +01:00
|
|
|
flag_interface_refresh();
|
2015-08-28 03:29:34 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
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 */
|
2017-09-01 23:36:17 +02:00
|
|
|
char avatarstr[PATH_MAX + 11];
|
2018-10-08 06:47:51 +02:00
|
|
|
snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", user_settings->avatar_path);
|
2014-09-26 09:10:44 +02:00
|
|
|
execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE);
|
|
|
|
|
2016-09-23 00:31:52 +02:00
|
|
|
time_t last_save = get_unix_time();
|
2014-07-08 20:46:50 +02:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
while (true) {
|
2016-09-17 20:26:23 +02:00
|
|
|
do_toxic(m);
|
2015-11-05 23:46:00 +01:00
|
|
|
|
2016-09-23 00:31:52 +02:00
|
|
|
time_t cur_time = get_unix_time();
|
2014-07-08 20:46:50 +02:00
|
|
|
|
2018-10-28 21:18:17 +01:00
|
|
|
if (user_settings->autosave_freq > 0 && timed_out(last_save, user_settings->autosave_freq)) {
|
2014-07-09 06:05:13 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (store_data(m, DATA_FILE) != 0) {
|
2020-11-30 23:55:57 +01:00
|
|
|
line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-03 18:17:48 +01:00
|
|
|
long int sleep_duration = tox_iteration_interval(m) * 1000;
|
|
|
|
sleep_thread(sleep_duration);
|
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;
|
2020-05-18 19:58:55 +02:00
|
|
|
}
|