Cleaned up friend request code.

This commit is contained in:
Håvard Pettersson 2014-09-18 01:01:36 +02:00
parent a467fb7455
commit 7d5ffb89a5
4 changed files with 77 additions and 54 deletions

View File

@ -12,18 +12,36 @@
#include "tox-weechat-friend-requests.h" #include "tox-weechat-friend-requests.h"
void void
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request) tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
const uint8_t *client_id,
const char *message)
{ {
free(request->message); // check friend request count
free(request); 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;
} }
void struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_friend_request *request)
{
request->identity = identity; request->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->prev_request = identity->last_friend_request;
request->next_request = NULL; request->next_request = NULL;
@ -39,17 +57,19 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *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)
{ {
struct t_tox_weechat_identity *identity = request->identity;
if (request == identity->last_friend_request)
identity->last_friend_request = request->prev_request;
if (request->prev_request) if (request->prev_request)
request->prev_request->next_request = request->next_request; request->prev_request->next_request = request->next_request;
else
identity->friend_requests = request->next_request;
if (request->next_request) if (request->next_request)
request->next_request->prev_request = request->prev_request; request->next_request->prev_request = request->prev_request;
if (request == request->identity->friend_requests) --(identity->friend_request_count);
request->identity->friend_requests = request->next_request;
if (request == request->identity->last_friend_request)
request->identity->last_friend_request = request->prev_request;
tox_weechat_friend_request_free(request);
} }
void void
@ -83,14 +103,35 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
} }
void void
tox_weechat_friend_requests_init() 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;
} }
void void
tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity) tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity)
{
for (struct t_tox_weechat_friend_request *request = identity->friend_requests;
request;
request = request->next_request)
{
// TODO: actually save requests
}
}
void
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
{
free(request->message);
free(request);
}
void
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity)
{ {
// TODO: persist requests
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

@ -20,8 +20,12 @@ struct t_tox_weechat_friend_request
}; };
void void
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity, tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity);
struct t_tox_weechat_friend_request *request);
void
tox_weechat_friend_request_new(struct t_tox_weechat_identity *identity,
const uint8_t *client_id,
const char *message);
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);
@ -34,6 +38,9 @@ tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num); unsigned int num);
void void
tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity); tox_weechat_friend_request_save_identity(struct t_tox_weechat_identity *identity);
void
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity);
#endif // TOX_WEECHAT_FRIEND_REQUESTS_H #endif // TOX_WEECHAT_FRIEND_REQUESTS_H

View File

@ -10,6 +10,7 @@
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-config.h" #include "tox-weechat-config.h"
#include "tox-weechat-friend-requests.h"
#include "tox-weechat-chats.h" #include "tox-weechat-chats.h"
#include "tox-weechat-tox-callbacks.h" #include "tox-weechat-tox-callbacks.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
@ -175,9 +176,8 @@ tox_weechat_identity_new(const char *name)
identity->tox_do_timer = NULL; identity->tox_do_timer = NULL;
identity->chats = identity->last_chat = NULL; identity->chats = identity->last_chat = NULL;
// TODO: load from disk // initialize friend requests
identity->friend_requests = identity->last_friend_request = NULL; tox_weechat_friend_request_init_identity(identity);;
identity->friend_request_count = 0;
// set up config // set up config
tox_weechat_config_init_identity(identity); tox_weechat_config_init_identity(identity);
@ -338,7 +338,9 @@ 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;
// TODO: free more things // save friend requests
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

@ -163,35 +163,8 @@ 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);
struct t_config_option *option = tox_weechat_friend_request_new(identity, public_key, message_nt);
identity->options[TOX_WEECHAT_IDENTITY_OPTION_MAX_FRIEND_REQUESTS]; free(message_nt);
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;
}
// TODO: move to t-w-f-r.h
struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
memcpy(request->address, public_key, TOX_CLIENT_ID_SIZE);
request->message = tox_weechat_null_terminate(message, length);
tox_weechat_friend_request_add(identity, request);
char *hex_address = malloc(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);
free(hex_address);
} }