tox-weechat/src/tox-weechat-friend-requests.c

99 lines
2.6 KiB
C
Raw Normal View History

2014-09-02 18:47:08 +02:00
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
2014-09-07 03:04:42 +02:00
#include "tox-weechat-tox.h"
2014-09-02 18:47:08 +02:00
#include "tox-weechat-utils.h"
#include "tox-weechat-friend-requests.h"
#define MAX_FRIEND_REQUESTS 250
void
2014-09-07 03:04:42 +02:00
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
2014-09-02 18:47:08 +02:00
{
free(request->message);
free(request);
}
void
2014-09-07 03:04:42 +02:00
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_friend_request *request)
2014-09-02 18:47:08 +02:00
{
2014-09-07 03:04:42 +02:00
request->identity = identity;
2014-09-02 18:47:08 +02:00
2014-09-07 03:04:42 +02:00
request->prev_request = identity->last_friend_request;
request->next_request = NULL;
2014-09-02 18:47:08 +02:00
2014-09-07 03:04:42 +02:00
if (identity->friend_requests == NULL)
identity->friend_requests = request;
else
identity->last_friend_request->next_request = request;
identity->last_friend_request = request;
++(identity->friend_request_count);
2014-09-02 18:47:08 +02:00
}
void
2014-09-07 03:04:42 +02:00
tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
2014-09-02 18:47:08 +02:00
{
2014-09-07 03:04:42 +02:00
if (request->prev_request)
request->prev_request->next_request = request->next_request;
if (request->next_request)
request->next_request->prev_request = request->prev_request;
2014-09-02 18:47:08 +02:00
2014-09-07 03:04:42 +02:00
if (request == request->identity->friend_requests)
request->identity->friend_requests = request->next_request;
if (request == request->identity->last_friend_request)
request->identity->last_friend_request = request->prev_request;
2014-09-02 18:47:08 +02:00
tox_weechat_friend_request_free(request);
}
void
2014-09-07 03:04:42 +02:00
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
2014-09-02 18:47:08 +02:00
{
2014-09-07 03:04:42 +02:00
tox_add_friend_norequest(request->identity->tox, request->address);
2014-09-02 18:47:08 +02:00
tox_weechat_friend_request_remove(request);
}
void
2014-09-07 03:04:42 +02:00
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
2014-09-02 18:47:08 +02:00
{
tox_weechat_friend_request_remove(request);
}
2014-09-07 03:04:42 +02:00
struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num)
2014-09-02 18:47:08 +02:00
{
2014-09-07 03:04:42 +02:00
if (num < 1 || num > identity->friend_request_count) return NULL;
2014-09-02 18:47:08 +02:00
unsigned int i = 1;
2014-09-07 03:04:42 +02:00
struct t_tox_weechat_friend_request *request = identity->friend_requests;
while (i != num && request->next_request)
2014-09-02 18:47:08 +02:00
{
2014-09-07 03:04:42 +02:00
request = request->next_request;
2014-09-02 18:47:08 +02:00
++i;
}
return request;
}
void
tox_weechat_friend_requests_init()
{
}
void
2014-09-07 03:04:42 +02:00
tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity)
2014-09-02 18:47:08 +02:00
{
// TODO: persist requests
2014-09-07 03:04:42 +02:00
while (identity->friend_requests)
tox_weechat_friend_request_remove(identity->friend_requests);
2014-09-02 18:47:08 +02:00
}