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

490 lines
12 KiB
C
Raw Normal View History

/*
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef SIGWINCH
#define SIGWINCH 28
#endif
#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-08-28 19:04:54 +02:00
#ifdef _WIN32
#include <direct.h>
#include <winsock2.h>
#include <ws2tcpip.h>
2013-08-08 16:00:12 +02:00
#else
#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
#include <tox/tox.h>
#include "configdir.h"
#include "toxic_windows.h"
2013-08-13 00:50:43 +02:00
#include "friendlist.h"
#include "prompt.h"
#include "misc_tools.h"
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;
char *SRVLIST_FILE = NULL;
ToxWindow *prompt = NULL;
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-08-07 00:27:51 +02:00
static void init_term()
{
2013-08-16 19:11:09 +02:00
/* Setup terminal */
signal(SIGWINCH, on_window_resize);
#if HAVE_WIDECHAR
if (setlocale(LC_ALL, "") == NULL) {
fprintf(stderr, "Could not set your locale, plese check your locale settings or"
"disable wide char support\n");
exit(EXIT_FAILURE);
}
#endif
2013-08-16 19:11:09 +02:00
initscr();
cbreak();
keypad(stdscr, 1);
noecho();
timeout(100);
if (has_colors()) {
start_color();
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-08-23 23:03:44 +02:00
static Tox *init_tox()
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
/* Init core */
Tox *m = tox_new(TOX_ENABLE_IPV6_DEFAULT);
2013-10-10 10:52:05 +02:00
if (m == NULL)
return NULL;
2013-08-16 19:11:09 +02:00
/* Callbacks */
tox_callback_connectionstatus(m, on_connectionchange, NULL);
2013-08-24 00:21:01 +02:00
tox_callback_friendrequest(m, on_request, NULL);
tox_callback_friendmessage(m, on_message, NULL);
tox_callback_namechange(m, on_nickchange, NULL);
tox_callback_userstatus(m, on_statuschange, NULL);
tox_callback_statusmessage(m, on_statusmessagechange, NULL);
2013-08-24 00:21:01 +02:00
tox_callback_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-10-10 10:52:05 +02:00
tox_callback_file_sendrequest(m, on_file_sendrequest, NULL);
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-08-24 00:21:01 +02:00
tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
2013-08-28 19:04:54 +02:00
#elif defined(_WIN32)
2013-08-24 00:21:01 +02:00
tox_setname(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-08-24 00:21:01 +02:00
tox_setname(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-08-24 00:21:01 +02:00
tox_setname(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;
}
#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
#define SERVERLEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7)
2013-08-10 21:46:29 +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];
int serverlist_load()
2013-08-10 21:46:29 +02:00
{
FILE *fp = NULL;
fp = fopen(SRVLIST_FILE, "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) && linecnt < MAXSERVERS) {
if (strlen(line) > MINLINE) {
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-08-16 19:11:09 +02:00
}
if (linecnt < 1) {
fclose(fp);
return 2;
}
2013-08-10 21:46:29 +02:00
fclose(fp);
return 0;
}
2013-08-16 19:11:09 +02:00
int init_connection_helper(Tox *m, int linenumber)
{
return tox_bootstrap_from_address(m, servers[linenumber], TOX_ENABLE_IPV6_DEFAULT,
ports[linenumber], keys[linenumber]);
}
2013-08-16 19:11:09 +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
*/
static uint8_t init_connection_serverlist_loaded = 0;
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;
if (!linecnt)
return 4;
res = 6;
int linenumber;
for(linenumber = 0; linenumber < linecnt; linenumber++)
if (init_connection_helper(m, linenumber))
res = 0;
return res;
}
2013-08-16 19:11:09 +02:00
/* empty serverlist file */
return 5;
2013-08-10 21:46:29 +02:00
}
2013-08-23 23:03:44 +02:00
static void do_tox(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;
2013-08-23 23:03:44 +02:00
if (!dht_on && !tox_isconnected(m) && !(conn_try++ % 100)) {
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");
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);
}
2013-08-23 23:03:44 +02:00
} else if (!dht_on && tox_isconnected(m)) {
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-08-16 19:11:09 +02:00
wprintw(prompt->window, "\nDHT connected.\n");
2013-08-23 23:03:44 +02:00
} else if (dht_on && !tox_isconnected(m)) {
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-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-08-16 19:11:09 +02:00
2013-08-23 23:03:44 +02:00
tox_do(m);
}
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
{
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-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
2013-08-23 23:03:44 +02:00
tox_save(m, 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 (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");
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");
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-08-16 19:11:09 +02:00
2013-09-09 21:33:15 +02:00
uint8_t name[TOX_MAX_NAME_LENGTH];
while (tox_getname(m, i, name) != -1) {
2013-08-15 12:11:48 +02:00
on_friendadded(m, i);
2013-08-23 23:03:44 +02:00
i++;
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);
exit(EXIT_FAILURE);
2013-08-15 12:11:48 +02:00
}
}
}
2013-10-10 10:52:05 +02:00
void do_file_senders(Tox *m)
{
int i;
for (i = 0; i < NUM_FILE_SENDERS; ++i) {
if (file_senders[i].file == NULL)
continue;
while (true) {
if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenum,
file_senders[i].nextpiece, file_senders[i].piecelen))
return;
2013-10-11 10:42:30 +02:00
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m,
file_senders[i].friendnum), file_senders[i].file);
2013-10-10 10:52:05 +02:00
if (file_senders[i].piecelen == 0) {
fclose(file_senders[i].file);
file_senders[i].file = NULL;
2013-10-11 10:42:30 +02:00
tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenum,
3, 0, 0);
2013-10-11 06:23:39 +02:00
/* TODO: move this alert to chat window */
2013-10-11 10:42:30 +02:00
wprintw(prompt->window, "File '%s' successfuly sent to %s.\n",
file_senders[i].filename, file_senders[i].friendname);
2013-10-10 10:52:05 +02:00
return;
}
}
}
}
void exit_toxic(Tox *m)
{
store_data(m, DATA_FILE);
2013-09-12 00:07:26 +02:00
free(DATA_FILE);
free(SRVLIST_FILE);
free(prompt->stb);
tox_kill(m);
endwin();
exit(EXIT_SUCCESS);
}
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;
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;
}
}
}
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) {
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
}
}
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-08-23 23:03:44 +02:00
Tox *m = init_tox();
if (m == NULL) {
endwin();
fprintf(stderr, "Failed to initialize network. Aborting...\n");
exit(EXIT_FAILURE);
}
prompt = init_windows(m);
2013-08-16 19:11:09 +02:00
if (f_loadfromfile)
load_data(m, DATA_FILE);
if (f_flag == -1) {
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");
attroff(COLOR_PAIR(RED) | A_BOLD);
2013-08-03 21:12:02 +02:00
}
if (config_err) {
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");
attroff(COLOR_PAIR(RED) | A_BOLD);
2013-08-16 19:11:09 +02:00
}
prompt_init_statusbar(prompt, m);
2013-08-16 19:11:09 +02:00
while (true) {
do_tox(m, prompt);
2013-10-10 10:52:05 +02:00
do_file_senders(m);
2013-08-16 19:11:09 +02:00
draw_active_window(m);
}
2013-08-16 19:11:09 +02:00
exit_toxic(m);
2013-08-16 19:11:09 +02:00
return 0;
}