From f79de0229be0306412f2a9afd05d4a4e21c38437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Tue, 2 Sep 2014 13:59:07 +0200 Subject: [PATCH] Added tox-weechat-tox.* --- src/tox-weechat-tox.c | 128 ++++++++++++++++++++++++++++++++++++++++++ src/tox-weechat-tox.h | 18 ++++++ 2 files changed, 146 insertions(+) create mode 100644 src/tox-weechat-tox.c create mode 100644 src/tox-weechat-tox.h diff --git a/src/tox-weechat-tox.c b/src/tox-weechat-tox.c new file mode 100644 index 0000000..6a5f8ee --- /dev/null +++ b/src/tox-weechat-tox.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include "tox-weechat.h" +#include "tox-weechat-utils.h" + +#include "tox-weechat-tox.h" + +#define INITIAL_NAME "WeeChatter" + +#define BOOTSTRAP_ADDRESS "192.254.75.98" +#define BOOTSTRAP_PORT 33445 +#define BOOTSTRAP_KEY "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F" + +Tox *tox = NULL; + +/** + * Return the path to the Tox data file. Must be freed. + */ +char * +tox_weechat_data_path() +{ + const char *weechat_dir = weechat_info_get("weechat_dir", NULL); + const char *file_path = "/tox/data"; + + weechat_mkdir_home("tox", 0755); + + int path_length = strlen(weechat_dir) + strlen(file_path) + 1; + char *tox_data_path = malloc(path_length); + + strcpy(tox_data_path, weechat_dir); + strcat(tox_data_path, file_path); + tox_data_path[path_length-1] = 0; + + return tox_data_path; +} + +/** + * Load Tox data from a file path. + * + * Returns 0 on success, -1 on failure. */ +int +tox_weechat_load_file(Tox *tox, char *path) +{ + FILE *file = fopen(path, "r"); + if (file) + { + // get file size + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + rewind(file); + + // allocate a buffer and read file into it + uint8_t *data = malloc(sizeof(*data) * size); + fread(data, sizeof(uint8_t), size, file); + fclose(file); + + // try loading the data + int status = tox_load(tox, data, size); + free(data); + + return status; + } + + return -1; +} + +/** + * Continous timer keeping Tox running. + */ +int +tox_weechat_do_timer_cb(void *data, + int remaining_calls) +{ + tox_do(tox); + weechat_hook_timer(tox_do_interval(tox), 0, 1, tox_weechat_do_timer_cb, data); + + return WEECHAT_RC_OK; +} + +void +tox_weechat_tox_init() +{ + tox = tox_new(0); + + // try loading Tox saved data + char *data_file_path = tox_weechat_data_path(); + if (tox_weechat_load_file(tox, data_file_path) == -1) + { + // couldn't load Tox, set a default name + tox_set_name(tox, (uint8_t *)INITIAL_NAME, strlen(INITIAL_NAME)); + } + free(data_file_path); + + // bootstrap + uint8_t *pub_key = tox_weechat_hex2bin(BOOTSTRAP_KEY); + tox_bootstrap_from_address(tox, BOOTSTRAP_ADDRESS, htons(BOOTSTRAP_PORT), pub_key); + free(pub_key); + + // start tox_do loop + tox_weechat_do_timer_cb(NULL, 0); +} + +void +tox_weechat_tox_end() +{ + // save Tox data to a buffer + uint32_t size = tox_size(tox); + uint8_t *data = malloc(sizeof(*data) * size); + tox_save(tox, data); + + // save buffer to a file + char *data_file_path = tox_weechat_data_path(); + FILE *data_file = fopen(data_file_path, "w"); + fwrite(data, sizeof(data[0]), size, data_file); + fclose(data_file); + free(data_file_path); + + // free the Tox object + tox_kill(tox); +} + diff --git a/src/tox-weechat-tox.h b/src/tox-weechat-tox.h new file mode 100644 index 0000000..63f9aac --- /dev/null +++ b/src/tox-weechat-tox.h @@ -0,0 +1,18 @@ +#ifndef TOX_WEECHAT_TOX_H +#define TOX_WEECHAT_TOX_H + +#include + +extern Tox *tox; + +/** + * Initialize the Tox object, bootstrap the DHT and start working. + */ +void tox_weechat_tox_init(); + +/** + * Dump Tox to file and de-initialize. + */ +void tox_weechat_tox_end(); + +#endif // TOX_WEECHAT_TOX_H