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

113 lines
2.8 KiB
C
Raw Normal View History

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"
#include "twc-sqlite.h"
2014-09-28 03:29:34 +02:00
#include "twc-utils.h"
#include "twc-friend-request.h"
/**
2014-10-02 00:34:42 +02:00
* Add a new friend request to a profile.
2014-09-28 03:29:34 +02:00
*
* Returns 0 on success, -1 on a full friend request list, -2 on other error.
*/
int
twc_friend_request_add(struct t_twc_profile *profile,
const uint8_t *client_id,
const char *message)
{
// 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);
// add to list
if (twc_sqlite_add_friend_request(profile, request) == -1)
return -2;
2014-09-28 03:29:34 +02:00
return 0;
}
/**
* 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)
{
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,
int64_t index)
2014-09-28 03:29:34 +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);
}
/**
* Free all friend requests from a list.
2014-09-28 03:29:34 +02:00
*/
void
twc_friend_request_free_list(struct t_twc_list *list)
2014-09-28 03:29:34 +02:00
{
struct t_twc_friend_request *request;
while ((request = twc_list_pop(list)))
2014-09-28 03:29:34 +02:00
twc_friend_request_free(request);
free(list);
2014-09-28 03:29:34 +02:00
}