tox-weechat/src/twc-friend-request.c

116 lines
3.0 KiB
C
Raw Normal View History

2014-09-28 01:29:34 +00:00
/*
2018-04-12 21:42:34 +00:00
* Copyright (c) 2018 Håvard Pettersson <mail@haavard.me>
2014-09-28 01:29:34 +00:00
*
* 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/>.
*/
#include <string.h>
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>
2014-09-28 01:29:34 +00:00
#include "twc-list.h"
2014-09-28 03:55:18 +00:00
#include "twc-profile.h"
2014-09-28 01:29:34 +00:00
#include "twc-utils.h"
#include "twc.h"
2014-09-28 01:29:34 +00:00
#include "twc-friend-request.h"
/**
2015-01-06 15:11:55 +00:00
* Add a new friend request to a profile.
2014-09-28 01:29:34 +00:00
*
2015-01-06 15:11:55 +00:00
* Returns index on success, -1 on a full friend request list and -2 for any
* other error.
2014-09-28 01:29:34 +00:00
*/
int
twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id,
2014-09-28 01:29:34 +00:00
const char *message)
{
size_t max_request_count = TWC_PROFILE_OPTION_INTEGER(
profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS);
2015-01-06 15:11:55 +00:00
if (profile->friend_requests->count >= max_request_count)
return -1;
/* create a new request */
struct t_twc_friend_request *request =
malloc(sizeof(struct t_twc_friend_request));
2014-09-28 01:29:34 +00:00
if (!request)
return -2;
request->profile = profile;
request->message = strdup(message);
memcpy(request->tox_id, client_id, TOX_PUBLIC_KEY_SIZE);
2014-09-28 01:29:34 +00:00
2015-01-06 15:11:55 +00:00
if (!twc_list_item_new_data_add(profile->friend_requests, request))
return -2;
2014-09-28 01:29:34 +00:00
2015-01-06 15:11:55 +00:00
return profile->friend_requests->count - 1;
2014-09-28 01:29:34 +00:00
}
/**
* Accept a friend request. Remove and free the request.
*/
2015-08-26 18:02:36 +00:00
bool
2014-09-28 01:29:34 +00:00
twc_friend_request_accept(struct t_twc_friend_request *request)
{
2016-12-05 01:08:39 +00:00
TOX_ERR_FRIEND_ADD err = TOX_ERR_FRIEND_ADD_OK;
2015-08-26 18:02:36 +00:00
tox_friend_add_norequest(request->profile->tox, request->tox_id, &err);
2014-09-28 01:29:34 +00:00
twc_friend_request_remove(request);
2015-08-26 18:02:36 +00:00
return err == TOX_ERR_FRIEND_ADD_OK;
2014-09-28 01:29:34 +00:00
}
/**
* Remove and free a friend request from its profile.
*/
void
twc_friend_request_remove(struct t_twc_friend_request *request)
{
2015-01-06 15:11:55 +00:00
twc_list_remove_with_data(request->profile->friend_requests, request);
2014-09-28 01:29:34 +00:00
}
/**
* Get friend request with a given index.
*/
struct t_twc_friend_request *
2014-10-11 10:36:51 +00:00
twc_friend_request_with_index(struct t_twc_profile *profile, int64_t index)
2014-09-28 01:29:34 +00:00
{
2015-01-06 15:11:55 +00:00
return twc_list_get(profile->friend_requests, index)->friend_request;
2014-09-28 01:29:34 +00:00
}
/**
* Free a friend request.
*/
void
twc_friend_request_free(struct t_twc_friend_request *request)
{
free(request->message);
free(request);
}
/**
* Free all friend requests from a list.
2014-09-28 01:29:34 +00:00
*/
void
twc_friend_request_free_list(struct t_twc_list *list)
2014-09-28 01:29:34 +00:00
{
struct t_twc_friend_request *request;
while ((request = twc_list_pop(list)))
2014-09-28 01:29:34 +00:00
twc_friend_request_free(request);
free(list);
2014-09-28 01:29:34 +00:00
}