2014-10-04 21:42:30 +02:00
|
|
|
/*
|
2018-04-12 23:42:34 +02:00
|
|
|
* Copyright (c) 2018 Håvard Pettersson <mail@haavard.me>
|
2014-10-04 21:42:30 +02: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>
|
2017-02-11 04:08:26 +01:00
|
|
|
#include <weechat/weechat-plugin.h>
|
2015-09-02 22:35:13 +02:00
|
|
|
|
2015-09-02 22:54:34 +02:00
|
|
|
#ifdef TOXAV_ENABLED
|
2017-02-11 04:08:26 +01:00
|
|
|
#include <tox/toxav.h>
|
|
|
|
#endif /* TOXAV_ENABLED */
|
2014-10-04 21:42:30 +02:00
|
|
|
|
|
|
|
#include "twc-list.h"
|
|
|
|
#include "twc-profile.h"
|
|
|
|
#include "twc-utils.h"
|
2017-02-11 04:08:26 +01:00
|
|
|
#include "twc.h"
|
2014-10-04 21:42:30 +02:00
|
|
|
|
|
|
|
#include "twc-group-invite.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new group invite to a profile.
|
|
|
|
*
|
2014-10-04 23:13:49 +02:00
|
|
|
* Returns the index of the invite on success, -1 on error.
|
2014-10-04 21:42:30 +02:00
|
|
|
*/
|
|
|
|
int
|
2017-02-11 04:08:26 +01:00
|
|
|
twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number,
|
|
|
|
uint8_t group_chat_type, uint8_t *data, size_t size)
|
2014-10-04 21:42:30 +02:00
|
|
|
{
|
2017-02-11 04:08:26 +01:00
|
|
|
/* create a new invite object */
|
|
|
|
struct t_twc_group_chat_invite *invite =
|
|
|
|
malloc(sizeof(struct t_twc_group_chat_invite));
|
2014-10-04 21:42:30 +02:00
|
|
|
if (!invite)
|
|
|
|
return -1;
|
|
|
|
|
2014-10-05 02:16:43 +02:00
|
|
|
uint8_t *data_copy = malloc(size);
|
|
|
|
memcpy(data_copy, data, size);
|
|
|
|
|
2014-10-04 21:42:30 +02:00
|
|
|
invite->profile = profile;
|
2014-10-04 23:13:49 +02:00
|
|
|
invite->friend_number = friend_number;
|
2014-11-19 04:49:39 +01:00
|
|
|
invite->group_chat_type = group_chat_type;
|
2014-10-05 02:16:43 +02:00
|
|
|
invite->data = data_copy;
|
2014-10-04 21:42:30 +02:00
|
|
|
invite->data_size = size;
|
|
|
|
|
2018-04-12 15:45:17 +02:00
|
|
|
if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN))
|
2018-12-22 21:16:45 +01:00
|
|
|
invite->autojoin_delay = TWC_PROFILE_OPTION_INTEGER(
|
|
|
|
profile, TWC_PROFILE_OPTION_AUTOJOIN_DELAY);
|
2018-04-12 15:45:17 +02:00
|
|
|
else
|
|
|
|
invite->autojoin_delay = 0;
|
|
|
|
|
2014-10-04 23:13:49 +02:00
|
|
|
twc_list_item_new_data_add(profile->group_chat_invites, invite);
|
|
|
|
|
|
|
|
return profile->group_chat_invites->count - 1;
|
2014-10-04 21:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-04 23:13:49 +02:00
|
|
|
* Accept a group chat invite. Remove and free the invite. Returns group chat
|
|
|
|
* number. -1 on failure.
|
2014-10-04 21:42:30 +02:00
|
|
|
*/
|
2014-10-04 23:13:49 +02:00
|
|
|
int
|
|
|
|
twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite)
|
2014-10-04 21:42:30 +02:00
|
|
|
{
|
2014-11-19 04:49:39 +01:00
|
|
|
int rc;
|
2016-12-22 13:35:08 +01:00
|
|
|
TOX_ERR_CONFERENCE_JOIN err = TOX_ERR_CONFERENCE_JOIN_OK;
|
2014-11-19 04:49:39 +01:00
|
|
|
switch (invite->group_chat_type)
|
|
|
|
{
|
2016-12-22 13:35:08 +01:00
|
|
|
case TOX_CONFERENCE_TYPE_TEXT:
|
2017-02-11 04:08:26 +01:00
|
|
|
rc =
|
|
|
|
tox_conference_join(invite->profile->tox, invite->friend_number,
|
|
|
|
invite->data, invite->data_size, &err);
|
2015-09-02 22:35:13 +02:00
|
|
|
break;
|
|
|
|
#ifdef TOXAV_ENABLED
|
2016-12-22 13:35:08 +01:00
|
|
|
case TOX_CONFERENCE_TYPE_AV:
|
2015-09-02 22:35:13 +02:00
|
|
|
rc = toxav_join_av_groupchat(invite->profile->tox,
|
2017-02-11 04:08:26 +01:00
|
|
|
invite->friend_number, invite->data,
|
|
|
|
invite->data_size, NULL, NULL);
|
2015-09-02 22:35:13 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
rc = -1;
|
|
|
|
break;
|
2014-11-19 04:49:39 +01:00
|
|
|
}
|
|
|
|
|
2014-10-04 21:42:30 +02:00
|
|
|
twc_group_chat_invite_remove(invite);
|
2014-10-04 23:13:49 +02:00
|
|
|
|
2016-12-22 13:35:08 +01:00
|
|
|
if (err != TOX_ERR_CONFERENCE_JOIN_OK)
|
|
|
|
return -1;
|
2014-10-04 23:13:49 +02:00
|
|
|
return rc;
|
2014-10-04 21:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove and free a group chat invite.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite)
|
|
|
|
{
|
|
|
|
twc_list_remove_with_data(invite->profile->group_chat_invites, invite);
|
|
|
|
twc_group_chat_invite_free(invite);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get group chat invite with a given index.
|
|
|
|
*/
|
|
|
|
struct t_twc_group_chat_invite *
|
2017-02-11 04:08:26 +01:00
|
|
|
twc_group_chat_invite_with_index(struct t_twc_profile *profile, size_t index)
|
2014-10-04 21:42:30 +02:00
|
|
|
{
|
2014-10-10 11:36:54 +02:00
|
|
|
struct t_twc_list_item *item =
|
|
|
|
twc_list_get(profile->group_chat_invites, index);
|
|
|
|
if (item)
|
|
|
|
return item->group_chat_invite;
|
|
|
|
else
|
|
|
|
return NULL;
|
2014-10-04 21:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a group chat invite.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite)
|
|
|
|
{
|
2014-10-05 02:16:43 +02:00
|
|
|
free(invite->data);
|
2014-10-04 21:42:30 +02:00
|
|
|
free(invite);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a list of group chat invites.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_group_chat_invite_free_list(struct t_twc_list *list)
|
|
|
|
{
|
|
|
|
struct t_twc_group_chat_invite *invite;
|
|
|
|
while ((invite = twc_list_pop(list)))
|
|
|
|
twc_group_chat_invite_free(invite);
|
|
|
|
|
|
|
|
free(list);
|
|
|
|
}
|