mirror of
https://github.com/Tha14/toxic.git
synced 2025-12-06 22:56:35 +01:00
Mostly finished with new API port
- File transfers currently don't support pausing/resuming - Avatars are not yet done
This commit is contained in:
119
src/toxic.c
119
src/toxic.c
@@ -49,7 +49,7 @@
|
||||
#include "friendlist.h"
|
||||
#include "prompt.h"
|
||||
#include "misc_tools.h"
|
||||
#include "file_senders.h"
|
||||
#include "file_transfers.h"
|
||||
#include "line_info.h"
|
||||
#include "settings.h"
|
||||
#include "log.h"
|
||||
@@ -78,6 +78,8 @@ char *BLOCK_FILE = NULL;
|
||||
ToxWindow *prompt = NULL;
|
||||
|
||||
#define AUTOSAVE_FREQ 60
|
||||
#define MIN_PASSWORD_LEN 6
|
||||
#define MAX_PASSWORD_LEN 64
|
||||
|
||||
struct Winthread Winthread;
|
||||
struct cqueue_thread cqueue_thread;
|
||||
@@ -85,6 +87,7 @@ struct audio_thread audio_thread;
|
||||
struct arg_opts arg_opts;
|
||||
struct user_settings *user_settings = NULL;
|
||||
|
||||
|
||||
static struct user_password {
|
||||
bool data_is_encrypted;
|
||||
char pass[MAX_PASSWORD_LEN + 1];
|
||||
@@ -120,7 +123,6 @@ void exit_toxic_success(Tox *m)
|
||||
{
|
||||
store_data(m, DATA_FILE);
|
||||
memset(&user_password, 0, sizeof(struct user_password));
|
||||
close_all_file_senders(m);
|
||||
kill_all_windows(m);
|
||||
terminate_notify();
|
||||
|
||||
@@ -356,43 +358,10 @@ int init_connection(Tox *m)
|
||||
return 4;
|
||||
}
|
||||
|
||||
#define TRY_CONNECT 10 /* Seconds between connection attempts when DHT is not connected */
|
||||
|
||||
static void do_connection(Tox *m, ToxWindow *prompt)
|
||||
{
|
||||
if (arg_opts.no_connect == 1)
|
||||
return;
|
||||
|
||||
static int conn_err = 0;
|
||||
static bool was_connected = false;
|
||||
static uint64_t last_conn_try = 0;
|
||||
uint64_t curtime = get_unix_time();
|
||||
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);
|
||||
} else if (was_connected && !is_connected) {
|
||||
was_connected = false;
|
||||
prompt_update_connectionstatus(prompt, was_connected);
|
||||
} 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)
|
||||
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Auto-connect failed with error code %d", conn_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void load_friendlist(Tox *m)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t numfriends = tox_count_friendlist(m);
|
||||
size_t i;
|
||||
size_t numfriends = tox_self_get_friend_list_size(m);
|
||||
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist_onFriendAdded(NULL, m, i, false);
|
||||
@@ -400,10 +369,6 @@ static void load_friendlist(Tox *m)
|
||||
sort_friendlist_index();
|
||||
}
|
||||
|
||||
|
||||
#define MIN_PASSWORD_LEN 6
|
||||
#define MAX_PASSWORD_LEN 64
|
||||
|
||||
/* return length of password on success, 0 on failure */
|
||||
static int password_prompt(char *buf, int size)
|
||||
{
|
||||
@@ -547,6 +512,7 @@ int store_data(Tox *m, const char *path)
|
||||
|
||||
static void init_tox_callbacks(Tox *m)
|
||||
{
|
||||
tox_callback_self_connection_status(m, prompt_onSelfConnectionChange, NULL);
|
||||
tox_callback_friend_connection_status(m, on_connectionchange, NULL);
|
||||
tox_callback_friend_typing(m, on_typing_change, NULL);
|
||||
tox_callback_friend_request(m, on_request, NULL);
|
||||
@@ -560,24 +526,25 @@ static void init_tox_callbacks(Tox *m)
|
||||
tox_callback_group_action(m, on_groupaction, NULL);
|
||||
tox_callback_group_namelist_change(m, on_group_namelistchange, NULL);
|
||||
tox_callback_group_title(m, on_group_titlechange, NULL);
|
||||
tox_callback_file_chunk_request(m, on_file_sendrequest, NULL);
|
||||
tox_callback_file_recv(m, on_file_recv, NULL);
|
||||
tox_callback_file_chunk_request(m, on_file_chunk_request, NULL);
|
||||
tox_callback_file_recv_control(m, on_file_control, NULL);
|
||||
tox_callback_file_recv_chunk(m, on_file_data, NULL);
|
||||
tox_callback_file_recv_chunk(m, on_file_recv_chunk, NULL);
|
||||
}
|
||||
|
||||
static void init_tox_options(Tox_Options *tox_opts)
|
||||
static void init_tox_options(struct Tox_Options *tox_opts)
|
||||
{
|
||||
tox_opts->ipv6_enabled = !arg_opts.use_ipv4;
|
||||
tox_opts->udp_enabled = !arg_opts.force_tcp;
|
||||
tox_opts->proxy_type = arg_opts.proxy_type;
|
||||
|
||||
if (!tox_opts.ipv6_enabled)
|
||||
if (!tox_opts->ipv6_enabled)
|
||||
queue_init_message("Forcing IPv4 connection");
|
||||
|
||||
if (tox_opts->proxy_type != TOX_PROXY_TYPE_NONE) {
|
||||
tox_opts->proxy_port = arg_opts.proxy_port;
|
||||
snprintf(tox_opts->proxy_address, sizeof(tox_opts->proxy_address), "%s", arg_opts.proxy_address);
|
||||
const char *ps = tox_opts.proxy_type == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP";
|
||||
tox_opts->proxy_host = arg_opts.proxy_address;
|
||||
const char *ps = tox_opts->proxy_type == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP";
|
||||
|
||||
char tmp[48];
|
||||
snprintf(tmp, sizeof(tmp), "Using %s proxy %s : %d", ps, arg_opts.proxy_address, arg_opts.proxy_port);
|
||||
@@ -594,14 +561,19 @@ static void init_tox_options(Tox_Options *tox_opts)
|
||||
}
|
||||
}
|
||||
|
||||
static Tox *load_tox(char *data_path, Tox_Options *tox_opts, TOX_ERR_NEW *err)
|
||||
/* Returns a new Tox object on success.
|
||||
* If object fails to initialize the toxic process will terminate.
|
||||
*/
|
||||
static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts)
|
||||
{
|
||||
Tox *m = NULL;
|
||||
|
||||
FILE *fp = fopen(data_path, "rb");
|
||||
|
||||
if (fp != NULL) {
|
||||
off_t len = file_size(data_path);
|
||||
|
||||
if (len == -1) {
|
||||
if (len == 0) {
|
||||
fclose(fp);
|
||||
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);
|
||||
}
|
||||
@@ -634,7 +606,7 @@ static Tox *load_tox(char *data_path, Tox_Options *tox_opts, TOX_ERR_NEW *err)
|
||||
if (!arg_opts.unencrypt_data)
|
||||
user_password.data_is_encrypted = true;
|
||||
|
||||
int pwlen = 0;
|
||||
size_t pwlen = 0;
|
||||
system("clear"); // TODO: is this portable?
|
||||
printf("Enter password (q to quit) ");
|
||||
|
||||
@@ -652,28 +624,27 @@ static Tox *load_tox(char *data_path, Tox_Options *tox_opts, TOX_ERR_NEW *err)
|
||||
continue;
|
||||
}
|
||||
|
||||
m = tox_encrypted_new(tox_opts, (uint8_t *) buf, len, user_password.pass, pwlen, err);
|
||||
TOX_ERR_ENCRYPTED_NEW enc_err;
|
||||
m = tox_encrypted_new(tox_opts, (uint8_t *) buf, len, (uint8_t *) user_password.pass, pwlen, &enc_err);
|
||||
|
||||
if (err == TOX_ERR_NEW_OK) {
|
||||
if (enc_err == TOX_ERR_ENCRYPTED_NEW_OK) {
|
||||
break;
|
||||
} else if (err == TOX_ERR_NEW_LOAD_DECRYPTION_FAILED) {
|
||||
} else if (enc_err == TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED) {
|
||||
system("clear");
|
||||
sleep(1);
|
||||
printf("Invalid password. Try again. ");
|
||||
} else {
|
||||
return NULL;
|
||||
exit_toxic_err("tox_encrypted_new() failed", enc_err);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m = tox_new(tox_opts, (uint8_t *) buf, len, err);
|
||||
TOX_ERR_NEW err;
|
||||
m = tox_new(tox_opts, (uint8_t *) buf, len, &err);
|
||||
|
||||
if (err != TOX_ERR_NEW_OK)
|
||||
return NULL;
|
||||
exit_toxic_err("tox_new() failed", err);
|
||||
}
|
||||
|
||||
load_friendlist(m);
|
||||
load_blocklist(BLOCK_FILE);
|
||||
|
||||
free(buf);
|
||||
fclose(fp);
|
||||
} else {
|
||||
@@ -681,10 +652,11 @@ static Tox *load_tox(char *data_path, Tox_Options *tox_opts, TOX_ERR_NEW *err)
|
||||
if (file_exists(data_path))
|
||||
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);
|
||||
|
||||
m = tox_new(tox_opts, NULL, 0, err);
|
||||
TOX_ERR_NEW err;
|
||||
m = tox_new(tox_opts, NULL, 0, &err);
|
||||
|
||||
if (err != TOX_ERR_NEW_OK)
|
||||
return NULL;
|
||||
exit_toxic_err("tox_new() failed", err);
|
||||
|
||||
if (store_data(m, data_path) == -1)
|
||||
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);
|
||||
@@ -693,18 +665,22 @@ static Tox *load_tox(char *data_path, Tox_Options *tox_opts, TOX_ERR_NEW *err)
|
||||
return m;
|
||||
}
|
||||
|
||||
static Tox *load_toxic(char *data_path, TOX_ERR_NEW *err)
|
||||
static Tox *load_toxic(char *data_path)
|
||||
{
|
||||
Tox_Options tox_opts;
|
||||
struct Tox_Options tox_opts;
|
||||
init_tox_options(&tox_opts);
|
||||
|
||||
Tox *m = load_tox(data_path, &tox_opts, err);
|
||||
Tox *m = load_tox(data_path, &tox_opts);
|
||||
|
||||
if (err != TOX_ERR_NEW_OK)
|
||||
return NULL;
|
||||
if (m == NULL)
|
||||
exit_toxic_err("load_tox() failed", FATALERR_TOX_INIT);
|
||||
|
||||
init_tox_callbacks(m);
|
||||
tox_self_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL);
|
||||
load_friendlist(m);
|
||||
load_blocklist(BLOCK_FILE);
|
||||
|
||||
if (tox_self_get_name_size(m) == 0)
|
||||
tox_self_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL);
|
||||
|
||||
return m;
|
||||
}
|
||||
@@ -713,9 +689,6 @@ static void do_toxic(Tox *m, ToxWindow *prompt)
|
||||
{
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
|
||||
do_connection(m, prompt);
|
||||
do_file_senders(m);
|
||||
|
||||
if (arg_opts.no_connect == 0)
|
||||
tox_iterate(m); /* main toxcore loop */
|
||||
|
||||
@@ -1045,11 +1018,7 @@ int main(int argc, char *argv[])
|
||||
queue_init_message("X failed to initialize");
|
||||
#endif
|
||||
|
||||
TOX_ERR_NEW err;
|
||||
Tox *m = load_toxic(DATA_FILE, &err);
|
||||
|
||||
if (m == NULL || err != TOX_ERR_NEW_OK)
|
||||
exit_toxic_err("Tox instance failed to initialize", err);
|
||||
Tox *m = load_toxic(DATA_FILE);
|
||||
|
||||
if (arg_opts.encrypt_data && !datafile_exists)
|
||||
arg_opts.encrypt_data = 0;
|
||||
|
||||
Reference in New Issue
Block a user