1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 02:53:02 +01:00
toxic/src/toxic.c

897 lines
25 KiB
C
Raw Normal View History

2014-02-25 08:28:24 +01:00
/* main.c
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <curses.h>
2013-08-08 16:00:12 +02:00
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
2013-08-11 02:59:16 +02:00
#include <signal.h>
#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>
#include <getopt.h>
#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>
2013-08-08 16:00:12 +02:00
#include <tox/tox.h>
#include "configdir.h"
#include "toxic.h"
#include "windows.h"
2013-08-13 00:50:43 +02:00
#include "friendlist.h"
#include "prompt.h"
#include "misc_tools.h"
#include "file_senders.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"
#include "device.h"
#include "message_queue.h"
#ifdef _AUDIO
#include "audio_call.h"
#endif /* _AUDIO */
2014-03-07 03:14:04 +01:00
2013-08-25 01:16:43 +02:00
#ifndef PACKAGE_DATADIR
#define PACKAGE_DATADIR "."
#endif
2013-09-09 06:56:47 +02:00
#ifdef _AUDIO
ToxAv *av;
#endif /* _AUDIO */
2014-03-08 16:36:42 +01: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;
ToxWindow *prompt = NULL;
2014-07-08 20:46:50 +02:00
#define AUTOSAVE_FREQ 60
2014-03-13 11:06:53 +01:00
struct _Winthread Winthread;
struct _cqueue_thread cqueue_thread;
2014-07-31 18:48:49 +02:00
struct arg_opts arg_opts;
2014-07-21 01:12:13 +02:00
struct user_settings *user_settings_ = NULL;
2014-03-13 11:06:53 +01:00
2014-07-09 02:21:51 +02:00
static void catch_SIGINT(int sig)
2013-08-11 02:59:16 +02:00
{
2014-07-09 06:05:13 +02:00
Winthread.sig_exit_toxic = true;
}
static void catch_SIGSEGV(int sig)
{
2014-08-03 21:31:56 +02:00
freopen("/dev/tty", "w", stderr);
endwin();
fprintf(stderr, "Caught SIGSEGV: Aborting toxic session.\n");
exit(EXIT_FAILURE);
}
2014-07-01 05:56:47 +02:00
static void flag_window_resize(int sig)
{
Winthread.flag_resize = true;
}
static void init_signal_catchers(void)
{
signal(SIGWINCH, flag_window_resize);
signal(SIGINT, catch_SIGINT);
signal(SIGSEGV, catch_SIGSEGV);
}
void exit_toxic_success(Tox *m)
{
store_data(m, DATA_FILE);
2014-06-28 18:55:05 +02:00
close_all_file_senders(m);
kill_all_windows(m);
free(DATA_FILE);
2014-08-02 05:00:52 +02:00
free(BLOCK_FILE);
2014-07-21 01:12:13 +02:00
free(user_settings_);
2014-07-22 20:38:32 +02:00
#ifdef _SOUND_NOTIFY
2014-08-02 00:37:02 +02:00
// sound_notify(NULL, self_log_out, NT_ALWAYS, NULL);
2014-07-22 20:38:32 +02:00
#endif /* _SOUND_NOTIFY */
2014-07-21 01:12:13 +02:00
terminate_notify();
#ifdef _AUDIO
terminate_audio();
#endif /* _AUDIO */
tox_kill(m);
endwin();
exit(EXIT_SUCCESS);
}
void exit_toxic_err(const char *errmsg, int errcode)
{
if (errmsg == NULL)
errmsg = "No error message";
2014-08-03 21:31:56 +02:00
freopen("/dev/tty", "w", stderr);
endwin();
fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg);
exit(EXIT_FAILURE);
2013-08-11 02:59:16 +02:00
}
static void init_term(void)
2013-08-07 00:27:51 +02:00
{
#if HAVE_WIDECHAR
2014-06-30 00:14:28 +02:00
if (!arg_opts.default_locale) {
if (setlocale(LC_ALL, "") == NULL)
exit_toxic_err("Could not set your locale, please check your locale settings or "
2014-07-01 05:56:47 +02:00
"disable unicode support with the -d flag.", FATALERR_LOCALE_SET);
2014-06-30 00:14:28 +02:00
}
#endif
2013-08-16 19:11:09 +02:00
initscr();
cbreak();
keypad(stdscr, 1);
noecho();
timeout(100);
if (has_colors()) {
short bg_color = COLOR_BLACK;
2013-08-16 19:11:09 +02:00
start_color();
2014-07-21 01:12:13 +02:00
if (user_settings_->colour_theme == NATIVE_COLS) {
if (assume_default_colors(-1, -1) == OK)
bg_color = -1;
}
init_pair(0, COLOR_WHITE, COLOR_BLACK);
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();
}
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. */
static void queue_init_message(const char *msg)
{
int i = init_messages.num;
++init_messages.num;
char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num);
if (new_msgs == NULL)
exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY);
new_msgs[i] = malloc(MAX_STR_SIZE);
if (new_msgs[i] == NULL)
exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY);
snprintf(new_msgs[i], MAX_STR_SIZE, "%s", msg);
init_messages.msgs = new_msgs;
}
2014-08-27 05:21:32 +02:00
/* called after messages have been printed to console and are no longer needed */
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]);
}
static Tox *init_tox(void)
2013-08-07 00:27:51 +02:00
{
Tox_Options tox_opts;
tox_opts.ipv6enabled = !arg_opts.use_ipv4;
tox_opts.udp_disabled = arg_opts.force_tcp;
2014-08-15 02:31:52 +02:00
tox_opts.proxy_enabled = arg_opts.use_proxy;
if (tox_opts.proxy_enabled) {
tox_opts.proxy_port = arg_opts.proxy_port;
snprintf(tox_opts.proxy_address, sizeof(tox_opts.proxy_address), "%s", arg_opts.proxy_address);
char tmp[48];
snprintf(tmp, sizeof(tmp), "Using proxy %s : %d",
arg_opts.proxy_address, arg_opts.proxy_port);
queue_init_message(tmp);
}
if (tox_opts.udp_disabled) {
queue_init_message("UDP disabled");
} else if (tox_opts.proxy_enabled) {
const char *msg = "WARNING: Using a proxy without disabling UDP may leak your real IP address.";
queue_init_message(msg);
msg = "Use the -t option to disable UDP.";
queue_init_message(msg);
2014-08-15 02:31:52 +02:00
}
2013-08-16 19:11:09 +02:00
/* Init core */
Tox *m = tox_new(&tox_opts);
if (tox_opts.ipv6enabled && m == NULL) {
queue_init_message("IPv6 failed to initialize");
tox_opts.ipv6enabled = 0;
m = tox_new(&tox_opts);
}
if (!tox_opts.ipv6enabled)
queue_init_message("Forcing IPv4 connection");
2014-08-15 02:31:52 +02:00
if (tox_opts.proxy_enabled && m == NULL)
exit_toxic_err("Proxy error", FATALERR_PROXY);
2014-08-15 02:31:52 +02:00
if (m == NULL)
return NULL;
2013-08-16 19:11:09 +02:00
/* Callbacks */
tox_callback_connection_status(m, on_connectionchange, NULL);
2014-02-23 10:28:33 +01:00
tox_callback_typing_change(m, on_typing_change, NULL);
tox_callback_friend_request(m, on_request, NULL);
tox_callback_friend_message(m, on_message, NULL);
tox_callback_name_change(m, on_nickchange, NULL);
tox_callback_user_status(m, on_statuschange, NULL);
tox_callback_status_message(m, on_statusmessagechange, NULL);
tox_callback_friend_action(m, on_action, NULL);
2013-09-15 22:38:38 +02:00
tox_callback_group_invite(m, on_groupinvite, NULL);
tox_callback_group_message(m, on_groupmessage, NULL);
2013-12-14 02:57:32 +01:00
tox_callback_group_action(m, on_groupaction, NULL);
tox_callback_group_namelist_change(m, on_group_namelistchange, NULL);
tox_callback_file_send_request(m, on_file_sendrequest, NULL);
2013-10-10 10:52:05 +02:00
tox_callback_file_control(m, on_file_control, NULL);
tox_callback_file_data(m, on_file_data, NULL);
2014-09-07 08:43:53 +02:00
tox_callback_read_receipt(m, on_read_receipt, NULL);
2013-10-10 10:52:05 +02:00
2013-08-18 17:59:34 +02:00
#ifdef __linux__
tox_set_name(m, (uint8_t *) "Cool dude", strlen("Cool dude"));
2014-05-08 22:13:33 +02:00
#elif defined(__FreeBSD__)
tox_set_name(m, (uint8_t *) "Nerd", strlen("Nerd"));
2013-08-19 09:23:40 +02:00
#elif defined(__APPLE__)
tox_set_name(m, (uint8_t *) "Hipster", strlen("Hipster")); /* This used to users of other Unixes are hipsters */
2013-08-18 17:59:34 +02:00
#else
tox_set_name(m, (uint8_t *) "Registered Minix user #4", strlen("Registered Minix user #4"));
2013-08-18 17:59:34 +02:00
#endif
2013-10-10 10:52:05 +02:00
2013-08-16 19:11:09 +02:00
return m;
}
#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
#define NODELEN (MAX_NODE_LINE - TOX_CLIENT_ID_SIZE - 7)
2013-08-10 21:46:29 +02:00
static struct _toxNodes {
int lines;
char nodes[MAXNODES][NODELEN];
uint16_t ports[MAXNODES];
char keys[MAXNODES][TOX_CLIENT_ID_SIZE];
} toxNodes;
static int load_nodelist(const char *filename)
2013-08-10 21:46:29 +02:00
{
if (!filename)
return 1;
FILE *fp = fopen(filename, "r");
2013-08-16 19:11:09 +02:00
if (fp == NULL)
2013-08-16 19:11:09 +02:00
return 1;
char line[MAX_NODE_LINE];
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
if (strlen(line) > MIN_NODE_LINE) {
const char *name = strtok(line, " ");
const char *port = strtok(NULL, " ");
const char *key_ascii = strtok(NULL, " ");
/* invalid line */
if (name == NULL || port == NULL || key_ascii == NULL)
continue;
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name);
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
toxNodes.ports[toxNodes.lines] = atoi(port);
char *key_binary = hex_string_to_bin(key_ascii);
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_CLIENT_ID_SIZE);
free(key_binary);
toxNodes.lines++;
}
2013-08-16 19:11:09 +02:00
}
2013-08-10 21:46:29 +02:00
fclose(fp);
if (toxNodes.lines < 1)
return 1;
return 0;
}
2013-08-16 19:11:09 +02:00
2014-02-25 01:18:43 +01:00
int init_connection_helper(Tox *m, int line)
{
return tox_bootstrap_from_address(m, toxNodes.nodes[line], toxNodes.ports[line], (uint8_t *) toxNodes.keys[line]);
}
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
*
* return codes:
2014-03-03 16:55:10 +01:00
* 1: failed to open node file
* 2: no line of sufficient length in node file
2014-02-25 01:41:02 +01:00
* 3: failed to resolve name to IP
2014-03-03 16:55:10 +01:00
* 4: nodelist file contains no acceptable line
*/
2014-02-25 01:18:43 +01:00
static bool srvlist_loaded = false;
#define NUM_INIT_NODES 5
int init_connection(Tox *m)
{
if (toxNodes.lines > 0) /* already loaded nodelist */
return init_connection_helper(m, rand() % toxNodes.lines) ? 0 : 3;
/* only once:
2014-03-03 16:55:10 +01:00
* - load the nodelist
* - connect to "everyone" inside
*/
2014-02-25 01:18:43 +01:00
if (!srvlist_loaded) {
srvlist_loaded = true;
int res;
if (!arg_opts.nodes_path[0])
res = load_nodelist(PACKAGE_DATADIR "/DHTnodes");
else
res = load_nodelist(arg_opts.nodes_path);
if (res != 0)
return res;
2014-02-25 01:41:02 +01:00
res = 3;
int i;
int n = MIN(NUM_INIT_NODES, toxNodes.lines);
for (i = 0; i < n; ++i) {
if (init_connection_helper(m, rand() % toxNodes.lines))
res = 0;
2014-03-13 11:06:53 +01: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
}
#define TRY_CONNECT 10 /* Seconds between connection attempts when DHT is not connected */
2014-02-22 23:58:36 +01:00
static void do_connection(Tox *m, ToxWindow *prompt)
2013-08-07 00:27:51 +02:00
{
if (arg_opts.no_connect == 1)
return;
char msg[MAX_STR_SIZE] = {0};
2014-03-25 08:17:22 +01:00
2013-08-16 19:11:09 +02:00
static int conn_err = 0;
static bool was_connected = false;
static uint64_t last_conn_try = 0;
uint64_t curtime = get_unix_time();
2014-02-22 23:58:36 +01:00
bool is_connected = tox_isconnected(m);
if (was_connected && is_connected)
return;
if (!was_connected && is_connected) {
was_connected = true;
prompt_update_connectionstatus(prompt, was_connected);
snprintf(msg, sizeof(msg), "DHT connected");
} else if (was_connected && !is_connected) {
was_connected = false;
prompt_update_connectionstatus(prompt, was_connected);
snprintf(msg, sizeof(msg), "DHT disconnected. Attempting to reconnect.");
} else if (!was_connected && !is_connected && timed_out(last_conn_try, curtime, TRY_CONNECT)) {
/* if autoconnect has already failed there's no point in trying again */
if (conn_err == 0) {
last_conn_try = curtime;
if ((conn_err = init_connection(m)) != 0)
snprintf(msg, sizeof(msg), "Auto-connect failed with error code %d", conn_err);
}
2013-08-13 04:04:07 +02:00
}
2014-03-25 08:17:22 +01:00
if (msg[0])
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
}
2014-03-05 14:01:12 +01:00
static void load_friendlist(Tox *m)
{
2014-04-11 00:16:27 +02:00
uint32_t i;
2014-03-05 14:01:12 +01:00
uint32_t numfriends = tox_count_friendlist(m);
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-15 12:11:48 +02:00
/*
* Store Messenger to given location
2014-07-17 09:35:18 +02:00
* Return 0 stored successfully or ignoring data file
2014-07-31 18:48:49 +02:00
* Return -1 file path is NULL
* Return -2 malloc failed
* Return -3 opening path failed
* Return -4 fwrite failed
2013-08-15 12:11:48 +02:00
*/
2013-08-23 23:03:44 +02:00
int store_data(Tox *m, char *path)
2013-08-07 00:27:51 +02:00
{
if (arg_opts.ignore_data_file)
return 0;
2013-08-16 19:11:09 +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
2014-07-17 09:35:18 +02:00
int len = tox_size(m);
char *buf = malloc(len);
2013-08-16 19:11:09 +02:00
2013-09-09 21:33:15 +02:00
if (buf == NULL)
2014-07-31 18:48:49 +02:00
return -2;
2013-08-16 19:11:09 +02:00
tox_save(m, (uint8_t *) buf);
2014-07-17 09:35:18 +02:00
FILE *fd = fopen(path, "wb");
2013-08-16 19:11:09 +02:00
2013-08-07 00:27:51 +02:00
if (fd == NULL) {
2013-08-15 12:11:48 +02:00
free(buf);
2014-07-31 18:48:49 +02:00
return -3;
}
2013-08-15 12:11:48 +02:00
if (fwrite(buf, len, 1, fd) != 1) {
free(buf);
fclose(fd);
2014-07-31 18:48:49 +02:00
return -4;
2013-08-15 12:11:48 +02:00
}
free(buf);
fclose(fd);
return 0;
}
2013-08-23 23:03:44 +02:00
static void load_data(Tox *m, char *path)
2013-08-15 12:11:48 +02:00
{
if (arg_opts.ignore_data_file)
return;
2013-08-16 19:11:09 +02:00
2013-08-15 12:11:48 +02:00
FILE *fd;
2013-09-24 21:23:09 +02:00
if ((fd = fopen(path, "rb")) != NULL) {
2013-08-15 12:11:48 +02:00
fseek(fd, 0, SEEK_END);
2014-07-17 09:35:18 +02:00
int len = ftell(fd);
2013-08-15 12:11:48 +02:00
fseek(fd, 0, SEEK_SET);
2014-07-17 09:35:18 +02:00
char *buf = malloc(len);
2013-08-16 19:11:09 +02:00
2013-08-15 12:11:48 +02:00
if (buf == NULL) {
fclose(fd);
exit_toxic_err("failed in load_data", FATALERR_MEMORY);
2013-08-15 12:11:48 +02:00
}
2013-08-16 19:11:09 +02:00
2013-08-15 12:11:48 +02:00
if (fread(buf, len, 1, fd) != 1) {
free(buf);
fclose(fd);
exit_toxic_err("failed in load_data", FATALERR_FREAD);
2013-08-15 12:11:48 +02:00
}
2013-08-16 19:11:09 +02:00
tox_load(m, (uint8_t *) buf, len);
2014-03-05 14:01:12 +01:00
load_friendlist(m);
2014-07-31 18:48:49 +02:00
load_blocklist(BLOCK_FILE);
2013-08-15 12:11:48 +02:00
free(buf);
fclose(fd);
} else {
if (store_data(m, path) != 0)
exit_toxic_err("failed in load_data", FATALERR_STORE_DATA);
}
}
2014-02-22 23:58:36 +01:00
static void do_toxic(Tox *m, ToxWindow *prompt)
{
2014-06-30 17:42:11 +02:00
pthread_mutex_lock(&Winthread.lock);
2014-02-22 23:58:36 +01:00
do_connection(m, prompt);
do_file_senders(m);
2014-08-13 01:13:57 +02:00
if (arg_opts.no_connect == 0)
tox_do(m); /* main tox-core loop */
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-03-13 11:06:53 +01: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();
Winthread.flag_resize = false;
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-02-22 23:58:36 +01:00
}
void *thread_cqueue(void *data)
{
Tox *m = (Tox *) data;
while (true) {
pthread_mutex_lock(&Winthread.lock);
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
ToxWindow *toxwin = get_window_ptr(i);
if (toxwin != NULL && toxwin->is_chat && tox_get_friend_connection_status(m, toxwin->num) == 1)
2014-09-09 20:06:28 +02:00
cqueue_try_send(toxwin, m, toxwin->num);
}
pthread_mutex_unlock(&Winthread.lock);
2014-09-09 20:06:28 +02:00
usleep(7000);
}
}
static void print_usage(void)
2014-06-21 01:58:00 +02:00
{
fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n");
2014-06-30 00:14:28 +02:00
fprintf(stderr, " -4, --ipv4 Force IPv4 connection\n");
fprintf(stderr, " -b --debug Enable stderr for debugging\n");
2014-06-30 00:14:28 +02:00
fprintf(stderr, " -c, --config Use specified config file\n");
fprintf(stderr, " -d, --default-locale Use default POSIX locale\n");
fprintf(stderr, " -f, --file Use specified data file\n");
2014-06-30 00:14:28 +02:00
fprintf(stderr, " -h, --help Show this message and exit\n");
fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n");
fprintf(stderr, " -o, --noconnect Do not connect to the DHT network\n");
2014-08-15 02:31:52 +02:00
fprintf(stderr, " -p, --proxy Use proxy: Requires [IP] [port]\n");
2014-08-13 01:13:57 +02:00
fprintf(stderr, " -r, --dnslist Use specified DNSservers file\n");
2014-08-15 02:31:52 +02:00
fprintf(stderr, " -t, --force-tcp Force TCP connection (use this with proxies)\n");
fprintf(stderr, " -x, --nodata Ignore data file\n");
}
static void set_default_opts(void)
{
arg_opts.use_ipv4 = 0;
arg_opts.ignore_data_file = 0;
arg_opts.debug = 0;
2014-06-30 00:14:28 +02:00
arg_opts.default_locale = 0;
2014-08-02 19:04:29 +02:00
arg_opts.use_custom_data = 0;
arg_opts.no_connect = 0;
arg_opts.force_tcp = 0;
2014-08-15 02:31:52 +02:00
arg_opts.use_proxy = 0;
}
static void parse_args(int argc, char *argv[])
{
set_default_opts();
static struct option long_opts[] = {
{"file", required_argument, 0, 'f'},
{"nodata", no_argument, 0, 'x'},
{"ipv4", no_argument, 0, '4'},
{"debug", no_argument, 0, 'b'},
{"default-locale", no_argument, 0, 'd'},
{"config", required_argument, 0, 'c'},
{"nodes", required_argument, 0, 'n'},
{"help", no_argument, 0, 'h'},
{"noconnect", no_argument, 0, 'o'},
2014-08-13 01:13:57 +02:00
{"dnslist", required_argument, 0, 'r'},
{"force-tcp", no_argument, 0, 't'},
2014-08-15 02:31:52 +02:00
{"proxy", required_argument, 0, 'p'},
2014-08-27 20:30:06 +02:00
{NULL, no_argument, NULL, 0},
};
const char *opts_str = "4bdhotxc:f:n:r:p:";
int opt, indexptr;
while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
switch (opt) {
case '4':
arg_opts.use_ipv4 = 1;
break;
case 'b':
arg_opts.debug = 1;
queue_init_message("stderr enabled");
break;
case 'c':
snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
2014-08-28 04:45:11 +02:00
if (!file_exists(arg_opts.config_path))
queue_init_message("Config file not found");
2014-08-27 05:21:32 +02:00
break;
case 'd':
arg_opts.default_locale = 1;
queue_init_message("Using default POSIX locale");
break;
case 'f':
2014-08-02 19:04:29 +02:00
arg_opts.use_custom_data = 1;
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)
2014-07-17 09:35:18 +02:00
exit_toxic_err("failed in parse_args", FATALERR_MEMORY);
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-27 05:21:32 +02:00
char tmp[PATH_MAX];
snprintf(tmp, sizeof(tmp), "Using '%s' data file", DATA_FILE);
queue_init_message(tmp);
break;
case 'n':
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
2014-08-28 04:45:11 +02:00
if (!file_exists(arg_opts.nodes_path))
queue_init_message("DHTnodes file not found");
break;
case 'o':
arg_opts.no_connect = 1;
queue_init_message("DHT disabled");
break;
2014-08-15 02:31:52 +02:00
case 'p':
arg_opts.use_proxy = 1;
snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);
if (++optind > argc || argv[optind-1][0] == '-')
exit_toxic_err("Proxy error", FATALERR_PROXY);
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-28 04:45:11 +02:00
if (!file_exists(arg_opts.dns_path))
queue_init_message("DNSservers file not found");
2014-08-13 01:13:57 +02:00
break;
case 't':
arg_opts.force_tcp = 1;
break;
case 'x':
arg_opts.ignore_data_file = 1;
queue_init_message("Ignoring data file");
2014-06-30 00:14:28 +02:00
break;
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"
static int init_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;
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);
2013-08-16 19:11:09 +02:00
if (DATA_FILE == NULL ) {
if (config_err) {
2014-07-31 18:48:49 +02:00
DATA_FILE = strdup(DATANAME);
BLOCK_FILE = strdup(BLOCKNAME);
2014-07-17 09:35:18 +02:00
2014-07-31 18:48:49 +02:00
if (DATA_FILE == NULL || BLOCK_FILE == NULL)
exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY);
2013-08-16 19:11:09 +02:00
} else {
2014-07-31 18:48:49 +02:00
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-07-31 18:48:49 +02:00
if (DATA_FILE == NULL || BLOCK_FILE == NULL)
exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY);
strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR);
2014-07-31 18:48:49 +02:00
strcat(DATA_FILE, DATANAME);
strcpy(BLOCK_FILE, user_config_dir);
strcat(BLOCK_FILE, CONFIGDIR);
strcat(BLOCK_FILE, BLOCKNAME);
2013-08-16 19:11:09 +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;
}
#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 */
2014-08-01 21:05:10 +02:00
static useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, uint64_t cur_time, useconds_t msleepval)
{
useconds_t new_sleep = msleepval;
++(*loopcount);
2014-08-01 21:05:10 +02:00
if (*looptimer == cur_time)
return new_sleep;
2014-08-01 21:05:10 +02:00
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-07-31 18:48:49 +02:00
int main(int argc, char *argv[])
{
init_signal_catchers();
2014-07-31 18:48:49 +02:00
parse_args(argc, argv);
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);
int config_err = init_data_files();
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-07-21 01:12:13 +02:00
user_settings_ = calloc(1, sizeof(struct user_settings));
2014-04-07 10:42:10 +02:00
2014-07-21 01:12:13 +02:00
if (user_settings_ == NULL)
exit_toxic_err("failed in main", FATALERR_MEMORY);
2014-04-07 10:42:10 +02:00
char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL;
2014-07-21 01:12:13 +02:00
int settings_err = settings_load(user_settings_, p);
Tox *m = init_tox();
2014-05-16 20:00:01 +02:00
init_term();
/* enable stderr for debugging */
if (!arg_opts.debug)
freopen("/dev/null", "w", stderr);
if (m == NULL)
exit_toxic_err("failed in main", FATALERR_NETWORKINIT);
if (!arg_opts.ignore_data_file)
load_data(m, DATA_FILE);
prompt = init_windows(m);
2014-07-26 01:55:16 +02:00
prompt_init_statusbar(prompt, m);
2014-03-13 11:06:53 +01:00
/* thread for ncurses stuff */
if (pthread_mutex_init(&Winthread.lock, NULL) != 0)
exit_toxic_err("failed in main", FATALERR_MUTEX_INIT);
2014-03-13 11:06:53 +01:00
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0)
exit_toxic_err("failed in main", FATALERR_THREAD_CREATE);
2013-08-16 19:11:09 +02:00
/* 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);
#ifdef _AUDIO
2014-03-14 04:30:44 +01:00
2014-03-08 16:36:42 +01:00
av = init_audio(prompt, m);
2014-07-26 01:55:16 +02:00
2014-07-21 01:12:13 +02:00
set_primary_device(input, user_settings_->audio_in_dev);
set_primary_device(output, user_settings_->audio_out_dev);
#elif _SOUND_NOTIFY
if ( init_devices() == de_InternalError )
queue_init_message("Failed to init audio devices");
2014-07-22 20:38:32 +02:00
#endif /* _AUDIO */
2014-07-21 01:12:13 +02:00
init_notify(60, 3000);
2014-07-22 20:38:32 +02:00
#ifdef _SOUND_NOTIFY
2014-08-02 00:37:02 +02:00
// sound_notify(prompt, self_log_in, 0, NULL);
2014-07-22 20:38:32 +02:00
#endif /* _SOUND_NOTIFY */
2014-07-26 01:55:16 +02:00
const char *msg;
2014-08-02 00:37:02 +02:00
if (config_err) {
2014-07-31 18:48:49 +02:00
msg = "Unable to determine configuration directory. Defaulting to 'data' for data file...";
queue_init_message(msg);
2013-08-16 19:11:09 +02:00
}
2014-08-15 02:31:52 +02:00
if (settings_err == -1)
queue_init_message("Failed to load user settings");
2014-08-15 02:31:52 +02:00
print_init_messages(prompt);
2014-08-27 05:21:32 +02:00
cleanup_init_messages();
uint64_t last_save = (uint64_t) time(NULL);
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-07-08 20:46:50 +02:00
store_data(m, 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;
}
msleepval = optimal_msleepval(&looptimer, &loopcount, cur_time, msleepval);
usleep(msleepval);
2014-03-13 11:06:53 +01:00
}
2013-08-16 19:11:09 +02:00
return 0;
}