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,12 +162,14 @@ 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);
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];
}
}
char *name = twc_get_name_nt(profile->tox, friend_numbers[i]);
if (weechat_strcasecmp(name, search_string) == 0)
@ -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,
TOX_ERR_FRIEND_ADD err;
(void)tox_friend_add(profile->tox,
(uint8_t *)address,
(uint8_t *)message,
strlen(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,
TOX_ERR_SET_INFO err;
tox_self_set_status_message(profile->tox,
(uint8_t *)message,
strlen(message));
if (result == -1)
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,12 +68,16 @@ 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);
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);
}
}
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

@ -82,7 +82,7 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
free(dir_path);
// save Tox data to a buffer
uint32_t size = tox_get_savedata_size(profile->tox);
size_t size = tox_get_savedata_size(profile->tox);
uint8_t *data = malloc(size);
tox_get_savedata(profile->tox, data);
@ -90,7 +90,7 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
FILE *file = fopen(full_path, "w");
if (file)
{
size_t saved_size = fwrite(data, sizeof(data[0]), size, file);
size_t saved_size = fwrite(data, 1, size, file);
fclose(file);
return saved_size == size;
@ -273,7 +273,9 @@ twc_profile_load(struct t_twc_profile *profile)
// 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)
@ -289,21 +291,33 @@ twc_profile_load(struct t_twc_profile *profile)
// 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);
uint8_t name[length];
TOX_ERR_FRIEND_QUERY err;
size_t length = tox_friend_get_name_size(tox, friend_number, &err);
// if no name, return client ID instead
if (!length)
if ((err != TOX_ERR_FRIEND_QUERY_OK) ||
(length == 0))
return twc_get_friend_id_short(tox, friend_number);
tox_get_name(tox, friend_number, name);
uint8_t name[length];
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