Updated to new tox API. Fixed profile loading.

This commit is contained in:
Michael Raitza 2015-08-26 16:48:33 +02:00
parent ed96d36711
commit b376432f04
9 changed files with 248 additions and 216 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]; uint8_t binary_key[TOX_ADDRESS_SIZE];
twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key); twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key);
TOX_ERR_BOOTSTRAP err;
int result = tox_bootstrap_from_address(tox, address, port, int result = tox_bootstrap(tox, address, port,
binary_key); binary_key, &err);
return result; 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) twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number)
{ {
uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; 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]; char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name); twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name);

View File

@ -142,14 +142,14 @@ enum TWC_FRIEND_MATCH
enum TWC_FRIEND_MATCH enum TWC_FRIEND_MATCH
twc_match_friend(struct t_twc_profile *profile, const char *search_string) twc_match_friend(struct t_twc_profile *profile, const char *search_string)
{ {
uint32_t friend_count = tox_count_friendlist(profile->tox); uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count); uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_get_friendlist(profile->tox, friend_numbers, friend_count); tox_self_get_friend_list(profile->tox, friend_numbers);
int32_t match = TWC_FRIEND_MATCH_NOMATCH; int32_t match = TWC_FRIEND_MATCH_NOMATCH;
char *endptr; 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) if (endptr == search_string + strlen(search_string)
&& tox_friend_exists(profile->tox, friend_number)) && tox_friend_exists(profile->tox, friend_number))
return friend_number; return friend_number;
@ -162,7 +162,7 @@ twc_match_friend(struct t_twc_profile *profile, const char *search_string)
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
tox_get_client_id(profile->tox, friend_numbers[i], tox_id); tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL); // do error handling
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
if (weechat_strcasecmp(hex_id, search_string) == 0) if (weechat_strcasecmp(hex_id, search_string) == 0)
@ -227,9 +227,9 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
// /friend or /friend list // /friend or /friend list
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0)) if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{ {
size_t friend_count = tox_count_friendlist(profile->tox); size_t friend_count = tox_self_get_friend_list_size(profile->tox);
int32_t friend_numbers[friend_count]; uint32_t friend_numbers[friend_count];
tox_get_friendlist(profile->tox, friend_numbers, friend_count); tox_self_get_friend_list(profile->tox, friend_numbers);
if (friend_count == 0) if (friend_count == 0)
{ {
@ -245,7 +245,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
for (size_t i = 0; i < friend_count; ++i) 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 *name = twc_get_name_nt(profile->tox, friend_number);
char *hex_address = twc_get_friend_id_short(profile->tox, char *hex_address = twc_get_friend_id_short(profile->tox,
friend_number); friend_number);
@ -306,7 +306,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
if (friend_number == TWC_FRIEND_MATCH_AMBIGUOUS) if (friend_number == TWC_FRIEND_MATCH_AMBIGUOUS)
fail = true; fail = true;
else if (friend_number != TWC_FRIEND_MATCH_NOMATCH) 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) if (fail)
{ {
@ -318,12 +318,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 *)address,
(uint8_t *)message, (uint8_t *)message,
strlen(message)); strlen(message), &err);
switch (result) switch (err)
{ {
case TOX_ERR_FRIEND_ADD_OK: case TOX_ERR_FRIEND_ADD_OK:
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
@ -363,7 +364,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
default: default:
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%sCould not add friend (unknown error %d).", "%sCould not add friend (unknown error %d).",
weechat_prefix("error"), result); weechat_prefix("error"), err);
break; break;
} }
@ -377,7 +378,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer,
TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]); TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]);
char *name = twc_get_name_nt(profile->tox, friend_number); 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, weechat_printf(profile->buffer,
"%sRemoved %s from friend list.", "%sRemoved %s from friend list.",
@ -693,7 +694,7 @@ twc_cmd_myid(void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE_LOADED(profile); TWC_CHECK_PROFILE_LOADED(profile);
uint8_t address[TOX_ADDRESS_SIZE]; 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]; char address_str[TOX_ADDRESS_SIZE * 2 + 1];
twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str); twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str);
@ -722,8 +723,8 @@ twc_cmd_name(void *data, struct t_gui_buffer *buffer,
char *name = argv_eol[1]; char *name = argv_eol[1];
int result = tox_set_name(profile->tox, (uint8_t *)name, strlen(name)); int result = tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), NULL);
if (result == -1) if (!result)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%s%s", "%s%s",
@ -789,8 +790,8 @@ twc_cmd_nospam(void *data, struct t_gui_buffer *buffer,
new_nospam = random(); new_nospam = random();
} }
uint32_t old_nospam = tox_get_nospam(profile->tox); uint32_t old_nospam = tox_self_get_nospam(profile->tox);
tox_set_nospam(profile->tox, new_nospam); tox_self_set_nospam(profile->tox, new_nospam);
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%snew nospam has been set; this changes your Tox ID! To " "%snew nospam has been set; this changes your Tox ID! To "
@ -887,7 +888,7 @@ twc_cmd_status(void *data, struct t_gui_buffer *buffer,
else else
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
tox_set_user_status(profile->tox, status); tox_self_set_status(profile->tox, status);
weechat_bar_item_update("away"); weechat_bar_item_update("away");
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -906,10 +907,10 @@ twc_cmd_statusmsg(void *data, struct t_gui_buffer *buffer,
char *message = argc > 1 ? argv_eol[1] : " "; char *message = argc > 1 ? argv_eol[1] : " ";
int result = tox_set_status_message(profile->tox, bool result = tox_self_set_status_message(profile->tox,
(uint8_t *)message, (uint8_t *)message,
strlen(message)); strlen(message), NULL);
if (result == -1) if (!result)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%s%s", "%s%s",

View File

@ -57,9 +57,9 @@ twc_completion_friend(void *data,
if (!profile) if (!profile)
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
uint32_t friend_count = tox_count_friendlist(profile->tox); uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
int32_t *friend_numbers = malloc(sizeof(int32_t) * friend_count); uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_get_friendlist(profile->tox, friend_numbers, friend_count); tox_self_get_friend_list(profile->tox, friend_numbers);
for (uint32_t i = 0; i < friend_count; ++i) for (uint32_t i = 0; i < friend_count; ++i)
{ {
@ -68,7 +68,7 @@ twc_completion_friend(void *data,
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
tox_get_client_id(profile->tox, friend_numbers[i], tox_id); tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL); // do error handling
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
weechat_hook_completion_list_add(completion, hex_id, 0, weechat_hook_completion_list_add(completion, hex_id, 0,

View File

@ -67,7 +67,7 @@ twc_friend_request_add(struct t_twc_profile *profile,
void void
twc_friend_request_accept(struct t_twc_friend_request *request) twc_friend_request_accept(struct t_twc_friend_request *request)
{ {
tox_add_friend_norequest(request->profile->tox, request->tox_id); tox_friend_add_norequest(request->profile->tox, request->tox_id, NULL); //do error handling
twc_friend_request_remove(request); twc_friend_request_remove(request);
} }

View File

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

View File

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

View File

@ -82,7 +82,7 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
free(dir_path); free(dir_path);
// save Tox data to a buffer // 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); uint8_t *data = malloc(size);
tox_get_savedata(profile->tox, data); 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"); FILE *file = fopen(full_path, "w");
if (file) if (file)
{ {
size_t saved_size = fwrite(data, sizeof(data[0]), size, file); size_t saved_size = fwrite(data, 1, size, file);
fclose(file); fclose(file);
return saved_size == size; return saved_size == size;
@ -273,7 +273,9 @@ twc_profile_load(struct t_twc_profile *profile)
// create Tox options object // create Tox options object
struct Tox_Options options; struct Tox_Options options;
twc_profile_set_options(&options, profile);
tox_options_default(&options);
//twc_profile_set_options(options, profile);
// print a proxy message // print a proxy message
if (options.proxy_type != TOX_PROXY_TYPE_NONE) if (options.proxy_type != TOX_PROXY_TYPE_NONE)
@ -289,21 +291,33 @@ twc_profile_load(struct t_twc_profile *profile)
// try loading data file // try loading data file
char *path = twc_profile_expanded_data_path(profile); char *path = twc_profile_expanded_data_path(profile);
uint8_t *data; FILE *file = NULL;
size_t data_size = 0; size_t data_size;
enum t_twc_rc data_rc = twc_read_file(path, &data, &data_size); if (!(file = fopen(path, "r")))
data_size = 0;
if (data_rc == TWC_RC_ERROR_MALLOC) else {
{ fseek(file, 0, SEEK_END);
weechat_printf(profile->buffer, data_size = ftell(file);
"%scould not load Tox data file, aborting (malloc error)",
weechat_prefix("error"));
return TWC_RC_ERROR_MALLOC;
} }
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 // create Tox
TOX_ERR_NEW rc; 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) if (rc != TOX_ERR_NEW_OK)
{ {
twc_tox_new_print_error(profile, &options, rc); 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_action(profile->tox, twc_group_action_callback, profile);
tox_callback_group_namelist_change(profile->tox, twc_group_namelist_change_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); 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 * char *
twc_get_name_nt(Tox *tox, int32_t friend_number) 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;
uint8_t name[length]; size_t length = tox_friend_get_name_size(tox, friend_number, &err);
// if no name, return client ID instead if ((err != TOX_ERR_FRIEND_QUERY_OK) ||
if (!length) (length == 0))
return twc_get_friend_id_short(tox, friend_number); 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); return twc_null_terminate(name, length);
} }
@ -98,9 +100,18 @@ twc_get_name_nt(Tox *tox, int32_t friend_number)
char * char *
twc_get_status_message_nt(Tox *tox, int32_t friend_number) 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]; 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); 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 * char *
twc_get_self_name_nt(Tox *tox) 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]; uint8_t name[length];
tox_get_self_name(tox, name); tox_self_get_name(tox, name);
return twc_null_terminate(name, length); return twc_null_terminate(name, length);
} }
@ -141,11 +152,16 @@ char *
twc_get_friend_id_short(Tox *tox, int32_t friend_number) twc_get_friend_id_short(Tox *tox, int32_t friend_number)
{ {
uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; 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); size_t short_id_length = weechat_config_integer(twc_config_short_id_size);
char *hex_address = malloc(short_id_length + 1); 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, twc_bin2hex(client_id,
short_id_length / 2, short_id_length / 2,
hex_address); hex_address);
@ -193,18 +209,18 @@ enum t_twc_rc
twc_read_file(const char *path, uint8_t **data, size_t *size) twc_read_file(const char *path, uint8_t **data, size_t *size)
{ {
FILE *file; FILE *file;
if (file = fopen(path, "r")) if ((file = fopen(path, "r")))
{ {
// get file size // get file size
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
*size = ftell(file); *size = ftell(file);
rewind(file); rewind(file);
if (data = malloc(sizeof(*data) * *size)) if ((data = malloc(sizeof(**data) * *size)))
{ {
fread(data, sizeof(uint8_t), *size, file); size_t rs = fread(data, sizeof(uint8_t), *size, file);
fclose(file); fclose(file);
return TWC_RC_OK; return rs == *size ? TWC_RC_OK : TWC_RC_ERROR;
} }
else else
{ {