Cleaned up and documented friend request code.

master
Håvard Pettersson 10 years ago
parent 49a864fc38
commit cf95a320b2

@ -32,6 +32,11 @@
#include "tox-weechat-friend-requests.h"
/**
* Add a new friend request to an identity.
*
* Returns 0 on success, -1 on a full friend list.
*/
int
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
const uint8_t *client_id,
@ -43,14 +48,12 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
unsigned int max_requests = weechat_config_integer(option);
if (identity->friend_request_count >= max_requests)
{
return -1;
}
struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
request->identity = identity;
request->message = strdup(message);
memcpy(request->address, client_id, TOX_CLIENT_ID_SIZE);
memcpy(request->tox_id, client_id, TOX_CLIENT_ID_SIZE);
// add to list
request->prev_request = identity->last_friend_request;
@ -67,6 +70,9 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
return 0;
}
/**
* Remove and free a friend request from its identity.
*/
void
tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
{
@ -83,21 +89,23 @@ tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
request->next_request->prev_request = request->prev_request;
--(identity->friend_request_count);
}
void
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
{
tox_add_friend_norequest(request->identity->tox, request->address);
tox_weechat_friend_request_remove(request);
tox_weechat_friend_request_free(request);
}
/**
* Accept a friend request. Removes and frees the request.
*/
void
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
{
tox_add_friend_norequest(request->identity->tox, request->tox_id);
tox_weechat_friend_request_remove(request);
}
/**
* Return the friend request from the identity with the number num.
*/
struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num)
@ -115,23 +123,20 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
return request;
}
/**
* Remove all friend requests from an identity and add new ones from the JSON
* array json_request_array.
*/
void
tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity)
tox_weechat_friend_requests_load_json(struct t_tox_weechat_identity *identity,
json_t *json_request_array)
{
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)
json_array_foreach(json_request_array, index, json_request)
{
char client_id[TOX_CLIENT_ID_SIZE];
const char *message;
@ -150,38 +155,37 @@ tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity
}
}
void
tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity)
/**
* Save all friend requests for an identity to a json array. Returns NULL on
* error.
*/
json_t *
tox_weechat_friend_requests_save_json(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);
if (!json_array)
return NULL;
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);
tox_weechat_bin2hex(request->tox_id, TOX_CLIENT_ID_SIZE, hex_id);
json_t *json_id = json_string(hex_id);
json_t *json_message = json_string(request->message);
if (!json_request || !json_id || !json_message)
break;
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);
@ -190,8 +194,13 @@ tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity
json_array_append(friend_requests, json_request);
json_decref(json_request);
}
return friend_requests;
}
/**
* Free a friend request.
*/
void
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
{
@ -199,9 +208,13 @@ tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
free(request);
}
/**
* Free all friend requests from an identity.
*/
void
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity)
{
while (identity->friend_requests)
tox_weechat_friend_request_remove(identity->friend_requests);
}

@ -24,12 +24,12 @@
#include <tox/tox.h>
/**
* Represents a friend request with a Tox ID and a message.
*/
struct t_tox_weechat_friend_request
{
// public address of friend request
uint8_t address[TOX_CLIENT_ID_SIZE];
// message sent with request
uint8_t tox_id[TOX_CLIENT_ID_SIZE];
char *message;
struct t_tox_weechat_identity *identity;
@ -38,9 +38,6 @@ struct t_tox_weechat_friend_request
struct t_tox_weechat_friend_request *prev_request;
};
void
tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity);
int
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
const uint8_t *client_id,
@ -49,15 +46,12 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
void
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request);
void
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request);
struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num);
void
tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity);
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request);
void
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity);

Loading…
Cancel
Save