2014-09-28 03:29:34 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <weechat/weechat-plugin.h>
|
|
|
|
#include <tox/tox.h>
|
|
|
|
|
|
|
|
#include "twc.h"
|
|
|
|
#include "twc-list.h"
|
2014-09-28 05:55:18 +02:00
|
|
|
#include "twc-profile.h"
|
2014-10-04 19:42:30 +02:00
|
|
|
#include "twc-sqlite.h"
|
2014-09-28 03:29:34 +02:00
|
|
|
#include "twc-utils.h"
|
|
|
|
|
|
|
|
#include "twc-friend-request.h"
|
|
|
|
|
|
|
|
/**
|
2014-10-04 23:13:49 +02:00
|
|
|
* Add a new friend request to a profile. Return it's index for accepting.
|
2014-09-28 03:29:34 +02:00
|
|
|
*
|
2014-10-04 23:13:49 +02:00
|
|
|
* Returns ID on success, -1 on a full friend request list, -2 on other error.
|
2014-09-28 03:29:34 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
twc_friend_request_add(struct t_twc_profile *profile,
|
|
|
|
const uint8_t *client_id,
|
|
|
|
const char *message)
|
|
|
|
{
|
2014-10-11 11:48:24 +02:00
|
|
|
int max_request_count =
|
|
|
|
TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS);
|
|
|
|
int current_request_count = twc_sqlite_friend_request_count(profile);
|
|
|
|
if (current_request_count >= max_request_count)
|
|
|
|
return -1;
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
// create a new request
|
|
|
|
struct t_twc_friend_request *request
|
|
|
|
= malloc(sizeof(struct t_twc_friend_request));
|
|
|
|
if (!request)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
request->profile = profile;
|
|
|
|
request->message = strdup(message);
|
|
|
|
memcpy(request->tox_id, client_id, TOX_CLIENT_ID_SIZE);
|
|
|
|
|
2014-10-06 09:43:06 +02:00
|
|
|
int rc = twc_sqlite_add_friend_request(profile, request) == -1;
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
// add to list
|
2014-10-06 09:43:06 +02:00
|
|
|
if (rc == -1)
|
2014-10-04 19:42:30 +02:00
|
|
|
return -2;
|
2014-09-28 03:29:34 +02:00
|
|
|
|
2014-10-06 09:43:06 +02:00
|
|
|
return rc;
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept a friend request. Remove and free the request.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_friend_request_accept(struct t_twc_friend_request *request)
|
|
|
|
{
|
|
|
|
tox_add_friend_norequest(request->profile->tox, request->tox_id);
|
|
|
|
twc_friend_request_remove(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove and free a friend request from its profile.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_friend_request_remove(struct t_twc_friend_request *request)
|
|
|
|
{
|
2014-10-04 19:42:30 +02:00
|
|
|
twc_sqlite_delete_friend_request_with_id(request->profile,
|
|
|
|
request->request_id);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get friend request with a given index.
|
|
|
|
*/
|
|
|
|
struct t_twc_friend_request *
|
|
|
|
twc_friend_request_with_index(struct t_twc_profile *profile,
|
2014-10-04 19:42:30 +02:00
|
|
|
int64_t index)
|
2014-09-28 03:29:34 +02:00
|
|
|
{
|
2014-10-04 19:42:30 +02:00
|
|
|
return twc_sqlite_friend_request_with_id(profile, index);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a friend request.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_friend_request_free(struct t_twc_friend_request *request)
|
|
|
|
{
|
|
|
|
free(request->message);
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-04 19:42:30 +02:00
|
|
|
* Free all friend requests from a list.
|
2014-09-28 03:29:34 +02:00
|
|
|
*/
|
|
|
|
void
|
2014-10-04 19:42:30 +02:00
|
|
|
twc_friend_request_free_list(struct t_twc_list *list)
|
2014-09-28 03:29:34 +02:00
|
|
|
{
|
|
|
|
struct t_twc_friend_request *request;
|
2014-10-04 19:42:30 +02:00
|
|
|
while ((request = twc_list_pop(list)))
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_friend_request_free(request);
|
|
|
|
|
2014-10-04 19:42:30 +02:00
|
|
|
free(list);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|