Added a data.json file for storing things.

For now, the file contains pending friend requests.
This commit is contained in:
Håvard Pettersson 2014-09-18 17:51:19 +02:00
parent 66b7f06002
commit 4dcc088e1d
6 changed files with 105 additions and 25 deletions

View File

@ -22,6 +22,7 @@ add_library(tox MODULE
src/tox-weechat-gui.c src/tox-weechat-gui.c
src/tox-weechat-utils.c src/tox-weechat-utils.c
src/tox-weechat-config.c src/tox-weechat-config.c
src/tox-weechat-json.c
) )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -Werror-implicit-function-declaration -Wno-unused-parameter") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -Werror-implicit-function-declaration -Wno-unused-parameter")

View File

@ -9,11 +9,12 @@
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-identities.h" #include "tox-weechat-identities.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
#include "tox-weechat-json.h"
#include "tox-weechat-friend-requests.h" #include "tox-weechat-friend-requests.h"
void int
tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity, tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
const uint8_t *client_id, const uint8_t *client_id,
const char *message) const char *message)
{ {
@ -21,12 +22,10 @@ tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
struct t_config_option *option = struct t_config_option *option =
identity->options[TOX_WEECHAT_IDENTITY_OPTION_MAX_FRIEND_REQUESTS]; identity->options[TOX_WEECHAT_IDENTITY_OPTION_MAX_FRIEND_REQUESTS];
unsigned int max_requests = weechat_config_integer(option); unsigned int max_requests = weechat_config_integer(option);
if (identity->friend_request_count >= max_requests) if (identity->friend_request_count >= max_requests)
{ {
weechat_printf(identity->buffer, return -1;
"%sReceived a friend request, but your friend request list is full!",
weechat_prefix("warning"));
return;
} }
struct t_tox_weechat_friend_request *request = malloc(sizeof(*request)); struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
@ -34,14 +33,6 @@ tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
request->message = strdup(message); request->message = strdup(message);
memcpy(request->address, client_id, TOX_CLIENT_ID_SIZE); memcpy(request->address, client_id, TOX_CLIENT_ID_SIZE);
char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1];
tox_weechat_bin2hex(request->address, TOX_CLIENT_ID_SIZE, hex_address);
weechat_printf(identity->buffer,
"%sReceived a friend request from %s: \"%s\"",
weechat_prefix("network"),
hex_address,
request->message);
// add to list // add to list
request->prev_request = identity->last_friend_request; request->prev_request = identity->last_friend_request;
request->next_request = NULL; request->next_request = NULL;
@ -53,6 +44,8 @@ tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
identity->last_friend_request = request; identity->last_friend_request = request;
++(identity->friend_request_count); ++(identity->friend_request_count);
return 0;
} }
void void
@ -106,20 +99,77 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
void void
tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity) tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity)
{ {
// TODO: load from file
identity->friend_requests = identity->last_friend_request = NULL; identity->friend_requests = identity->last_friend_request = NULL;
identity->friend_request_count = 0; identity->friend_request_count = 0;
json_t *identity_object = tox_weechat_json_get_identity_object(identity);
if (!identity_object) return;
json_t *friend_requests = json_object_get(identity_object,
tox_weechat_json_key_friend_requests);
size_t index;
json_t *json_request;
json_array_foreach(friend_requests, index, json_request)
{
char client_id[TOX_CLIENT_ID_SIZE];
const char *message;
json_t *json_id = json_object_get(json_request,
tox_weechat_json_friend_request_key_client_id);
json_t *json_message = json_object_get(json_request,
tox_weechat_json_friend_request_key_message);
tox_weechat_hex2bin(json_string_value(json_id), client_id);
message = json_string_value(json_message);
tox_weechat_friend_request_add(identity,
(uint8_t *)client_id,
message);
}
} }
void void
tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity) tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity)
{ {
json_t *identity_object = tox_weechat_json_get_identity_object(identity);
if (!identity_object) return;
json_t *friend_requests = json_array();
json_object_set(identity_object,
tox_weechat_json_key_friend_requests,
friend_requests);
json_decref(friend_requests);
for (struct t_tox_weechat_friend_request *request = identity->friend_requests; for (struct t_tox_weechat_friend_request *request = identity->friend_requests;
request; request;
request = request->next_request) request = request->next_request)
{ {
json_t *json_request = json_object();
if (!json_request)
// TODO: proper error handling
return;
char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1];
tox_weechat_bin2hex(request->address, TOX_CLIENT_ID_SIZE, hex_id);
json_t *json_id = json_string(hex_id);
json_t *json_message = json_string(request->message);
json_object_set(json_request,
tox_weechat_json_friend_request_key_client_id,
json_id);
json_decref(json_id);
json_object_set(json_request,
tox_weechat_json_friend_request_key_message,
json_message);
json_decref(json_message);
json_array_append(friend_requests, json_request);
json_decref(json_request);
} }
} }

View File

@ -22,8 +22,8 @@ struct t_tox_weechat_friend_request
void void
tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity); tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity);
void int
tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity, tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
const uint8_t *client_id, const uint8_t *client_id,
const char *message); const char *message);

View File

@ -174,9 +174,7 @@ tox_weechat_identity_new(const char *name)
identity->buffer = NULL; identity->buffer = NULL;
identity->tox_do_timer = NULL; identity->tox_do_timer = NULL;
identity->chats = identity->last_chat = NULL; identity->chats = identity->last_chat = NULL;
identity->friend_requests = identity->last_friend_request = NULL;
// initialize friend requests
tox_weechat_friend_request_init_identity(identity);;
// set up config // set up config
tox_weechat_config_init_identity(identity); tox_weechat_config_init_identity(identity);
@ -223,6 +221,13 @@ tox_weechat_identity_connect(struct t_tox_weechat_identity *identity)
(uint8_t *)name, strlen(name)); (uint8_t *)name, strlen(name));
} }
// initialize friend requests
tox_weechat_friend_request_init_identity(identity);
weechat_printf(identity->buffer,
"%sYou have %d pending friend requests.",
weechat_prefix("network"),
identity->friend_request_count);
// bootstrap DHT // bootstrap DHT
int max_bootstrap_nodes = 5; int max_bootstrap_nodes = 5;
int bootstrap_nodes = max_bootstrap_nodes > tox_weechat_bootstrap_count ? int bootstrap_nodes = max_bootstrap_nodes > tox_weechat_bootstrap_count ?
@ -322,6 +327,10 @@ tox_weechat_identity_for_buffer(struct t_gui_buffer *buffer)
void void
tox_weechat_identity_free(struct t_tox_weechat_identity *identity) tox_weechat_identity_free(struct t_tox_weechat_identity *identity)
{ {
// save friend requests
tox_weechat_friend_request_save_identity(identity);
tox_weechat_friend_request_free_identity(identity);
// disconnect // disconnect
tox_weechat_identity_disconnect(identity); tox_weechat_identity_disconnect(identity);
@ -337,10 +346,7 @@ tox_weechat_identity_free(struct t_tox_weechat_identity *identity)
if (identity->next_identity) if (identity->next_identity)
identity->next_identity->prev_identity = identity->prev_identity; identity->next_identity->prev_identity = identity->prev_identity;
// save friend requests // free remaining vars
tox_weechat_friend_request_save_identity(identity);
tox_weechat_friend_request_free_identity(identity);
free(identity->name); free(identity->name);
free(identity); free(identity);
} }

View File

@ -170,8 +170,27 @@ tox_weechat_callback_friend_request(Tox *tox,
void *data) void *data)
{ {
struct t_tox_weechat_identity *identity = data; struct t_tox_weechat_identity *identity = data;
char *message_nt = tox_weechat_null_terminate(message, length); char *message_nt = tox_weechat_null_terminate(message, length);
tox_weechat_friend_request_new(identity, public_key, message_nt); int rc = tox_weechat_friend_request_add(identity, public_key, message_nt);
free(message_nt); free(message_nt);
if (rc == 0)
{
char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1];
tox_weechat_bin2hex(public_key, TOX_CLIENT_ID_SIZE, hex_address);
weechat_printf(identity->buffer,
"%sReceived a friend request from %s: \"%s\"",
weechat_prefix("network"),
hex_address,
message_nt);
}
if (rc == -1)
{
weechat_printf(identity->buffer,
"%sReceived a friend request, but your friend request list is full!",
weechat_prefix("warning"));
}
} }

View File

@ -7,6 +7,7 @@
#include "tox-weechat-gui.h" #include "tox-weechat-gui.h"
#include "tox-weechat-friend-requests.h" #include "tox-weechat-friend-requests.h"
#include "tox-weechat-config.h" #include "tox-weechat-config.h"
#include "tox-weechat-json.h"
#include "tox-weechat.h" #include "tox-weechat.h"
@ -25,6 +26,8 @@ weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
{ {
weechat_plugin = plugin; weechat_plugin = plugin;
tox_weechat_json_init();
tox_weechat_config_init(); tox_weechat_config_init();
tox_weechat_config_read(); tox_weechat_config_read();
tox_weechat_commands_init(); tox_weechat_commands_init();
@ -40,6 +43,7 @@ weechat_plugin_end(struct t_weechat_plugin *plugin)
{ {
tox_weechat_config_write(); tox_weechat_config_write();
tox_weechat_identity_free_all(); tox_weechat_identity_free_all();
tox_weechat_json_save();
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }