Merge branch 'spacefrogg-master'

This commit is contained in:
Håvard Pettersson 2015-08-26 20:08:26 +02:00
commit d27f817777
11 changed files with 335 additions and 272 deletions

View File

@ -70,9 +70,10 @@ twc_bootstrap_tox(Tox *tox, const char *address, uint16_t port,
{
uint8_t binary_key[TOX_ADDRESS_SIZE];
twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key);
TOX_ERR_BOOTSTRAP err;
int result = tox_bootstrap_from_address(tox, address, port,
binary_key);
int result = tox_bootstrap(tox, address, port,
binary_key, &err);
return result;
}

View File

@ -103,7 +103,10 @@ struct t_twc_chat *
twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number)
{
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
tox_get_client_id(profile->tox, friend_number, client_id);
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_number, client_id, &err);
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
return NULL;
char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name);

View File

@ -142,14 +142,14 @@ enum TWC_FRIEND_MATCH
enum TWC_FRIEND_MATCH
twc_match_friend(struct t_twc_profile *profile, const char *search_string)
{
uint32_t friend_count = tox_count_friendlist(profile->tox);
int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count);
tox_get_friendlist(profile->tox, friend_numbers, friend_count);
uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_self_get_friend_list(profile->tox, friend_numbers);
int32_t match = TWC_FRIEND_MATCH_NOMATCH;
char *endptr;
unsigned long friend_number = strtoul(search_string, &endptr, 10);
uint32_t friend_number = (uint32_t)strtoul(search_string, &endptr, 10);
if (endptr == search_string + strlen(search_string)
&& tox_friend_exists(profile->tox, friend_number))
return friend_number;
@ -162,11 +162,13 @@ twc_match_friend(struct t_twc_profile *profile, const char *search_string)
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
tox_get_client_id(profile->tox, friend_numbers[i], tox_id);
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
if (tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL))
{
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
if (weechat_strcasecmp(hex_id, search_string) == 0)
return friend_numbers[i];
if (weechat_strcasecmp(hex_id, search_string) == 0)
return friend_numbers[i];
}
}
char *name = twc_get_name_nt(profile->tox, friend_numbers[i]);
@ -227,9 +229,9 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
// /friend or /friend list
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{
size_t friend_count = tox_count_friendlist(profile->tox);
int32_t friend_numbers[friend_count];
tox_get_friendlist(profile->tox, friend_numbers, friend_count);
size_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t friend_numbers[friend_count];
tox_self_get_friend_list(profile->tox, friend_numbers);
if (friend_count == 0)
{
@ -245,7 +247,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
for (size_t i = 0; i < friend_count; ++i)
{
int32_t friend_number = friend_numbers[i];
uint32_t friend_number = friend_numbers[i];
char *name = twc_get_name_nt(profile->tox, friend_number);
char *hex_address = twc_get_friend_id_short(profile->tox,
friend_number);
@ -306,7 +308,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
if (friend_number == TWC_FRIEND_MATCH_AMBIGUOUS)
fail = true;
else if (friend_number != TWC_FRIEND_MATCH_NOMATCH)
fail = tox_del_friend(profile->tox, friend_number) != 0;
fail = !tox_friend_delete(profile->tox, friend_number, NULL);
if (fail)
{
@ -318,12 +320,13 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
}
}
TOX_ERR_FRIEND_ADD result = tox_add_friend(profile->tox,
(uint8_t *)address,
(uint8_t *)message,
strlen(message));
TOX_ERR_FRIEND_ADD err;
(void)tox_friend_add(profile->tox,
(uint8_t *)address,
(uint8_t *)message,
strlen(message), &err);
switch (result)
switch (err)
{
case TOX_ERR_FRIEND_ADD_OK:
weechat_printf(profile->buffer,
@ -363,7 +366,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
default:
weechat_printf(profile->buffer,
"%sCould not add friend (unknown error %d).",
weechat_prefix("error"), result);
weechat_prefix("error"), err);
break;
}
@ -377,7 +380,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]);
char *name = twc_get_name_nt(profile->tox, friend_number);
if (tox_del_friend(profile->tox, friend_number) == 0)
if (tox_friend_delete(profile->tox, friend_number, NULL) == 0)
{
weechat_printf(profile->buffer,
"%sRemoved %s from friend list.",
@ -406,20 +409,39 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
if (weechat_strcasecmp(argv[2], "all") == 0)
{
size_t index;
size_t count = 0;
struct t_twc_list_item *item;
twc_list_foreach(profile->friend_requests, index, item)
{
if (accept)
twc_friend_request_accept(item->friend_request);
{
if (twc_friend_request_accept(item->friend_request))
{
++count;
}
else
{
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(item->friend_request->tox_id,
TOX_PUBLIC_KEY_SIZE,
hex_address);
weechat_printf(profile->buffer,
"%sCould not accept friend request from %s",
weechat_prefix("error"), hex_address);
}
}
else
{
twc_friend_request_remove(item->friend_request);
++count;
}
}
weechat_printf(profile->buffer,
"%s%s %d friend requests.",
weechat_prefix("network"),
accept ? "Accepted" : "Declined",
index);
count);
return WEECHAT_RC_OK;
}
@ -441,18 +463,30 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
hex_address);
if (accept)
twc_friend_request_accept(request);
{
if (twc_friend_request_accept(request))
{
weechat_printf(profile->buffer,
"%sCould not accept friend request from %s",
weechat_prefix("error"), hex_address);
}
else
{
weechat_printf(profile->buffer,
"%sAccepted friend request from %s.",
weechat_prefix("network"), hex_address);
}
}
else
{
twc_friend_request_remove(request);
weechat_printf(profile->buffer,
"%sDeclined friend request from %s.",
weechat_prefix("network"), hex_address);
}
twc_friend_request_free(request);
weechat_printf(profile->buffer,
"%s%s friend request from %s.",
weechat_prefix("network"),
accept ? "Accepted" : "Declined",
hex_address);
return WEECHAT_RC_OK;
}
}
@ -693,7 +727,7 @@ twc_cmd_myid(void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE_LOADED(profile);
uint8_t address[TOX_ADDRESS_SIZE];
tox_get_address(profile->tox, address);
tox_self_get_address(profile->tox, address);
char address_str[TOX_ADDRESS_SIZE * 2 + 1];
twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str);
@ -720,15 +754,29 @@ twc_cmd_name(void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
char *name = argv_eol[1];
const char *name = argv_eol[1];
int result = tox_set_name(profile->tox, (uint8_t *)name, strlen(name));
if (result == -1)
TOX_ERR_SET_INFO err;
tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), &err);
if (err != TOX_ERR_SET_INFO_OK)
{
char *err_msg;
switch (err)
{
case TOX_ERR_SET_INFO_NULL:
err_msg = "no name given";
break;
case TOX_ERR_SET_INFO_TOO_LONG:
err_msg = "name too long";
break;
default:
err_msg = "unknown error";
break;
}
weechat_printf(profile->buffer,
"%s%s",
"%s%s%s",
weechat_prefix("error"),
"Could not change name.");
"Could not change name: ", err_msg);
return WEECHAT_RC_OK;
}
@ -789,8 +837,8 @@ twc_cmd_nospam(void *data, struct t_gui_buffer *buffer,
new_nospam = random();
}
uint32_t old_nospam = tox_get_nospam(profile->tox);
tox_set_nospam(profile->tox, new_nospam);
uint32_t old_nospam = tox_self_get_nospam(profile->tox);
tox_self_set_nospam(profile->tox, new_nospam);
weechat_printf(profile->buffer,
"%snew nospam has been set; this changes your Tox ID! To "
@ -887,7 +935,7 @@ twc_cmd_status(void *data, struct t_gui_buffer *buffer,
else
return WEECHAT_RC_ERROR;
tox_set_user_status(profile->tox, status);
tox_self_set_status(profile->tox, status);
weechat_bar_item_update("away");
return WEECHAT_RC_OK;
@ -906,15 +954,29 @@ twc_cmd_statusmsg(void *data, struct t_gui_buffer *buffer,
char *message = argc > 1 ? argv_eol[1] : " ";
int result = tox_set_status_message(profile->tox,
(uint8_t *)message,
strlen(message));
if (result == -1)
TOX_ERR_SET_INFO err;
tox_self_set_status_message(profile->tox,
(uint8_t *)message,
strlen(message), &err);
if (err != TOX_ERR_SET_INFO_OK)
{
char *err_msg;
switch (err)
{
case TOX_ERR_SET_INFO_NULL:
err_msg = "no status given";
break;
case TOX_ERR_SET_INFO_TOO_LONG:
err_msg = "status too long";
break;
default:
err_msg = "unknown error";
break;
}
weechat_printf(profile->buffer,
"%s%s",
"%s%s%s",
weechat_prefix("error"),
"Could not set status message.");
"Could not set status message: ", err_msg);
}
return WEECHAT_RC_OK;

View File

@ -57,9 +57,9 @@ twc_completion_friend(void *data,
if (!profile)
return WEECHAT_RC_OK;
uint32_t friend_count = tox_count_friendlist(profile->tox);
int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count);
tox_get_friendlist(profile->tox, friend_numbers, friend_count);
uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_self_get_friend_list(profile->tox, friend_numbers);
for (uint32_t i = 0; i < friend_count; ++i)
{
@ -68,11 +68,15 @@ twc_completion_friend(void *data,
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
tox_get_client_id(profile->tox, friend_numbers[i], tox_id);
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, &err);
if (err == TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
{
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
weechat_hook_completion_list_add(completion, hex_id, 0,
WEECHAT_LIST_POS_SORT);
weechat_hook_completion_list_add(completion, hex_id, 0,
WEECHAT_LIST_POS_SORT);
}
}
if (flags & TWC_COMPLETE_FRIEND_NAME)

View File

@ -64,11 +64,14 @@ twc_friend_request_add(struct t_twc_profile *profile,
/**
* Accept a friend request. Remove and free the request.
*/
void
bool
twc_friend_request_accept(struct t_twc_friend_request *request)
{
tox_add_friend_norequest(request->profile->tox, request->tox_id);
TOX_ERR_FRIEND_ADD err;
tox_friend_add_norequest(request->profile->tox, request->tox_id, &err);
twc_friend_request_remove(request);
return err == TOX_ERR_FRIEND_ADD_OK;
}
/**

View File

@ -40,7 +40,7 @@ twc_friend_request_add(struct t_twc_profile *profile,
const uint8_t *client_id,
const char *message);
void
bool
twc_friend_request_accept(struct t_twc_friend_request *request);
void

View File

@ -40,9 +40,12 @@ twc_bar_item_away(void *data,
if (!profile || !(profile->tox))
return NULL;
char *status = NULL;;
switch (tox_get_self_user_status(profile->tox))
char *status;
switch (tox_self_get_status(profile->tox))
{
case TOX_USER_STATUS_NONE:
status = NULL;
break;
case TOX_USER_STATUS_BUSY:
status = strdup("busy");
break;

View File

@ -75,7 +75,7 @@ twc_message_queue_add_friend_message(struct t_twc_profile *profile,
// flush if friend is online
if (profile->tox
&& tox_get_friend_connection_status(profile->tox, friend_number) == 1)
&& (tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE))
twc_message_queue_flush_friend(profile, friend_number);
}
@ -96,24 +96,17 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile,
struct t_twc_queued_message *queued_message = item->queued_message;
// TODO: store and deal with message IDs
uint32_t rc = 0;
switch(queued_message->message_type)
{
case TWC_MESSAGE_TYPE_MESSAGE:
rc = tox_send_message(profile->tox,
TOX_ERR_FRIEND_SEND_MESSAGE err;
(void)tox_friend_send_message(profile->tox,
friend_number,
queued_message->message_type == TWC_MESSAGE_TYPE_MESSAGE?
TOX_MESSAGE_TYPE_NORMAL:
TOX_MESSAGE_TYPE_ACTION,
(uint8_t *)queued_message->message,
strlen(queued_message->message));
break;
case TWC_MESSAGE_TYPE_ACTION:
rc = tox_send_action(profile->tox,
friend_number,
(uint8_t *)queued_message->message,
strlen(queued_message->message));
break;
}
strlen(queued_message->message),
&err);
if (rc == 0)
if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK)
{
// break if message send failed
break;

View File

@ -51,13 +51,13 @@ struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS];
char *
twc_profile_expanded_data_path(struct t_twc_profile *profile)
{
const char *weechat_dir = weechat_info_get ("weechat_dir", NULL);
const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE);
char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir);
char *full_path = weechat_string_replace(home_expanded, "%p", profile->name);
free(home_expanded);
const char *weechat_dir = weechat_info_get ("weechat_dir", NULL);
const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE);
char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir);
char *full_path = weechat_string_replace(home_expanded, "%p", profile->name);
free(home_expanded);
return full_path;
return full_path;
}
/**
@ -70,33 +70,33 @@ twc_profile_expanded_data_path(struct t_twc_profile *profile)
int
twc_profile_save_data_file(struct t_twc_profile *profile)
{
if (!(profile->tox))
return -1;
if (!(profile->tox))
return -1;
char *full_path = twc_profile_expanded_data_path(profile);
char *full_path = twc_profile_expanded_data_path(profile);
// create containing folder if it doesn't exist
char *rightmost_slash = strrchr(full_path, '/');
char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path);
weechat_mkdir_parents(dir_path, 0755);
free(dir_path);
// create containing folder if it doesn't exist
char *rightmost_slash = strrchr(full_path, '/');
char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path);
weechat_mkdir_parents(dir_path, 0755);
free(dir_path);
// save Tox data to a buffer
uint32_t size = tox_get_savedata_size(profile->tox);
uint8_t *data = malloc(size);
tox_get_savedata(profile->tox, data);
// save Tox data to a buffer
size_t size = tox_get_savedata_size(profile->tox);
uint8_t *data = malloc(size);
tox_get_savedata(profile->tox, data);
// save buffer to a file
FILE *file = fopen(full_path, "w");
if (file)
// save buffer to a file
FILE *file = fopen(full_path, "w");
if (file)
{
size_t saved_size = fwrite(data, sizeof(data[0]), size, file);
fclose(file);
size_t saved_size = fwrite(data, 1, size, file);
fclose(file);
return saved_size == size;
return saved_size == size;
}
return -1;
return -1;
}
/**
@ -106,12 +106,12 @@ int
twc_profile_buffer_close_callback(void *data,
struct t_gui_buffer *buffer)
{
struct t_twc_profile *profile = data;
struct t_twc_profile *profile = data;
profile->buffer = NULL;
twc_profile_unload(profile);
profile->buffer = NULL;
twc_profile_unload(profile);
return WEECHAT_RC_OK;
return WEECHAT_RC_OK;
}
/**
@ -120,7 +120,7 @@ twc_profile_buffer_close_callback(void *data,
void
twc_profile_init()
{
twc_profiles = twc_list_new();
twc_profiles = twc_list_new();
}
/**
@ -129,30 +129,30 @@ twc_profile_init()
struct t_twc_profile *
twc_profile_new(const char *name)
{
struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile));
profile->name = strdup(name);
struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile));
profile->name = strdup(name);
// add to profile list
twc_list_item_new_data_add(twc_profiles, profile);
// add to profile list
twc_list_item_new_data_add(twc_profiles, profile);
// set up internal vars
profile->tox = NULL;
profile->buffer = NULL;
profile->tox_do_timer = NULL;
profile->tox_online = false;
// set up internal vars
profile->tox = NULL;
profile->buffer = NULL;
profile->tox_do_timer = NULL;
profile->tox_online = false;
profile->chats = twc_list_new();
profile->friend_requests = twc_list_new();
profile->group_chat_invites = twc_list_new();
profile->message_queues = weechat_hashtable_new(32,
WEECHAT_HASHTABLE_INTEGER,
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
profile->chats = twc_list_new();
profile->friend_requests = twc_list_new();
profile->group_chat_invites = twc_list_new();
profile->message_queues = weechat_hashtable_new(32,
WEECHAT_HASHTABLE_INTEGER,
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
// set up config
twc_config_init_profile(profile);
// set up config
twc_config_init_profile(profile);
return profile;
return profile;
}
/**
@ -162,32 +162,32 @@ void
twc_profile_set_options(struct Tox_Options *options,
struct t_twc_profile *profile)
{
tox_options_default(options);
tox_options_default(options);
const char *proxy_host =
TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS);
if (proxy_host)
options->proxy_host = proxy_host;
const char *proxy_host =
TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS);
if (proxy_host)
options->proxy_host = proxy_host;
switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE))
switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE))
{
case TWC_PROXY_NONE:
options->proxy_type = TOX_PROXY_TYPE_NONE;
break;
case TWC_PROXY_SOCKS5:
options->proxy_type = TOX_PROXY_TYPE_SOCKS5;
break;
case TWC_PROXY_HTTP:
options->proxy_type = TOX_PROXY_TYPE_HTTP;
break;
case TWC_PROXY_NONE:
options->proxy_type = TOX_PROXY_TYPE_NONE;
break;
case TWC_PROXY_SOCKS5:
options->proxy_type = TOX_PROXY_TYPE_SOCKS5;
break;
case TWC_PROXY_HTTP:
options->proxy_type = TOX_PROXY_TYPE_HTTP;
break;
}
options->proxy_port =
TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT);
options->udp_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP);
options->ipv6_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6);
options->proxy_port =
TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT);
options->udp_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP);
options->ipv6_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6);
}
void
@ -195,54 +195,54 @@ twc_tox_new_print_error(struct t_twc_profile *profile,
struct Tox_Options *options,
TOX_ERR_NEW error)
{
switch (error)
switch (error)
{
case TOX_ERR_NEW_MALLOC:
weechat_printf(profile->buffer,
"%scould not load Tox (malloc error)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PORT_ALLOC:
weechat_printf(profile->buffer,
"%scould not load Tox (failed to allocate a port)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PROXY_BAD_TYPE:
weechat_printf(profile->buffer,
"%scould not load Tox (internal error; bad proxy type)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PROXY_BAD_HOST:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid proxy host: \"%s\")",
weechat_prefix("error"), options->proxy_host);
break;
case TOX_ERR_NEW_PROXY_BAD_PORT:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid proxy port: \"%d\")",
weechat_prefix("error"), options->proxy_port);
break;
case TOX_ERR_NEW_PROXY_NOT_FOUND:
weechat_printf(profile->buffer,
"%scould not load Tox (proxy host not found: \"%s\")",
weechat_prefix("error"), options->proxy_host);
break;
case TOX_ERR_NEW_LOAD_ENCRYPTED:
weechat_printf(profile->buffer,
"%scould not load Tox (encrypted data files are not yet supported)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid data file, some data "
"may have been loaded; use -force to try using it)",
weechat_prefix("error"));
break;
default:
weechat_printf(profile->buffer,
"%scould not load Tox (unknown error %d)",
weechat_prefix("error"), error);
break;
case TOX_ERR_NEW_MALLOC:
weechat_printf(profile->buffer,
"%scould not load Tox (malloc error)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PORT_ALLOC:
weechat_printf(profile->buffer,
"%scould not load Tox (failed to allocate a port)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PROXY_BAD_TYPE:
weechat_printf(profile->buffer,
"%scould not load Tox (internal error; bad proxy type)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_PROXY_BAD_HOST:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid proxy host: \"%s\")",
weechat_prefix("error"), options->proxy_host);
break;
case TOX_ERR_NEW_PROXY_BAD_PORT:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid proxy port: \"%d\")",
weechat_prefix("error"), options->proxy_port);
break;
case TOX_ERR_NEW_PROXY_NOT_FOUND:
weechat_printf(profile->buffer,
"%scould not load Tox (proxy host not found: \"%s\")",
weechat_prefix("error"), options->proxy_host);
break;
case TOX_ERR_NEW_LOAD_ENCRYPTED:
weechat_printf(profile->buffer,
"%scould not load Tox (encrypted data files are not yet supported)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
weechat_printf(profile->buffer,
"%scould not load Tox (invalid data file, some data "
"may have been loaded; use -force to try using it)",
weechat_prefix("error"));
break;
default:
weechat_printf(profile->buffer,
"%scould not load Tox (unknown error %d)",
weechat_prefix("error"), error);
break;
}
}
@ -258,52 +258,66 @@ twc_profile_load(struct t_twc_profile *profile)
return TWC_RC_ERROR;
if (!(profile->buffer))
{
// create main buffer
profile->buffer = weechat_buffer_new(profile->name,
NULL, NULL,
twc_profile_buffer_close_callback, profile);
if (!(profile->buffer))
return TWC_RC_ERROR;
}
{
// create main buffer
profile->buffer = weechat_buffer_new(profile->name,
NULL, NULL,
twc_profile_buffer_close_callback, profile);
if (!(profile->buffer))
return TWC_RC_ERROR;
}
weechat_printf(profile->buffer,
"%s profile %s connecting",
"%sprofile %s connecting",
weechat_prefix("network"), profile->name);
// create Tox options object
struct Tox_Options options;
twc_profile_set_options(&options, profile);
tox_options_default(&options);
//twc_profile_set_options(options, profile);
// print a proxy message
if (options.proxy_type != TOX_PROXY_TYPE_NONE)
{
weechat_printf(profile->buffer,
"%susing %s proxy %s:%d",
weechat_prefix("network"),
options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" :
TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" :
NULL,
options.proxy_host, options.proxy_port);
}
{
weechat_printf(profile->buffer,
"%susing %s proxy %s:%d",
weechat_prefix("network"),
options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" :
TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" :
NULL,
options.proxy_host, options.proxy_port);
}
// try loading data file
char *path = twc_profile_expanded_data_path(profile);
uint8_t *data;
size_t data_size = 0;
enum t_twc_rc data_rc = twc_read_file(path, &data, &data_size);
if (data_rc == TWC_RC_ERROR_MALLOC)
{
weechat_printf(profile->buffer,
"%scould not load Tox data file, aborting (malloc error)",
weechat_prefix("error"));
return TWC_RC_ERROR_MALLOC;
FILE *file = NULL;
size_t data_size;
if (!(file = fopen(path, "r")))
data_size = 0;
else {
fseek(file, 0, SEEK_END);
data_size = ftell(file);
}
uint8_t data[data_size];
if (file) {
rewind(file);
if ((data_size != fread(&data, 1, data_size, file))) {
fclose(file);
weechat_printf(profile->buffer, "%scould not load Tox data file, aborting",
weechat_prefix("error"));
return TWC_RC_ERROR;
}
fclose(file);
}
options.savedata_type = (data_size == 0)? TOX_SAVEDATA_TYPE_NONE: TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = data;
options.savedata_length = data_size;
// create Tox
TOX_ERR_NEW rc;
profile->tox = tox_new(&options, data, data_size, &rc);
profile->tox = tox_new(&options, &rc);
if (rc != TOX_ERR_NEW_OK)
{
twc_tox_new_print_error(profile, &options, rc);
@ -354,6 +368,7 @@ twc_profile_load(struct t_twc_profile *profile)
tox_callback_group_action(profile->tox, twc_group_action_callback, profile);
tox_callback_group_namelist_change(profile->tox, twc_group_namelist_change_callback, profile);
tox_callback_group_title(profile->tox, twc_group_title_callback, profile);
return TWC_RC_OK;
}
/**

View File

@ -81,14 +81,16 @@ twc_null_terminate(const uint8_t *str, size_t length)
char *
twc_get_name_nt(Tox *tox, int32_t friend_number)
{
size_t length = tox_get_name_size(tox, friend_number);
TOX_ERR_FRIEND_QUERY err;
size_t length = tox_friend_get_name_size(tox, friend_number, &err);
if ((err != TOX_ERR_FRIEND_QUERY_OK) ||
(length == 0))
return twc_get_friend_id_short(tox, friend_number);
uint8_t name[length];
// if no name, return client ID instead
if (!length)
return twc_get_friend_id_short(tox, friend_number);
tox_get_name(tox, friend_number, name);
tox_friend_get_name(tox, friend_number, name, &err);
return twc_null_terminate(name, length);
}
@ -98,9 +100,18 @@ twc_get_name_nt(Tox *tox, int32_t friend_number)
char *
twc_get_status_message_nt(Tox *tox, int32_t friend_number)
{
size_t length = tox_get_status_message_size(tox, friend_number);
TOX_ERR_FRIEND_QUERY err;
size_t length = tox_friend_get_status_message_size(tox, friend_number, &err);
if ((err != TOX_ERR_FRIEND_QUERY_OK) ||
(length == SIZE_MAX)) {
char *msg = malloc(1);
*msg = 0;
return msg;
}
uint8_t message[length];
tox_get_status_message(tox, friend_number, message, length);
tox_friend_get_status_message(tox, friend_number, message, &err);
return twc_null_terminate(message, length);
}
@ -127,9 +138,9 @@ twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number)
char *
twc_get_self_name_nt(Tox *tox)
{
size_t length = tox_get_self_name_size(tox);
size_t length = tox_self_get_name_size(tox);
uint8_t name[length];
tox_get_self_name(tox, name);
tox_self_get_name(tox, name);
return twc_null_terminate(name, length);
}
@ -141,11 +152,16 @@ char *
twc_get_friend_id_short(Tox *tox, int32_t friend_number)
{
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
tox_get_client_id(tox, friend_number, client_id);
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
size_t short_id_length = weechat_config_integer(twc_config_short_id_size);
char *hex_address = malloc(short_id_length + 1);
tox_friend_get_public_key(tox, friend_number, client_id, &err);
// return a zero public key on failure
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
memset(client_id, 0, TOX_PUBLIC_KEY_SIZE);
twc_bin2hex(client_id,
short_id_length / 2,
hex_address);
@ -182,37 +198,3 @@ twc_hash_tox_id(const uint8_t *tox_id)
return hash;
}
/**
* Read an entire file into memory.
*
* @return TWC_RC_OK on success, TWC_RC_ERROR if file can not be opened, and
* TWC_RC_ERROR_MALLOC if an appropriate buffer can not be allocated.
*/
enum t_twc_rc
twc_read_file(const char *path, uint8_t **data, size_t *size)
{
FILE *file;
if (file = fopen(path, "r"))
{
// get file size
fseek(file, 0, SEEK_END);
*size = ftell(file);
rewind(file);
if (data = malloc(sizeof(*data) * *size))
{
fread(data, sizeof(uint8_t), *size, file);
fclose(file);
return TWC_RC_OK;
}
else
{
fclose(file);
return TWC_RC_ERROR_MALLOC;
}
}
return TWC_RC_ERROR;
}

View File

@ -54,8 +54,5 @@ twc_uint32_reverse_bytes(uint32_t num);
unsigned long long
twc_hash_tox_id(const uint8_t *tox_id);
enum t_twc_rc
twc_read_file(const char *path, uint8_t **data, size_t *size);
#endif // TOX_WEECHAT_UTILS_H