Allow build without encryptsave library + formatting cleanup.

This commit is contained in:
Håvard Pettersson 2015-09-07 19:44:17 +02:00
parent 3b69de11cb
commit 801d863626
2 changed files with 162 additions and 146 deletions

View File

@ -26,7 +26,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(WeeChat REQUIRED)
find_package(Tox REQUIRED
COMPONENTS CORE
OPTIONAL_COMPONENTS AV)
OPTIONAL_COMPONENTS AV ENCRYPTSAVE)
set(PLUGIN_PATH "lib/weechat/plugins" CACHE PATH
"Path to install the plugin binary to.")
@ -60,6 +60,10 @@ if(Tox_AV_FOUND)
add_definitions(-DTOXAV_ENABLED)
endif()
if(Tox_ENCRYPTSAVE_FOUND)
add_definitions(-DTOXENCRYPTSAVE_ENABLED)
endif()
# remove lib prefix (libtox.so -> tox.so)
set_target_properties(tox PROPERTIES PREFIX "")

View File

@ -24,7 +24,9 @@
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#ifdef TOXENCRYPTSAVE_ENABLED
#include <tox/toxencryptsave.h>
#endif // TOXENCRYPTSAVE_ENABLED
#include "twc.h"
#include "twc-list.h"
@ -65,8 +67,6 @@ twc_profile_expanded_data_path(struct t_twc_profile *profile)
* Save a profile's Tox data to disk.
*
* Returns 0 on success, -1 on failure.
*
* TODO: support encrypted save files
*/
int
twc_profile_save_data_file(struct t_twc_profile *profile)
@ -85,17 +85,16 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
// save Tox data to a buffer
size_t size = tox_get_savedata_size(profile->tox);
uint8_t data[size];
uint8_t enc_data[size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
uint8_t *d = data;
tox_get_savedata(profile->tox, data);
#ifdef TOXENCRYPTSAVE_ENABLED
uint8_t enc_data[size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
char *pw = weechat_config_string(profile->options[TWC_PROFILE_OPTION_PASSPHRASE]);
if (pw)
pw = weechat_string_eval_expression(pw, NULL, NULL, NULL);
if (pw)
{
pw = weechat_string_eval_expression(pw, NULL, NULL, NULL);
if (!tox_pass_encrypt(data, size, (uint8_t *)pw, strlen(pw), enc_data, NULL))
{
free(pw);
@ -106,6 +105,7 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
d = enc_data;
size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
}
#endif // TOXENCRYPTSAVE_ENABLED
// save buffer to a file
FILE *file = fopen(full_path, "w");
@ -265,7 +265,6 @@ twc_tox_new_print_error(struct t_twc_profile *profile,
weechat_prefix("error"), error);
break;
}
}
/**
@ -314,18 +313,24 @@ twc_profile_load(struct t_twc_profile *profile)
char *path = twc_profile_expanded_data_path(profile);
FILE *file = NULL;
size_t data_size;
if (!(file = fopen(path, "r")))
{
data_size = 0;
else {
}
else
{
fseek(file, 0, SEEK_END);
data_size = ftell(file);
}
uint8_t data[data_size];
uint8_t dec_data[data_size];
if (file) {
uint8_t data[data_size];
if (file)
{
rewind(file);
if ((data_size != fread(&data, 1, data_size, 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"));
@ -333,6 +338,10 @@ twc_profile_load(struct t_twc_profile *profile)
}
fclose(file);
}
#if TOXENCRYPTSAVE_ENABLED
uint8_t dec_data[data_size];
if (data_size && tox_is_data_encrypted(data))
{
char *pw = weechat_config_string(profile->options[TWC_PROFILE_OPTION_PASSPHRASE]);
@ -357,7 +366,10 @@ twc_profile_load(struct t_twc_profile *profile)
options.savedata_data = dec_data;
}
else
{
options.savedata_data = data;
}
#endif // TOXENCRYPTSAVE_ENABLED
options.savedata_type = (data_size == 0) ? TOX_SAVEDATA_TYPE_NONE : TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_length = data_size;