1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-03 16:47:46 +02:00
toxic/src/toxic.c

665 lines
17 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>
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-03-07 03:14:04 +01:00
#ifdef _SUPPORT_AUDIO
#include "audio_call.h"
2014-03-07 03:14:04 +01:00
#endif /* _SUPPORT_AUDIO */
2013-08-25 01:16:43 +02:00
#ifndef PACKAGE_DATADIR
#define PACKAGE_DATADIR "."
#endif
2013-09-09 06:56:47 +02:00
2014-03-08 16:36:42 +01:00
#ifdef _SUPPORT_AUDIO
ToxAv *av;
2014-03-08 16:36:42 +01:00
#endif /* _SUPPORT_AUDIO */
2013-08-15 12:11:48 +02:00
/* Export for use in Callbacks */
char *DATA_FILE = NULL;
ToxWindow *prompt = NULL;
2014-07-08 20:46:50 +02:00
#define AUTOSAVE_FREQ 60
struct arg_opts {
int ignore_data_file;
int use_ipv4;
2014-06-30 00:14:28 +02:00
int default_locale;
char config_path[MAX_STR_SIZE];
char nodes_path[MAX_STR_SIZE];
} arg_opts;
2013-11-30 00:52:21 +01:00
2014-03-13 11:06:53 +01:00
struct _Winthread Winthread;
2014-04-07 10:42:10 +02:00
struct user_settings *user_settings = NULL;
2014-03-13 11:06:53 +01:00
static void ignore_SIGINT(int sig)
2013-08-11 02:59:16 +02:00
{
return;
}
2014-07-01 05:56:47 +02:00
static void flag_window_resize(int sig)
{
Winthread.flag_resize = true;
}
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();
log_disable(prompt->chatwin->log);
line_info_cleanup(prompt->chatwin->hst);
free(DATA_FILE);
free(prompt->stb);
free(prompt->help);
free(prompt->chatwin->log);
free(prompt->chatwin->hst);
free(prompt->chatwin);
free(user_settings);
#ifdef _SUPPORT_AUDIO
terminate_audio();
#endif /* _SUPPORT_AUDIO */
tox_kill(m);
endwin();
exit(EXIT_SUCCESS);
}
void exit_toxic_err(const char *errmsg, int errcode)
{
if (errmsg == NULL)
errmsg = "No error message";
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
{
2014-07-01 05:56:47 +02:00
signal(SIGWINCH, flag_window_resize);
#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();
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();
}
2013-12-02 15:02:34 +01:00
static Tox *init_tox(int ipv4)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
/* Init core */
2013-12-02 15:02:34 +01:00
int ipv6 = !ipv4;
Tox *m = tox_new(ipv6);
2013-10-10 10:52:05 +02:00
/*
* TOX_ENABLE_IPV6_DEFAULT is always 1.
* Checking it is redundant, this *should* be doing ipv4 fallback
*/
2014-03-05 07:01:10 +01:00
if (ipv6 && m == NULL) {
fprintf(stderr, "IPv6 didn't initialize, trying IPv4\n");
m = tox_new(0);
}
if (ipv4)
fprintf(stderr, "Forcing IPv4 connection\n");
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);
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 MINLINE 50 /* IP: 7 + port: 5 + key: 38 + spaces: 2 = 70. ! (& e.g. tox.im = 6) */
#define MAXLINE 256 /* Approx max number of chars in a sever line (name + port + key) */
2014-03-03 16:55:10 +01:00
#define MAXNODES 50
#define NODELEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
2013-08-10 21:46:29 +02:00
static struct _toxNodes {
int lines;
char nodes[MAXNODES][NODELEN];
uint16_t ports[MAXNODES];
char keys[MAXNODES][TOX_CLIENT_ID_SIZE];
} toxNodes;
static int nodelist_load(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[MAXLINE];
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
if (strlen(line) > MINLINE) {
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] = htons(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
}
if (toxNodes.lines < 1) {
2013-08-16 19:11:09 +02:00
fclose(fp);
return 2;
}
2013-08-10 21:46:29 +02:00
fclose(fp);
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], TOX_ENABLE_IPV6_DEFAULT,
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 = nodelist_load(PACKAGE_DATADIR "/DHTnodes");
else
res = nodelist_load(arg_opts.nodes_path);
if (toxNodes.lines < 1)
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
2014-02-22 23:58:36 +01:00
static void do_connection(Tox *m, ToxWindow *prompt)
2013-08-07 00:27:51 +02:00
{
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);
2014-03-25 08:17:22 +01:00
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, msg, SYS_MSG, 0, 0);
}
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);
}
2013-08-15 12:11:48 +02:00
/*
* Store Messenger to given location
* Return 0 stored successfully
2013-09-09 06:56:47 +02:00
* Return 1 file path is NULL
* Return 2 malloc failed
* Return 3 opening path failed
* Return 4 fwrite failed
2013-08-15 12:11:48 +02:00
*/
2013-08-23 23:03:44 +02:00
int store_data(Tox *m, char *path)
2013-08-07 00:27:51 +02:00
{
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)
return 1;
2013-08-15 12:11:48 +02:00
FILE *fd;
int len;
char *buf;
2013-08-23 23:03:44 +02:00
len = tox_size(m);
buf = malloc(len);
2013-08-16 19:11:09 +02:00
2013-09-09 21:33:15 +02:00
if (buf == NULL)
2013-09-09 06:56:47 +02:00
return 2;
2013-08-16 19:11:09 +02:00
tox_save(m, (uint8_t *) buf);
2013-09-24 23:47:42 +02:00
fd = fopen(path, "wb");
2013-08-16 19:11:09 +02:00
2013-08-07 00:27:51 +02:00
if (fd == NULL) {
2013-08-15 12:11:48 +02:00
free(buf);
2013-09-09 06:56:47 +02:00
return 3;
}
2013-08-15 12:11:48 +02:00
if (fwrite(buf, len, 1, fd) != 1) {
free(buf);
fclose(fd);
2013-09-09 06:56:47 +02:00
return 4;
2013-08-15 12:11:48 +02:00
}
free(buf);
fclose(fd);
return 0;
}
2013-08-23 23:03:44 +02:00
static void load_data(Tox *m, char *path)
2013-08-15 12:11:48 +02:00
{
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;
int len;
char *buf;
2013-08-15 12:11:48 +02:00
2013-09-24 21:23:09 +02:00
if ((fd = fopen(path, "rb")) != NULL) {
2013-08-15 12:11:48 +02:00
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
2013-08-16 19:11:09 +02:00
2013-08-15 12:11:48 +02:00
if (buf == NULL) {
fclose(fd);
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);
2013-08-15 12:11:48 +02:00
free(buf);
fclose(fd);
} else {
int st;
2013-08-16 19:11:09 +02:00
if ((st = 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-06-30 17:42:11 +02:00
pthread_mutex_unlock(&Winthread.lock);
tox_do(m); /* main tox-core loop */
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-02-22 23:58:36 +01:00
}
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, " -f, --file Use specified data file\n");
fprintf(stderr, " -x, --nodata Ignore data file\n");
fprintf(stderr, " -4, --ipv4 Force IPv4 connection\n");
fprintf(stderr, " -d, --default_locale Use default locale\n");
fprintf(stderr, " -c, --config Use specified config file\n");
fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n");
fprintf(stderr, " -h, --help Show this message and exit\n");
}
static void set_default_opts(void)
{
arg_opts.use_ipv4 = 0;
arg_opts.ignore_data_file = 0;
2014-06-30 00:14:28 +02:00
arg_opts.default_locale = 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'},
2014-06-30 00:14:28 +02:00
{"default_locale", no_argument, 0, 'd'},
{"config", required_argument, 0, 'c'},
{"nodes", required_argument, 0, 'n'},
{"help", no_argument, 0, 'h'},
};
2014-06-30 00:14:28 +02:00
const char *opts_str = "4xdf:c:n:h";
int opt, indexptr;
while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
switch (opt) {
case 'f':
DATA_FILE = strdup(optarg);
break;
case 'x':
arg_opts.ignore_data_file = 1;
break;
case '4':
arg_opts.use_ipv4 = 1;
break;
case 'c':
snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
break;
case 'n':
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
break;
2014-06-30 00:14:28 +02:00
case 'd':
arg_opts.default_locale = 1;
break;
case 'h':
default:
print_usage();
exit(EXIT_SUCCESS);
}
2014-06-21 01:58:00 +02:00
}
}
2013-08-07 00:27:51 +02:00
int main(int argc, char *argv[])
{
char *user_config_dir = get_user_config_dir();
2013-08-16 19:11:09 +02:00
int config_err = 0;
parse_args(argc, argv);
2013-08-16 19:11:09 +02:00
2014-02-25 08:28:24 +01:00
/* Make sure all written files are read/writeable only by the current user. */
2014-02-22 23:58:36 +01:00
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
signal(SIGINT, ignore_SIGINT);
2013-08-16 19:11:09 +02:00
config_err = create_user_config_dir(user_config_dir);
2013-08-16 19:11:09 +02:00
if (DATA_FILE == NULL ) {
if (config_err) {
DATA_FILE = strdup("data");
} else {
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
if (DATA_FILE == NULL)
exit_toxic_err("failed in main", FATALERR_MEMORY);
strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data");
2013-08-16 19:11:09 +02:00
}
}
2013-08-16 19:11:09 +02:00
free(user_config_dir);
2014-04-07 10:42:10 +02:00
/* init user_settings struct and load settings from conf file */
user_settings = malloc(sizeof(struct user_settings));
if (user_settings == NULL)
exit_toxic_err("failed in main", FATALERR_MEMORY);
2014-04-07 10:42:10 +02:00
2014-04-08 23:20:21 +02:00
memset(user_settings, 0, sizeof(struct user_settings));
2014-04-07 10:42:10 +02:00
char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL;
int settings_err = settings_load(user_settings, p);
Tox *m = init_tox(arg_opts.use_ipv4);
2014-05-16 20:00:01 +02:00
init_term();
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-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
char *msg;
2014-03-14 04:30:44 +01:00
#ifdef _SUPPORT_AUDIO
2014-03-14 04:30:44 +01:00
2014-03-08 16:36:42 +01:00
av = init_audio(prompt, m);
2014-05-16 20:00:01 +02:00
2014-06-21 01:58:00 +02:00
set_primary_device(input, user_settings->audio_in_dev);
set_primary_device(output, user_settings->audio_out_dev);
2014-03-07 03:14:04 +01:00
#endif /* _SUPPORT_AUDIO */
2014-03-14 04:30:44 +01:00
if (config_err) {
msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile...";
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2013-08-16 19:11:09 +02:00
}
2014-04-07 10:42:10 +02:00
if (settings_err == -1) {
msg = "Failed to load user settings";
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
}
2014-03-14 04:30:44 +01:00
sort_friendlist_index();
prompt_init_statusbar(prompt, m);
2014-07-08 20:46:50 +02:00
uint64_t last_save = get_unix_time();
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)) {
store_data(m, DATA_FILE);
last_save = cur_time;
}
2014-06-27 00:22:10 +02:00
usleep(40000);
2014-03-13 11:06:53 +01:00
}
2013-08-16 19:11:09 +02:00
return 0;
}