Added a data.json file for storing things.
For now, the file contains pending friend requests.
This commit is contained in:
parent
66b7f06002
commit
4dcc088e1d
6 changed files with 105 additions and 25 deletions
|
@ -9,11 +9,12 @@
|
|||
#include "tox-weechat.h"
|
||||
#include "tox-weechat-identities.h"
|
||||
#include "tox-weechat-utils.h"
|
||||
#include "tox-weechat-json.h"
|
||||
|
||||
#include "tox-weechat-friend-requests.h"
|
||||
|
||||
void
|
||||
tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
|
||||
int
|
||||
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
|
||||
const uint8_t *client_id,
|
||||
const char *message)
|
||||
{
|
||||
|
@ -21,12 +22,10 @@ tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
|
|||
struct t_config_option *option =
|
||||
identity->options[TOX_WEECHAT_IDENTITY_OPTION_MAX_FRIEND_REQUESTS];
|
||||
unsigned int max_requests = weechat_config_integer(option);
|
||||
|
||||
if (identity->friend_request_count >= max_requests)
|
||||
{
|
||||
weechat_printf(identity->buffer,
|
||||
"%sReceived a friend request, but your friend request list is full!",
|
||||
weechat_prefix("warning"));
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
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);
|
||||
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
|
||||
request->prev_request = identity->last_friend_request;
|
||||
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->friend_request_count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -106,20 +99,77 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
|
|||
void
|
||||
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_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
|
||||
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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue