Cleaned up and documented friend request code.

This commit is contained in:
Håvard Pettersson 2014-09-20 21:43:22 +02:00
parent 49a864fc38
commit cf95a320b2
2 changed files with 52 additions and 45 deletions

View File

@ -32,6 +32,11 @@
#include "tox-weechat-friend-requests.h" #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 int
tox_weechat_friend_request_add(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,
@ -43,14 +48,12 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
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)
{
return -1; return -1;
}
struct t_tox_weechat_friend_request *request = malloc(sizeof(*request)); struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
request->identity = identity; request->identity = identity;
request->message = strdup(message); 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 // add to list
request->prev_request = identity->last_friend_request; request->prev_request = identity->last_friend_request;
@ -67,6 +70,9 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
return 0; return 0;
} }
/**
* Remove and free a friend request from its identity.
*/
void void
tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request) 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; request->next_request->prev_request = request->prev_request;
--(identity->friend_request_count); --(identity->friend_request_count);
tox_weechat_friend_request_free(request);
} }
/**
* Accept a friend request. Removes and frees the request.
*/
void void
tox_weechat_accept_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->address); tox_add_friend_norequest(request->identity->tox, request->tox_id);
tox_weechat_friend_request_remove(request);
}
void
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
{
tox_weechat_friend_request_remove(request); tox_weechat_friend_request_remove(request);
} }
/**
* Return the friend request from the identity with the number num.
*/
struct t_tox_weechat_friend_request * struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity, tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num) unsigned int num)
@ -115,23 +123,20 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
return request; return request;
} }
/**
* Remove all friend requests from an identity and add new ones from the JSON
* array json_request_array.
*/
void 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_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; size_t index;
json_t *json_request; json_t *json_request;
json_array_foreach(json_request_array, index, json_request)
json_array_foreach(friend_requests, index, json_request)
{ {
char client_id[TOX_CLIENT_ID_SIZE]; char client_id[TOX_CLIENT_ID_SIZE];
const char *message; 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_t *friend_requests = json_array();
if (!json_array)
json_object_set(identity_object, return NULL;
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(); json_t *json_request = json_object();
if (!json_request)
// TODO: proper error handling
return;
char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1]; 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_id = json_string(hex_id);
json_t *json_message = json_string(request->message); json_t *json_message = json_string(request->message);
if (!json_request || !json_id || !json_message)
break;
json_object_set(json_request, json_object_set(json_request,
tox_weechat_json_friend_request_key_client_id, tox_weechat_json_friend_request_key_client_id,
json_id); json_id);
json_decref(json_id); json_decref(json_id);
json_object_set(json_request, json_object_set(json_request,
tox_weechat_json_friend_request_key_message, tox_weechat_json_friend_request_key_message,
json_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_array_append(friend_requests, json_request);
json_decref(json_request); json_decref(json_request);
} }
return friend_requests;
} }
/**
* Free a friend request.
*/
void void
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request) 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(request);
} }
/**
* Free all friend requests from an identity.
*/
void void
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity) tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity)
{ {
while (identity->friend_requests) while (identity->friend_requests)
tox_weechat_friend_request_remove(identity->friend_requests); tox_weechat_friend_request_remove(identity->friend_requests);
} }

View File

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