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

208 lines
6.4 KiB
C
Raw Normal View History

2014-09-18 16:55:52 +00:00
/*
* Copyright (c) 2014 Håvard Pettersson <haavard.pettersson@gmail.com>
*
* This file is part of Tox-WeeChat.
*
* Tox-WeeChat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox-WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
2014-09-02 16:47:08 +00:00
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
2014-09-18 14:08:48 +00:00
#include <jansson.h>
2014-09-02 16:47:08 +00:00
#include "tox-weechat.h"
#include "tox-weechat-identities.h"
2014-09-02 16:47:08 +00:00
#include "tox-weechat-utils.h"
#include "tox-weechat-json.h"
2014-09-02 16:47:08 +00:00
#include "tox-weechat-friend-requests.h"
int
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
2014-09-17 23:01:36 +00:00
const uint8_t *client_id,
const char *message)
2014-09-02 16:47:08 +00:00
{
2014-09-17 23:01:36 +00:00
// check friend request count
struct t_config_option *option =
identity->options[TOX_WEECHAT_IDENTITY_OPTION_MAX_FRIEND_REQUESTS];
unsigned int max_requests = weechat_config_integer(option);
2014-09-17 23:01:36 +00:00
if (identity->friend_request_count >= max_requests)
{
return -1;
2014-09-17 23:01:36 +00:00
}
2014-09-02 16:47:08 +00:00
2014-09-17 23:01:36 +00:00
struct t_tox_weechat_friend_request *request = malloc(sizeof(*request));
2014-09-07 01:04:42 +00:00
request->identity = identity;
2014-09-17 23:01:36 +00:00
request->message = strdup(message);
memcpy(request->address, client_id, TOX_CLIENT_ID_SIZE);
// add to list
2014-09-07 01:04:42 +00:00
request->prev_request = identity->last_friend_request;
request->next_request = NULL;
2014-09-02 16:47:08 +00:00
2014-09-07 01:04:42 +00: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);
return 0;
2014-09-02 16:47:08 +00:00
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
2014-09-02 16:47:08 +00:00
{
2014-09-17 23:01:36 +00:00
struct t_tox_weechat_identity *identity = request->identity;
if (request == identity->last_friend_request)
identity->last_friend_request = request->prev_request;
2014-09-07 01:04:42 +00:00
if (request->prev_request)
request->prev_request->next_request = request->next_request;
2014-09-17 23:01:36 +00:00
else
identity->friend_requests = request->next_request;
2014-09-07 01:04:42 +00:00
if (request->next_request)
request->next_request->prev_request = request->prev_request;
2014-09-02 16:47:08 +00:00
2014-09-17 23:01:36 +00:00
--(identity->friend_request_count);
2014-09-02 16:47:08 +00:00
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
tox_add_friend_norequest(request->identity->tox, request->address);
2014-09-02 16:47:08 +00:00
tox_weechat_friend_request_remove(request);
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
2014-09-02 16:47:08 +00:00
{
tox_weechat_friend_request_remove(request);
}
2014-09-07 01:04:42 +00: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 16:47:08 +00:00
{
if (num >= identity->friend_request_count) return NULL;
2014-09-02 16:47:08 +00:00
unsigned int i = 0;
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_friend_request *request = identity->friend_requests;
while (i != num && request->next_request)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
request = request->next_request;
2014-09-02 16:47:08 +00:00
++i;
}
return request;
}
void
2014-09-17 23:01:36 +00:00
tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity)
2014-09-02 16:47:08 +00:00
{
2014-09-17 23:01:36 +00:00
identity->friend_requests = identity->last_friend_request = NULL;
2014-09-17 23:01:36 +00:00
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), TOX_CLIENT_ID_SIZE * 2, client_id);
message = json_string_value(json_message);
tox_weechat_friend_request_add(identity,
(uint8_t *)client_id,
message);
}
2014-09-17 23:01:36 +00:00
}
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);
2014-09-17 23:01:36 +00:00
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);
2014-09-17 23:01:36 +00:00
}
}
void
tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
{
free(request->message);
free(request);
2014-09-02 16:47:08 +00:00
}
void
2014-09-17 23:01:36 +00:00
tox_weechat_friend_request_free_identity(struct t_tox_weechat_identity *identity)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
while (identity->friend_requests)
tox_weechat_friend_request_remove(identity->friend_requests);
2014-09-02 16:47:08 +00:00
}