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

View File

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