2013-07-30 01:32:39 +02:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-08-23 00:37:19 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 09:45:21 +02:00
|
|
|
#ifndef SIGWINCH
|
|
|
|
#define SIGWINCH 28
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <curses.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <errno.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2013-08-11 02:59:16 +02:00
|
|
|
#include <signal.h>
|
2013-08-21 23:19:35 +02:00
|
|
|
#include <locale.h>
|
2013-08-23 23:03:44 +02:00
|
|
|
#include <string.h>
|
2013-11-15 01:14:54 +01:00
|
|
|
#include <time.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-28 19:04:54 +02:00
|
|
|
#ifdef _WIN32
|
2013-08-27 09:45:21 +02:00
|
|
|
#include <direct.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#else
|
2013-08-27 09:45:21 +02:00
|
|
|
#include <netdb.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2013-08-23 23:03:44 +02:00
|
|
|
#include <sys/socket.h>
|
2013-08-08 16:00:12 +02:00
|
|
|
#endif
|
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-08 10:15:07 +02:00
|
|
|
#include "configdir.h"
|
2013-08-23 00:18:53 +02:00
|
|
|
#include "toxic_windows.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "prompt.h"
|
|
|
|
#include "misc_tools.h"
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-25 01:16:43 +02:00
|
|
|
#ifndef PACKAGE_DATADIR
|
|
|
|
#define PACKAGE_DATADIR "."
|
|
|
|
#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;
|
2013-08-18 23:16:39 +02:00
|
|
|
char *SRVLIST_FILE = NULL;
|
2013-09-07 01:59:45 +02:00
|
|
|
ToxWindow *prompt = NULL;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-11-30 00:52:21 +01:00
|
|
|
FileSender file_senders[MAX_FILES];
|
|
|
|
uint8_t max_file_senders_index;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
void on_window_resize(int sig)
|
2013-08-11 02:59:16 +02:00
|
|
|
{
|
|
|
|
endwin();
|
|
|
|
refresh();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2013-11-24 03:19:59 +01:00
|
|
|
static void init_term(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
/* Setup terminal */
|
|
|
|
signal(SIGWINCH, on_window_resize);
|
2013-08-25 00:59:57 +02:00
|
|
|
#if HAVE_WIDECHAR
|
|
|
|
if (setlocale(LC_ALL, "") == NULL) {
|
2013-09-11 06:02:27 +02:00
|
|
|
fprintf(stderr, "Could not set your locale, plese check your locale settings or"
|
2013-08-25 00:59:57 +02:00
|
|
|
"disable wide char support\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-25 00:59:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
2013-08-16 19:11:09 +02:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
keypad(stdscr, 1);
|
|
|
|
noecho();
|
|
|
|
timeout(100);
|
|
|
|
|
|
|
|
if (has_colors()) {
|
|
|
|
start_color();
|
2013-09-05 06:47:33 +02:00
|
|
|
init_pair(0, COLOR_WHITE, COLOR_BLACK);
|
2013-08-16 19:11:09 +02:00
|
|
|
init_pair(1, COLOR_GREEN, COLOR_BLACK);
|
|
|
|
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
|
|
|
init_pair(3, COLOR_RED, COLOR_BLACK);
|
|
|
|
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
|
2013-08-18 04:43:10 +02:00
|
|
|
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
init_pair(7, COLOR_BLACK, COLOR_BLACK);
|
|
|
|
init_pair(8, COLOR_BLACK, COLOR_WHITE);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh();
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 15:02:34 +01:00
|
|
|
static Tox *init_tox(int ipv4)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
/* Init core */
|
2013-12-02 15:02:34 +01:00
|
|
|
int ipv6 = !ipv4;
|
|
|
|
Tox *m = tox_new(ipv6);
|
2013-10-10 10:52:05 +02:00
|
|
|
|
2013-12-07 03:43:57 +01:00
|
|
|
if (TOX_ENABLE_IPV6_DEFAULT && m == NULL) {
|
|
|
|
fprintf(stderr, "IPv6 didn't initialize, trying IPv4 only\n");
|
|
|
|
m = tox_new(0);
|
|
|
|
}
|
|
|
|
|
2013-09-11 06:02:27 +02:00
|
|
|
if (m == NULL)
|
2013-09-11 01:34:29 +02:00
|
|
|
return NULL;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
/* Callbacks */
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_callback_connection_status(m, on_connectionchange, NULL);
|
|
|
|
tox_callback_friend_request(m, on_request, NULL);
|
|
|
|
tox_callback_friend_message(m, on_message, NULL);
|
|
|
|
tox_callback_name_change(m, on_nickchange, NULL);
|
|
|
|
tox_callback_user_status(m, on_statuschange, NULL);
|
|
|
|
tox_callback_status_message(m, on_statusmessagechange, NULL);
|
2013-12-11 01:51:40 +01:00
|
|
|
tox_callback_friend_action(m, on_action, NULL);
|
2013-09-15 22:38:38 +02:00
|
|
|
tox_callback_group_invite(m, on_groupinvite, NULL);
|
|
|
|
tox_callback_group_message(m, on_groupmessage, NULL);
|
2013-12-14 02:57:32 +01:00
|
|
|
tox_callback_group_action(m, on_groupaction, NULL);
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_callback_group_namelist_change(m, on_group_namelistchange, NULL);
|
|
|
|
tox_callback_file_send_request(m, on_file_sendrequest, NULL);
|
2013-10-10 10:52:05 +02:00
|
|
|
tox_callback_file_control(m, on_file_control, NULL);
|
|
|
|
tox_callback_file_data(m, on_file_data, NULL);
|
|
|
|
|
2013-08-18 17:59:34 +02:00
|
|
|
#ifdef __linux__
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
|
2013-08-28 19:04:54 +02:00
|
|
|
#elif defined(_WIN32)
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux"));
|
2013-08-19 09:23:40 +02:00
|
|
|
#elif defined(__APPLE__)
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters
|
2013-08-18 17:59:34 +02:00
|
|
|
#else
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_set_name(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4"));
|
2013-08-18 17:59:34 +02:00
|
|
|
#endif
|
2013-10-10 10:52:05 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return m;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
#define MINLINE 50 /* IP: 7 + port: 5 + key: 38 + spaces: 2 = 70. ! (& e.g. tox.im = 6) */
|
|
|
|
#define MAXLINE 256 /* Approx max number of chars in a sever line (name + port + key) */
|
2013-08-10 21:46:29 +02:00
|
|
|
#define MAXSERVERS 50
|
2013-09-15 14:31:59 +02:00
|
|
|
#define SERVERLEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
|
2013-08-10 21:46:29 +02:00
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
static int linecnt = 0;
|
|
|
|
static char servers[MAXSERVERS][SERVERLEN];
|
|
|
|
static uint16_t ports[MAXSERVERS];
|
|
|
|
static uint8_t keys[MAXSERVERS][TOX_CLIENT_ID_SIZE];
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
int serverlist_load(void)
|
2013-08-10 21:46:29 +02:00
|
|
|
{
|
2013-08-18 23:16:39 +02:00
|
|
|
FILE *fp = NULL;
|
2013-08-21 01:37:05 +02:00
|
|
|
|
2013-08-18 23:16:39 +02:00
|
|
|
fp = fopen(SRVLIST_FILE, "r");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-11 06:02:27 +02:00
|
|
|
if (fp == NULL)
|
2013-08-16 19:11:09 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
char line[MAXLINE];
|
|
|
|
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
|
2013-09-15 14:31:59 +02:00
|
|
|
if (strlen(line) > MINLINE) {
|
2013-09-17 03:21:54 +02:00
|
|
|
char *name = strtok(line, " ");
|
|
|
|
char *port = strtok(NULL, " ");
|
|
|
|
char *key_ascii = strtok(NULL, " ");
|
|
|
|
/* invalid line */
|
|
|
|
if (name == NULL || port == NULL || key_ascii == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strncpy(servers[linecnt], name, SERVERLEN);
|
|
|
|
servers[linecnt][SERVERLEN - 1] = 0;
|
|
|
|
ports[linecnt] = htons(atoi(port));
|
|
|
|
|
|
|
|
uint8_t *key_binary = hex_string_to_bin(key_ascii);
|
|
|
|
memcpy(keys[linecnt], key_binary, TOX_CLIENT_ID_SIZE);
|
|
|
|
free(key_binary);
|
|
|
|
|
|
|
|
linecnt++;
|
2013-09-15 14:31:59 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (linecnt < 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return 2;
|
|
|
|
}
|
2013-08-10 21:46:29 +02:00
|
|
|
|
|
|
|
fclose(fp);
|
2013-09-15 14:31:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
int init_connection_helper(Tox *m, int linenumber)
|
|
|
|
{
|
2013-09-17 03:21:54 +02:00
|
|
|
return tox_bootstrap_from_address(m, servers[linenumber], TOX_ENABLE_IPV6_DEFAULT,
|
|
|
|
ports[linenumber], keys[linenumber]);
|
2013-09-15 14:31:59 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
/* Connects to a random DHT server listed in the DHTservers file
|
|
|
|
*
|
|
|
|
* return codes:
|
|
|
|
* 1: failed to open server file
|
|
|
|
* 2: no line of sufficient length in server file
|
|
|
|
* 3: (old, removed) failed to split a selected line in the server file
|
|
|
|
* 4: failed to resolve name to IP
|
|
|
|
* 5: serverlist file contains no acceptable line
|
|
|
|
*/
|
2014-02-13 12:24:58 +01:00
|
|
|
static int init_connection_serverlist_loaded = 0;
|
2013-09-15 14:31:59 +02:00
|
|
|
int init_connection(Tox *m)
|
|
|
|
{
|
|
|
|
if (linecnt > 0) /* already loaded serverlist */
|
|
|
|
return init_connection_helper(m, rand() % linecnt) ? 0 : 4;
|
|
|
|
|
|
|
|
/* only once:
|
|
|
|
* - load the serverlist
|
|
|
|
* - connect to "everyone" inside
|
|
|
|
*/
|
|
|
|
if (!init_connection_serverlist_loaded) {
|
|
|
|
init_connection_serverlist_loaded = 1;
|
|
|
|
int res = serverlist_load();
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2013-09-17 03:21:54 +02:00
|
|
|
if (!linecnt)
|
|
|
|
return 4;
|
2013-09-15 14:31:59 +02:00
|
|
|
|
2013-09-17 03:21:54 +02:00
|
|
|
res = 6;
|
2013-09-15 14:31:59 +02:00
|
|
|
int linenumber;
|
|
|
|
for(linenumber = 0; linenumber < linecnt; linenumber++)
|
|
|
|
if (init_connection_helper(m, linenumber))
|
2013-09-17 03:21:54 +02:00
|
|
|
res = 0;
|
2013-09-15 14:31:59 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-15 14:31:59 +02:00
|
|
|
/* empty serverlist file */
|
|
|
|
return 5;
|
2013-08-10 21:46:29 +02:00
|
|
|
}
|
|
|
|
|
2014-02-21 06:45:29 +01:00
|
|
|
static void do_connection(Tox *m, ToxWindow *prompt)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
static int conn_try = 0;
|
|
|
|
static int conn_err = 0;
|
|
|
|
static bool dht_on = false;
|
|
|
|
|
2014-02-21 07:57:13 +01:00
|
|
|
bool is_connected = tox_isconnected(m);
|
|
|
|
|
|
|
|
if (!dht_on && !is_connected && !(conn_try++ % 100)) {
|
2013-12-06 11:07:35 +01:00
|
|
|
prep_prompt_win();
|
2013-08-16 19:11:09 +02:00
|
|
|
if (!conn_err) {
|
2013-09-28 01:55:11 +02:00
|
|
|
wprintw(prompt->window, "Establishing connection...\n");
|
2013-11-16 03:36:05 +01:00
|
|
|
if ((conn_err = init_connection(m)))
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
|
|
|
|
}
|
2014-02-21 07:57:13 +01:00
|
|
|
} else if (!dht_on && is_connected) {
|
2013-08-16 19:11:09 +02:00
|
|
|
dht_on = true;
|
2013-09-07 03:37:16 +02:00
|
|
|
prompt_update_connectionstatus(prompt, dht_on);
|
2013-12-06 11:07:35 +01:00
|
|
|
|
|
|
|
prep_prompt_win();
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "\nDHT connected.\n");
|
2014-02-21 07:57:13 +01:00
|
|
|
} else if (dht_on && !is_connected) {
|
2013-08-16 19:11:09 +02:00
|
|
|
dht_on = false;
|
2013-09-07 03:37:16 +02:00
|
|
|
prompt_update_connectionstatus(prompt, dht_on);
|
2013-12-06 11:07:35 +01:00
|
|
|
|
|
|
|
prep_prompt_win();
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
2013-08-13 04:04:07 +02:00
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 02:56:22 +02:00
|
|
|
int f_loadfromfile;
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
/*
|
|
|
|
* Store Messenger to given location
|
|
|
|
* Return 0 stored successfully
|
2013-09-09 06:56:47 +02:00
|
|
|
* Return 1 file path is NULL
|
|
|
|
* Return 2 malloc failed
|
|
|
|
* Return 3 opening path failed
|
|
|
|
* Return 4 fwrite failed
|
2013-08-15 12:11:48 +02:00
|
|
|
*/
|
2013-08-23 23:03:44 +02:00
|
|
|
int store_data(Tox *m, char *path)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 02:56:22 +02:00
|
|
|
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
|
|
|
return 0;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-09 06:56:47 +02:00
|
|
|
if (path == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
FILE *fd;
|
|
|
|
size_t len;
|
|
|
|
uint8_t *buf;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
len = tox_size(m);
|
2013-07-30 01:32:39 +02:00
|
|
|
buf = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-09 21:33:15 +02:00
|
|
|
if (buf == NULL)
|
2013-09-09 06:56:47 +02:00
|
|
|
return 2;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
tox_save(m, buf);
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-09-24 23:47:42 +02:00
|
|
|
fd = fopen(path, "wb");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
if (fd == NULL) {
|
2013-08-15 12:11:48 +02:00
|
|
|
free(buf);
|
2013-09-09 06:56:47 +02:00
|
|
|
return 3;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (fwrite(buf, len, 1, fd) != 1) {
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
2013-09-09 06:56:47 +02:00
|
|
|
return 4;
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void load_data(Tox *m, char *path)
|
2013-08-15 12:11:48 +02:00
|
|
|
{
|
2013-08-16 02:56:22 +02:00
|
|
|
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
|
|
|
return;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
FILE *fd;
|
|
|
|
size_t len;
|
|
|
|
uint8_t *buf;
|
|
|
|
|
2013-09-24 21:23:09 +02:00
|
|
|
if ((fd = fopen(path, "rb")) != NULL) {
|
2013-08-15 12:11:48 +02:00
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
len = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
buf = malloc(len);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
fclose(fd);
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if (fread(buf, len, 1, fd) != 1) {
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "fread() failed. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
tox_load(m, buf, len);
|
2013-08-15 12:11:48 +02:00
|
|
|
|
2013-08-25 01:16:43 +02:00
|
|
|
uint32_t i = 0;
|
2013-09-09 21:33:15 +02:00
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH];
|
2013-11-30 22:09:45 +01:00
|
|
|
|
|
|
|
while (tox_get_name(m, i, name) != -1)
|
2013-12-04 07:08:26 +01:00
|
|
|
on_friendadded(m, i++, false);
|
2013-08-15 12:11:48 +02:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
fclose(fd);
|
|
|
|
} else {
|
|
|
|
int st;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
if ((st = store_data(m, path)) != 0) {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-15 12:11:48 +02:00
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-30 01:26:59 +01:00
|
|
|
void close_file_sender(int i)
|
2013-11-15 01:14:54 +01:00
|
|
|
{
|
|
|
|
fclose(file_senders[i].file);
|
|
|
|
memset(&file_senders[i], 0, sizeof(FileSender));
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
2013-11-29 04:28:40 +01:00
|
|
|
for (j = max_file_senders_index; j > 0; --j) {
|
2013-11-15 01:14:54 +01:00
|
|
|
if (file_senders[j-1].active)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-29 04:28:40 +01:00
|
|
|
max_file_senders_index = j;
|
2013-11-15 01:14:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_file_senders(Tox *m)
|
2013-10-10 10:52:05 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-11-29 04:28:40 +01:00
|
|
|
for (i = 0; i < max_file_senders_index; ++i) {
|
2013-10-19 04:18:48 +02:00
|
|
|
if (!file_senders[i].active)
|
2013-10-10 10:52:05 +02:00
|
|
|
continue;
|
|
|
|
|
2013-11-19 00:52:46 +01:00
|
|
|
uint8_t *pathname = file_senders[i].pathname;
|
|
|
|
uint8_t filenum = file_senders[i].filenum;
|
|
|
|
int friendnum = file_senders[i].friendnum;
|
2013-11-19 04:22:18 +01:00
|
|
|
FILE *fp = file_senders[i].file;
|
2013-11-19 00:52:46 +01:00
|
|
|
uint64_t current_time = (uint64_t) time(NULL);
|
2013-10-18 01:53:29 +02:00
|
|
|
|
2013-11-19 04:22:18 +01:00
|
|
|
/* If file transfer has timed out kill transfer and send kill control */
|
|
|
|
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = file_senders[i].toxwin->chatwin;
|
2013-11-19 22:00:24 +01:00
|
|
|
|
|
|
|
if (ctx != NULL) {
|
|
|
|
wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname);
|
2013-11-29 01:45:28 +01:00
|
|
|
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
2013-11-19 22:00:24 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
2013-11-19 04:22:18 +01:00
|
|
|
close_file_sender(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-20 13:47:19 +01:00
|
|
|
while (true) {
|
2013-11-29 16:14:59 +01:00
|
|
|
if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece,
|
2013-11-29 11:30:40 +01:00
|
|
|
file_senders[i].piecelen) == -1)
|
2013-11-15 01:34:40 +01:00
|
|
|
break;
|
2013-10-10 10:52:05 +02:00
|
|
|
|
2013-11-19 04:22:18 +01:00
|
|
|
file_senders[i].timestamp = current_time;
|
|
|
|
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_file_data_size(m, friendnum), fp);
|
2013-10-10 10:52:05 +02:00
|
|
|
|
|
|
|
if (file_senders[i].piecelen == 0) {
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = file_senders[i].toxwin->chatwin;
|
2013-11-19 22:00:24 +01:00
|
|
|
|
|
|
|
if (ctx != NULL) {
|
|
|
|
wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname);
|
2013-11-29 01:45:28 +01:00
|
|
|
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
2013-11-19 22:00:24 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
|
2013-11-17 21:45:26 +01:00
|
|
|
close_file_sender(i);
|
2013-11-15 01:34:40 +01:00
|
|
|
break;
|
2013-10-10 10:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:45:29 +01:00
|
|
|
void exit_toxic(Tox *m)
|
2013-11-29 04:28:40 +01:00
|
|
|
{
|
2014-02-21 06:45:29 +01:00
|
|
|
store_data(m, DATA_FILE);
|
|
|
|
|
2013-11-29 04:28:40 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max_file_senders_index; ++i) {
|
2013-11-29 07:30:10 +01:00
|
|
|
if (file_senders[i].active)
|
|
|
|
fclose(file_senders[i].file);
|
2013-11-29 04:28:40 +01:00
|
|
|
}
|
|
|
|
|
2013-09-12 00:07:26 +02:00
|
|
|
free(DATA_FILE);
|
|
|
|
free(SRVLIST_FILE);
|
2013-09-13 08:02:49 +02:00
|
|
|
free(prompt->stb);
|
2013-12-06 04:55:14 +01:00
|
|
|
free(prompt->promptbuf);
|
2013-09-11 06:12:03 +02:00
|
|
|
tox_kill(m);
|
|
|
|
endwin();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:45:29 +01:00
|
|
|
static void do_toxic(Tox *m, ToxWindow *prompt)
|
|
|
|
{
|
|
|
|
do_connection(m, prompt);
|
|
|
|
draw_active_window(m);
|
2014-02-21 07:57:13 +01:00
|
|
|
do_file_senders(m);
|
2014-02-21 06:45:29 +01:00
|
|
|
|
2014-02-21 07:57:13 +01:00
|
|
|
/* main tox-core loop */
|
2014-02-21 06:45:29 +01:00
|
|
|
tox_do(m);
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
char *user_config_dir = get_user_config_dir();
|
|
|
|
int config_err = 0;
|
|
|
|
|
|
|
|
f_loadfromfile = 1;
|
|
|
|
int f_flag = 0;
|
|
|
|
int i = 0;
|
2013-12-02 15:02:34 +01:00
|
|
|
int f_use_ipv4 = 0;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2014-02-22 03:21:12 +01:00
|
|
|
// Make sure all written files are read/writeable only by the current user.
|
|
|
|
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
if (argv[i] == NULL)
|
|
|
|
break;
|
|
|
|
else if (argv[i][0] == '-') {
|
|
|
|
if (argv[i][1] == 'f') {
|
|
|
|
if (argv[i + 1] != NULL)
|
|
|
|
DATA_FILE = strdup(argv[i + 1]);
|
|
|
|
else
|
|
|
|
f_flag = -1;
|
|
|
|
} else if (argv[i][1] == 'n') {
|
|
|
|
f_loadfromfile = 0;
|
2013-12-02 15:02:34 +01:00
|
|
|
} else if (argv[i][1] == '4') {
|
|
|
|
f_use_ipv4 = 1;
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 11:21:38 +02:00
|
|
|
config_err = create_user_config_dir(user_config_dir);
|
2013-08-16 19:11:09 +02:00
|
|
|
if (DATA_FILE == NULL ) {
|
|
|
|
if (config_err) {
|
|
|
|
DATA_FILE = strdup("data");
|
|
|
|
} else {
|
|
|
|
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
2013-09-14 11:21:38 +02:00
|
|
|
if (DATA_FILE != NULL) {
|
2013-09-09 06:56:47 +02:00
|
|
|
strcpy(DATA_FILE, user_config_dir);
|
|
|
|
strcat(DATA_FILE, CONFIGDIR);
|
|
|
|
strcat(DATA_FILE, "data");
|
2013-09-12 00:07:26 +02:00
|
|
|
} else {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
2013-09-12 00:07:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-09 06:56:47 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 11:21:38 +02:00
|
|
|
|
|
|
|
if (config_err) {
|
|
|
|
SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers");
|
|
|
|
} else {
|
|
|
|
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
|
|
|
|
if (SRVLIST_FILE != NULL) {
|
|
|
|
strcpy(SRVLIST_FILE, user_config_dir);
|
|
|
|
strcat(SRVLIST_FILE, CONFIGDIR);
|
|
|
|
strcat(SRVLIST_FILE, "DHTservers");
|
|
|
|
} else {
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
free(user_config_dir);
|
|
|
|
|
|
|
|
init_term();
|
2013-12-02 15:02:34 +01:00
|
|
|
Tox *m = init_tox(f_use_ipv4);
|
2013-09-11 06:02:27 +02:00
|
|
|
|
|
|
|
if (m == NULL) {
|
2013-09-11 01:34:29 +02:00
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "Failed to initialize network. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-11 01:34:29 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
prompt = init_windows(m);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
if (f_loadfromfile)
|
|
|
|
load_data(m, DATA_FILE);
|
|
|
|
|
|
|
|
if (f_flag == -1) {
|
2013-09-05 06:47:33 +02:00
|
|
|
attron(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
|
|
|
|
"defaulting to 'data' for a keyfile...\n");
|
2013-09-05 06:47:33 +02:00
|
|
|
attroff(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-03 21:12:02 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 19:21:36 +02:00
|
|
|
if (config_err) {
|
2013-09-05 06:47:33 +02:00
|
|
|
attron(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(prompt->window, "Unable to determine configuration directory.\n"
|
|
|
|
"defaulting to 'data' for a keyfile...\n");
|
2013-09-05 06:47:33 +02:00
|
|
|
attroff(COLOR_PAIR(RED) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
prompt_init_statusbar(prompt, m);
|
2014-02-08 00:44:10 +01:00
|
|
|
sort_friendlist_index(m);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2014-02-21 06:45:29 +01:00
|
|
|
while (true)
|
|
|
|
do_toxic(m, prompt);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
return 0;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|